Skip to content

Commit 9ad14b2

Browse files
matthijskooijmancmaglie
authored andcommitted
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).
1 parent 275c0a0 commit 9ad14b2

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

Diff for: hardware/arduino/avr/cores/arduino/HardwareSerial.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,15 @@ void HardwareSerial::flush()
197197

198198
size_t HardwareSerial::write(uint8_t c)
199199
{
200+
// If the buffer and the data register is empty, just write the byte
201+
// to the data register and be done. This shortcut helps
202+
// significantly improve the effective datarate at high (>
203+
// 500kbit/s) bitrates, where interrupt overhead becomes a slowdown.
204+
if (_tx_buffer_head == _tx_buffer_tail && bit_is_set(*_ucsra, UDRE0)) {
205+
*_udr = c;
206+
sbi(*_ucsra, TXC0);
207+
return 1;
208+
}
200209
int i = (_tx_buffer_head + 1) % SERIAL_BUFFER_SIZE;
201210

202211
// If the output buffer is full, there's nothing for it other than to

0 commit comments

Comments
 (0)