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 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_GetID(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 if ((uart_handle->Init.Mode & UART_MODE_RX) == UART_MODE_RX)
295 {
296 ASSERT(uart_handle->hdmarx != NULL);
297
298 uart_handle_->hdmarx->Init.Mode = DMA_CIRCULAR;
299 HAL_DMA_Init(uart_handle_->hdmarx);
300
301 __HAL_UART_ENABLE_IT(uart_handle, UART_IT_IDLE);
302
303 HAL_UART_Receive_DMA(uart_handle, reinterpret_cast<uint8_t *>(dma_buff_rx_.addr_),
304 dma_buff_rx_.size_);
305 _read_port = ReadFun;
306 }
307}
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:64
ErrorCode(* ReadFun)(ReadPort &port)
Function pointer type for read operations.
Definition libxr_rw.hpp:245
ErrorCode(* WriteFun)(WritePort &port)
Function pointer type for write operations.
Definition libxr_rw.hpp:241

Member Function Documentation

◆ ReadFun()

ErrorCode STM32UART::ReadFun ( ReadPort & port)
static

Definition at line 266 of file stm32_uart.cpp.

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

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

310{
311 uart_handle_->Init.BaudRate = config.baudrate;
312
313 switch (config.parity)
314 {
316 uart_handle_->Init.Parity = UART_PARITY_NONE;
317 uart_handle_->Init.WordLength = UART_WORDLENGTH_8B;
318 break;
320 uart_handle_->Init.Parity = UART_PARITY_EVEN;
321 uart_handle_->Init.WordLength = UART_WORDLENGTH_9B;
322 break;
324 uart_handle_->Init.Parity = UART_PARITY_ODD;
325 uart_handle_->Init.WordLength = UART_WORDLENGTH_9B;
326 break;
327 default:
328 ASSERT(false);
329 }
330
331 switch (config.stop_bits)
332 {
333 case 1:
334 uart_handle_->Init.StopBits = UART_STOPBITS_1;
335 break;
336 case 2:
337 uart_handle_->Init.StopBits = UART_STOPBITS_2;
338 break;
339 default:
340 ASSERT(false);
341 }
342
343 if (HAL_UART_Init(uart_handle_) != HAL_OK)
344 {
345 return ErrorCode::INIT_ERR;
346 }
347 return ErrorCode::OK;
348}
@ NO_PARITY
无校验 / No parity
@ ODD
奇校验 / Odd parity
@ EVEN
偶校验 / Even parity
uint8_t stop_bits
停止位长度 / Number of stop bits
Definition uart.hpp:48
Parity parity
校验模式 / Parity mode
Definition uart.hpp:46
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 if (!uart->dma_buff_tx_.HasPending())
199 {
200 WriteInfoBlock info;
201 if (port.queue_info_->Peek(info) != ErrorCode::OK)
202 {
203 return ErrorCode::EMPTY;
204 }
205
206 uint8_t *buffer = nullptr;
207 bool use_pending = false;
208
209 if (uart->uart_handle_->gState == HAL_UART_STATE_READY)
210 {
211 buffer = reinterpret_cast<uint8_t *>(uart->dma_buff_tx_.ActiveBuffer());
212 }
213 else
214 {
215 buffer = reinterpret_cast<uint8_t *>(uart->dma_buff_tx_.PendingBuffer());
216 use_pending = true;
217 }
218
219 if (port.queue_data_->PopBatch(reinterpret_cast<uint8_t *>(buffer),
220 info.data.size_) != ErrorCode::OK)
221 {
222 ASSERT(false);
223 return ErrorCode::EMPTY;
224 }
225
226 if (use_pending)
227 {
228 uart->dma_buff_tx_.SetPendingLength(info.data.size_);
229 uart->dma_buff_tx_.EnablePending();
230 if (uart->uart_handle_->gState == HAL_UART_STATE_READY &&
231 uart->dma_buff_tx_.HasPending())
232 {
233 uart->dma_buff_tx_.Switch();
234 }
235 else
236 {
237 return ErrorCode::FAILED;
238 }
239 }
240
241 port.queue_info_->Pop(uart->write_info_active_);
242
243#if __DCACHE_PRESENT
244 SCB_CleanDCache_by_Addr(
245 reinterpret_cast<uint32_t *>(uart->dma_buff_tx_.ActiveBuffer()), info.data.size_);
246#endif
247
248 auto ans = HAL_UART_Transmit_DMA(
249 uart->uart_handle_, static_cast<uint8_t *>(uart->dma_buff_tx_.ActiveBuffer()),
250 info.data.size_);
251
252 if (ans != HAL_OK)
253 {
254 port.Finish(false, ErrorCode::FAILED, info, 0);
255 return ErrorCode::FAILED;
256 }
257 else
258 {
259 return ErrorCode::OK;
260 }
261 }
262
263 return ErrorCode::FAILED;
264}
size_t size_
数据大小(字节)。 The size of the data (in bytes).
uint8_t * PendingBuffer()
获取备用缓冲区的指针 Returns the pending (inactive) buffer
void EnablePending()
手动启用 pending 状态 Manually sets the pending state to true
bool HasPending() const
判断是否有待切换的缓冲区 Checks whether a pending buffer is ready
void SetPendingLength(size_t size)
设置备用缓冲区的数据长度 Sets the size of the pending buffer
void Switch()
切换到备用缓冲区(若其有效) Switches to the pending buffer if it's valid
uint8_t * ActiveBuffer()
获取当前正在使用的缓冲区指针 Returns the currently active buffer
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:208

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: