Unverified Commit 3ee031ab authored by Earle F. Philhower, III's avatar Earle F. Philhower, III Committed by GitHub

Add ::overflow() return to SerialUART/SerialPIO (#547)

Matching the Arduino SoftwareSerial API
parent d1f9bce0
...@@ -127,7 +127,7 @@ void __not_in_flash_func(SerialPIO::_handleIRQ)() { ...@@ -127,7 +127,7 @@ void __not_in_flash_func(SerialPIO::_handleIRQ)() {
asm volatile("" ::: "memory"); // Ensure the queue is written before the written count advances asm volatile("" ::: "memory"); // Ensure the queue is written before the written count advances
_writer = next_writer; _writer = next_writer;
} else { } else {
// TODO: Overflow _overflow = true;
} }
} }
} }
...@@ -146,6 +146,7 @@ SerialPIO::~SerialPIO() { ...@@ -146,6 +146,7 @@ SerialPIO::~SerialPIO() {
} }
void SerialPIO::begin(unsigned long baud, uint16_t config) { void SerialPIO::begin(unsigned long baud, uint16_t config) {
_overflow = false;
_baud = baud; _baud = baud;
switch (config & SERIAL_PARITY_MASK) { switch (config & SERIAL_PARITY_MASK) {
case SERIAL_PARITY_EVEN: case SERIAL_PARITY_EVEN:
...@@ -289,6 +290,17 @@ int SerialPIO::read() { ...@@ -289,6 +290,17 @@ int SerialPIO::read() {
return -1; return -1;
} }
bool SerialPIO::overflow() {
CoreMutex m(&_mutex);
if (!_running || !m || (_rx == NOPIN)) {
return false;
}
bool hold = _overflow;
_overflow = false;
return hold;
}
int SerialPIO::available() { int SerialPIO::available() {
CoreMutex m(&_mutex); CoreMutex m(&_mutex);
if (!_running || !m || (_rx == NOPIN)) { if (!_running || !m || (_rx == NOPIN)) {
......
...@@ -47,6 +47,7 @@ public: ...@@ -47,6 +47,7 @@ public:
virtual int availableForWrite() override; virtual int availableForWrite() override;
virtual void flush() override; virtual void flush() override;
virtual size_t write(uint8_t c) override; virtual size_t write(uint8_t c) override;
bool overflow();
using Print::write; using Print::write;
operator bool() override; operator bool() override;
...@@ -60,6 +61,7 @@ private: ...@@ -60,6 +61,7 @@ private:
int _bits; int _bits;
uart_parity_t _parity; uart_parity_t _parity;
int _stop; int _stop;
bool _overflow;
mutex_t _mutex; mutex_t _mutex;
PIOProgram *_txPgm; PIOProgram *_txPgm;
......
...@@ -131,6 +131,7 @@ void SerialUART::begin(unsigned long baud, uint16_t config) { ...@@ -131,6 +131,7 @@ void SerialUART::begin(unsigned long baud, uint16_t config) {
if (_running) { if (_running) {
end(); end();
} }
_overflow = false;
_queue = new uint8_t[_fifoSize]; _queue = new uint8_t[_fifoSize];
_baud = baud; _baud = baud;
uart_init(_uart, baud); uart_init(_uart, baud);
...@@ -271,6 +272,16 @@ int SerialUART::read() { ...@@ -271,6 +272,16 @@ int SerialUART::read() {
return -1; return -1;
} }
bool SerialUART::overflow() {
CoreMutex m(&_mutex);
if (!_running || !m) {
return false;
}
bool hold = _overflow;
_overflow = false;
return hold;
}
int SerialUART::available() { int SerialUART::available() {
CoreMutex m(&_mutex); CoreMutex m(&_mutex);
if (!_running || !m) { if (!_running || !m) {
...@@ -387,7 +398,7 @@ void __not_in_flash_func(SerialUART::_handleIRQ)(bool inIRQ) { ...@@ -387,7 +398,7 @@ void __not_in_flash_func(SerialUART::_handleIRQ)(bool inIRQ) {
// Avoid using division or mod because the HW divider could be in use // Avoid using division or mod because the HW divider could be in use
_writer = next_writer; _writer = next_writer;
} else { } else {
// TODO: Overflow _overflow = true;
} }
} }
if (inIRQ) { if (inIRQ) {
......
...@@ -60,6 +60,7 @@ public: ...@@ -60,6 +60,7 @@ public:
virtual size_t write(uint8_t c) override; virtual size_t write(uint8_t c) override;
virtual size_t write(const uint8_t *p, size_t len) override; virtual size_t write(const uint8_t *p, size_t len) override;
using Print::write; using Print::write;
bool overflow();
operator bool() override; operator bool() override;
// Not to be called by users, only from the IRQ handler. In public so that the C-language IQR callback can access it // 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 @@ private: ...@@ -73,6 +74,7 @@ private:
int _baud; int _baud;
mutex_t _mutex; mutex_t _mutex;
bool _polling = false; bool _polling = false;
bool _overflow;
// Lockless, IRQ-handled circular queue // Lockless, IRQ-handled circular queue
uint32_t _writer; uint32_t _writer;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment