Skip to content

Remove very bad loop in Serial transmit driver that causes the CPU #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 19, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions cores/arduino/UART.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -107,8 +106,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);
Expand All @@ -121,6 +118,22 @@ 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) {
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)
Expand Down Expand Up @@ -253,9 +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
if ( (!(SREG & CPU_I_bm)) || (!((*_hwserial_module).CTRLA & USART_DREIE_bm)) ) {
_tx_data_empty_irq();
}
_poll_tx_data_empty();
}
// If we get here, nothing is queued anymore (DREIE is disabled) and
// the hardware finished transmission (TXCIF is set).
Expand Down Expand Up @@ -294,18 +305,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
}
_poll_tx_data_empty();
}

_tx_buffer[_tx_buffer_head] = c;
Expand Down
2 changes: 2 additions & 0 deletions cores/arduino/UART.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 _poll_tx_data_empty(void);
};

#if defined(HWSERIAL0)
Expand Down