Skip to content

Commit 576f894

Browse files
Add setFIFOSize to UART Serial ports (#410)
Allow setting the size of the receive buffer by the application using a call to Serial1/2.setFIFOSize(xxx) before the begin() call.
1 parent 039cbcf commit 576f894

File tree

5 files changed

+26
-6
lines changed

5 files changed

+26
-6
lines changed

cores/rp2040/SerialPIO.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ void __not_in_flash_func(SerialPIO::_handleIRQ)() {
127127
SerialPIO::SerialPIO(pin_size_t tx, pin_size_t rx, size_t fifosize) {
128128
_tx = tx;
129129
_rx = rx;
130-
_fifosize = fifosize;
130+
_fifosize = fifosize + 1; // Always one unused entry
131131
_queue = new uint8_t[_fifosize];
132132
mutex_init(&_mutex);
133133
}

cores/rp2040/SerialUART.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ bool SerialUART::setTX(pin_size_t pin) {
6565
return false;
6666
}
6767

68+
bool SerialUART::setFIFOSize(size_t size) {
69+
if (!size || _running) {
70+
return false;
71+
}
72+
_fifoSize = size + 1; // Always 1 unused entry
73+
return true;
74+
}
75+
6876
SerialUART::SerialUART(uart_inst_t *uart, pin_size_t tx, pin_size_t rx) {
6977
_uart = uart;
7078
_tx = tx;
@@ -76,6 +84,7 @@ static void _uart0IRQ();
7684
static void _uart1IRQ();
7785

7886
void SerialUART::begin(unsigned long baud, uint16_t config) {
87+
_queue = new uint8_t[_fifoSize];
7988
_baud = baud;
8089
uart_init(_uart, baud);
8190
int bits, stop;
@@ -140,6 +149,7 @@ void SerialUART::end() {
140149
irq_set_enabled(UART1_IRQ, false);
141150
}
142151
uart_deinit(_uart);
152+
delete[] _queue;
143153
_running = false;
144154
}
145155

@@ -170,7 +180,7 @@ int SerialUART::read() {
170180
while ((now - start) < _timeout) {
171181
if (_writer != _reader) {
172182
auto ret = _queue[_reader];
173-
_reader = (_reader + 1) % sizeof(_queue);
183+
_reader = (_reader + 1) % _fifoSize;
174184
return ret;
175185
}
176186
delay(1);
@@ -184,7 +194,7 @@ int SerialUART::available() {
184194
if (!_running || !m) {
185195
return 0;
186196
}
187-
return (_writer - _reader) % sizeof(_queue);
197+
return (_writer - _reader) % _fifoSize;
188198
}
189199

190200
int SerialUART::availableForWrite() {
@@ -250,10 +260,10 @@ void __not_in_flash_func(SerialUART::_handleIRQ)() {
250260
uart_get_hw(_uart)->icr |= UART_UARTICR_RTIC_BITS | UART_UARTICR_RXIC_BITS;
251261
while (uart_is_readable(_uart)) {
252262
auto val = uart_getc(_uart);
253-
if ((_writer + 1) % sizeof(_queue) != _reader) {
263+
if ((_writer + 1) % _fifoSize != _reader) {
254264
_queue[_writer] = val;
255265
asm volatile("" ::: "memory"); // Ensure the queue is written before the written count advances
256-
_writer = (_writer + 1) % sizeof(_queue);
266+
_writer = (_writer + 1) % _fifoSize;
257267
} else {
258268
// TODO: Overflow
259269
}

cores/rp2040/SerialUART.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class SerialUART : public HardwareSerial {
4040
ret &= setTX(tx);
4141
return ret;
4242
}
43+
bool setFIFOSize(size_t size);
4344

4445
void begin(unsigned long baud = 115200) override {
4546
begin(baud, SERIAL_8N1);
@@ -70,7 +71,8 @@ class SerialUART : public HardwareSerial {
7071
// Lockless, IRQ-handled circular queue
7172
uint32_t _writer;
7273
uint32_t _reader;
73-
uint8_t _queue[32];
74+
size_t _fifoSize = 32;
75+
uint8_t *_queue;
7476
};
7577

7678
extern SerialUART Serial1; // HW UART 0

docs/serial.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,12 @@ Configure their pins using the ``setXXX`` calls prior to calling ``begin()``
2424
Serial1.setTX(pin);
2525
Serial1.begin(baud);
2626
27+
The size of the receive FIFO may also be adjusted from the default 32 bytes by
28+
using the ``setFIFOSize`` call prior to calling ``begin()``
29+
30+
.. code:: cpp
31+
Serial1.setFIFOSize(128);
32+
Serial1.begin(baud);
33+
2734
For detailed information about the Serial ports, see the
2835
Arduino `Serial Reference <https://www.arduino.cc/reference/en/language/functions/communication/serial/>`_ .

keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ resumeOtherCore KEYWORD2
3434
PIOProgram KEYWORD2
3535
prepare KEYWORD2
3636
SerialPIO KEYWORD2
37+
setFIFOSize KEYWORD2

0 commit comments

Comments
 (0)