Skip to content

Commit 3ee031a

Browse files
Add ::overflow() return to SerialUART/SerialPIO (#547)
Matching the Arduino SoftwareSerial API
1 parent d1f9bce commit 3ee031a

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

cores/rp2040/SerialPIO.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ void __not_in_flash_func(SerialPIO::_handleIRQ)() {
127127
asm volatile("" ::: "memory"); // Ensure the queue is written before the written count advances
128128
_writer = next_writer;
129129
} else {
130-
// TODO: Overflow
130+
_overflow = true;
131131
}
132132
}
133133
}
@@ -146,6 +146,7 @@ SerialPIO::~SerialPIO() {
146146
}
147147

148148
void SerialPIO::begin(unsigned long baud, uint16_t config) {
149+
_overflow = false;
149150
_baud = baud;
150151
switch (config & SERIAL_PARITY_MASK) {
151152
case SERIAL_PARITY_EVEN:
@@ -289,6 +290,17 @@ int SerialPIO::read() {
289290
return -1;
290291
}
291292

293+
bool SerialPIO::overflow() {
294+
CoreMutex m(&_mutex);
295+
if (!_running || !m || (_rx == NOPIN)) {
296+
return false;
297+
}
298+
299+
bool hold = _overflow;
300+
_overflow = false;
301+
return hold;
302+
}
303+
292304
int SerialPIO::available() {
293305
CoreMutex m(&_mutex);
294306
if (!_running || !m || (_rx == NOPIN)) {

cores/rp2040/SerialPIO.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class SerialPIO : public HardwareSerial {
4747
virtual int availableForWrite() override;
4848
virtual void flush() override;
4949
virtual size_t write(uint8_t c) override;
50+
bool overflow();
5051
using Print::write;
5152
operator bool() override;
5253

@@ -60,6 +61,7 @@ class SerialPIO : public HardwareSerial {
6061
int _bits;
6162
uart_parity_t _parity;
6263
int _stop;
64+
bool _overflow;
6365
mutex_t _mutex;
6466

6567
PIOProgram *_txPgm;

cores/rp2040/SerialUART.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ void SerialUART::begin(unsigned long baud, uint16_t config) {
131131
if (_running) {
132132
end();
133133
}
134+
_overflow = false;
134135
_queue = new uint8_t[_fifoSize];
135136
_baud = baud;
136137
uart_init(_uart, baud);
@@ -271,6 +272,16 @@ int SerialUART::read() {
271272
return -1;
272273
}
273274

275+
bool SerialUART::overflow() {
276+
CoreMutex m(&_mutex);
277+
if (!_running || !m) {
278+
return false;
279+
}
280+
bool hold = _overflow;
281+
_overflow = false;
282+
return hold;
283+
}
284+
274285
int SerialUART::available() {
275286
CoreMutex m(&_mutex);
276287
if (!_running || !m) {
@@ -387,7 +398,7 @@ void __not_in_flash_func(SerialUART::_handleIRQ)(bool inIRQ) {
387398
// Avoid using division or mod because the HW divider could be in use
388399
_writer = next_writer;
389400
} else {
390-
// TODO: Overflow
401+
_overflow = true;
391402
}
392403
}
393404
if (inIRQ) {

cores/rp2040/SerialUART.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class SerialUART : public HardwareSerial {
6060
virtual size_t write(uint8_t c) override;
6161
virtual size_t write(const uint8_t *p, size_t len) override;
6262
using Print::write;
63+
bool overflow();
6364
operator bool() override;
6465

6566
// Not to be called by users, only from the IRQ handler. In public so that the C-language IQR callback can access it
@@ -73,6 +74,7 @@ class SerialUART : public HardwareSerial {
7374
int _baud;
7475
mutex_t _mutex;
7576
bool _polling = false;
77+
bool _overflow;
7678

7779
// Lockless, IRQ-handled circular queue
7880
uint32_t _writer;

0 commit comments

Comments
 (0)