@@ -116,6 +116,13 @@ int uart_get_debug();
116116// ####################################################################################################
117117// ####################################################################################################
118118
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+
119126void ICACHE_RAM_ATTR uart_interrupt_handler (uart_t * uart) {
120127
121128 // -------------- UART 0 --------------
@@ -160,7 +167,7 @@ size_t uart_get_tx_fifo_room(uart_t* uart) {
160167 if (uart == 0 )
161168 return 0 ;
162169 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 );
164171 }
165172 return 0 ;
166173}
@@ -177,7 +184,7 @@ void uart_transmit_char(uart_t* uart, char c) {
177184 if (uart == 0 )
178185 return ;
179186 if (uart->txEnabled ) {
180- USF (uart->uart_nr ) = c ;
187+ UART_TRANSMIT_CHAR (uart->uart_nr , c) ;
181188 }
182189}
183190
@@ -241,15 +248,15 @@ void uart_arm_tx_interrupt(uart_t* uart) {
241248 if (uart == 0 )
242249 return ;
243250 if (uart->txEnabled ) {
244- USIE (uart->uart_nr ) |= ( 1 << UIFE );
251+ UART_ARM_TX_INTERRUPT (uart->uart_nr );
245252 }
246253}
247254
248255void uart_disarm_tx_interrupt (uart_t * uart) {
249256 if (uart == 0 )
250257 return ;
251258 if (uart->txEnabled ) {
252- USIE (uart->uart_nr ) &= ~( 1 << UIFE );
259+ UART_DISARM_TX_INTERRUPT (uart->uart_nr );
253260 }
254261}
255262
@@ -536,13 +543,13 @@ void HardwareSerial::setDebugOutput(bool en) {
536543 }
537544}
538545
539- bool HardwareSerial::isTxEnabled (void ) {
546+ bool ICACHE_RAM_ATTR HardwareSerial::isTxEnabled (void ) {
540547 if (_uart == 0 )
541548 return false ;
542549 return _uart->txEnabled ;
543550}
544551
545- bool HardwareSerial::isRxEnabled (void ) {
552+ bool ICACHE_RAM_ATTR HardwareSerial::isRxEnabled (void ) {
546553 if (_uart == 0 )
547554 return false ;
548555 return _uart->rxEnabled ;
@@ -604,11 +611,12 @@ void HardwareSerial::flush() {
604611 if (!_written)
605612 return ;
606613
614+ const int uart_nr = _uart->uart_nr ;
607615 while (true ) {
608616 {
609617 InterruptLock il;
610618 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) {
612620 break ;
613621 }
614622 }
@@ -623,15 +631,16 @@ size_t HardwareSerial::write(uint8_t c) {
623631 _written = true ;
624632
625633 bool tx_now = false ;
634+ const int uart_nr = _uart->uart_nr ;
626635 while (true ) {
627636 {
628637 InterruptLock il;
629638 if (_tx_buffer->empty ()) {
630- if (uart_get_tx_fifo_room (_uart ) > 0 ) {
639+ if (UART_GET_TX_FIFO_ROOM (uart_nr ) > 0 ) {
631640 tx_now = true ;
632641 } else {
633642 _tx_buffer->write (c);
634- uart_arm_tx_interrupt (_uart );
643+ UART_ARM_TX_INTERRUPT (uart_nr );
635644 }
636645 break ;
637646 } else if (_tx_buffer->write (c)) {
@@ -641,7 +650,7 @@ size_t HardwareSerial::write(uint8_t c) {
641650 yield ();
642651 }
643652 if (tx_now) {
644- uart_transmit_char (_uart , c);
653+ UART_TRANSMIT_CHAR (uart_nr , c);
645654 }
646655 return 1 ;
647656}
@@ -650,26 +659,27 @@ HardwareSerial::operator bool() const {
650659 return _uart != 0 ;
651660}
652661
653- void HardwareSerial::_rx_complete_irq (char c) {
662+ void ICACHE_RAM_ATTR HardwareSerial::_rx_complete_irq (char c) {
654663 if (_rx_buffer) {
655664 _rx_buffer->write (c);
656665 }
657666}
658667
659- void HardwareSerial::_tx_empty_irq (void ) {
668+ void ICACHE_RAM_ATTR HardwareSerial::_tx_empty_irq (void ) {
660669 if (_uart == 0 )
661670 return ;
662671 if (_tx_buffer == 0 )
663672 return ;
673+ const int uart_nr = _uart->uart_nr ;
664674 size_t queued = _tx_buffer->getSize ();
665675 if (!queued) {
666- uart_disarm_tx_interrupt (_uart );
676+ UART_DISARM_TX_INTERRUPT (uart_nr );
667677 return ;
668678 }
669679
670- size_t room = uart_get_tx_fifo_room (_uart );
680+ size_t room = UART_GET_TX_FIFO_ROOM (uart_nr );
671681 int n = static_cast <int >((queued < room) ? queued : room);
672682 while (n--) {
673- uart_transmit_char (_uart , _tx_buffer->read ());
683+ UART_TRANSMIT_CHAR (uart_nr , _tx_buffer->read ());
674684 }
675685}
0 commit comments