From 525bb7d014343ccf2e117920270808f9a5bde463 Mon Sep 17 00:00:00 2001 From: egil Date: Fri, 2 Aug 2019 15:41:49 +0200 Subject: [PATCH 1/5] Remove very bad loop in Serial transmit driver that causes the CPU to spend most of its time looping in the interrupt driver, entirely locking out other operations and also causing incoming data on other Serial ports to be lost. --- cores/arduino/UART.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/cores/arduino/UART.cpp b/cores/arduino/UART.cpp index 8e0520bc..7bc8d47b 100644 --- a/cores/arduino/UART.cpp +++ b/cores/arduino/UART.cpp @@ -107,8 +107,6 @@ void UartClass::_tx_data_empty_irq(void) (*_hwserial_module).TXDATAL = c; - while(!((*_hwserial_module).STATUS & USART_DREIF_bm)); - if (_tx_buffer_head == _tx_buffer_tail) { // Buffer empty, so disable "data register empty" interrupt (*_hwserial_module).CTRLA &= (~USART_DREIE_bm); From 8462a2576644968352b527887c26157805627d62 Mon Sep 17 00:00:00 2001 From: egil Date: Fri, 2 Aug 2019 20:15:47 +0200 Subject: [PATCH 2/5] Make one common method to handle the interrupt emulation, and also test DREIF befre actually invoking the emulated interrupt. --- cores/arduino/UART.cpp | 33 +++++++++++++++++++-------------- cores/arduino/UART.h | 2 ++ 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/cores/arduino/UART.cpp b/cores/arduino/UART.cpp index 7bc8d47b..a05804cb 100644 --- a/cores/arduino/UART.cpp +++ b/cores/arduino/UART.cpp @@ -119,6 +119,22 @@ void UartClass::_tx_data_empty_irq(void) } } +// To invoke data empty "interrupt" via a call, use this method +void UartClass::_tx_data_empty_soft(void) { + if ( (!(SREG & CPU_I_bm)) || (!((*_hwserial_module).CTRLA & USART_DREIE_bm)) ) { + // Interrupts are disabled either globally or for data register empty, + // so we'll have to poll the "data register empty" flag ourselves. + // If it is set, pretend an interrupt has happened and call the handler + //to free up space for us. + + // Invoke interrupt handler only if conditions data register is empty + if ((*_hwserial_module).STATUS & USART_DREIF_bm) { + _tx_data_empty_irq(); + } + } + // In case interrupts are enabled, the interrupt routine will be invoked by itself +} + // Public Methods ////////////////////////////////////////////////////////////// void UartClass::begin(unsigned long baud, uint16_t config) @@ -251,9 +267,7 @@ void UartClass::flush() // If interrupts are globally disabled or the and DR empty interrupt is disabled, // poll the "data register empty" interrupt flag to prevent deadlock - if ( (!(SREG & CPU_I_bm)) || (!((*_hwserial_module).CTRLA & USART_DREIE_bm)) ) { - _tx_data_empty_irq(); - } + _tx_data_empty_soft(); } // If we get here, nothing is queued anymore (DREIE is disabled) and // the hardware finished transmission (TXCIF is set). @@ -292,18 +306,9 @@ size_t UartClass::write(uint8_t c) tx_buffer_index_t i = (_tx_buffer_head + 1) % SERIAL_TX_BUFFER_SIZE; //If the output buffer is full, there's nothing for it other than to - //wait for the interrupt handler to empty it a bit + //wait for the interrupt handler to empty it a bit (or emulate interrupts) while (i == _tx_buffer_tail) { - if ( ( !(SREG & CPU_I_bm) ) || ( !((*_hwserial_module).CTRLA & USART_DREIE_bm) ) ) { - // Interrupts are disabled either globally or for data register empty, - // so we'll have to poll the "data register empty" flag ourselves. - // If it is set, pretend an interrupt has happened and call the handler - //to free up space for us. - - _tx_data_empty_irq(); - } else { - // nop, the interrupt handler will free up space for us - } + _tx_data_empty_soft(); } _tx_buffer[_tx_buffer_head] = c; diff --git a/cores/arduino/UART.h b/cores/arduino/UART.h index ef966e9f..c2f987fd 100644 --- a/cores/arduino/UART.h +++ b/cores/arduino/UART.h @@ -169,6 +169,8 @@ class UartClass : public HardwareSerial // Interrupt handlers - Not intended to be called externally inline void _rx_complete_irq(void); void _tx_data_empty_irq(void); + private: + void _tx_data_empty_soft(void); }; #if defined(HWSERIAL0) From 106efcf201bcfeb94906176e5e993573e91ea876 Mon Sep 17 00:00:00 2001 From: egil Date: Sun, 4 Aug 2019 07:35:43 +0200 Subject: [PATCH 3/5] Remove comment no longer valid --- cores/arduino/UART.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/cores/arduino/UART.cpp b/cores/arduino/UART.cpp index a05804cb..076cd068 100644 --- a/cores/arduino/UART.cpp +++ b/cores/arduino/UART.cpp @@ -88,7 +88,6 @@ void serialEventRun(void) void UartClass::_tx_data_empty_irq(void) { // Check if tx buffer already empty. - // This interrupt-handler can be called "manually" from flush(); if (_tx_buffer_head == _tx_buffer_tail) { // Buffer empty, so disable "data register empty" interrupt (*_hwserial_module).CTRLA &= (~USART_DREIE_bm); From 08e530fdc7b14f36041e52fd3a38e9f649d622c0 Mon Sep 17 00:00:00 2001 From: egil Date: Mon, 5 Aug 2019 18:26:36 +0200 Subject: [PATCH 4/5] Improve name of DRE polling function --- cores/arduino/UART.cpp | 6 +++--- cores/arduino/UART.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cores/arduino/UART.cpp b/cores/arduino/UART.cpp index 076cd068..8bde78ff 100644 --- a/cores/arduino/UART.cpp +++ b/cores/arduino/UART.cpp @@ -119,7 +119,7 @@ void UartClass::_tx_data_empty_irq(void) } // To invoke data empty "interrupt" via a call, use this method -void UartClass::_tx_data_empty_soft(void) { +void UartClass::_poll_tx_data_empty(void) { if ( (!(SREG & CPU_I_bm)) || (!((*_hwserial_module).CTRLA & USART_DREIE_bm)) ) { // Interrupts are disabled either globally or for data register empty, // so we'll have to poll the "data register empty" flag ourselves. @@ -266,7 +266,7 @@ void UartClass::flush() // If interrupts are globally disabled or the and DR empty interrupt is disabled, // poll the "data register empty" interrupt flag to prevent deadlock - _tx_data_empty_soft(); + _poll_tx_data_empty(); } // If we get here, nothing is queued anymore (DREIE is disabled) and // the hardware finished transmission (TXCIF is set). @@ -307,7 +307,7 @@ size_t UartClass::write(uint8_t c) //If the output buffer is full, there's nothing for it other than to //wait for the interrupt handler to empty it a bit (or emulate interrupts) while (i == _tx_buffer_tail) { - _tx_data_empty_soft(); + _poll_tx_data_empty(); } _tx_buffer[_tx_buffer_head] = c; diff --git a/cores/arduino/UART.h b/cores/arduino/UART.h index c2f987fd..574f0c35 100644 --- a/cores/arduino/UART.h +++ b/cores/arduino/UART.h @@ -170,7 +170,7 @@ class UartClass : public HardwareSerial inline void _rx_complete_irq(void); void _tx_data_empty_irq(void); private: - void _tx_data_empty_soft(void); + void _poll_tx_data_empty(void); }; #if defined(HWSERIAL0) From 3b231831577e9fdc3f6108e6eed0cb8bf03c0d94 Mon Sep 17 00:00:00 2001 From: egil Date: Tue, 6 Aug 2019 11:33:19 +0200 Subject: [PATCH 5/5] Formatting according to Arduino standards --- cores/arduino/UART.cpp | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/cores/arduino/UART.cpp b/cores/arduino/UART.cpp index 8bde78ff..0990136d 100644 --- a/cores/arduino/UART.cpp +++ b/cores/arduino/UART.cpp @@ -119,17 +119,18 @@ void UartClass::_tx_data_empty_irq(void) } // To invoke data empty "interrupt" via a call, use this method -void UartClass::_poll_tx_data_empty(void) { +void UartClass::_poll_tx_data_empty(void) +{ if ( (!(SREG & CPU_I_bm)) || (!((*_hwserial_module).CTRLA & USART_DREIE_bm)) ) { - // Interrupts are disabled either globally or for data register empty, - // so we'll have to poll the "data register empty" flag ourselves. - // If it is set, pretend an interrupt has happened and call the handler - //to free up space for us. - - // Invoke interrupt handler only if conditions data register is empty - if ((*_hwserial_module).STATUS & USART_DREIF_bm) { - _tx_data_empty_irq(); - } + // Interrupts are disabled either globally or for data register empty, + // so we'll have to poll the "data register empty" flag ourselves. + // If it is set, pretend an interrupt has happened and call the handler + //to free up space for us. + + // Invoke interrupt handler only if conditions data register is empty + if ((*_hwserial_module).STATUS & USART_DREIF_bm) { + _tx_data_empty_irq(); + } } // In case interrupts are enabled, the interrupt routine will be invoked by itself } @@ -266,7 +267,7 @@ void UartClass::flush() // If interrupts are globally disabled or the and DR empty interrupt is disabled, // poll the "data register empty" interrupt flag to prevent deadlock - _poll_tx_data_empty(); + _poll_tx_data_empty(); } // If we get here, nothing is queued anymore (DREIE is disabled) and // the hardware finished transmission (TXCIF is set). @@ -307,7 +308,7 @@ size_t UartClass::write(uint8_t c) //If the output buffer is full, there's nothing for it other than to //wait for the interrupt handler to empty it a bit (or emulate interrupts) while (i == _tx_buffer_tail) { - _poll_tx_data_empty(); + _poll_tx_data_empty(); } _tx_buffer[_tx_buffer_head] = c;