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

278 : UART(&_read_port, &_write_port),
279 _read_port(dma_buff_rx.size_),
280 _write_port(tx_queue_size, dma_buff_tx.size_ / 2),
281 dma_buff_rx_(dma_buff_rx),
282 dma_buff_tx_(dma_buff_tx),
283 uart_handle_(uart_handle),
284 id_(stm32_uart_get_id(uart_handle_->Instance))
285{
286 ASSERT(id_ != STM32_UART_ID_ERROR);
287
288 map[id_] = this;
289
290 if ((uart_handle->Init.Mode & UART_MODE_TX) == UART_MODE_TX)
291 {
292 ASSERT(uart_handle_->hdmatx != NULL);
293 _write_port = WriteFun;
294 }
295
296 SetRxDMA();
297}
size_t size_
数据大小(字节)。 The size of the data (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.
Definition libxr_rw.hpp:245

Member Function Documentation

◆ ReadFun()

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

Definition at line 274 of file stm32_uart.cpp.

274{ return ErrorCode::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 299 of file stm32_uart.cpp.

300{
301 HAL_UART_DeInit(uart_handle_);
302 uart_handle_->Init.BaudRate = config.baudrate;
303
304 switch (config.parity)
305 {
307 uart_handle_->Init.Parity = UART_PARITY_NONE;
308 uart_handle_->Init.WordLength = UART_WORDLENGTH_8B;
309 break;
311 uart_handle_->Init.Parity = UART_PARITY_EVEN;
312 uart_handle_->Init.WordLength = UART_WORDLENGTH_9B;
313 break;
315 uart_handle_->Init.Parity = UART_PARITY_ODD;
316 uart_handle_->Init.WordLength = UART_WORDLENGTH_9B;
317 break;
318 default:
319 ASSERT(false);
320 }
321
322 switch (config.stop_bits)
323 {
324 case 1:
325 uart_handle_->Init.StopBits = UART_STOPBITS_1;
326 break;
327 case 2:
328 uart_handle_->Init.StopBits = UART_STOPBITS_2;
329 break;
330 default:
331 ASSERT(false);
332 }
333
334 if (HAL_UART_Init(uart_handle_) != HAL_OK)
335 {
336 return ErrorCode::INIT_ERR;
337 }
338
339 last_rx_pos_ = 0;
340
341 SetRxDMA();
342
343 if (tx_busy_.IsSet())
344 {
345 HAL_UART_Transmit_DMA(uart_handle_, dma_buff_tx_.ActiveBuffer(),
346 dma_buff_tx_.GetActiveLength());
347 }
348
349 return ErrorCode::OK;
350}
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
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 352 of file stm32_uart.cpp.

353{
354 if ((uart_handle_->Init.Mode & UART_MODE_RX) == UART_MODE_RX)
355 {
356 ASSERT(uart_handle_->hdmarx != NULL);
357
358 uart_handle_->hdmarx->Init.Mode = DMA_CIRCULAR;
359 HAL_DMA_Init(uart_handle_->hdmarx);
360
361 HAL_UARTEx_ReceiveToIdle_DMA(
362 uart_handle_, reinterpret_cast<uint8_t*>(dma_buff_rx_.addr_), dma_buff_rx_.size_);
363 _read_port = ReadFun;
364 }
365}
void * addr_
数据存储地址。 The storage address of the data.
ErrorCode(* ReadFun)(ReadPort &port, bool in_isr)
Function pointer type for read operations.
Definition libxr_rw.hpp:249

◆ WriteFun()

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

Definition at line 195 of file stm32_uart.cpp.

196{
197 STM32UART* uart = CONTAINER_OF(&port, STM32UART, _write_port);
198
199 if (uart->in_tx_isr.IsSet())
200 {
201 return ErrorCode::PENDING;
202 }
203
204 if (!uart->dma_buff_tx_.HasPending())
205 {
206 WriteInfoBlock info;
207 if (port.queue_info_->Peek(info) != ErrorCode::OK)
208 {
209 return ErrorCode::PENDING;
210 }
211
212 uint8_t* buffer = nullptr;
213 bool use_pending = false;
214
215 if (uart->uart_handle_->gState == HAL_UART_STATE_READY)
216 {
217 buffer = reinterpret_cast<uint8_t*>(uart->dma_buff_tx_.ActiveBuffer());
218 }
219 else
220 {
221 buffer = reinterpret_cast<uint8_t*>(uart->dma_buff_tx_.PendingBuffer());
222 use_pending = true;
223 }
224
225 if (port.queue_data_->PopBatch(reinterpret_cast<uint8_t*>(buffer), info.data.size_) !=
226 ErrorCode::OK)
227 {
228 ASSERT(false);
229 return ErrorCode::EMPTY;
230 }
231
232 if (use_pending)
233 {
234 uart->dma_buff_tx_.SetPendingLength(info.data.size_);
235 uart->dma_buff_tx_.EnablePending();
236 if (uart->uart_handle_->gState == HAL_UART_STATE_READY &&
237 uart->dma_buff_tx_.HasPending())
238 {
239 uart->dma_buff_tx_.Switch();
240 }
241 else
242 {
243 return ErrorCode::PENDING;
244 }
245 }
246
247 port.queue_info_->Pop(uart->write_info_active_);
248
249#if defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
250 SCB_CleanDCache_by_Addr(
251 reinterpret_cast<uint32_t*>(uart->dma_buff_tx_.ActiveBuffer()), info.data.size_);
252#endif
253
254 uart->dma_buff_tx_.SetActiveLength(info.data.size_);
255 uart->tx_busy_.Set();
256 auto ans = HAL_UART_Transmit_DMA(
257 uart->uart_handle_, static_cast<uint8_t*>(uart->dma_buff_tx_.ActiveBuffer()),
258 info.data.size_);
259
260 if (ans != HAL_OK)
261 {
262 uart->tx_busy_.Clear();
263 return ErrorCode::FAILED;
264 }
265 else
266 {
267 return ErrorCode::OK;
268 }
269 }
270
271 return ErrorCode::PENDING;
272}
size_t size_
数据大小(字节)。 The size of the data (in bytes).
void SetActiveLength(size_t length)
设置当前活动缓冲区的数据长度 Sets the size of the active buffer
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 * PendingBuffer() const
获取备用缓冲区的指针 Returns the pending (inactive) buffer
void Switch()
切换到备用缓冲区(若其有效) Switches to the pending buffer if it's valid
void Set() noexcept
置位标志 / Set the flag
Definition flag.hpp:125
void Clear() noexcept
清除标志 / Clear the flag
Definition flag.hpp:130
ErrorCode PopBatch(Data *data, size_t size)
批量弹出数据 / Pops multiple elements from the queue
STM32 UART 驱动实现 / STM32 UART driver implementation.
ConstRawData data
Data buffer. 数据缓冲区。
Definition libxr_rw.hpp:263

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: