diff options
author | Matthijs Kooijman <matthijs@stdin.nl> | 2013-12-18 23:21:45 +0100 |
---|---|---|
committer | Cristian Maglie <c.maglie@bug.st> | 2014-01-22 12:06:02 +0100 |
commit | c3cd35f1979290d072ab77a6805dc4419f2ef6fb (patch) | |
tree | 7522e8c793e103ce22ca1ba83c048bcabc8e8486 | |
parent | 49fc2ab8ad43b86f7668da8d0186abe0d48cd6d9 (diff) |
In HardwareSerial::write, bypass the queue when it's empty
This helps improve the effective datarate on high (>500kbit/s) bitrates,
by skipping the interrupt and associated overhead. At 1 Mbit/s the
implementation previously got up to about 600-700 kbit/s, but now it
actually gets up to the 1Mbit/s (values are rough estimates, though).
-rw-r--r-- | cores/arduino/HardwareSerial.cpp | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/cores/arduino/HardwareSerial.cpp b/cores/arduino/HardwareSerial.cpp index 5d62273..9f0a2ec 100644 --- a/cores/arduino/HardwareSerial.cpp +++ b/cores/arduino/HardwareSerial.cpp @@ -197,6 +197,15 @@ void HardwareSerial::flush() size_t HardwareSerial::write(uint8_t c) { + // If the buffer and the data register is empty, just write the byte + // to the data register and be done. This shortcut helps + // significantly improve the effective datarate at high (> + // 500kbit/s) bitrates, where interrupt overhead becomes a slowdown. + if (_tx_buffer_head == _tx_buffer_tail && bit_is_set(*_ucsra, UDRE0)) { + *_udr = c; + sbi(*_ucsra, TXC0); + return 1; + } int i = (_tx_buffer_head + 1) % SERIAL_BUFFER_SIZE; // If the output buffer is full, there's nothing for it other than to |