196{
199 {
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 {
230 if (uart->uart_handle_->gState == HAL_UART_STATE_READY &&
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()),
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.