libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::STM32UART Class Reference
Inheritance diagram for LibXR::STM32UART:
[legend]
Collaboration diagram for LibXR::STM32UART:
[legend]

Public Member Functions

 STM32UART (UART_HandleTypeDef *uart_handle, RawData dma_buff_rx, RawData dma_buff_tx, uint32_t tx_queue_size=5)
 
ErrorCode SetConfig (UART::Configuration config)
 设置 UART 配置 / Sets the UART configuration
 
- Public Member Functions inherited from LibXR::UART
template<typename ReadPortType = ReadPort, typename WritePortType = WritePort>
 UART (ReadPortType *read_port, WritePortType *write_port)
 UART 构造函数 / UART constructor.
 
template<typename OperationType , typename = std::enable_if_t<std::is_base_of_v< WriteOperation, std::decay_t<OperationType>>>>
ErrorCode Write (ConstRawData data, OperationType &&op)
 
template<typename OperationType , typename = std::enable_if_t<std::is_base_of_v< ReadOperation, std::decay_t<OperationType>>>>
ErrorCode Read (RawData data, OperationType &&op)
 

Static Public Member Functions

static ErrorCode WriteFun (WritePort &port)
 
static ErrorCode ReadFun (ReadPort &port)
 

Data Fields

ReadPort _read_port
 
WritePort _write_port
 
RawData dma_buff_rx_
 
DoubleBuffer dma_buff_tx_
 
WriteInfoBlock write_info_active_
 
size_t last_rx_pos_ = 0
 
UART_HandleTypeDef * uart_handle_
 
stm32_uart_id_t id_ = STM32_UART_ID_ERROR
 
- Data Fields inherited from LibXR::UART
ReadPortread_port_
 读取端口 / Read port
 
WritePortwrite_port_
 写入端口 / Write port
 

Static Public Attributes

static STM32UARTmap [STM32_UART_NUMBER] = {nullptr}
 

Additional Inherited Members

- Public Types inherited from LibXR::UART
enum class  Parity : uint8_t { NO_PARITY = 0 , EVEN = 1 , ODD = 2 }
 奇偶校验模式 / Parity mode More...
 

Detailed Description

Definition at line 113 of file stm32_uart.hpp.

Constructor & Destructor Documentation

◆ STM32UART()

STM32UART::STM32UART ( UART_HandleTypeDef * uart_handle,
RawData dma_buff_rx,
RawData dma_buff_tx,
uint32_t tx_queue_size = 5 )

Definition at line 275 of file stm32_uart.cpp.

277 : UART(&_read_port, &_write_port),
278 _read_port(dma_buff_rx.size_),
279 _write_port(tx_queue_size, dma_buff_tx.size_ / 2),
280 dma_buff_rx_(dma_buff_rx),
281 dma_buff_tx_(dma_buff_tx),
282 uart_handle_(uart_handle),
283 id_(STM32_UART_GetID(uart_handle_->Instance))
284{
285 ASSERT(id_ != STM32_UART_ID_ERROR);
286
287 map[id_] = this;
288
289 if ((uart_handle->Init.Mode & UART_MODE_TX) == UART_MODE_TX)
290 {
291 ASSERT(uart_handle_->hdmatx != NULL);
292 _write_port = WriteFun;
293 }
294
295 if ((uart_handle->Init.Mode & UART_MODE_RX) == UART_MODE_RX)
296 {
297 ASSERT(uart_handle->hdmarx != NULL);
298
299 uart_handle_->hdmarx->Init.Mode = DMA_CIRCULAR;
300 HAL_DMA_Init(uart_handle_->hdmarx);
301
302 __HAL_UART_ENABLE_IT(uart_handle, UART_IT_IDLE);
303
304 HAL_UART_Receive_DMA(uart_handle, reinterpret_cast<uint8_t *>(dma_buff_rx_.addr_),
305 dma_buff_rx_.size_);
306 _read_port = ReadFun;
307 }
308}
size_t size_
数据大小(字节)。 The size of the data (in bytes).
void * addr_
数据存储地址。 The storage address of the data.
UART(ReadPortType *read_port, WritePortType *write_port)
UART 构造函数 / UART constructor.
Definition uart.hpp:66
ErrorCode(* ReadFun)(ReadPort &port)
Function pointer type for read operations.
Definition libxr_rw.hpp:246
ErrorCode(* WriteFun)(WritePort &port)
Function pointer type for write operations.
Definition libxr_rw.hpp:242

Member Function Documentation

◆ ReadFun()

ErrorCode STM32UART::ReadFun ( ReadPort & port)
static

Definition at line 267 of file stm32_uart.cpp.

268{
269 STM32UART *uart = CONTAINER_OF(&port, STM32UART, _read_port);
270 UNUSED(uart);
271
272 return ErrorCode::EMPTY;
273}

◆ SetConfig()

ErrorCode STM32UART::SetConfig ( UART::Configuration config)
virtual

设置 UART 配置 / Sets the UART configuration

Parameters
configUART 配置信息 / UART configuration settings
Returns
返回操作状态,成功时返回 ErrorCode::OK,否则返回相应错误码 / Returns the operation status, ErrorCode::OK if successful, otherwise an error code

该方法为纯虚函数,子类必须实现具体的 UART 配置逻辑。 This is a pure virtual function. Subclasses must implement the specific UART configuration logic.

Implements LibXR::UART.

Definition at line 310 of file stm32_uart.cpp.

311{
312 uart_handle_->Init.BaudRate = config.baudrate;
313
314 switch (config.parity)
315 {
317 uart_handle_->Init.Parity = UART_PARITY_NONE;
318 uart_handle_->Init.WordLength = UART_WORDLENGTH_8B;
319 break;
321 uart_handle_->Init.Parity = UART_PARITY_EVEN;
322 uart_handle_->Init.WordLength = UART_WORDLENGTH_9B;
323 break;
325 uart_handle_->Init.Parity = UART_PARITY_ODD;
326 uart_handle_->Init.WordLength = UART_WORDLENGTH_9B;
327 break;
328 default:
329 ASSERT(false);
330 }
331
332 switch (config.stop_bits)
333 {
334 case 1:
335 uart_handle_->Init.StopBits = UART_STOPBITS_1;
336 break;
337 case 2:
338 uart_handle_->Init.StopBits = UART_STOPBITS_2;
339 break;
340 default:
341 ASSERT(false);
342 }
343
344 if (HAL_UART_Init(uart_handle_) != HAL_OK)
345 {
346 return ErrorCode::INIT_ERR;
347 }
348 return ErrorCode::OK;
349}
@ NO_PARITY
无校验 / No parity
@ ODD
奇校验 / Odd parity
@ EVEN
偶校验 / Even parity
uint8_t stop_bits
停止位长度 / Number of stop bits
Definition uart.hpp:50
Parity parity
校验模式 / Parity mode
Definition uart.hpp:47
uint32_t baudrate
波特率 / Baud rate
Definition uart.hpp:45

◆ WriteFun()

ErrorCode STM32UART::WriteFun ( WritePort & port)
static

Definition at line 195 of file stm32_uart.cpp.

196{
197 STM32UART *uart = CONTAINER_OF(&port, STM32UART, _write_port);
198
199 if (!uart->dma_buff_tx_.HasPending())
200 {
201 WriteInfoBlock info;
202 if (port.queue_info_->Peek(info) != ErrorCode::OK)
203 {
204 return ErrorCode::EMPTY;
205 }
206
207 uint8_t *buffer = nullptr;
208 bool use_pending = false;
209
210 if (uart->uart_handle_->gState == HAL_UART_STATE_READY)
211 {
212 buffer = reinterpret_cast<uint8_t *>(uart->dma_buff_tx_.ActiveBuffer());
213 }
214 else
215 {
216 buffer = reinterpret_cast<uint8_t *>(uart->dma_buff_tx_.PendingBuffer());
217 use_pending = true;
218 }
219
220 if (port.queue_data_->PopBatch(reinterpret_cast<uint8_t *>(buffer),
221 info.data.size_) != ErrorCode::OK)
222 {
223 ASSERT(false);
224 return ErrorCode::EMPTY;
225 }
226
227 if (use_pending)
228 {
229 uart->dma_buff_tx_.SetPendingLength(info.data.size_);
230 uart->dma_buff_tx_.EnablePending();
231 if (uart->uart_handle_->gState == HAL_UART_STATE_READY &&
232 uart->dma_buff_tx_.HasPending())
233 {
234 uart->dma_buff_tx_.Switch();
235 }
236 else
237 {
238 return ErrorCode::FAILED;
239 }
240 }
241
242 port.queue_info_->Pop(uart->write_info_active_);
243
244#if defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
245 SCB_CleanDCache_by_Addr(
246 reinterpret_cast<uint32_t *>(uart->dma_buff_tx_.ActiveBuffer()), info.data.size_);
247#endif
248
249 auto ans = HAL_UART_Transmit_DMA(
250 uart->uart_handle_, static_cast<uint8_t *>(uart->dma_buff_tx_.ActiveBuffer()),
251 info.data.size_);
252
253 if (ans != HAL_OK)
254 {
255 port.Finish(false, ErrorCode::FAILED, info, 0);
256 return ErrorCode::FAILED;
257 }
258 else
259 {
260 return ErrorCode::OK;
261 }
262 }
263
264 return ErrorCode::FAILED;
265}
size_t size_
数据大小(字节)。 The size of the data (in bytes).
void SetPendingLength(size_t length)
设置备用缓冲区的数据长度 Sets the size of the pending buffer
void EnablePending()
手动启用 pending 状态 Manually sets the pending state to true
bool HasPending() const
判断是否有待切换的缓冲区 Checks whether a pending buffer is ready
uint8_t * ActiveBuffer() const
获取当前正在使用的缓冲区指针 Returns the currently active buffer
uint8_t * PendingBuffer() const
获取备用缓冲区的指针 Returns the pending (inactive) buffer
void Switch()
切换到备用缓冲区(若其有效) Switches to the pending buffer if it's valid
ErrorCode PopBatch(Data *data, size_t size)
批量弹出数据 / Pops multiple elements from the queue
void Finish(bool in_isr, ErrorCode ans, WriteInfoBlock &info, uint32_t size)
更新写入操作的状态。 Updates the status of the write operation.
Definition libxr_rw.cpp:207
ConstRawData data
Data buffer. 数据缓冲区。
Definition libxr_rw.hpp:260

Field Documentation

◆ _read_port

ReadPort LibXR::STM32UART::_read_port

Definition at line 125 of file stm32_uart.hpp.

◆ _write_port

WritePort LibXR::STM32UART::_write_port

Definition at line 126 of file stm32_uart.hpp.

◆ dma_buff_rx_

RawData LibXR::STM32UART::dma_buff_rx_

Definition at line 128 of file stm32_uart.hpp.

◆ dma_buff_tx_

DoubleBuffer LibXR::STM32UART::dma_buff_tx_

Definition at line 129 of file stm32_uart.hpp.

◆ id_

stm32_uart_id_t LibXR::STM32UART::id_ = STM32_UART_ID_ERROR

Definition at line 136 of file stm32_uart.hpp.

◆ last_rx_pos_

size_t LibXR::STM32UART::last_rx_pos_ = 0

Definition at line 132 of file stm32_uart.hpp.

◆ map

STM32UART * STM32UART::map = {nullptr}
static

Definition at line 138 of file stm32_uart.hpp.

◆ uart_handle_

UART_HandleTypeDef* LibXR::STM32UART::uart_handle_

Definition at line 134 of file stm32_uart.hpp.

◆ write_info_active_

WriteInfoBlock LibXR::STM32UART::write_info_active_

Definition at line 130 of file stm32_uart.hpp.


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