libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
ch32_uart.hpp
1#pragma once
2
3#include "libxr.hpp"
4#include DEF2STR(LIBXR_CH32_CONFIG_FILE)
5
6#include "ch32_uart_def.hpp"
7#include "latest_snapshot.hpp"
8#include "libxr_def.hpp"
9#include "libxr_rw.hpp"
10#include "model/uart_circular_dma_rx_model.hpp"
11#include "model/uart_dma_tx_model.hpp"
12#include "model/uart_rx_config_gate.hpp"
13#include "uart.hpp"
14
15namespace LibXR
16{
17
28class CH32UART : public UART
29{
30 friend class UartCircularDmaRxModel;
31 friend class UartDmaTxModel<CH32UART>;
32
33 public:
37 CH32UART(ch32_uart_id_t id, RawData dma_rx, RawData dma_tx, GPIO_TypeDef* tx_gpio_port,
38 uint16_t tx_gpio_pin, GPIO_TypeDef* rx_gpio_port, uint16_t rx_gpio_pin,
39 uint32_t pin_remap = 0, uint32_t tx_queue_size = 5,
40 UART::Configuration config = {115200, UART::Parity::NO_PARITY, 8, 1});
41
43
44 static ErrorCode WriteFun(WritePort& port, bool in_isr);
45 static ErrorCode ReadFun(ReadPort& port, bool in_isr);
46
47 void TxDmaIRQHandler();
48 void RxDmaIRQHandler();
49 void OnRxDataAvailable(bool in_isr);
50
51 ch32_uart_id_t id_;
52 uint16_t uart_mode_;
53
54 ReadPort _read_port;
55 WritePort _write_port;
56
58 UartRxConfigGate rx_config_gate_;
59 UartCircularDmaRxModel rx_dma_model_;
60 UartDmaTxModel<CH32UART> tx_dma_model_;
61
62 USART_TypeDef* instance_;
63 DMA_Channel_TypeDef* dma_rx_channel_;
64 DMA_Channel_TypeDef* dma_tx_channel_;
65
66 static CH32UART* map_[CH32_UART_NUMBER];
67
68 private:
69 void OnConfigRequested() { rx_config_gate_.RequestConfig(); }
70 bool ApplyPendingConfig(bool in_isr);
71 bool OnConfigApplied(bool)
72 {
73 rx_config_gate_.LeaveConfig();
74 return true;
75 }
76
83 void StartCircularDmaRx(uint8_t* data, size_t size)
84 {
85 dma_rx_channel_->MADDR = reinterpret_cast<uint32_t>(data);
86 dma_rx_channel_->CNTR = size;
87 DMA_Cmd(dma_rx_channel_, ENABLE);
88 }
89
94 [[nodiscard]] size_t GetCircularDmaRxRemaining() const { return dma_rx_channel_->CNTR; }
95
103 void PrepareCircularDmaRxForCpu(uint8_t*, size_t) {}
104
114 bool StartDmaTx(uint8_t* data, size_t size, int block);
115};
116
117} // namespace LibXR
CH32 UART 驱动实现 / CH32 UART driver implementation.
Definition ch32_uart.hpp:29
void RxDmaIRQHandler()
DMA中断处理函数
ErrorCode SetConfig(UART::Configuration config)
设置 UART 配置 / Sets the UART configuration
size_t GetCircularDmaRxRemaining() const
获取 CH32 RX DMA 剩余传输计数 / Get the CH32 RX DMA remaining count
Definition ch32_uart.hpp:94
void PrepareCircularDmaRxForCpu(uint8_t *, size_t)
为 CPU 访问准备 CH32 循环 DMA RX 存储区 / Prepare CH32 circular DMA RX storage for CPU access
void StartCircularDmaRx(uint8_t *data, size_t size)
配置并启动 CH32 UART 循环 RX DMA 通道 / Configure and start the CH32 UART circular RX DMA channel
Definition ch32_uart.hpp:83
bool StartDmaTx(uint8_t *data, size_t size, int block)
配置并启动一个 active UART TX DMA 载荷 / Configure and start one active UART TX DMA payload
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})
构造 UART 对象 / Construct UART object
Definition ch32_uart.cpp:15
SPSC mailbox retaining only the latest fully published value.
可写原始数据视图 / Mutable raw data view
ReadPort class for handling read operations.
Definition read_port.hpp:18
通用异步收发传输(UART)基类 / Abstract base class for Universal Asynchronous Receiver-Transmitter (UART)
Definition uart.hpp:19
@ NO_PARITY
无校验 / No parity
UART 基于位置的循环 DMA 接收模型 / Position-based circular DMA RX model for UART.
UART 单次 DMA 发送执行模型 / UART one-shot DMA TX execution model.
Serialize UART RX hardware callbacks against configuration / 串行化 UART RX 硬件回调与配置事务
WritePort class for handling write operations.
LibXR 命名空间
Definition ch32_can.hpp:14
ErrorCode
定义错误码枚举
ErrorCode(* ReadFun)(ReadPort &port, bool in_isr)
Function pointer type for read notifications.
ErrorCode(* WriteFun)(WritePort &port, bool in_isr)
Function pointer type for write operations.
UART 配置结构体 / UART configuration structure.
Definition uart.hpp:44