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()

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

Definition at line 190 of file stm32_uart.hpp.

192 : UART(&_read_port, &_write_port),
193 _read_port(dma_buff_rx.size_),
194 _write_port(tx_queue_size, dma_buff_tx.size_ / 2),
195 dma_buff_rx_(dma_buff_rx),
196 dma_buff_tx_(dma_buff_tx),
197 uart_handle_(uart_handle),
198 id_(STM32_UART_GetID(uart_handle_->Instance))
199 {
200 ASSERT(id_ != STM32_UART_ID_ERROR);
201
202 map[id_] = this;
203
204 if ((uart_handle->Init.Mode & UART_MODE_TX) == UART_MODE_TX)
205 {
206 ASSERT(uart_handle_->hdmatx != NULL);
207 _write_port = WriteFun;
208 }
209
210 if ((uart_handle->Init.Mode & UART_MODE_RX) == UART_MODE_RX)
211 {
212 ASSERT(uart_handle->hdmarx != NULL);
213
214 uart_handle_->hdmarx->Init.Mode = DMA_CIRCULAR;
215 HAL_DMA_Init(uart_handle_->hdmarx);
216
217 __HAL_UART_ENABLE_IT(uart_handle, UART_IT_IDLE);
218
219 HAL_UART_Receive_DMA(uart_handle, reinterpret_cast<uint8_t *>(dma_buff_rx_.addr_),
220 dma_buff_rx_.size_);
221 _read_port = ReadFun;
222 }
223 }
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:247
ErrorCode(* WriteFun)(WritePort &port)
Function pointer type for write operations.
Definition libxr_rw.hpp:243

Member Function Documentation

◆ ReadFun()

static ErrorCode LibXR::STM32UART::ReadFun ( ReadPort & port)
inlinestatic

Definition at line 182 of file stm32_uart.hpp.

183 {
184 STM32UART *uart = CONTAINER_OF(&port, STM32UART, _read_port);
185 UNUSED(uart);
186
187 return ErrorCode::EMPTY;
188 }

◆ SetConfig()

ErrorCode LibXR::STM32UART::SetConfig ( UART::Configuration config)
inlinevirtual

设置 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 225 of file stm32_uart.hpp.

226 {
227 uart_handle_->Init.BaudRate = config.baudrate;
228
229 switch (config.parity)
230 {
232 uart_handle_->Init.Parity = UART_PARITY_NONE;
233 uart_handle_->Init.WordLength = UART_WORDLENGTH_8B;
234 break;
236 uart_handle_->Init.Parity = UART_PARITY_EVEN;
237 uart_handle_->Init.WordLength = UART_WORDLENGTH_9B;
238 break;
240 uart_handle_->Init.Parity = UART_PARITY_ODD;
241 uart_handle_->Init.WordLength = UART_WORDLENGTH_9B;
242 break;
243 default:
244 ASSERT(false);
245 }
246
247 switch (config.stop_bits)
248 {
249 case 1:
250 uart_handle_->Init.StopBits = UART_STOPBITS_1;
251 break;
252 case 2:
253 uart_handle_->Init.StopBits = UART_STOPBITS_2;
254 break;
255 default:
256 ASSERT(false);
257 }
258
259 if (HAL_UART_Init(uart_handle_) != HAL_OK)
260 {
261 return ErrorCode::INIT_ERR;
262 }
263 return ErrorCode::OK;
264 }
@ NO_PARITY
无校验 / No parity
@ ODD
奇校验 / Odd parity
@ EVEN
偶校验 / Even parity

◆ WriteFun()

static ErrorCode LibXR::STM32UART::WriteFun ( WritePort & port)
inlinestatic

Definition at line 116 of file stm32_uart.hpp.

117 {
118 STM32UART *uart = CONTAINER_OF(&port, STM32UART, _write_port);
119 if (!uart->dma_buff_tx_.HasPending())
120 {
121 WriteInfoBlock info;
122 if (port.queue_info_->Peek(info) != ErrorCode::OK)
123 {
124 return ErrorCode::EMPTY;
125 }
126
127 uint8_t *buffer = nullptr;
128 bool use_pending = false;
129
130 if (uart->uart_handle_->gState == HAL_UART_STATE_READY)
131 {
132 buffer = reinterpret_cast<uint8_t *>(uart->dma_buff_tx_.ActiveBuffer());
133 }
134 else
135 {
136 buffer = reinterpret_cast<uint8_t *>(uart->dma_buff_tx_.PendingBuffer());
137 use_pending = true;
138 }
139
140 if (port.queue_data_->PopBatch(reinterpret_cast<uint8_t *>(buffer),
141 info.data.size_) != ErrorCode::OK)
142 {
143 ASSERT(false);
144 return ErrorCode::EMPTY;
145 }
146
147 if (use_pending)
148 {
149 uart->dma_buff_tx_.EnablePending();
150 if (uart->uart_handle_->gState == HAL_UART_STATE_READY)
151 {
152 uart->dma_buff_tx_.Switch();
153 }
154 else
155 {
156 return ErrorCode::FAILED;
157 }
158 }
159
160 port.queue_info_->Pop(uart->write_info_active_);
161
162#if __DCACHE_PRESENT
163 SCB_CleanDCache_by_Addr(reinterpret_cast<uint32_t *>(uart->dma_buff_tx_.ActiveBuffer()),
164 info.data.size_);
165#endif
166
167 auto ans = HAL_UART_Transmit_DMA(
168 uart->uart_handle_, static_cast<uint8_t *>(uart->dma_buff_tx_.ActiveBuffer()),
169 info.data.size_);
170
171 if (ans != HAL_OK)
172 {
173 port.Finish(false, ErrorCode::FAILED, info, 0);
174 }
175
176 return ErrorCode::FAILED;
177 }
178
179 return ErrorCode::FAILED;
180 }

Field Documentation

◆ _read_port

ReadPort LibXR::STM32UART::_read_port

Definition at line 266 of file stm32_uart.hpp.

◆ _write_port

WritePort LibXR::STM32UART::_write_port

Definition at line 267 of file stm32_uart.hpp.

◆ dma_buff_rx_

RawData LibXR::STM32UART::dma_buff_rx_

Definition at line 269 of file stm32_uart.hpp.

◆ dma_buff_tx_

DoubleBuffer LibXR::STM32UART::dma_buff_tx_

Definition at line 270 of file stm32_uart.hpp.

◆ id_

stm32_uart_id_t LibXR::STM32UART::id_ = STM32_UART_ID_ERROR

Definition at line 277 of file stm32_uart.hpp.

◆ last_rx_pos_

size_t LibXR::STM32UART::last_rx_pos_ = 0

Definition at line 273 of file stm32_uart.hpp.

◆ map

STM32UART * STM32UART::map = {nullptr}
static

Definition at line 279 of file stm32_uart.hpp.

◆ uart_handle_

UART_HandleTypeDef* LibXR::STM32UART::uart_handle_

Definition at line 275 of file stm32_uart.hpp.

◆ write_info_active_

WriteInfoBlock LibXR::STM32UART::write_info_active_

Definition at line 271 of file stm32_uart.hpp.


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