Skip to content

Commit 0be4e8c

Browse files
matthijskooijmancmaglie
authored andcommitted
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 ccd8880 commit 0be4e8c

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

hardware/arduino/avr/cores/arduino/HardwareSerial.cpp

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

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

244244
// Constructors ////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)