Skip to content

added optional custom handler to Serial interrupt #93

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

Closed
wants to merge 2 commits into from
Closed
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
62 changes: 59 additions & 3 deletions cores/arduino/UARTClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <stdio.h>
#include <string.h>
#include "UARTClass.h"
#include "Arduino.h"

// Constructors ////////////////////////////////////////////////////////////////

Expand All @@ -31,6 +32,9 @@ UARTClass::UARTClass( Uart *pUart, IRQn_Type dwIrq, uint32_t dwId, RingBuffer *p
_pUart=pUart;
_dwIrq=dwIrq;
_dwId=dwId;

_isrRx = NULL;
_isrTx = NULL;
}

// Public Methods //////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -166,13 +170,47 @@ size_t UARTClass::write( const uint8_t uc_data )
return 1;
}

void UARTClass::attachInterrupt_Receive( isrRx_t fn )
{
// pause interrupts
uint8_t oldISR = ((__get_PRIMASK() & 0x1) == 0 && (__get_FAULTMASK() & 0x1) == 0); noInterrupts();

// set custom function
_isrRx = fn;

// restore old interrupt setting
if (oldISR != 0) { interrupts(); }
}

void UARTClass::attachInterrupt_Send( isrTx_t fn )
{
// pause interrupts
uint8_t oldISR = ((__get_PRIMASK() & 0x1) == 0 && (__get_FAULTMASK() & 0x1) == 0); noInterrupts();

// set custom function for TX empty
_isrTx = fn;

// restore old interrupt setting
if (oldISR != 0) { interrupts(); }
}

void UARTClass::IrqHandler( void )
{
uint32_t status = _pUart->UART_SR;

// Did we receive data?
if ((status & UART_SR_RXRDY) == UART_SR_RXRDY)
_rx_buffer->store_char(_pUart->UART_RHR);
if ((status & UART_SR_RXRDY) == UART_SR_RXRDY) {

// custom function was attached -> call it with data and status byte
if (_isrRx) {
_isrRx(_pUart->UART_RHR, status);
}
// no custom function attached -> store data in ring buffer
else {
_rx_buffer->store_char(_pUart->UART_RHR);
}

}

// Do we need to keep sending data?
if ((status & UART_SR_TXRDY) == UART_SR_TXRDY)
Expand All @@ -185,11 +223,29 @@ void UARTClass::IrqHandler( void )
{
// Mask off transmit interrupt so we don't get it anymore
_pUart->UART_IDR = UART_IDR_TXRDY;

// if custom routine attached, activate TXBUFE interrupt -> delay call until transmission finished
if (_isrTx != NULL) {
_pUart->UART_IER = UART_IER_TXEMPTY;
}
}

}

// Is data transmission finished? Used for call of attached custom function at end of transmission?
if ((status & UART_SR_TXEMPTY) == UART_SR_TXEMPTY)
{
// Mask off interrupt so we don't get it anymore
_pUart->UART_IDR = UART_IDR_TXEMPTY;

// if custom routine attached, call it
if (_isrTx != NULL) {
_isrTx();
}
}

// Acknowledge errors
if ((status & UART_SR_OVRE) == UART_SR_OVRE || (status & UART_SR_FRAME) == UART_SR_FRAME)
if ((status & UART_SR_OVRE) == UART_SR_OVRE || (status & UART_SR_FRAME) == UART_SR_FRAME || (status & UART_SR_RXBRK) == UART_SR_RXBRK)
{
// TODO: error reporting outside ISR
_pUart->UART_CR |= UART_CR_RSTSTA;
Expand Down
11 changes: 11 additions & 0 deletions cores/arduino/UARTClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#define SERIAL_8M1 UARTClass::Mode_8M1
#define SERIAL_8S1 UARTClass::Mode_8S1

// missing in CMSIS
#define UART_SR_RXBRK (0x1u << 2)

class UARTClass : public HardwareSerial
{
Expand Down Expand Up @@ -60,6 +62,13 @@ class UARTClass : public HardwareSerial

void IrqHandler(void);

typedef void (* isrRx_t)(uint8_t data, uint32_t status);
typedef void (* isrTx_t)( void );
void attachInterrupt_Receive( isrRx_t fn );
void detachInterrupt_Receive( void ) { attachInterrupt_Receive( (isrRx_t) NULL); };
void attachInterrupt_Send( isrTx_t fn );
void detachInterrupt_Send( void ) { attachInterrupt_Send( (isrTx_t) NULL); };

operator bool() { return true; }; // UART always active

protected:
Expand All @@ -72,6 +81,8 @@ class UARTClass : public HardwareSerial
IRQn_Type _dwIrq;
uint32_t _dwId;

isrRx_t _isrRx;
isrTx_t _isrTx;
};

#endif // _UART_CLASS_