Corium 1.1.0
High-Performance Zero-Heap C++20 MPSC Application Runtime
Loading...
Searching...
No Matches
DmaUartAdapter.hpp
Go to the documentation of this file.
1
7#pragma once
8
9#include <array>
10#include <cstddef>
11#include <cstdint>
12#include <span>
13
14#include "corium/EventSink.hpp"
16
17namespace corium::embedded {
18
21template <std::size_t BufferSize = 512>
23 static_assert((BufferSize & (BufferSize - 1)) == 0, "BufferSize must be a power of two.");
24
25public:
26 constexpr DmaUartRxBuffer() = default;
27
29 [[nodiscard]] uint8_t* dmaBuffer() noexcept {
30 return _buffer.data();
31 }
32
34 [[nodiscard]] const uint8_t* dmaBuffer() const noexcept {
35 return _buffer.data();
36 }
37
39 [[nodiscard]] static constexpr std::size_t capacity() noexcept {
40 return BufferSize;
41 }
42
47 template <typename Callback>
48 void processAvailableBytes(std::size_t currentDmaWriteIndex, Callback&& onPacket) {
49 currentDmaWriteIndex = currentDmaWriteIndex & (BufferSize - 1);
50
51 if (currentDmaWriteIndex == _tail) {
52 return; // No new bytes received
53 }
54
55 if (currentDmaWriteIndex > _tail) {
56 // Contiguous slice
57 std::size_t len = currentDmaWriteIndex - _tail;
58 onPacket(std::span<const uint8_t>(_buffer.data() + _tail, len));
59 _tail = currentDmaWriteIndex;
60 } else {
61 // Wrapped around circular buffer
62 std::size_t firstPart = BufferSize - _tail;
63 if (firstPart > 0) {
64 onPacket(std::span<const uint8_t>(_buffer.data() + _tail, firstPart));
65 }
66 if (currentDmaWriteIndex > 0) {
67 onPacket(std::span<const uint8_t>(_buffer.data(), currentDmaWriteIndex));
68 }
69 _tail = currentDmaWriteIndex;
70 }
71 }
72
74 void reset() noexcept {
75 _tail = 0;
76 }
77
79 [[nodiscard]] std::size_t tail() const noexcept {
80 return _tail;
81 }
82
83private:
84 alignas(4) std::array<uint8_t, BufferSize> _buffer{};
85 std::size_t _tail{0};
86};
87
90template <typename EventVariant, std::size_t BufferSize = 512>
92public:
94 : _sink(sink)
95 {}
96
98 [[nodiscard]] DmaUartRxBuffer<BufferSize>& rxBuffer() noexcept {
99 return _rxBuffer;
100 }
101
103 [[nodiscard]] const DmaUartRxBuffer<BufferSize>& rxBuffer() const noexcept {
104 return _rxBuffer;
105 }
106
111 template <typename TargetEvent>
112 void processFromIsr(std::size_t currentDmaWriteIndex, EventPriority priority = EventPriority::Normal) {
113 _rxBuffer.processAvailableBytes(currentDmaWriteIndex, [this, priority](std::span<const uint8_t> data) {
114 _sink.post(EventVariant{TargetEvent{data}}, priority);
115 });
116 }
117
118private:
120 DmaUartRxBuffer<BufferSize> _rxBuffer{};
121};
122
123} // namespace corium::embedded
Non-allocating type-erased fat pointer handle for lock-free event posting.
Bounded and multi-tier priority MPSC queueing policies.
Non-blocking hardware ISR adapter for DMA UART controllers.
Definition DmaUartAdapter.hpp:91
DmaUartRxBuffer< BufferSize > & rxBuffer() noexcept
Access underlying circular DMA RX buffer.
Definition DmaUartAdapter.hpp:98
DmaUartAdapter(EventSinkT< EventVariant > sink) noexcept
Definition DmaUartAdapter.hpp:93
void processFromIsr(std::size_t currentDmaWriteIndex, EventPriority priority=EventPriority::Normal)
Process newly received DMA bytes and post constructed event into sink.
Definition DmaUartAdapter.hpp:112
const DmaUartRxBuffer< BufferSize > & rxBuffer() const noexcept
Access const circular DMA RX buffer.
Definition DmaUartAdapter.hpp:103
Statically-allocated circular DMA UART RX buffer.
Definition DmaUartAdapter.hpp:22
std::size_t tail() const noexcept
Current tail index in the circular buffer.
Definition DmaUartAdapter.hpp:79
static constexpr std::size_t capacity() noexcept
Capacity of circular DMA buffer in bytes.
Definition DmaUartAdapter.hpp:39
const uint8_t * dmaBuffer() const noexcept
Const pointer to raw memory buffer.
Definition DmaUartAdapter.hpp:34
void processAvailableBytes(std::size_t currentDmaWriteIndex, Callback &&onPacket)
Update tail position based on hardware DMA counter and extract available bytes. Typically invoked fro...
Definition DmaUartAdapter.hpp:48
void reset() noexcept
Reset circular tail index.
Definition DmaUartAdapter.hpp:74
uint8_t * dmaBuffer() noexcept
Direct pointer to raw memory buffer for DMA peripheral configuration (e.g. HAL_UART_Receive_DMA).
Definition DmaUartAdapter.hpp:29
constexpr DmaUartRxBuffer()=default
Definition CanAdapter.hpp:16
EventPriority
Event priority levels for multi-priority queue policies.
Definition QueuePolicies.hpp:32