@@ -116,6 +116,13 @@ int uart_get_debug();
116
116
// ####################################################################################################
117
117
// ####################################################################################################
118
118
119
+ // These function internals can be used from interrupt handlers to ensure they
120
+ // are in instruction RAM, or anywhere that the uart_nr has been validated.
121
+ #define UART_GET_TX_FIFO_ROOM (uart_nr ) (UART_TX_FIFO_SIZE - ((USS(uart_nr) >> USTXC) & 0xff ))
122
+ #define UART_TRANSMIT_CHAR (uart_nr, c ) do { USF (uart_nr) = (c); } while (0 )
123
+ #define UART_ARM_TX_INTERRUPT (uart_nr ) do { USIE (uart_nr) |= (1 << UIFE); } while (0 )
124
+ #define UART_DISARM_TX_INTERRUPT (uart_nr ) do { USIE (uart_nr) &= ~(1 << UIFE); } while (0 )
125
+
119
126
void ICACHE_RAM_ATTR uart_interrupt_handler (uart_t * uart) {
120
127
121
128
// -------------- UART 0 --------------
@@ -160,7 +167,7 @@ size_t uart_get_tx_fifo_room(uart_t* uart) {
160
167
if (uart == 0 )
161
168
return 0 ;
162
169
if (uart->txEnabled ) {
163
- return UART_TX_FIFO_SIZE - (( USS ( uart->uart_nr ) >> USTXC) & 0xff );
170
+ return UART_GET_TX_FIFO_ROOM ( uart->uart_nr );
164
171
}
165
172
return 0 ;
166
173
}
@@ -177,7 +184,7 @@ void uart_transmit_char(uart_t* uart, char c) {
177
184
if (uart == 0 )
178
185
return ;
179
186
if (uart->txEnabled ) {
180
- USF (uart->uart_nr ) = c ;
187
+ UART_TRANSMIT_CHAR (uart->uart_nr , c) ;
181
188
}
182
189
}
183
190
@@ -241,15 +248,15 @@ void uart_arm_tx_interrupt(uart_t* uart) {
241
248
if (uart == 0 )
242
249
return ;
243
250
if (uart->txEnabled ) {
244
- USIE (uart->uart_nr ) |= ( 1 << UIFE );
251
+ UART_ARM_TX_INTERRUPT (uart->uart_nr );
245
252
}
246
253
}
247
254
248
255
void uart_disarm_tx_interrupt (uart_t * uart) {
249
256
if (uart == 0 )
250
257
return ;
251
258
if (uart->txEnabled ) {
252
- USIE (uart->uart_nr ) &= ~( 1 << UIFE );
259
+ UART_DISARM_TX_INTERRUPT (uart->uart_nr );
253
260
}
254
261
}
255
262
@@ -536,13 +543,13 @@ void HardwareSerial::setDebugOutput(bool en) {
536
543
}
537
544
}
538
545
539
- bool HardwareSerial::isTxEnabled (void ) {
546
+ bool ICACHE_RAM_ATTR HardwareSerial::isTxEnabled (void ) {
540
547
if (_uart == 0 )
541
548
return false ;
542
549
return _uart->txEnabled ;
543
550
}
544
551
545
- bool HardwareSerial::isRxEnabled (void ) {
552
+ bool ICACHE_RAM_ATTR HardwareSerial::isRxEnabled (void ) {
546
553
if (_uart == 0 )
547
554
return false ;
548
555
return _uart->rxEnabled ;
@@ -604,11 +611,12 @@ void HardwareSerial::flush() {
604
611
if (!_written)
605
612
return ;
606
613
614
+ const int uart_nr = _uart->uart_nr ;
607
615
while (true ) {
608
616
{
609
617
InterruptLock il;
610
618
if (_tx_buffer->getSize () == 0 &&
611
- uart_get_tx_fifo_room (_uart ) >= UART_TX_FIFO_SIZE) {
619
+ UART_GET_TX_FIFO_ROOM (uart_nr ) >= UART_TX_FIFO_SIZE) {
612
620
break ;
613
621
}
614
622
}
@@ -623,15 +631,16 @@ size_t HardwareSerial::write(uint8_t c) {
623
631
_written = true ;
624
632
625
633
bool tx_now = false ;
634
+ const int uart_nr = _uart->uart_nr ;
626
635
while (true ) {
627
636
{
628
637
InterruptLock il;
629
638
if (_tx_buffer->empty ()) {
630
- if (uart_get_tx_fifo_room (_uart ) > 0 ) {
639
+ if (UART_GET_TX_FIFO_ROOM (uart_nr ) > 0 ) {
631
640
tx_now = true ;
632
641
} else {
633
642
_tx_buffer->write (c);
634
- uart_arm_tx_interrupt (_uart );
643
+ UART_ARM_TX_INTERRUPT (uart_nr );
635
644
}
636
645
break ;
637
646
} else if (_tx_buffer->write (c)) {
@@ -641,7 +650,7 @@ size_t HardwareSerial::write(uint8_t c) {
641
650
yield ();
642
651
}
643
652
if (tx_now) {
644
- uart_transmit_char (_uart , c);
653
+ UART_TRANSMIT_CHAR (uart_nr , c);
645
654
}
646
655
return 1 ;
647
656
}
@@ -650,26 +659,27 @@ HardwareSerial::operator bool() const {
650
659
return _uart != 0 ;
651
660
}
652
661
653
- void HardwareSerial::_rx_complete_irq (char c) {
662
+ void ICACHE_RAM_ATTR HardwareSerial::_rx_complete_irq (char c) {
654
663
if (_rx_buffer) {
655
664
_rx_buffer->write (c);
656
665
}
657
666
}
658
667
659
- void HardwareSerial::_tx_empty_irq (void ) {
668
+ void ICACHE_RAM_ATTR HardwareSerial::_tx_empty_irq (void ) {
660
669
if (_uart == 0 )
661
670
return ;
662
671
if (_tx_buffer == 0 )
663
672
return ;
673
+ const int uart_nr = _uart->uart_nr ;
664
674
size_t queued = _tx_buffer->getSize ();
665
675
if (!queued) {
666
- uart_disarm_tx_interrupt (_uart );
676
+ UART_DISARM_TX_INTERRUPT (uart_nr );
667
677
return ;
668
678
}
669
679
670
- size_t room = uart_get_tx_fifo_room (_uart );
680
+ size_t room = UART_GET_TX_FIFO_ROOM (uart_nr );
671
681
int n = static_cast <int >((queued < room) ? queued : room);
672
682
while (n--) {
673
- uart_transmit_char (_uart , _tx_buffer->read ());
683
+ UART_TRANSMIT_CHAR (uart_nr , _tx_buffer->read ());
674
684
}
675
685
}
0 commit comments