Deluge Firmware 1.3.0
Build date: 2026.11.28
Loading...
Searching...
No Matches
rx_buffer.h
1#ifndef DELUGE_DRIVERS_UART_RX_BUFFER_H
2#define DELUGE_DRIVERS_UART_RX_BUFFER_H
3
4#include <stdbool.h>
5#include <stdint.h>
6
7// Leave one slot empty so the DMA producer cannot catch the consumer and make
8// a full ring indistinguishable from an empty one.
9static inline uint32_t uartRingSpace(uint32_t size, uint32_t read_pos, uint32_t write_pos) {
10 return (read_pos - write_pos - 1) & (size - 1);
11}
12
13// Inspect a snapshot of the unread part of a power-of-two DMA ring without
14// advancing its consumer. Replacing all matches also consumes duplicate acks.
15// Call only from the same task context as the normal consumer, not an ISR.
16static inline bool uartReplaceByteInRing(volatile uint8_t* buffer, uint32_t size, uint32_t read_pos, uint32_t write_pos,
17 uint8_t value, uint8_t replacement) {
18 bool found = false;
19 uint32_t mask = size - 1;
20 write_pos &= mask;
21 for (uint32_t pos = read_pos & mask; pos != write_pos; pos = (pos + 1) & mask) {
22 if (buffer[pos] == value) {
23 buffer[pos] = replacement;
24 found = true;
25 }
26 }
27 return found;
28}
29
30// Main-loop staging keeps deferred panel input out of the DMA ring. One slot
31// stays free for uartInsertFakeChar; normal put-back immediately follows a read.
33 uint8_t bytes[256];
34 uint32_t read_pos;
35 uint32_t count;
36};
37
38static inline bool uartReadStagedByte(struct UartRxQueue* queue, uint8_t* value) {
39 if (!queue->count) {
40 return false;
41 }
42 *value = queue->bytes[queue->read_pos];
43 queue->read_pos = (queue->read_pos + 1) % sizeof(queue->bytes);
44 --queue->count;
45 return true;
46}
47
48static inline void uartPrependStagedByte(struct UartRxQueue* queue, uint8_t value) {
49 queue->read_pos = (queue->read_pos + sizeof(queue->bytes) - 1) % sizeof(queue->bytes);
50 queue->bytes[queue->read_pos] = value;
51 ++queue->count;
52}
53
54static inline bool uartStagePendingBytes(struct UartRxQueue* queue, volatile uint8_t* buffer, uint32_t size,
55 uint32_t* read_pos, uint32_t write_pos, uint8_t value, uint8_t replacement) {
56 bool found = false;
57 uint32_t retained = 0;
58 // An acknowledgement may already have been staged before its request was
59 // committed. Remove it here as well, preserving the order of panel bytes.
60 for (uint32_t offset = 0; offset < queue->count; ++offset) {
61 uint8_t byte = queue->bytes[(queue->read_pos + offset) % sizeof(queue->bytes)];
62 if (byte == value) {
63 found = true;
64 }
65 else if (byte != replacement) {
66 queue->bytes[(queue->read_pos + retained++) % sizeof(queue->bytes)] = byte;
67 }
68 }
69 queue->count = retained;
70 // Scan even when staging is full: committed SELECT/DESELECT must finish.
71 found = uartReplaceByteInRing(buffer, size, *read_pos, write_pos, value, replacement) || found;
72 uint32_t mask = size - 1;
73 write_pos &= mask;
74 while (*read_pos != write_pos) {
75 uint8_t byte = buffer[*read_pos];
76 if (byte != replacement) {
77 if (queue->count >= sizeof(queue->bytes) - 1) {
78 break;
79 }
80 queue->bytes[(queue->read_pos + queue->count++) % sizeof(queue->bytes)] = byte;
81 }
82 *read_pos = (*read_pos + 1) & mask;
83 }
84 return found;
85}
86
87#endif // DELUGE_DRIVERS_UART_RX_BUFFER_H
Definition rx_buffer.h:32