Skip to content

Commit 9b7b23f

Browse files
Prevent losing bytes in HardwareSerial::end()
end() already waited for the buffer to be empty, but then there could still be two bytes in the hardware registers that still need to be transmitted (which were dropped or kept in the buffer, depending on the exact timing). This changes the wait loop to a call to the flush() function, which already takes care of really waiting for all bytes to be transmitted, meaning it is safe to turn off the transmitter.
1 parent b58f239 commit 9b7b23f

File tree

1 file changed

+1
-2
lines changed

1 file changed

+1
-2
lines changed

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,7 @@ void HardwareSerial::begin(unsigned long baud, byte config)
138138
void HardwareSerial::end()
139139
{
140140
// wait for transmission of outgoing data
141-
while (_tx_buffer_head != _tx_buffer_tail)
142-
;
141+
flush();
143142

144143
cbi(*_ucsrb, RXEN0);
145144
cbi(*_ucsrb, TXEN0);

0 commit comments

Comments
 (0)