SimpleKernel 1.17.0
Loading...
Searching...
No Matches
pl011::Pl011 Class Reference

PL011 串口驱动 More...

#include <pl011.hpp>

Collaboration diagram for pl011::Pl011:
Collaboration graph

Public Member Functions

auto PutChar (uint8_t c) const -> void
 写入一个字符
 
auto GetChar () const -> uint8_t
 阻塞式读取一个字符
 
auto TryGetChar () const -> std::optional< uint8_t >
 非阻塞式尝试读取一个字符
 
auto HasData () const -> bool
 检查接收缓冲区是否有数据可读(不消耗数据)
 
auto GetMaskedInterruptStatus () const -> uint32_t
 读取屏蔽后的中断状态寄存器(MIS)
 
auto GetRawInterruptStatus () const -> uint32_t
 读取原始中断状态寄存器(RIS)
 
auto ClearInterrupt (uint32_t mask) const -> void
 清除指定中断
 
auto IsInterruptPending () const -> bool
 检查是否有中断挂起
 
template<typename Callback >
auto HandleInterrupt (Callback &&callback) -> void
 处理串口接收中断,对每个收到的字符调用回调
 
构造/析构函数
 Pl011 (uint64_t dev_addr, uint64_t clock=0, uint64_t baud_rate=0)
 构造函数
 
 Pl011 ()=default
 
 Pl011 (const Pl011 &)=delete
 
 Pl011 (Pl011 &&)=default
 
auto operator= (const Pl011 &) -> Pl011 &=delete
 
auto operator= (Pl011 &&) -> Pl011 &=default
 
 ~Pl011 ()=default
 

Private Attributes

MmioAccessor mmio_ {}
 
uint64_t base_clock_ {0}
 
uint64_t baud_rate_ {0}
 

Static Private Attributes

static constexpr uint32_t kRegDR = 0x00
 data register
 
static constexpr uint32_t kRegRSRECR = 0x04
 receive status or error clear
 
static constexpr uint32_t kRegFR = 0x18
 flag register
 
static constexpr uint32_t kRegIBRD = 0x24
 integer baud register
 
static constexpr uint32_t kRegFBRD = 0x28
 fractional baud register
 
static constexpr uint32_t kRegLCRH = 0x2C
 line control register
 
static constexpr uint32_t kRegCR = 0x30
 control register
 
static constexpr uint32_t kRegIMSC = 0x38
 interrupt mask set/clear
 
static constexpr uint32_t kRegRIS = 0x3C
 raw interrupt status register
 
static constexpr uint32_t kRegMIS = 0x40
 masked interrupt status register
 
static constexpr uint32_t kRegICR = 0x44
 interrupt clear register
 
static constexpr uint32_t kFRTxFIFO = (1 << 5)
 flag register bits
 
static constexpr uint32_t kFRRXFE = (1 << 4)
 
static constexpr uint32_t kLCRHWlen8 = (3 << 5)
 line control register bits
 
static constexpr uint32_t kCREnable = (1 << 0)
 control register bits
 
static constexpr uint32_t kCRTxEnable = (1 << 8)
 
static constexpr uint32_t kCRRxEnable = (1 << 9)
 
static constexpr uint32_t kIMSCRxim = (1 << 4)
 interrupt mask bits
 

Detailed Description

PL011 串口驱动

通过 MMIO 访问 PL011 UART 寄存器,提供字符读写功能。 Header-only 实现,使用 MmioAccessor 进行寄存器访问。

See also
https://developer.arm.com/documentation/ddi0183/g/

Definition at line 22 of file pl011.hpp.

Constructor & Destructor Documentation

◆ Pl011() [1/4]

pl011::Pl011::Pl011 ( uint64_t  dev_addr,
uint64_t  clock = 0,
uint64_t  baud_rate = 0 
)
inlineexplicit

构造函数

Parameters
dev_addr设备 MMIO 基地址
clock串口时钟(0 表示不设置波特率)
baud_rate波特率(0 表示不设置波特率)

Definition at line 124 of file pl011.hpp.

125 : mmio_(dev_addr), base_clock_(clock), baud_rate_(baud_rate) {
126 mmio_.Write<uint32_t>(kRegRSRECR, 0);
127 mmio_.Write<uint32_t>(kRegCR, 0);
128
129 if (baud_rate_ != 0) {
130 uint32_t divisor = (base_clock_ * 4) / baud_rate_;
131 mmio_.Write<uint32_t>(kRegIBRD, divisor >> 6);
132 mmio_.Write<uint32_t>(kRegFBRD, divisor & 0x3f);
133 }
134
135 mmio_.Write<uint32_t>(kRegLCRH, kLCRHWlen8);
136 mmio_.Write<uint32_t>(kRegIMSC, kIMSCRxim);
138 }
static constexpr uint32_t kRegRSRECR
receive status or error clear
Definition pl011.hpp:151
static constexpr uint32_t kRegIBRD
integer baud register
Definition pl011.hpp:155
static constexpr uint32_t kLCRHWlen8
line control register bits
Definition pl011.hpp:176
static constexpr uint32_t kRegCR
control register
Definition pl011.hpp:161
MmioAccessor mmio_
Definition pl011.hpp:186
static constexpr uint32_t kRegIMSC
interrupt mask set/clear
Definition pl011.hpp:163
static constexpr uint32_t kCREnable
control register bits
Definition pl011.hpp:179
static constexpr uint32_t kRegLCRH
line control register
Definition pl011.hpp:159
uint64_t baud_rate_
Definition pl011.hpp:188
static constexpr uint32_t kRegFBRD
fractional baud register
Definition pl011.hpp:157
static constexpr uint32_t kCRTxEnable
Definition pl011.hpp:180
static constexpr uint32_t kCRRxEnable
Definition pl011.hpp:181
uint64_t base_clock_
Definition pl011.hpp:187
static constexpr uint32_t kIMSCRxim
interrupt mask bits
Definition pl011.hpp:184
auto Write(size_t offset, T val) const -> void
Write to MMIO register.
Here is the call graph for this function:

◆ Pl011() [2/4]

pl011::Pl011::Pl011 ( )
default

◆ Pl011() [3/4]

pl011::Pl011::Pl011 ( const Pl011 )
delete

◆ Pl011() [4/4]

pl011::Pl011::Pl011 ( Pl011 &&  )
default

◆ ~Pl011()

pl011::Pl011::~Pl011 ( )
default

Member Function Documentation

◆ ClearInterrupt()

auto pl011::Pl011::ClearInterrupt ( uint32_t  mask) const -> void
inline

清除指定中断

向 ICR 寄存器写入位掩码以清除对应的中断。

Parameters
mask要清除的中断位掩码

Definition at line 93 of file pl011.hpp.

93 {
94 mmio_.Write<uint32_t>(kRegICR, mask);
95 }
static constexpr uint32_t kRegICR
interrupt clear register
Definition pl011.hpp:169
Here is the call graph for this function:

◆ GetChar()

auto pl011::Pl011::GetChar ( ) const -> uint8_t
inline

阻塞式读取一个字符

Returns
读取到的字符

Definition at line 38 of file pl011.hpp.

38 {
39 while (mmio_.Read<uint32_t>(kRegFR) & kFRRXFE) {
40 }
41 return static_cast<uint8_t>(mmio_.Read<uint32_t>(kRegDR));
42 }
static constexpr uint32_t kFRRXFE
Definition pl011.hpp:173
static constexpr uint32_t kRegDR
data register
Definition pl011.hpp:149
static constexpr uint32_t kRegFR
flag register
Definition pl011.hpp:153
auto Read(size_t offset) const -> T
Read from MMIO register.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetMaskedInterruptStatus()

auto pl011::Pl011::GetMaskedInterruptStatus ( ) const -> uint32_t
inline

读取屏蔽后的中断状态寄存器(MIS)

返回经 IMSC 屏蔽后的中断状态,只包含被使能的中断源。

Returns
中断状态位掩码
See also
ARM PL011 Technical Reference Manual

Definition at line 71 of file pl011.hpp.

71 {
72 return mmio_.Read<uint32_t>(kRegMIS);
73 }
static constexpr uint32_t kRegMIS
masked interrupt status register
Definition pl011.hpp:167
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetRawInterruptStatus()

auto pl011::Pl011::GetRawInterruptStatus ( ) const -> uint32_t
inline

读取原始中断状态寄存器(RIS)

返回所有中断源的原始状态(不经 IMSC 屏蔽)。

Returns
原始中断状态位掩码

Definition at line 82 of file pl011.hpp.

82 {
83 return mmio_.Read<uint32_t>(kRegRIS);
84 }
static constexpr uint32_t kRegRIS
raw interrupt status register
Definition pl011.hpp:165
Here is the call graph for this function:

◆ HandleInterrupt()

template<typename Callback >
auto pl011::Pl011::HandleInterrupt ( Callback &&  callback) -> void
inline

处理串口接收中断,对每个收到的字符调用回调

Parameters
callback字符接收回调,签名 void(uint8_t ch)

Definition at line 110 of file pl011.hpp.

110 {
111 while (HasData()) {
112 callback(GetChar());
113 }
114 }
auto HasData() const -> bool
检查接收缓冲区是否有数据可读(不消耗数据)
Definition pl011.hpp:59
auto GetChar() const -> uint8_t
阻塞式读取一个字符
Definition pl011.hpp:38
Here is the call graph for this function:

◆ HasData()

auto pl011::Pl011::HasData ( ) const -> bool
inline

检查接收缓冲区是否有数据可读(不消耗数据)

Returns
true 如果有数据可读

Definition at line 59 of file pl011.hpp.

59 {
60 return !(mmio_.Read<uint32_t>(kRegFR) & kFRRXFE);
61 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsInterruptPending()

auto pl011::Pl011::IsInterruptPending ( ) const -> bool
inline

检查是否有中断挂起

Returns
true 如果有屏蔽后的中断挂起

Definition at line 101 of file pl011.hpp.

101 {
102 return GetMaskedInterruptStatus() != 0;
103 }
auto GetMaskedInterruptStatus() const -> uint32_t
读取屏蔽后的中断状态寄存器(MIS)
Definition pl011.hpp:71
Here is the call graph for this function:

◆ operator=() [1/2]

auto pl011::Pl011::operator= ( const Pl011 ) -> Pl011 &=delete
delete

◆ operator=() [2/2]

auto pl011::Pl011::operator= ( Pl011 &&  ) -> Pl011 &=default
default

◆ PutChar()

auto pl011::Pl011::PutChar ( uint8_t  c) const -> void
inline

写入一个字符

Parameters
c待写入的字符

Definition at line 28 of file pl011.hpp.

28 {
29 while (mmio_.Read<uint32_t>(kRegFR) & kFRTxFIFO) {
30 }
31 mmio_.Write<uint32_t>(kRegDR, c);
32 }
static constexpr uint32_t kFRTxFIFO
flag register bits
Definition pl011.hpp:172
Here is the call graph for this function:

◆ TryGetChar()

auto pl011::Pl011::TryGetChar ( ) const -> std::optional<uint8_t>
inline

非阻塞式尝试读取一个字符

Returns
读取到的字符,如果没有数据则返回 std::nullopt

Definition at line 48 of file pl011.hpp.

48 {
49 if (mmio_.Read<uint32_t>(kRegFR) & kFRRXFE) {
50 return std::nullopt;
51 }
52 return static_cast<uint8_t>(mmio_.Read<uint32_t>(kRegDR));
53 }
Here is the call graph for this function:

Member Data Documentation

◆ base_clock_

uint64_t pl011::Pl011::base_clock_ {0}
private

Definition at line 187 of file pl011.hpp.

187{0};

◆ baud_rate_

uint64_t pl011::Pl011::baud_rate_ {0}
private

Definition at line 188 of file pl011.hpp.

188{0};

◆ kCREnable

constexpr uint32_t pl011::Pl011::kCREnable = (1 << 0)
staticconstexprprivate

control register bits

Definition at line 179 of file pl011.hpp.

◆ kCRRxEnable

constexpr uint32_t pl011::Pl011::kCRRxEnable = (1 << 9)
staticconstexprprivate

Definition at line 181 of file pl011.hpp.

◆ kCRTxEnable

constexpr uint32_t pl011::Pl011::kCRTxEnable = (1 << 8)
staticconstexprprivate

Definition at line 180 of file pl011.hpp.

◆ kFRRXFE

constexpr uint32_t pl011::Pl011::kFRRXFE = (1 << 4)
staticconstexprprivate

Definition at line 173 of file pl011.hpp.

◆ kFRTxFIFO

constexpr uint32_t pl011::Pl011::kFRTxFIFO = (1 << 5)
staticconstexprprivate

flag register bits

Definition at line 172 of file pl011.hpp.

◆ kIMSCRxim

constexpr uint32_t pl011::Pl011::kIMSCRxim = (1 << 4)
staticconstexprprivate

interrupt mask bits

Definition at line 184 of file pl011.hpp.

◆ kLCRHWlen8

constexpr uint32_t pl011::Pl011::kLCRHWlen8 = (3 << 5)
staticconstexprprivate

line control register bits

Definition at line 176 of file pl011.hpp.

◆ kRegCR

constexpr uint32_t pl011::Pl011::kRegCR = 0x30
staticconstexprprivate

control register

Definition at line 161 of file pl011.hpp.

◆ kRegDR

constexpr uint32_t pl011::Pl011::kRegDR = 0x00
staticconstexprprivate

data register

Definition at line 149 of file pl011.hpp.

◆ kRegFBRD

constexpr uint32_t pl011::Pl011::kRegFBRD = 0x28
staticconstexprprivate

fractional baud register

Definition at line 157 of file pl011.hpp.

◆ kRegFR

constexpr uint32_t pl011::Pl011::kRegFR = 0x18
staticconstexprprivate

flag register

Definition at line 153 of file pl011.hpp.

◆ kRegIBRD

constexpr uint32_t pl011::Pl011::kRegIBRD = 0x24
staticconstexprprivate

integer baud register

Definition at line 155 of file pl011.hpp.

◆ kRegICR

constexpr uint32_t pl011::Pl011::kRegICR = 0x44
staticconstexprprivate

interrupt clear register

Definition at line 169 of file pl011.hpp.

◆ kRegIMSC

constexpr uint32_t pl011::Pl011::kRegIMSC = 0x38
staticconstexprprivate

interrupt mask set/clear

Definition at line 163 of file pl011.hpp.

◆ kRegLCRH

constexpr uint32_t pl011::Pl011::kRegLCRH = 0x2C
staticconstexprprivate

line control register

Definition at line 159 of file pl011.hpp.

◆ kRegMIS

constexpr uint32_t pl011::Pl011::kRegMIS = 0x40
staticconstexprprivate

masked interrupt status register

Definition at line 167 of file pl011.hpp.

◆ kRegRIS

constexpr uint32_t pl011::Pl011::kRegRIS = 0x3C
staticconstexprprivate

raw interrupt status register

Definition at line 165 of file pl011.hpp.

◆ kRegRSRECR

constexpr uint32_t pl011::Pl011::kRegRSRECR = 0x04
staticconstexprprivate

receive status or error clear

Definition at line 151 of file pl011.hpp.

◆ mmio_

MmioAccessor pl011::Pl011::mmio_ {}
private

Definition at line 186 of file pl011.hpp.

186{};

The documentation for this class was generated from the following file: