File tree 2 files changed +9
-2
lines changed
hardware/arduino/cores/arduino
2 files changed +9
-2
lines changed Original file line number Diff line number Diff line change @@ -327,6 +327,8 @@ void HardwareSerial::begin(unsigned long baud)
327
327
*_ubrrh = baud_setting >> 8 ;
328
328
*_ubrrl = baud_setting;
329
329
330
+ transmitting = false ;
331
+
330
332
sbi (*_ucsrb, _rxen);
331
333
sbi (*_ucsrb, _txen);
332
334
sbi (*_ucsrb, _rxcie);
@@ -376,8 +378,9 @@ int HardwareSerial::read(void)
376
378
377
379
void HardwareSerial::flush ()
378
380
{
379
- while (_tx_buffer->head != _tx_buffer->tail )
380
- ;
381
+ // UDR is kept full while the buffer is not empty, so TXC triggers when EMPTY && SENT
382
+ while (transmitting && ! (*_ucsra & _BV (TXC0)));
383
+ transmitting = false ;
381
384
}
382
385
383
386
size_t HardwareSerial::write (uint8_t c)
@@ -394,6 +397,9 @@ size_t HardwareSerial::write(uint8_t c)
394
397
_tx_buffer->head = i;
395
398
396
399
sbi (*_ucsrb, _udrie);
400
+ // clear the TXC bit -- "can be cleared by writing a one to its bit location"
401
+ transmitting = true ;
402
+ sbi (*_ucsra, TXC0);
397
403
398
404
return 1 ;
399
405
}
Original file line number Diff line number Diff line change @@ -43,6 +43,7 @@ class HardwareSerial : public Stream
43
43
uint8_t _rxcie;
44
44
uint8_t _udrie;
45
45
uint8_t _u2x;
46
+ bool transmitting;
46
47
public:
47
48
HardwareSerial (ring_buffer *rx_buffer, ring_buffer *tx_buffer,
48
49
volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
You can’t perform that action at this time.
0 commit comments