diff --git a/avr/cores/MCUdude_corefiles/HardwareSerial.h b/avr/cores/MCUdude_corefiles/HardwareSerial.h index 17000c2c..0ce725fa 100755 --- a/avr/cores/MCUdude_corefiles/HardwareSerial.h +++ b/avr/cores/MCUdude_corefiles/HardwareSerial.h @@ -113,6 +113,8 @@ class HardwareSerial : public Stream unsigned char _rx_buffer[SERIAL_RX_BUFFER_SIZE]; unsigned char _tx_buffer[SERIAL_TX_BUFFER_SIZE]; + volatile int _rx_error; + public: inline HardwareSerial( volatile uint8_t *ubrrh, volatile uint8_t *ubrrl, @@ -131,6 +133,8 @@ class HardwareSerial : public Stream inline size_t write(long n) { return write((uint8_t)n); } inline size_t write(unsigned int n) { return write((uint8_t)n); } inline size_t write(int n) { return write((uint8_t)n); } + inline int rx_error(void) { return _rx_error; } + inline void clear_rx_error(void) { _rx_error = 0; } using Print::write; // pull in write(str) and write(buf, size) from Print operator bool() { return true; } diff --git a/avr/cores/MCUdude_corefiles/HardwareSerial_private.h b/avr/cores/MCUdude_corefiles/HardwareSerial_private.h index badfa873..2f3b7a61 100755 --- a/avr/cores/MCUdude_corefiles/HardwareSerial_private.h +++ b/avr/cores/MCUdude_corefiles/HardwareSerial_private.h @@ -100,7 +100,7 @@ HardwareSerial::HardwareSerial( void HardwareSerial::_rx_complete_irq(void) { - if (bit_is_clear(*_ucsra, UPE0)) { + if (bit_is_clear(*_ucsra, UPE0) && bit_is_clear(*_ucsra, FE0) && bit_is_clear(*_ucsra, DOR0)) { // No Parity error, read byte and store it in the buffer if there is // room unsigned char c = *_udr; @@ -114,7 +114,19 @@ void HardwareSerial::_rx_complete_irq(void) _rx_buffer[_rx_buffer_head] = c; _rx_buffer_head = i; } - } else { + } + else { + // Set that we had an error + if (bit_is_set(*_ucsra, UPE0)) { + _rx_error = -2; + } + else if (bit_is_set(*_ucsra, FE0)) { + _rx_error = -3; + } + else { + _rx_error = -4; + } + // Parity error, read byte but discard it *_udr; };