SimpleKernel 1.17.0
Loading...
Searching...
No Matches
ns16550a::Ns16550a Class Reference

NS16550A 串口驱动 More...

#include <ns16550a.hpp>

Collaboration diagram for ns16550a::Ns16550a:
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 GetInterruptId () const -> uint8_t
 读取中断标识寄存器(ISR / IIR)
 
auto IsInterruptPending () const -> bool
 检查是否有中断挂起
 
构造/析构函数
 Ns16550a ()=default
 
 Ns16550a (const Ns16550a &)=delete
 
 Ns16550a (Ns16550a &&)=default
 
auto operator= (const Ns16550a &) -> Ns16550a &=delete
 
auto operator= (Ns16550a &&) -> Ns16550a &=default
 
 ~Ns16550a ()=default
 

Static Public Member Functions

static auto Create (uint64_t dev_addr) -> Expected< Ns16550a >
 工厂方法:创建并初始化 NS16550A 驱动
 

Private Member Functions

 Ns16550a (uint64_t dev_addr)
 

Private Attributes

MmioAccessor mmio_ {}
 

Static Private Attributes

static constexpr uint8_t kRegRHR = 0
 read mode: Receive holding reg
 
static constexpr uint8_t kRegTHR = 0
 write mode: Transmit Holding Reg
 
static constexpr uint8_t kRegIER = 1
 write mode: interrupt enable reg
 
static constexpr uint8_t kRegFCR = 2
 write mode: FIFO control Reg
 
static constexpr uint8_t kRegISR = 2
 read mode: Interrupt Status Reg
 
static constexpr uint8_t kRegLCR = 3
 write mode: Line Control Reg
 
static constexpr uint8_t kRegMCR = 4
 write mode: Modem Control Reg
 
static constexpr uint8_t kRegLSR = 5
 read mode: Line Status Reg
 
static constexpr uint8_t kRegMSR = 6
 read mode: Modem Status Reg
 
static constexpr uint8_t kUartDLL = 0
 LSB of divisor Latch when enabled.
 
static constexpr uint8_t kUartDLM = 1
 MSB of divisor Latch when enabled.
 

Detailed Description

NS16550A 串口驱动

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

使用工厂方法 Create() 构造,将验证与初始化分离。

Definition at line 23 of file ns16550a.hpp.

Constructor & Destructor Documentation

◆ Ns16550a() [1/4]

ns16550a::Ns16550a::Ns16550a ( )
default

◆ Ns16550a() [2/4]

ns16550a::Ns16550a::Ns16550a ( const Ns16550a )
delete

◆ Ns16550a() [3/4]

ns16550a::Ns16550a::Ns16550a ( Ns16550a &&  )
default

◆ ~Ns16550a()

ns16550a::Ns16550a::~Ns16550a ( )
default

◆ Ns16550a() [4/4]

ns16550a::Ns16550a::Ns16550a ( uint64_t  dev_addr)
inlineexplicitprivate

Definition at line 150 of file ns16550a.hpp.

150: mmio_(dev_addr) {}
MmioAccessor mmio_
Definition ns16550a.hpp:148

Member Function Documentation

◆ Create()

static auto ns16550a::Ns16550a::Create ( uint64_t  dev_addr) -> Expected<Ns16550a>
inlinestatic

工厂方法:创建并初始化 NS16550A 驱动

Parameters
dev_addr设备 MMIO 基地址
Returns
成功返回已初始化的 Ns16550a 实例,失败返回错误

Definition at line 30 of file ns16550a.hpp.

30 {
31 if (dev_addr == 0) {
32 return std::unexpected(Error{ErrorCode::kInvalidArgument});
33 }
34
35 Ns16550a uart(dev_addr);
36
37 // UART 初始化序列
38 uart.mmio_.Write<uint8_t>(kRegIER, 0x00); // 禁用所有中断
39 uart.mmio_.Write<uint8_t>(kRegLCR, 0x80); // 启用 DLAB(设置波特率)
40 uart.mmio_.Write<uint8_t>(kUartDLL, 0x03); // 波特率低字节(38400)
41 uart.mmio_.Write<uint8_t>(kUartDLM, 0x00); // 波特率高字节
42 uart.mmio_.Write<uint8_t>(kRegLCR, 0x03); // 8 位,无校验,1 停止位
43 uart.mmio_.Write<uint8_t>(kRegFCR, 0x07); // 启用并清除 FIFO
44 uart.mmio_.Write<uint8_t>(kRegIER, 0x01); // 启用接收中断
45
46 return uart;
47 }
static constexpr uint8_t kRegIER
write mode: interrupt enable reg
Definition ns16550a.hpp:129
static constexpr uint8_t kUartDLM
MSB of divisor Latch when enabled.
Definition ns16550a.hpp:146
static constexpr uint8_t kRegFCR
write mode: FIFO control Reg
Definition ns16550a.hpp:131
static constexpr uint8_t kRegLCR
write mode: Line Control Reg
Definition ns16550a.hpp:135
static constexpr uint8_t kUartDLL
LSB of divisor Latch when enabled.
Definition ns16550a.hpp:144
@ kInvalidArgument
错误类型,用于 std::expected
Definition expected.hpp:343
volatile uint8_t * uart
TODO: Add description.
Definition main.cpp:10
Here is the caller graph for this function:

◆ GetChar()

auto ns16550a::Ns16550a::GetChar ( ) const -> uint8_t
inline

阻塞式读取一个字符

Returns
读取到的字符

Definition at line 63 of file ns16550a.hpp.

63 {
64 while ((mmio_.Read<uint8_t>(kRegLSR) & (1 << 0)) == 0) {
65 }
66 return mmio_.Read<uint8_t>(kRegRHR);
67 }
static constexpr uint8_t kRegLSR
read mode: Line Status Reg
Definition ns16550a.hpp:139
static constexpr uint8_t kRegRHR
read mode: Receive holding reg
Definition ns16550a.hpp:125
auto Read(size_t offset) const -> T
Read from MMIO register.
Here is the call graph for this function:

◆ GetInterruptId()

auto ns16550a::Ns16550a::GetInterruptId ( ) const -> uint8_t
inline

读取中断标识寄存器(ISR / IIR)

返回值 bit[0]:0=有中断挂起,1=无中断挂起 返回值 bit[3:1]:中断源标识

  • 0b011: 接收线状态错误
  • 0b010: 接收数据就绪
  • 0b110: 字符超时
  • 0b001: THR 空(发送就绪)
  • 0b000: Modem 状态变化
Returns
中断标识寄存器值

Definition at line 101 of file ns16550a.hpp.

101 {
102 return mmio_.Read<uint8_t>(kRegISR);
103 }
static constexpr uint8_t kRegISR
read mode: Interrupt Status Reg
Definition ns16550a.hpp:133
Here is the call graph for this function:

◆ HasData()

auto ns16550a::Ns16550a::HasData ( ) const -> bool
inline

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

Returns
true 如果有数据可读

Definition at line 84 of file ns16550a.hpp.

84 {
85 return (mmio_.Read<uint8_t>(kRegLSR) & (1 << 0)) != 0;
86 }
Here is the call graph for this function:

◆ IsInterruptPending()

auto ns16550a::Ns16550a::IsInterruptPending ( ) const -> bool
inline

检查是否有中断挂起

Returns
true 如果有中断挂起

Definition at line 109 of file ns16550a.hpp.

109 {
110 return (mmio_.Read<uint8_t>(kRegISR) & 0x01) == 0;
111 }
Here is the call graph for this function:

◆ operator=() [1/2]

auto ns16550a::Ns16550a::operator= ( const Ns16550a ) -> Ns16550a &=delete
delete

◆ operator=() [2/2]

auto ns16550a::Ns16550a::operator= ( Ns16550a &&  ) -> Ns16550a &=default
default

◆ PutChar()

auto ns16550a::Ns16550a::PutChar ( uint8_t  c) const -> void
inline

写入一个字符

Parameters
c待写入的字符

Definition at line 53 of file ns16550a.hpp.

53 {
54 while ((mmio_.Read<uint8_t>(kRegLSR) & (1 << 5)) == 0) {
55 }
56 mmio_.Write<uint8_t>(kRegTHR, c);
57 }
static constexpr uint8_t kRegTHR
write mode: Transmit Holding Reg
Definition ns16550a.hpp:127
auto Write(size_t offset, T val) const -> void
Write to MMIO register.
Here is the call graph for this function:

◆ TryGetChar()

auto ns16550a::Ns16550a::TryGetChar ( ) const -> std::optional<uint8_t>
inline

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

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

Definition at line 73 of file ns16550a.hpp.

73 {
74 if ((mmio_.Read<uint8_t>(kRegLSR) & (1 << 0)) != 0) {
75 return mmio_.Read<uint8_t>(kRegRHR);
76 }
77 return std::nullopt;
78 }
Here is the call graph for this function:

Member Data Documentation

◆ kRegFCR

constexpr uint8_t ns16550a::Ns16550a::kRegFCR = 2
staticconstexprprivate

write mode: FIFO control Reg

Definition at line 131 of file ns16550a.hpp.

◆ kRegIER

constexpr uint8_t ns16550a::Ns16550a::kRegIER = 1
staticconstexprprivate

write mode: interrupt enable reg

Definition at line 129 of file ns16550a.hpp.

◆ kRegISR

constexpr uint8_t ns16550a::Ns16550a::kRegISR = 2
staticconstexprprivate

read mode: Interrupt Status Reg

Definition at line 133 of file ns16550a.hpp.

◆ kRegLCR

constexpr uint8_t ns16550a::Ns16550a::kRegLCR = 3
staticconstexprprivate

write mode: Line Control Reg

Definition at line 135 of file ns16550a.hpp.

◆ kRegLSR

constexpr uint8_t ns16550a::Ns16550a::kRegLSR = 5
staticconstexprprivate

read mode: Line Status Reg

Definition at line 139 of file ns16550a.hpp.

◆ kRegMCR

constexpr uint8_t ns16550a::Ns16550a::kRegMCR = 4
staticconstexprprivate

write mode: Modem Control Reg

Definition at line 137 of file ns16550a.hpp.

◆ kRegMSR

constexpr uint8_t ns16550a::Ns16550a::kRegMSR = 6
staticconstexprprivate

read mode: Modem Status Reg

Definition at line 141 of file ns16550a.hpp.

◆ kRegRHR

constexpr uint8_t ns16550a::Ns16550a::kRegRHR = 0
staticconstexprprivate

read mode: Receive holding reg

Definition at line 125 of file ns16550a.hpp.

◆ kRegTHR

constexpr uint8_t ns16550a::Ns16550a::kRegTHR = 0
staticconstexprprivate

write mode: Transmit Holding Reg

Definition at line 127 of file ns16550a.hpp.

◆ kUartDLL

constexpr uint8_t ns16550a::Ns16550a::kUartDLL = 0
staticconstexprprivate

LSB of divisor Latch when enabled.

Definition at line 144 of file ns16550a.hpp.

◆ kUartDLM

constexpr uint8_t ns16550a::Ns16550a::kUartDLM = 1
staticconstexprprivate

MSB of divisor Latch when enabled.

Definition at line 146 of file ns16550a.hpp.

◆ mmio_

MmioAccessor ns16550a::Ns16550a::mmio_ {}
private

Definition at line 148 of file ns16550a.hpp.

148{};

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