Skip to content

Commit 83b9486

Browse files
Allow setting the SerialPIO FIFO depth in the constructor (#405)
Defaults to 32 bytes, like the HW Serial ports, but can be set to any desired value when instantiated.
1 parent dc54eeb commit 83b9486

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

cores/rp2040/SerialPIO.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,22 +114,29 @@ void __not_in_flash_func(SerialPIO::_handleIRQ)() {
114114
}
115115
}
116116

117-
if ((_writer + 1) % sizeof(_queue) != _reader) {
117+
if ((_writer + 1) % _fifosize != _reader) {
118118
_queue[_writer] = val & ((1 << _bits) - 1);
119119
asm volatile("" ::: "memory"); // Ensure the queue is written before the written count advances
120-
_writer = (_writer + 1) % sizeof(_queue);
120+
_writer = (_writer + 1) % _fifosize;
121121
} else {
122122
// TODO: Overflow
123123
}
124124
}
125125
}
126126

127-
SerialPIO::SerialPIO(pin_size_t tx, pin_size_t rx) {
127+
SerialPIO::SerialPIO(pin_size_t tx, pin_size_t rx, size_t fifosize) {
128128
_tx = tx;
129129
_rx = rx;
130+
_fifosize = fifosize;
131+
_queue = new uint8_t[_fifosize];
130132
mutex_init(&_mutex);
131133
}
132134

135+
SerialPIO::~SerialPIO() {
136+
end();
137+
delete[] _queue;
138+
}
139+
133140
void SerialPIO::begin(unsigned long baud, uint16_t config) {
134141
_baud = baud;
135142
switch (config & SERIAL_PARITY_MASK) {
@@ -273,7 +280,7 @@ int SerialPIO::read() {
273280
while ((now - start) < _timeout) {
274281
if (_writer != _reader) {
275282
auto ret = _queue[_reader];
276-
_reader = (_reader + 1) % sizeof(_queue);
283+
_reader = (_reader + 1) % _fifosize;
277284
return ret;
278285
}
279286
delay(1);
@@ -287,7 +294,7 @@ int SerialPIO::available() {
287294
if (!_running || !m || (_rx == NOPIN)) {
288295
return 0;
289296
}
290-
return (_writer - _reader) % sizeof(_queue);
297+
return (_writer - _reader) % _fifosize;
291298
}
292299

293300
int SerialPIO::availableForWrite() {

cores/rp2040/SerialPIO.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ extern "C" typedef struct uart_inst uart_inst_t;
3232
class SerialPIO : public HardwareSerial {
3333
public:
3434
static const pin_size_t NOPIN = 0xff; // Use in constructor to disable RX or TX unit
35-
SerialPIO(pin_size_t tx, pin_size_t rx);
35+
SerialPIO(pin_size_t tx, pin_size_t rx, size_t fifosize = 32);
36+
~SerialPIO();
3637

3738
void begin(unsigned long baud = 115200) override {
3839
begin(baud, SERIAL_8N1);
@@ -72,7 +73,8 @@ class SerialPIO : public HardwareSerial {
7273
int _rxBits;
7374

7475
// Lockless, IRQ-handled circular queue
76+
size_t _fifosize;
7577
uint32_t _writer;
7678
uint32_t _reader;
77-
uint8_t _queue[32];
79+
uint8_t *_queue;
7880
};

docs/piouart.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ one or two PIO state machines is included in the Arduino-Pico core. This
66
allows for up to 8 additional serial ports to be run from the RP2040 without
77
requiring additional CPU resources.
88

9-
Instantiate a ``SerialPIO(txpin, rxpin)`` object in your sketch and then
9+
Instantiate a ``SerialPIO(txpin, rxpin, fifosize)`` object in your sketch and then
1010
use it the same as any other serial port. Even, odd, and no parity modes
11-
are supported, as well as data sizes from 5- to 8-bits.
11+
are supported, as well as data sizes from 5- to 8-bits. Fifosize, if not
12+
specified, defaults to 32 bytes.
1213

1314
To instantiate only a serial transmit or receive unit, pass in
1415
``SerialPIO::NOPIN`` as the ``txpin`` or ``rxpin``.

0 commit comments

Comments
 (0)