diff options
author | David A. Mellis <d.mellis@arduino.cc> | 2012-08-28 08:02:54 -0400 |
---|---|---|
committer | David A. Mellis <d.mellis@arduino.cc> | 2012-08-28 08:02:54 -0400 |
commit | 00ab72619ea4364ed446d929e77143ed467514c1 (patch) | |
tree | df42e0427631cc91722eab8ea0ac010efefb24c8 | |
parent | c40ab91c41bc9e2ef99600baf8c47dbe191aeb0c (diff) |
Serial.flush() waits for last character to be transmitted (michele.mazzucchi)
http://code.google.com/p/arduino/issues/detail?id=871
-rw-r--r-- | cores/arduino/HardwareSerial.cpp | 10 | ||||
-rw-r--r-- | cores/arduino/HardwareSerial.h | 1 |
2 files changed, 9 insertions, 2 deletions
diff --git a/cores/arduino/HardwareSerial.cpp b/cores/arduino/HardwareSerial.cpp index f40ddee..867bc9e 100644 --- a/cores/arduino/HardwareSerial.cpp +++ b/cores/arduino/HardwareSerial.cpp @@ -327,6 +327,8 @@ try_again: *_ubrrh = baud_setting >> 8; *_ubrrl = baud_setting; + transmitting = false; + sbi(*_ucsrb, _rxen); sbi(*_ucsrb, _txen); sbi(*_ucsrb, _rxcie); @@ -376,8 +378,9 @@ int HardwareSerial::read(void) void HardwareSerial::flush() { - while (_tx_buffer->head != _tx_buffer->tail) - ; + // UDR is kept full while the buffer is not empty, so TXC triggers when EMPTY && SENT + while (transmitting && ! (*_ucsra & _BV(TXC0))); + transmitting = false; } size_t HardwareSerial::write(uint8_t c) @@ -394,6 +397,9 @@ size_t HardwareSerial::write(uint8_t c) _tx_buffer->head = i; sbi(*_ucsrb, _udrie); + // clear the TXC bit -- "can be cleared by writing a one to its bit location" + transmitting = true; + sbi(*_ucsra, TXC0); return 1; } diff --git a/cores/arduino/HardwareSerial.h b/cores/arduino/HardwareSerial.h index bf4924c..5bceb75 100644 --- a/cores/arduino/HardwareSerial.h +++ b/cores/arduino/HardwareSerial.h @@ -43,6 +43,7 @@ class HardwareSerial : public Stream uint8_t _rxcie; uint8_t _udrie; uint8_t _u2x; + bool transmitting; public: HardwareSerial(ring_buffer *rx_buffer, ring_buffer *tx_buffer, volatile uint8_t *ubrrh, volatile uint8_t *ubrrl, |