29
29
#include < inttypes.h>
30
30
#include " Arduino.h"
31
31
#include " cbuf.h"
32
+ #include " interrupts.h"
32
33
33
34
extern " C" {
34
35
#include " osapi.h"
@@ -556,6 +557,7 @@ int HardwareSerial::available(void) {
556
557
int result = 0 ;
557
558
558
559
if (_uart != NULL && _uart->rxEnabled ) {
560
+ InterruptLock il;
559
561
result = static_cast <int >(_rx_buffer->getSize ());
560
562
}
561
563
@@ -570,6 +572,7 @@ int HardwareSerial::peek(void) {
570
572
if (_uart == 0 )
571
573
return -1 ;
572
574
if (_uart->rxEnabled ) {
575
+ InterruptLock il;
573
576
return _rx_buffer->peek ();
574
577
} else {
575
578
return -1 ;
@@ -580,6 +583,7 @@ int HardwareSerial::read(void) {
580
583
if (_uart == 0 )
581
584
return -1 ;
582
585
if (_uart->rxEnabled ) {
586
+ InterruptLock il;
583
587
return _rx_buffer->read ();
584
588
} else {
585
589
return -1 ;
@@ -590,6 +594,7 @@ int HardwareSerial::availableForWrite(void) {
590
594
if (_uart == 0 )
591
595
return 0 ;
592
596
if (_uart->txEnabled ) {
597
+ InterruptLock il;
593
598
return static_cast <int >(_tx_buffer->room ());
594
599
} else {
595
600
return 0 ;
@@ -604,28 +609,41 @@ void HardwareSerial::flush() {
604
609
if (!_written)
605
610
return ;
606
611
607
- while (_tx_buffer->getSize () || uart_get_tx_fifo_room (_uart) < UART_TX_FIFO_SIZE)
612
+ while (true ) {
613
+ {
614
+ InterruptLock il;
615
+ if (_tx_buffer->getSize () == 0 &&
616
+ uart_get_tx_fifo_room (_uart) >= UART_TX_FIFO_SIZE) {
617
+ break ;
618
+ }
619
+ }
608
620
yield ();
609
-
621
+ }
610
622
_written = false ;
611
623
}
612
624
613
625
size_t HardwareSerial::write (uint8_t c) {
614
626
if (_uart == 0 || !_uart->txEnabled )
615
627
return 0 ;
616
628
_written = true ;
617
- size_t room = uart_get_tx_fifo_room (_uart);
618
- if (room > 0 && _tx_buffer->empty ()) {
619
- uart_transmit_char (_uart, c);
620
- return 1 ;
621
- }
622
629
623
- while (_tx_buffer->room () == 0 ) {
630
+ while (true ) {
631
+ {
632
+ InterruptLock il;
633
+ if (_tx_buffer->empty ()) {
634
+ if (uart_get_tx_fifo_room (_uart) > 0 ) {
635
+ uart_transmit_char (_uart, c);
636
+ } else {
637
+ _tx_buffer->write (c);
638
+ uart_arm_tx_interrupt (_uart);
639
+ }
640
+ break ;
641
+ } else if (_tx_buffer->write (c)) {
642
+ break ;
643
+ }
644
+ }
624
645
yield ();
625
646
}
626
-
627
- _tx_buffer->write (c);
628
- uart_arm_tx_interrupt (_uart);
629
647
return 1 ;
630
648
}
631
649
0 commit comments