aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cores/arduino/HardwareSerial.cpp20
-rw-r--r--cores/arduino/HardwareSerial.h3
2 files changed, 17 insertions, 6 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;
diff --git a/cores/arduino/HardwareSerial.h b/cores/arduino/HardwareSerial.h
index cf3f6bd..0ae51f4 100644
--- a/cores/arduino/HardwareSerial.h
+++ b/cores/arduino/HardwareSerial.h
@@ -72,7 +72,8 @@ class HardwareSerial : public Stream
volatile uint8_t *_ucsrb;
volatile uint8_t *_ucsrc;
volatile uint8_t *_udr;
- bool transmitting;
+ // Has any byte been written to the UART since begin()
+ bool _written;
volatile uint8_t _rx_buffer_head;
volatile uint8_t _rx_buffer_tail;