Skip to content

Commit 266e10e

Browse files
committed
add Serialx.attachInterrupt_Send
- add Serialx.attachInterrupt_Send() - rename Serialx.attachInterrupt() to Serialx.attachInterrupt_Receive()
1 parent 8447da1 commit 266e10e

File tree

2 files changed

+54
-12
lines changed

2 files changed

+54
-12
lines changed

cores/arduino/UARTClass.cpp

+46-7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <stdio.h>
2121
#include <string.h>
2222
#include "UARTClass.h"
23+
#include "Arduino.h"
2324

2425
// Constructors ////////////////////////////////////////////////////////////////
2526

@@ -32,7 +33,8 @@ UARTClass::UARTClass( Uart *pUart, IRQn_Type dwIrq, uint32_t dwId, RingBuffer *p
3233
_dwIrq=dwIrq;
3334
_dwId=dwId;
3435

35-
_isr = NULL;
36+
_isrRx = NULL;
37+
_isrTx = NULL;
3638
}
3739

3840
// Public Methods //////////////////////////////////////////////////////////////
@@ -168,9 +170,28 @@ size_t UARTClass::write( const uint8_t uc_data )
168170
return 1;
169171
}
170172

171-
void UARTClass::attachInterrupt( USART_isr_t fn )
173+
void UARTClass::attachInterrupt_Receive( isrRx_t fn )
172174
{
173-
_isr = fn;
175+
// pause interrupts
176+
uint8_t oldISR = ((__get_PRIMASK() & 0x1) == 0 && (__get_FAULTMASK() & 0x1) == 0); noInterrupts();
177+
178+
// set custom function
179+
_isrRx = fn;
180+
181+
// restore old interrupt setting
182+
if (oldISR != 0) { interrupts(); }
183+
}
184+
185+
void UARTClass::attachInterrupt_Send( isrTx_t fn )
186+
{
187+
// pause interrupts
188+
uint8_t oldISR = ((__get_PRIMASK() & 0x1) == 0 && (__get_FAULTMASK() & 0x1) == 0); noInterrupts();
189+
190+
// set custom function for TX empty
191+
_isrTx = fn;
192+
193+
// restore old interrupt setting
194+
if (oldISR != 0) { interrupts(); }
174195
}
175196

176197
void UARTClass::IrqHandler( void )
@@ -180,11 +201,11 @@ void UARTClass::IrqHandler( void )
180201
// Did we receive data?
181202
if ((status & UART_SR_RXRDY) == UART_SR_RXRDY) {
182203

183-
// user function was attached -> call it with data and status byte
184-
if (_isr) {
185-
_isr(_pUart->UART_RHR, status);
204+
// custom function was attached -> call it with data and status byte
205+
if (_isrRx) {
206+
_isrRx(_pUart->UART_RHR, status);
186207
}
187-
// no user function attached -> store in ring buffer
208+
// no custom function attached -> store data in ring buffer
188209
else {
189210
_rx_buffer->store_char(_pUart->UART_RHR);
190211
}
@@ -202,6 +223,24 @@ void UARTClass::IrqHandler( void )
202223
{
203224
// Mask off transmit interrupt so we don't get it anymore
204225
_pUart->UART_IDR = UART_IDR_TXRDY;
226+
227+
// if custom routine attached, activate TXBUFE interrupt -> delay call until transmission finished
228+
if (_isrTx != NULL) {
229+
_pUart->UART_IER = UART_IER_TXEMPTY;
230+
}
231+
}
232+
233+
}
234+
235+
// Is data transmission finished? Used for call of attached custom function at end of transmission?
236+
if ((status & UART_SR_TXEMPTY) == UART_SR_TXEMPTY)
237+
{
238+
// Mask off interrupt so we don't get it anymore
239+
_pUart->UART_IDR = UART_IDR_TXEMPTY;
240+
241+
// if custom routine attached, call it
242+
if (_isrTx != NULL) {
243+
_isrTx();
205244
}
206245
}
207246

cores/arduino/UARTClass.h

+8-5
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
// missing in CMSIS
3535
#define UART_SR_RXBRK (0x1u << 2)
3636

37-
3837
class UARTClass : public HardwareSerial
3938
{
4039
public:
@@ -63,9 +62,12 @@ class UARTClass : public HardwareSerial
6362

6463
void IrqHandler(void);
6564

66-
typedef void (*USART_isr_t)(uint8_t data, uint32_t status);
67-
void attachInterrupt( USART_isr_t fn );
68-
void detachInterrupt() { attachInterrupt( (USART_isr_t) NULL ); };
65+
typedef void (* isrRx_t)(uint8_t data, uint32_t status);
66+
typedef void (* isrTx_t)( void );
67+
void attachInterrupt_Receive( isrRx_t fn );
68+
void detachInterrupt_Receive( void ) { attachInterrupt_Receive( (isrRx_t) NULL); };
69+
void attachInterrupt_Send( isrTx_t fn );
70+
void detachInterrupt_Send( void ) { attachInterrupt_Send( (isrTx_t) NULL); };
6971

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

@@ -79,7 +81,8 @@ class UARTClass : public HardwareSerial
7981
IRQn_Type _dwIrq;
8082
uint32_t _dwId;
8183

82-
USART_isr_t _isr;
84+
isrRx_t _isrRx;
85+
isrTx_t _isrTx;
8386
};
8487

8588
#endif // _UART_CLASS_

0 commit comments

Comments
 (0)