libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
uart_circular_dma_rx_model.hpp
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5
6#include "libxr_assert.hpp"
7#include "libxr_rw.hpp"
8
9namespace LibXR
10{
11
36{
37 public:
43 explicit UartCircularDmaRxModel(RawData storage) : storage_(storage) {}
44
51 template <typename Backend>
52 void Start(Backend& backend)
53 {
55 backend.StartCircularDmaRx(Buffer(), BufferSize());
56 }
57
71 template <typename Backend>
72 void OnDataAvailable(Backend& backend, ReadPort& port, bool in_isr)
73 {
74 uint8_t* const buffer = Buffer();
75 const size_t capacity = BufferSize();
76 const size_t remaining = backend.GetCircularDmaRxRemaining();
77 if (remaining > capacity)
78 {
79 ASSERT(false);
80 return;
81 }
82
83 const size_t current_position = capacity - remaining;
84 backend.PrepareCircularDmaRxForCpu(buffer, capacity);
85
86 if (current_position == last_position_)
87 {
88 return;
89 }
90
91 if (current_position > last_position_)
92 {
93 (void)port.queue_data_->PushBatch(&buffer[last_position_],
94 current_position - last_position_);
95 }
96 else
97 {
98 (void)port.queue_data_->PushBatch(&buffer[last_position_],
99 capacity - last_position_);
100 (void)port.queue_data_->PushBatch(buffer, current_position);
101 }
102
103 last_position_ = current_position;
104 port.ProcessPendingReads(in_isr);
105 }
106
111 void ResetPosition() { last_position_ = 0U; }
112
117 [[nodiscard]] uint8_t* Buffer() const { return static_cast<uint8_t*>(storage_.addr_); }
118
123 [[nodiscard]] size_t BufferSize() const { return storage_.size_; }
124
129 [[nodiscard]] size_t LastPosition() const { return last_position_; }
130
131 private:
132 RawData storage_;
133 size_t last_position_ = 0U;
134};
135
136} // namespace LibXR
可写原始数据视图 / Mutable raw data view
size_t size_
数据字节数 / Data size in bytes
void * addr_
数据起始地址 / Data start address
ReadPort class for handling read operations.
Definition read_port.hpp:18
SPSCQueue< uint8_t > * queue_data_
RX payload queue. 接收数据字节队列。
Definition read_port.hpp:55
void ProcessPendingReads(bool in_isr)
Processes pending reads.
ErrorCode PushBatch(const Data *data, size_t size)
批量推入多个 payload。
UART 基于位置的循环 DMA 接收模型 / Position-based circular DMA RX model for UART.
uint8_t * Buffer() const
获取 DMA 可写缓冲区起始地址 / Get the DMA-writable buffer start address
size_t BufferSize() const
获取循环 DMA 缓冲区容量 / Get the circular DMA buffer capacity
size_t LastPosition() const
获取上次已消费的 DMA 写入位置 / Get the last consumed DMA write position
void OnDataAvailable(Backend &backend, ReadPort &port, bool in_isr)
消费上次 DMA 事件后新产生的数据 / Consume bytes produced since the previous DMA event
void ResetPosition()
将软件读取位置复位到 DMA 缓冲区起点 / Reset the software read position to the start of the DMA buffer
UartCircularDmaRxModel(RawData storage)
使用平台提供的 DMA 存储区构造接收模型 / Construct the RX model with platform-provided DMA storage
void Start(Backend &backend)
复位读取位置并启动循环 DMA / Reset the read position and start circular DMA
LibXR 命名空间
Definition ch32_can.hpp:14