Skip to content
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

Serial attach interrupt #95

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
73 changes: 70 additions & 3 deletions cores/arduino/UARTClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

Modified 23 November 2019 by Georg Icking-Konert
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "UARTClass.h"
#include "Arduino.h"

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

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

_isrRx = NULL;
_isrTx = NULL;
_rxArgs = NULL;
}

// Public Methods //////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -162,17 +169,59 @@ size_t UARTClass::write( const uint8_t uc_data )
{
// Bypass buffering and send character directly
_pUart->UART_THR = uc_data;

// if custom routine attached, activate TXBUFE interrupt -> delay call until transmission finished
// must be done here explicitely because UART_TXRDY interrupt is not activated here
if (_isrTx != NULL) {
_pUart->UART_IER = UART_IER_TXEMPTY;
}
}

return 1;
}

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

// set custom function, and store argument pointer
_isrRx = fn;
_rxArgs = args;

// 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, _rxArgs);
}
// 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 +234,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
14 changes: 14 additions & 0 deletions cores/arduino/UARTClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

Modified 23 November 2019 by Georg Icking-Konert
*/

#ifndef _UART_CLASS_
Expand All @@ -31,6 +33,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 +64,13 @@ class UARTClass : public HardwareSerial

void IrqHandler(void);

typedef void (* isrRx_t)(uint8_t data, uint32_t status, void* args);
typedef void (* isrTx_t)( void );
void attachInterrupt_Receive( isrRx_t fn, void* args = NULL );
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 +83,9 @@ class UARTClass : public HardwareSerial
IRQn_Type _dwIrq;
uint32_t _dwId;

isrRx_t _isrRx;
isrTx_t _isrTx;
void* _rxArgs;
};

#endif // _UART_CLASS_
Loading