Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 99c294c

Browse files
johnholmanfacchinm
authored andcommitted
Improve how TXCn bit is cleared in USCRnA register
Preserve values of configuration bits MPCMn and U2Xn. Avoid setting other read-only bits for datasheet conformance. See #3745
1 parent e9e43cf commit 99c294c

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

cores/arduino/HardwareSerial.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,10 @@ void HardwareSerial::_tx_udr_empty_irq(void)
9797

9898
// clear the TXC bit -- "can be cleared by writing a one to its bit
9999
// location". This makes sure flush() won't return until the bytes
100-
// actually got written
101-
sbi(*_ucsra, TXC0);
100+
// actually got written. Other r/w bits are preserved, and zeroes
101+
// written to the rest.
102+
103+
*_ucsra = ((*_ucsra) & ((1 << U2X0) | (1 << MPCM0))) | (1 << TXC0);
102104

103105
if (_tx_buffer_head == _tx_buffer_tail) {
104106
// Buffer empty, so disable interrupts
@@ -225,7 +227,7 @@ size_t HardwareSerial::write(uint8_t c)
225227
// 500kbit/s) bitrates, where interrupt overhead becomes a slowdown.
226228
if (_tx_buffer_head == _tx_buffer_tail && bit_is_set(*_ucsra, UDRE0)) {
227229
*_udr = c;
228-
sbi(*_ucsra, TXC0);
230+
*_ucsra = ((*_ucsra) & ((1 << U2X0) | (1 << MPCM0))) | (1 << TXC0);
229231
return 1;
230232
}
231233
tx_buffer_index_t i = (_tx_buffer_head + 1) % SERIAL_TX_BUFFER_SIZE;

0 commit comments

Comments
 (0)