aboutsummaryrefslogtreecommitdiff
path: root/cores/arduino
diff options
context:
space:
mode:
Diffstat (limited to 'cores/arduino')
-rw-r--r--cores/arduino/HardwareSerial.cpp11
1 files changed, 8 insertions, 3 deletions
diff --git a/cores/arduino/HardwareSerial.cpp b/cores/arduino/HardwareSerial.cpp
index 75e079f..560ef3a 100644
--- a/cores/arduino/HardwareSerial.cpp
+++ b/cores/arduino/HardwareSerial.cpp
@@ -246,9 +246,14 @@ size_t HardwareSerial::write(uint8_t c)
}
_tx_buffer[_tx_buffer_head] = c;
- _tx_buffer_head = i;
-
- sbi(*_ucsrb, UDRIE0);
+
+ // make atomic to prevent execution of ISR between setting the
+ // head pointer and setting the interrupt flag resulting in buffer
+ // retransmission
+ ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
+ _tx_buffer_head = i;
+ sbi(*_ucsrb, UDRIE0);
+ }
return 1;
}