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 "libxr_def.hpp"
12#include "libxr_rw.hpp"
13#include "uart.hpp"
14
15typedef enum
16{
17#ifdef USART1
18 STM32_USART1,
19#endif
20#ifdef USART2
21 STM32_USART2,
22#endif
23#ifdef USART3
24 STM32_USART3,
25#endif
26#ifdef USART4
27 STM32_USART4,
28#endif
29#ifdef USART5
30 STM32_USART5,
31#endif
32#ifdef USART6
33 STM32_USART6,
34#endif
35#ifdef USART7
36 STM32_USART7,
37#endif
38#ifdef USART8
39 STM32_USART8,
40#endif
41#ifdef USART9
42 STM32_USART9,
43#endif
44#ifdef USART10
45 STM32_USART10,
46#endif
47#ifdef USART11
48 STM32_USART11,
49#endif
50#ifdef USART12
51 STM32_USART12,
52#endif
53#ifdef USART13
54 STM32_USART13,
55#endif
56#ifdef UART1
57 STM32_UART1,
58#endif
59#ifdef UART2
60 STM32_UART2,
61#endif
62#ifdef UART3
63 STM32_UART3,
64#endif
65#ifdef UART4
66 STM32_UART4,
67#endif
68#ifdef UART5
69 STM32_UART5,
70#endif
71#ifdef UART6
72 STM32_UART6,
73#endif
74#ifdef UART7
75 STM32_UART7,
76#endif
77#ifdef UART8
78 STM32_UART8,
79#endif
80#ifdef UART9
81 STM32_UART9,
82#endif
83#ifdef UART10
84 STM32_UART10,
85#endif
86#ifdef UART11
87 STM32_UART11,
88#endif
89#ifdef UART12
90 STM32_UART12,
91#endif
92#ifdef UART13
93 STM32_UART13,
94#endif
95#ifdef LPUART1
96 STM32_LPUART1,
97#endif
98#ifdef LPUART2
99 STM32_LPUART2,
100#endif
101#ifdef LPUART3
102 STM32_LPUART3,
103#endif
104 STM32_UART_NUMBER,
105 STM32_UART_ID_ERROR
106} stm32_uart_id_t;
107
108stm32_uart_id_t STM32_UART_GetID(USART_TypeDef *addr);
109
110namespace LibXR
111{
112class STM32UART : public UART
113{
114 public:
115 static ErrorCode WriteFun(WritePort &port)
116 {
117 STM32UART *uart = CONTAINER_OF(&port, STM32UART, write_port_);
118 if (uart->uart_handle_->gState == HAL_UART_STATE_READY)
119 {
121 if (port.queue_info_->Peek(info) != ErrorCode::OK)
122 {
123 return ErrorCode::EMPTY;
124 }
125
126 if (port.queue_data_->PopBatch(
127 reinterpret_cast<uint8_t *>(uart->dma_buff_tx_.addr_), info.size) !=
128 ErrorCode::OK)
129 {
130 ASSERT(false);
131 return ErrorCode::EMPTY;
132 }
133
134 auto ans = HAL_UART_Transmit_DMA(uart->uart_handle_,
135 static_cast<uint8_t *>(uart->dma_buff_tx_.addr_),
136 info.size);
137 if (ans != HAL_OK)
138 {
139 port.queue_info_->Pop(info);
140 info.op.UpdateStatus(false, ErrorCode::FAILED);
141 return ErrorCode::FAILED;
142 }
143 info.op.MarkAsRunning();
144 }
145 else
146 {
147 }
148
149 return ErrorCode::OK;
150 }
151
152 static ErrorCode ReadFun(ReadPort &port)
153 {
154 STM32UART *uart = CONTAINER_OF(&port, STM32UART, read_port_);
155 UNUSED(uart);
157
158 if (port.queue_block_->Peek(block) != ErrorCode::OK)
159 {
160 return ErrorCode::EMPTY;
161 }
162
164
165 if (port.queue_data_->Size() >= block.data_.size_)
166 {
167 port.queue_data_->PopBatch(block.data_.addr_, block.data_.size_);
168 port.queue_block_->Pop();
169 port.read_size_ = block.data_.size_;
170 block.op_.UpdateStatus(false, ErrorCode::OK);
171 return ErrorCode::OK;
172 }
173 else
174 {
175 return ErrorCode::EMPTY;
176 }
177 }
178
182 dma_buff_rx_(dma_buff_rx),
183 dma_buff_tx_(dma_buff_tx),
184 uart_handle_(uart_handle),
185 id_(STM32_UART_GetID(uart_handle_->Instance))
186 {
187 ASSERT(id_ != STM32_UART_ID_ERROR);
188
189 map[id_] = this;
190
191 if ((uart_handle->Init.Mode & UART_MODE_TX) == UART_MODE_TX)
192 {
193 ASSERT(uart_handle_->hdmatx != NULL);
195 }
196
197 if ((uart_handle->Init.Mode & UART_MODE_RX) == UART_MODE_RX)
198 {
199 ASSERT(uart_handle->hdmarx != NULL);
201
202 HAL_UART_Receive_DMA(uart_handle, reinterpret_cast<uint8_t *>(dma_buff_rx_.addr_),
203 dma_buff_rx_.size_);
205 }
206 }
207
209 {
210 uart_handle_->Init.BaudRate = config.baudrate;
211
212 switch (config.parity)
213 {
215 uart_handle_->Init.Parity = UART_PARITY_NONE;
216 uart_handle_->Init.WordLength = UART_WORDLENGTH_8B;
217 break;
219 uart_handle_->Init.Parity = UART_PARITY_EVEN;
220 uart_handle_->Init.WordLength = UART_WORDLENGTH_9B;
221 break;
223 uart_handle_->Init.Parity = UART_PARITY_ODD;
224 uart_handle_->Init.WordLength = UART_WORDLENGTH_9B;
225 break;
226 default:
227 ASSERT(false);
228 }
229
230 switch (config.stop_bits)
231 {
232 case 1:
233 uart_handle_->Init.StopBits = UART_STOPBITS_1;
234 break;
235 case 2:
236 uart_handle_->Init.StopBits = UART_STOPBITS_2;
237 break;
238 default:
239 ASSERT(false);
240 }
241
242 if (HAL_UART_Init(uart_handle_) != HAL_OK)
243 {
244 return ErrorCode::INIT_ERR;
245 }
246 return ErrorCode::OK;
247 }
248
249 RawData dma_buff_rx_, dma_buff_tx_;
250
251 UART_HandleTypeDef *uart_handle_;
252
253 stm32_uart_id_t id_ = STM32_UART_ID_ERROR;
254
255 static STM32UART *map[STM32_UART_NUMBER]; // NOLINT
256};
257
258} // namespace LibXR
259
260#endif
void MarkAsRunning()
标记操作为运行状态。 Marks the operation as running.
Definition libxr_rw.hpp:236
原始数据封装类。 A class for encapsulating raw data.
size_t size_
数据大小(字节)。 The size of the data (in bytes).
void * addr_
数据存储地址。 The storage address of the data.
Read information block structure.
Definition libxr_rw.hpp:286
ReadOperation op_
Read operation instance. 读取操作实例。
Definition libxr_rw.hpp:289
ReadPort class for handling read operations.
Definition libxr_rw.hpp:309
ErrorCode SetConfig(UART::Configuration config)
设置 UART 配置 / Sets the UART configuration
通用异步收发传输(UART)基类 / Abstract base class for Universal Asynchronous Receiver-Transmitter (UART)
Definition uart.hpp:19
@ NO_PARITY
无校验 / No parity
@ ODD
奇校验 / Odd parity
@ EVEN
偶校验 / Even parity
WritePort write_port_
写入端口 / Write port
Definition uart.hpp:52
ReadPort read_port_
读取端口 / Read port
Definition uart.hpp:51
WritePort class for handling write operations.
Definition libxr_rw.hpp:503
LibXR Color Control Library / LibXR终端颜色控制库
ErrorCode(* ReadFun)(ReadPort &port)
Function pointer type for read operations.
Definition libxr_rw.hpp:279
ErrorCode(* WriteFun)(WritePort &port)
Function pointer type for write operations.
Definition libxr_rw.hpp:275
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值
UART 配置结构体 / UART configuration structure.
Definition uart.hpp:44