Skip to content

Prevent full UART TX buffer from hanging processor #262

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 1 commit into from
Jan 25, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion cores/arduino/SERCOM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ void SERCOM::initClockNVIC( void )

// Setting NVIC
NVIC_EnableIRQ(IdNvic);
NVIC_SetPriority (IdNvic, (1<<__NVIC_PRIO_BITS) - 1); /* set Priority */
NVIC_SetPriority (IdNvic, SERCOM_NVIC_PRIORITY); /* set Priority */

//Setting clock
GCLK->CLKCTRL.reg = GCLK_CLKCTRL_ID( clockId ) | // Generic Clock 0 (SERCOMx)
Expand Down
3 changes: 2 additions & 1 deletion cores/arduino/SERCOM.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@

#include "sam.h"

#define SERCOM_FREQ_REF 48000000
#define SERCOM_FREQ_REF 48000000
#define SERCOM_NVIC_PRIORITY ((1<<__NVIC_PRIO_BITS) - 1)

typedef enum
{
Expand Down
22 changes: 21 additions & 1 deletion cores/arduino/Uart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,27 @@ size_t Uart::write(const uint8_t data)
if (sercom->isDataRegisterEmptyUART() && txBuffer.available() == 0) {
sercom->writeDataUART(data);
} else {
while(txBuffer.isFull()); // spin lock until a spot opens up in the buffer
// spin lock until a spot opens up in the buffer
while(txBuffer.isFull()) {
uint8_t interruptsEnabled = ((__get_PRIMASK() & 0x1) == 0);

if (interruptsEnabled) {
uint32_t exceptionNumber = (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk);

if (exceptionNumber == 0 ||
NVIC_GetPriority((IRQn_Type)(exceptionNumber - 16)) > SERCOM_NVIC_PRIORITY) {
// no exception or called from an ISR with lower priority,
// wait for free buffer spot via IRQ
continue;
}
}

// interrupts are disabled or called from ISR with higher or equal priority than the SERCOM IRQ
// manually call the UART IRQ handler when the data register is empty
if (sercom->isDataRegisterEmptyUART()) {
IrqHandler();
}
}

txBuffer.store_char(data);

Expand Down