Skip to content

Commit 6e03352

Browse files
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 e1599f3 commit 6e03352

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
@@ -301,6 +301,15 @@ size_t HardwareSerial::write(uint8_t c)
301301
{
302302
bool interrupts_enabled = bit_is_set(SREG, SREG_I);
303303
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
304+
// If the buffer and the data register is empty, just write the byte
305+
// to the data register and be done. This shortcut helps
306+
// significantly improve the effective datarate at high (>
307+
// 500kbit/s) bitrates, where interrupt overhead becomes a slowdown.
308+
if (_tx_buffer_head == _tx_buffer_tail && bit_is_set(*_ucsra, UDRE0)) {
309+
*_udr = c;
310+
sbi(*_ucsra, TXC0);
311+
return 1;
312+
}
304313
int i = (_tx_buffer_head + 1) % SERIAL_BUFFER_SIZE;
305314

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

0 commit comments

Comments
 (0)