libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::STM32UART Class Reference

STM32 UART 驱动实现 / STM32 UART driver implementation. More...

#include <stm32_uart.hpp>

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)
 构造 UART 对象 / Construct UART object
 
ErrorCode SetConfig (UART::Configuration config)
 设置 UART 配置 / Sets the UART configuration
 
void SetRxDMA ()
 
- 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, bool in_isr=false)
 
template<typename OperationType , typename = std::enable_if_t<std::is_base_of_v< ReadOperation, std::decay_t<OperationType>>>>
ErrorCode Read (RawData data, OperationType &&op, bool in_isr=false)
 

Static Public Member Functions

static ErrorCode WriteFun (WritePort &port, bool in_isr)
 
static ErrorCode ReadFun (ReadPort &port, bool in_isr)
 

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_
 
Flag::Plain in_tx_isr
 
Flag::Plain tx_busy_
 
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

STM32 UART 驱动实现 / STM32 UART driver implementation.

Definition at line 117 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 )

构造 UART 对象 / Construct UART object

Definition at line 274 of file stm32_uart.cpp.

276 : UART(&_read_port, &_write_port),
277 _read_port(dma_buff_rx.size_),
278 _write_port(tx_queue_size, dma_buff_tx.size_ / 2),
279 dma_buff_rx_(dma_buff_rx),
280 dma_buff_tx_(dma_buff_tx),
281 uart_handle_(uart_handle),
282 id_(stm32_uart_get_id(uart_handle_->Instance))
283{
284 ASSERT(id_ != STM32_UART_ID_ERROR);
285
286 map[id_] = this;
287
288 if ((uart_handle->Init.Mode & UART_MODE_TX) == UART_MODE_TX)
289 {
290 ASSERT(uart_handle_->hdmatx != NULL);
291 _write_port = WriteFun;
292 }
293
294 SetRxDMA();
295}
size_t size_
数据字节数 / Data size in bytes
UART(ReadPortType *read_port, WritePortType *write_port)
UART 构造函数 / UART constructor.
Definition uart.hpp:66
ErrorCode(* WriteFun)(WritePort &port, bool in_isr)
Function pointer type for write operations.

Member Function Documentation

◆ ReadFun()

ErrorCode STM32UART::ReadFun ( ReadPort & port,
bool in_isr )
static

Definition at line 272 of file stm32_uart.cpp.

272{ return ErrorCode::PENDING; }
@ PENDING
等待中 | Pending

◆ 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 297 of file stm32_uart.cpp.

298{
299 HAL_UART_DeInit(uart_handle_);
300 uart_handle_->Init.BaudRate = config.baudrate;
301
302 switch (config.parity)
303 {
305 uart_handle_->Init.Parity = UART_PARITY_NONE;
306 uart_handle_->Init.WordLength = UART_WORDLENGTH_8B;
307 break;
309 uart_handle_->Init.Parity = UART_PARITY_EVEN;
310 uart_handle_->Init.WordLength = UART_WORDLENGTH_9B;
311 break;
313 uart_handle_->Init.Parity = UART_PARITY_ODD;
314 uart_handle_->Init.WordLength = UART_WORDLENGTH_9B;
315 break;
316 default:
317 ASSERT(false);
318 }
319
320 switch (config.stop_bits)
321 {
322 case 1:
323 uart_handle_->Init.StopBits = UART_STOPBITS_1;
324 break;
325 case 2:
326 uart_handle_->Init.StopBits = UART_STOPBITS_2;
327 break;
328 default:
329 ASSERT(false);
330 }
331
332 if (HAL_UART_Init(uart_handle_) != HAL_OK)
333 {
334 return ErrorCode::INIT_ERR;
335 }
336
337 last_rx_pos_ = 0;
338
339 SetRxDMA();
340
341 if (tx_busy_.IsSet())
342 {
343 HAL_UART_Transmit_DMA(uart_handle_, dma_buff_tx_.ActiveBuffer(),
344 dma_buff_tx_.GetActiveLength());
345 }
346
347 return ErrorCode::OK;
348}
uint8_t * ActiveBuffer() const
获取当前正在使用的缓冲区指针 Returns the currently active buffer
size_t GetActiveLength() const
获取当前活动缓冲区中准备好的数据长度 Gets the size of valid data in active buffer
bool IsSet() const noexcept
判断是否已置位 / Check whether the flag is set
Definition flag.hpp:138
@ NO_PARITY
无校验 / No parity
@ ODD
奇校验 / Odd parity
@ EVEN
偶校验 / Even parity
@ INIT_ERR
初始化错误 | Initialization error
@ OK
操作成功 | Operation successful
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

◆ SetRxDMA()

void STM32UART::SetRxDMA ( )

Definition at line 350 of file stm32_uart.cpp.

351{
352 if ((uart_handle_->Init.Mode & UART_MODE_RX) == UART_MODE_RX)
353 {
354 ASSERT(uart_handle_->hdmarx != NULL);
355
356 uart_handle_->hdmarx->Init.Mode = DMA_CIRCULAR;
357 HAL_DMA_Init(uart_handle_->hdmarx);
358
359 HAL_UARTEx_ReceiveToIdle_DMA(
360 uart_handle_, reinterpret_cast<uint8_t*>(dma_buff_rx_.addr_), dma_buff_rx_.size_);
361 _read_port = ReadFun;
362 }
363}
void * addr_
数据起始地址 / Data start address
ErrorCode(* ReadFun)(ReadPort &port, bool in_isr)
Function pointer type for read notifications.

◆ WriteFun()

ErrorCode STM32UART::WriteFun ( WritePort & port,
bool in_isr )
static

Definition at line 196 of file stm32_uart.cpp.

197{
198 auto* uart = LibXR::ContainerOf(&port, &STM32UART::_write_port);
199
200 if (uart->in_tx_isr.IsSet())
201 {
202 return ErrorCode::PENDING;
203 }
204
205 if (!uart->dma_buff_tx_.HasPending())
206 {
207 WriteInfoBlock info;
208 if (port.queue_info_->Peek(info) != ErrorCode::OK)
209 {
210 return ErrorCode::PENDING;
211 }
212
213 uint8_t* buffer = nullptr;
214 bool use_pending = false;
215
216 if (uart->uart_handle_->gState == HAL_UART_STATE_READY)
217 {
218 buffer = reinterpret_cast<uint8_t*>(uart->dma_buff_tx_.ActiveBuffer());
219 }
220 else
221 {
222 buffer = reinterpret_cast<uint8_t*>(uart->dma_buff_tx_.PendingBuffer());
223 use_pending = true;
224 }
225
226 if (port.queue_data_->PopBatch(reinterpret_cast<uint8_t*>(buffer), info.data.size_) !=
228 {
229 ASSERT(false);
230 return ErrorCode::EMPTY;
231 }
232
233 if (use_pending)
234 {
235 uart->dma_buff_tx_.SetPendingLength(info.data.size_);
236 uart->dma_buff_tx_.EnablePending();
237 if (uart->uart_handle_->gState == HAL_UART_STATE_READY &&
238 uart->dma_buff_tx_.HasPending())
239 {
240 uart->dma_buff_tx_.Switch();
241 }
242 else
243 {
244 return ErrorCode::PENDING;
245 }
246 }
247
248 port.queue_info_->Pop(uart->write_info_active_);
249
250 STM32_CleanDCacheByAddr(uart->dma_buff_tx_.ActiveBuffer(), info.data.size_);
251
252 uart->dma_buff_tx_.SetActiveLength(info.data.size_);
253 uart->tx_busy_.Set();
254 auto ans = HAL_UART_Transmit_DMA(
255 uart->uart_handle_, static_cast<uint8_t*>(uart->dma_buff_tx_.ActiveBuffer()),
256 info.data.size_);
257
258 if (ans != HAL_OK)
259 {
260 uart->tx_busy_.Clear();
261 return ErrorCode::FAILED;
262 }
263 else
264 {
265 return ErrorCode::OK;
266 }
267 }
268
269 return ErrorCode::PENDING;
270}
size_t size_
数据字节数 / Data size in bytes
ErrorCode PopBatch(Data *data, size_t size)
批量弹出数据 / Pops multiple elements from the queue
LockFreeQueue< uint8_t > * queue_data_
Payload queue for pending write bytes. 挂起写入字节的数据队列。
LockFreeQueue< WriteInfoBlock > * queue_info_
Metadata queue for pending write batches. 挂起写批次的元数据队列。
@ FAILED
操作失败 | Operation failed
@ EMPTY
为空 | Empty
OwnerType * ContainerOf(MemberType *ptr, MemberType OwnerType::*member) noexcept
通过成员指针恢复其所属对象指针
void STM32_CleanDCacheByAddr(const void *addr, size_t size)
Cleans D-Cache lines covering the specified memory range.
ConstRawData data
Data buffer. 数据缓冲区。

Field Documentation

◆ _read_port

ReadPort LibXR::STM32UART::_read_port

Definition at line 134 of file stm32_uart.hpp.

◆ _write_port

WritePort LibXR::STM32UART::_write_port

Definition at line 135 of file stm32_uart.hpp.

◆ dma_buff_rx_

RawData LibXR::STM32UART::dma_buff_rx_

Definition at line 137 of file stm32_uart.hpp.

◆ dma_buff_tx_

DoubleBuffer LibXR::STM32UART::dma_buff_tx_

Definition at line 138 of file stm32_uart.hpp.

◆ id_

stm32_uart_id_t LibXR::STM32UART::id_ = STM32_UART_ID_ERROR

Definition at line 147 of file stm32_uart.hpp.

◆ in_tx_isr

Flag::Plain LibXR::STM32UART::in_tx_isr

Definition at line 145 of file stm32_uart.hpp.

◆ last_rx_pos_

size_t LibXR::STM32UART::last_rx_pos_ = 0

Definition at line 141 of file stm32_uart.hpp.

◆ map

STM32UART * STM32UART::map = {nullptr}
static

Definition at line 149 of file stm32_uart.hpp.

◆ tx_busy_

Flag::Plain LibXR::STM32UART::tx_busy_

Definition at line 145 of file stm32_uart.hpp.

◆ uart_handle_

UART_HandleTypeDef* LibXR::STM32UART::uart_handle_

Definition at line 143 of file stm32_uart.hpp.

◆ write_info_active_

WriteInfoBlock LibXR::STM32UART::write_info_active_

Definition at line 139 of file stm32_uart.hpp.


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