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

Public Member Functions

 STM32UART (UART_HandleTypeDef *uart_handle, RawData dma_buff_rx, RawData dma_buff_tx, uint32_t rx_queue_size=5, uint32_t tx_queue_size=5)
 
ErrorCode SetConfig (UART::Configuration config)
 设置 UART 配置 / Sets the UART configuration
 
- Public Member Functions inherited from LibXR::UART
 UART (size_t rx_queue_size, size_t rx_buffer_size, size_t tx_queue_size, size_t tx_buffer_size)
 UART 构造函数 / UART constructor.
 

Static Public Member Functions

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

Data Fields

RawData dma_buff_rx_
 
RawData dma_buff_tx_
 
UART_HandleTypeDefuart_handle_
 
stm32_uart_id_t id_ = STM32_UART_ID_ERROR
 
- Data Fields inherited from LibXR::UART
ReadPort read_port_
 读取端口 / Read port
 
WritePort write_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 112 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  rx_queue_size = 5,
uint32_t  tx_queue_size = 5 
)
inline

Definition at line 179 of file stm32_uart.hpp.

182 dma_buff_rx_(dma_buff_rx),
183 dma_buff_tx_(dma_buff_tx),
184 uart_handle_(uart_handle),
185 id_(STM32_UART_GetID(uart_handle_->Instance))
186 {
187 ASSERT(id_ != STM32_UART_ID_ERROR);
188
189 map[id_] = this;
190
191 if ((uart_handle->Init.Mode & UART_MODE_TX) == UART_MODE_TX)
192 {
193 ASSERT(uart_handle_->hdmatx != NULL);
195 }
196
197 if ((uart_handle->Init.Mode & UART_MODE_RX) == UART_MODE_RX)
198 {
199 ASSERT(uart_handle->hdmarx != NULL);
201
202 HAL_UART_Receive_DMA(uart_handle, reinterpret_cast<uint8_t *>(dma_buff_rx_.addr_),
203 dma_buff_rx_.size_);
205 }
206 }
size_t size_
数据大小(字节)。 The size of the data (in bytes).
void * addr_
数据存储地址。 The storage address of the data.
UART(size_t rx_queue_size, size_t rx_buffer_size, size_t tx_queue_size, size_t tx_buffer_size)
UART 构造函数 / UART constructor.
Definition uart.hpp:64
WritePort write_port_
写入端口 / Write port
Definition uart.hpp:52
ReadPort read_port_
读取端口 / Read port
Definition uart.hpp:51
ErrorCode(* ReadFun)(ReadPort &port)
Function pointer type for read operations.
Definition libxr_rw.hpp:279
ErrorCode(* WriteFun)(WritePort &port)
Function pointer type for write operations.
Definition libxr_rw.hpp:275
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值

Member Function Documentation

◆ ReadFun()

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

Definition at line 152 of file stm32_uart.hpp.

153 {
154 STM32UART *uart = CONTAINER_OF(&port, STM32UART, read_port_);
155 UNUSED(uart);
156 ReadInfoBlock block;
157
158 if (port.queue_block_->Peek(block) != ErrorCode::OK)
159 {
160 return ErrorCode::EMPTY;
161 }
162
163 block.op_.MarkAsRunning();
164
165 if (port.queue_data_->Size() >= block.data_.size_)
166 {
167 port.queue_data_->PopBatch(block.data_.addr_, block.data_.size_);
168 port.queue_block_->Pop();
169 port.read_size_ = block.data_.size_;
170 block.op_.UpdateStatus(false, ErrorCode::OK);
171 return ErrorCode::OK;
172 }
173 else
174 {
175 return ErrorCode::EMPTY;
176 }
177 }

◆ 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 208 of file stm32_uart.hpp.

209 {
210 uart_handle_->Init.BaudRate = config.baudrate;
211
212 switch (config.parity)
213 {
215 uart_handle_->Init.Parity = UART_PARITY_NONE;
216 uart_handle_->Init.WordLength = UART_WORDLENGTH_8B;
217 break;
219 uart_handle_->Init.Parity = UART_PARITY_EVEN;
220 uart_handle_->Init.WordLength = UART_WORDLENGTH_9B;
221 break;
223 uart_handle_->Init.Parity = UART_PARITY_ODD;
224 uart_handle_->Init.WordLength = UART_WORDLENGTH_9B;
225 break;
226 default:
227 ASSERT(false);
228 }
229
230 switch (config.stop_bits)
231 {
232 case 1:
233 uart_handle_->Init.StopBits = UART_STOPBITS_1;
234 break;
235 case 2:
236 uart_handle_->Init.StopBits = UART_STOPBITS_2;
237 break;
238 default:
239 ASSERT(false);
240 }
241
242 if (HAL_UART_Init(uart_handle_) != HAL_OK)
243 {
244 return ErrorCode::INIT_ERR;
245 }
246 return ErrorCode::OK;
247 }
@ NO_PARITY
无校验 / No parity
@ ODD
奇校验 / Odd parity
@ EVEN
偶校验 / Even parity

◆ WriteFun()

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

Definition at line 115 of file stm32_uart.hpp.

116 {
117 STM32UART *uart = CONTAINER_OF(&port, STM32UART, write_port_);
118 if (uart->uart_handle_->gState == HAL_UART_STATE_READY)
119 {
120 WritePort::WriteInfo info;
121 if (port.queue_info_->Peek(info) != ErrorCode::OK)
122 {
123 return ErrorCode::EMPTY;
124 }
125
126 if (port.queue_data_->PopBatch(
127 reinterpret_cast<uint8_t *>(uart->dma_buff_tx_.addr_), info.size) !=
128 ErrorCode::OK)
129 {
130 ASSERT(false);
131 return ErrorCode::EMPTY;
132 }
133
134 auto ans = HAL_UART_Transmit_DMA(uart->uart_handle_,
135 static_cast<uint8_t *>(uart->dma_buff_tx_.addr_),
136 info.size);
137 if (ans != HAL_OK)
138 {
139 port.queue_info_->Pop(info);
140 info.op.UpdateStatus(false, ErrorCode::FAILED);
141 return ErrorCode::FAILED;
142 }
143 info.op.MarkAsRunning();
144 }
145 else
146 {
147 }
148
149 return ErrorCode::OK;
150 }

Field Documentation

◆ dma_buff_rx_

RawData LibXR::STM32UART::dma_buff_rx_

Definition at line 249 of file stm32_uart.hpp.

◆ dma_buff_tx_

RawData LibXR::STM32UART::dma_buff_tx_

Definition at line 249 of file stm32_uart.hpp.

◆ id_

stm32_uart_id_t LibXR::STM32UART::id_ = STM32_UART_ID_ERROR

Definition at line 253 of file stm32_uart.hpp.

◆ map

STM32UART * STM32UART::map = {nullptr}
static

Definition at line 255 of file stm32_uart.hpp.

◆ uart_handle_

UART_HandleTypeDef* LibXR::STM32UART::uart_handle_

Definition at line 251 of file stm32_uart.hpp.


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