libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
stm32_uart.hpp
1#pragma once
2
3#include "main.h"
4
5#ifdef HAL_UART_MODULE_ENABLED
6
7#ifdef UART
8#undef UART
9#endif
10
11#include "latest_snapshot.hpp"
12#include "libxr_def.hpp"
13#include "libxr_rw.hpp"
14#include "model/uart_circular_dma_rx_model.hpp"
15#include "model/uart_dma_tx_model.hpp"
16#include "model/uart_rx_config_gate.hpp"
17#include "stm32_dcache.hpp"
18#include "uart.hpp"
19
20typedef enum : uint8_t
21{
22#ifdef USART1
23 STM32_USART1,
24#endif
25#ifdef USART2
26 STM32_USART2,
27#endif
28#ifdef USART3
29 STM32_USART3,
30#endif
31#ifdef USART4
32 STM32_USART4,
33#endif
34#ifdef USART5
35 STM32_USART5,
36#endif
37#ifdef USART6
38 STM32_USART6,
39#endif
40#ifdef USART7
41 STM32_USART7,
42#endif
43#ifdef USART8
44 STM32_USART8,
45#endif
46#ifdef USART9
47 STM32_USART9,
48#endif
49#ifdef USART10
50 STM32_USART10,
51#endif
52#ifdef USART11
53 STM32_USART11,
54#endif
55#ifdef USART12
56 STM32_USART12,
57#endif
58#ifdef USART13
59 STM32_USART13,
60#endif
61#ifdef UART1
62 STM32_UART1,
63#endif
64#ifdef UART2
65 STM32_UART2,
66#endif
67#ifdef UART3
68 STM32_UART3,
69#endif
70#ifdef UART4
71 STM32_UART4,
72#endif
73#ifdef UART5
74 STM32_UART5,
75#endif
76#ifdef UART6
77 STM32_UART6,
78#endif
79#ifdef UART7
80 STM32_UART7,
81#endif
82#ifdef UART8
83 STM32_UART8,
84#endif
85#ifdef UART9
86 STM32_UART9,
87#endif
88#ifdef UART10
89 STM32_UART10,
90#endif
91#ifdef UART11
92 STM32_UART11,
93#endif
94#ifdef UART12
95 STM32_UART12,
96#endif
97#ifdef UART13
98 STM32_UART13,
99#endif
100#ifdef LPUART1
101 STM32_LPUART1,
102#endif
103#ifdef LPUART2
104 STM32_LPUART2,
105#endif
106#ifdef LPUART3
107 STM32_LPUART3,
108#endif
109 STM32_UART_NUMBER,
110 STM32_UART_ID_ERROR
111} stm32_uart_id_t;
112
113stm32_uart_id_t stm32_uart_get_id(USART_TypeDef* addr);
114
115namespace LibXR
116{
128class STM32UART : public UART
129{
130 friend class UartCircularDmaRxModel;
131 friend class UartDmaTxModel<STM32UART>;
132
133 public:
134 static ErrorCode WriteFun(WritePort& port, bool in_isr);
135
136 static ErrorCode ReadFun(ReadPort& port, bool in_isr);
137
141 STM32UART(UART_HandleTypeDef* uart_handle, RawData dma_buff_rx, RawData dma_buff_tx,
142 uint32_t tx_queue_size = 5);
143
145
146 void SetRxDMA();
147 void OnRxDataAvailable(bool in_isr);
148
149 ReadPort _read_port;
150 WritePort _write_port;
151
152 LatestSnapshot<UART::Configuration> requested_config_;
153 UartRxConfigGate rx_config_gate_;
154 UartCircularDmaRxModel rx_dma_model_;
155 UartDmaTxModel<STM32UART> tx_dma_model_;
156
157 UART_HandleTypeDef* uart_handle_;
158
159 stm32_uart_id_t id_ = STM32_UART_ID_ERROR;
160
161 static STM32UART* map[STM32_UART_NUMBER]; // NOLINT
162
163 private:
164 void OnConfigRequested() { rx_config_gate_.RequestConfig(); }
165 bool ApplyPendingConfig(bool in_isr);
166 bool OnConfigApplied(bool)
167 {
168 rx_config_gate_.LeaveConfig();
169 return true;
170 }
171
178 void StartCircularDmaRx(uint8_t* data, size_t size)
179 {
180 uart_handle_->hdmarx->Init.Mode = DMA_CIRCULAR;
181 HAL_DMA_Init(uart_handle_->hdmarx);
182 HAL_UARTEx_ReceiveToIdle_DMA(uart_handle_, data, size);
183 }
184
189 [[nodiscard]] size_t GetCircularDmaRxRemaining() const
190 {
191 return __HAL_DMA_GET_COUNTER(uart_handle_->hdmarx);
192 }
193
199 void PrepareCircularDmaRxForCpu(uint8_t* data, size_t size)
200 {
202 }
203
213 bool StartDmaTx(uint8_t* data, size_t size, int block);
214};
215
216} // namespace LibXR
217
218#endif
SPSC mailbox retaining only the latest fully published value.
可写原始数据视图 / Mutable raw data view
ReadPort class for handling read operations.
Definition read_port.hpp:18
STM32 UART 驱动实现 / STM32 UART driver implementation.
void StartCircularDmaRx(uint8_t *data, size_t size)
通过 STM32 HAL 配置并启动 UART 循环 RX DMA / Configure and start circular UART RX DMA through STM32 HAL
void PrepareCircularDmaRxForCpu(uint8_t *data, size_t size)
使循环 DMA RX 数据对 CPU 可见 / Make circular DMA RX data visible to the CPU
bool StartDmaTx(uint8_t *data, size_t size, int block)
通过 STM32 HAL 启动一个 active UART TX DMA 载荷 / Start one active UART TX DMA payload through STM32 HAL
STM32UART(UART_HandleTypeDef *uart_handle, RawData dma_buff_rx, RawData dma_buff_tx, uint32_t tx_queue_size=5)
构造 UART 对象 / Construct UART object
ErrorCode SetConfig(UART::Configuration config)
设置 UART 配置 / Sets the UART configuration
size_t GetCircularDmaRxRemaining() const
获取 STM32 RX DMA 剩余传输计数 / Get the STM32 RX DMA remaining count
通用异步收发传输(UART)基类 / Abstract base class for Universal Asynchronous Receiver-Transmitter (UART)
Definition uart.hpp:19
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
void STM32_InvalidateDCacheByAddr(const void *addr, size_t size)
Invalidates D-Cache lines covering the specified memory range.
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