Skip to content

Commit

Permalink
In HardwareSerial::write, bypass the queue when it's empty
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
matthijskooijman authored and cmaglie committed Jan 22, 2014
1 parent 275c0a0 commit 9ad14b2
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions hardware/arduino/avr/cores/arduino/HardwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9ad14b2

Please sign in to comment.