Skip to content

Commit c598b8e

Browse files
Disable the UDRE interrupt sooner in HardwareSerial
Before, the interrupt was disabled when it was triggered and it turned out there was no data to send. However, the interrupt can be disabled already when the last byte is written to the UART, since write() will always re-enable the interrupt when it adds new data to the buffer. Closes: arduino#1008
1 parent 282f0f0 commit c598b8e

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

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

+12-12
Original file line numberDiff line numberDiff line change
@@ -220,22 +220,22 @@ void HardwareSerial::_rx_complete_irq(void)
220220

221221
void HardwareSerial::_tx_udr_empty_irq(void)
222222
{
223+
// If interrupts are enabled, there must be more data in the output
224+
// buffer. Send the next byte
225+
unsigned char c = _tx_buffer[_tx_buffer_tail];
226+
_tx_buffer_tail = (_tx_buffer_tail + 1) % SERIAL_BUFFER_SIZE;
227+
228+
*_udr = c;
229+
230+
// clear the TXC bit -- "can be cleared by writing a one to its bit
231+
// location". This makes sure flush() won't return until the bytes
232+
// actually got written
233+
sbi(*_ucsra, TXC0);
234+
223235
if (_tx_buffer_head == _tx_buffer_tail) {
224236
// Buffer empty, so disable interrupts
225237
cbi(*_ucsrb, UDRIE0);
226238
}
227-
else {
228-
// There is more data in the output buffer. Send the next byte
229-
unsigned char c = _tx_buffer[_tx_buffer_tail];
230-
_tx_buffer_tail = (_tx_buffer_tail + 1) % SERIAL_BUFFER_SIZE;
231-
232-
*_udr = c;
233-
234-
// clear the TXC bit -- "can be cleared by writing a one to its bit
235-
// location". This makes sure flush() won't return until the bytes
236-
// actually got written
237-
sbi(*_ucsra, TXC0);
238-
}
239239
}
240240

241241
// Constructors ////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)