diff options
Diffstat (limited to 'cores/arduino/HardwareSerial.cpp')
-rw-r--r-- | cores/arduino/HardwareSerial.cpp | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/cores/arduino/HardwareSerial.cpp b/cores/arduino/HardwareSerial.cpp index 806fe5f..d78c205 100644 --- a/cores/arduino/HardwareSerial.cpp +++ b/cores/arduino/HardwareSerial.cpp @@ -276,6 +276,8 @@ void HardwareSerial::begin(unsigned long baud, byte config) *_ubrrh = baud_setting >> 8; *_ubrrl = baud_setting; + _written = false; + //set the data bits, parity, and stop bits #if defined(__AVR_ATmega8__) config |= 0x80; // select UCSRC register (shared with UBRRH) @@ -331,9 +333,15 @@ int HardwareSerial::read(void) void HardwareSerial::flush() { - // UDR is kept full while the buffer is not empty, so TXC triggers when EMPTY && SENT - while (transmitting && bit_is_clear(*_ucsra, TXC0)); - transmitting = false; + // If we have never written a byte, no need to flush. This special + // case is needed since there is no way to force the TXC (transmit + // complete) bit to 1 during initialization + if (!_written) + return; + + // UDR is kept full while the buffer is not empty, so TXC triggers + // when EMPTY && SENT + while (bit_is_clear(*_ucsra, TXC0)); } size_t HardwareSerial::write(uint8_t c) @@ -350,8 +358,10 @@ size_t HardwareSerial::write(uint8_t c) _tx_buffer_head = i; sbi(*_ucsrb, UDRIE0); - // clear the TXC bit -- "can be cleared by writing a one to its bit location" - transmitting = true; + _written = true; + // clear the TXC bit -- "can be cleared by writing a one to its bit + // location". This makes sure flush() won't return until the bytes + // actually got written sbi(*_ucsra, TXC0); return 1; |