libxr
Embedded C++ framework for bare-metal, RTOS and Linux
Loading...
Searching...
No Matches
ch32_uart.hpp
1#pragma once
2
3#include <atomic>
4
5#include "libxr.hpp"
6#include DEF2STR(LIBXR_CH32_CONFIG_FILE)
7
8#include "ch32_uart_def.hpp"
9#include "double_buffer.hpp"
10#include "libxr_def.hpp"
11#include "libxr_rw.hpp"
12#include "serialized_service.hpp"
13#include "uart.hpp"
14
15namespace LibXR
16{
17
32class CH32UART : public UART
33{
34 public:
49 CH32UART(ch32_uart_id_t id, RawData dma_rx, RawData dma_tx, GPIO_TypeDef* tx_gpio_port,
50 uint16_t tx_gpio_pin, GPIO_TypeDef* rx_gpio_port, uint16_t rx_gpio_pin,
51 uint32_t pin_remap = 0, uint32_t tx_queue_size = 5,
52 UART::Configuration config = {115200, UART::Parity::NO_PARITY, 8, 1});
53
65 ErrorCode SetConfig(UART::Configuration config, bool in_isr = false) override;
66
68 static void WriteFun(WritePort& port, bool in_isr);
69
71 void TxDmaIRQHandler();
73 void RxDmaIRQHandler();
75 void UartIRQHandler();
76
77 ch32_uart_id_t id_;
78 uint16_t uart_mode_;
79
84
89
91 size_t last_rx_pos_ = 0;
92
93 USART_TypeDef* instance_;
94 DMA_Channel_TypeDef* dma_rx_channel_;
95 DMA_Channel_TypeDef* dma_tx_channel_;
96
99
100 static CH32UART* map_[CH32_UART_NUMBER];
101
102 private:
104 enum class ConfigState : uint32_t
105 {
106 EMPTY = 0U,
107 RESERVED = 1U,
108 PUBLISHED = 2U,
109 };
110
111 static constexpr uint32_t TX_EVENT_WRITE = 1U << 0U;
112 static constexpr uint32_t TX_EVENT_DMA_DONE = 1U << 1U;
113 static constexpr uint32_t TX_EVENT_CONFIG = 1U << 2U;
114 static constexpr uint32_t TX_EVENT_TC = 1U << 3U;
115 static constexpr uint32_t TX_EVENT_RX_WORK = 1U << 4U;
116
118 void HandleTxService(uint32_t events, bool in_isr);
120 void FillTx(bool in_isr);
122 void HandleTxDone(bool in_isr);
124 void StartTxDma(bool in_isr);
126 void ApplyConfig(UART::Configuration config);
128 void TryApplyConfig(bool in_isr);
130 void HandleRxData(bool in_isr);
131
133 SerializedService tx_service_;
135 std::atomic<ConfigState> config_state_{ConfigState::EMPTY};
138 UART::Configuration pending_config_{};
139};
140
141} // namespace LibXR
使用循环接收 DMA 和双缓冲发送的 CH32 串口 / CH32 UART with circular RX DMA and double-buffered TX.
Definition ch32_uart.hpp:33
void RxDmaIRQHandler()
处理接收 DMA 进展通知 / Handle RX DMA progress.
ErrorCode SetConfig(UART::Configuration config, bool in_isr=false) override
提交串口配置请求 / Submit a UART configuration request.
size_t last_rx_pos_
上次已处理的 DMA 接收位置 / Last processed RX DMA position.
Definition ch32_uart.hpp:91
void TxDmaIRQHandler()
处理发送 DMA 完成通知 / Handle TX DMA completion.
DoubleBuffer dma_buff_tx_
当前与待发送的 DMA 半区 / Active and pending TX DMA halves.
Definition ch32_uart.hpp:88
ReadPort _read_port
接收字节队列与读请求 / RX byte queue and read requests.
Definition ch32_uart.hpp:81
CH32UART(ch32_uart_id_t id, RawData dma_rx, RawData dma_tx, GPIO_TypeDef *tx_gpio_port, uint16_t tx_gpio_pin, GPIO_TypeDef *rx_gpio_port, uint16_t rx_gpio_pin, uint32_t pin_remap=0, uint32_t tx_queue_size=5, UART::Configuration config={115200, UART::Parity::NO_PARITY, 8, 1})
构造串口并关联 DMA 缓冲区 / Construct a UART with DMA buffers.
Definition ch32_uart.cpp:35
WritePort _write_port
写请求及发送字节队列 / Write requests and TX byte queue.
Definition ch32_uart.hpp:83
void UartIRQHandler()
处理接收空闲和发送完成中断 / Handle RX idle and TX complete interrupts.
Flag::Plain tx_busy_
发送半区是否在使用,仅后端处理器访问 / TX half in use; service-only access.
Definition ch32_uart.hpp:98
RawData dma_buff_rx_
调用者提供的接收 DMA 缓冲区 / Caller-provided RX DMA buffer.
Definition ch32_uart.hpp:86
双缓冲区管理类 / Double buffer manager class
普通标志位(非原子)/ Non-atomic flag
Definition flag.hpp:115
可写原始数据视图 / Mutable raw data view
基于 SPSC 字节队列的读端口 / Read endpoint backed by an SPSC byte queue.
Definition read_port.hpp:30
事件合并与回调串行化服务 / Event-coalescing serialized-callback service.
通用异步收发传输(UART)基类 / Abstract base class for Universal Asynchronous Receiver-Transmitter (UART)
Definition uart.hpp:19
@ NO_PARITY
无校验 / No parity
基于 SPSC 字节队列的写端口 / Write endpoint backed by an SPSC byte queue.
LibXR 命名空间 / LibXR namespace.
Definition ch32_can.hpp:14
ErrorCode
定义错误码枚举 / Defines an enumeration for error codes
@ EMPTY
为空 / Empty
void(* WriteFun)(WritePort &port, bool in_isr)
写入进度通知函数类型 / Write progress notification function type.
UART 配置结构体 / UART configuration structure.
Definition uart.hpp:44