20
20
#include < stdio.h>
21
21
#include < string.h>
22
22
#include " UARTClass.h"
23
+ #include " Arduino.h"
23
24
24
25
// Constructors ////////////////////////////////////////////////////////////////
25
26
@@ -32,7 +33,8 @@ UARTClass::UARTClass( Uart *pUart, IRQn_Type dwIrq, uint32_t dwId, RingBuffer *p
32
33
_dwIrq=dwIrq;
33
34
_dwId=dwId;
34
35
35
- _isr = NULL ;
36
+ _isrRx = NULL ;
37
+ _isrTx = NULL ;
36
38
}
37
39
38
40
// Public Methods //////////////////////////////////////////////////////////////
@@ -168,9 +170,28 @@ size_t UARTClass::write( const uint8_t uc_data )
168
170
return 1 ;
169
171
}
170
172
171
- void UARTClass::attachInterrupt ( USART_isr_t fn )
173
+ void UARTClass::attachInterrupt_Receive ( isrRx_t fn )
172
174
{
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 (); }
174
195
}
175
196
176
197
void UARTClass::IrqHandler ( void )
@@ -180,11 +201,11 @@ void UARTClass::IrqHandler( void )
180
201
// Did we receive data?
181
202
if ((status & UART_SR_RXRDY) == UART_SR_RXRDY) {
182
203
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);
186
207
}
187
- // no user function attached -> store in ring buffer
208
+ // no custom function attached -> store data in ring buffer
188
209
else {
189
210
_rx_buffer->store_char (_pUart->UART_RHR );
190
211
}
@@ -202,6 +223,24 @@ void UARTClass::IrqHandler( void )
202
223
{
203
224
// Mask off transmit interrupt so we don't get it anymore
204
225
_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 ();
205
244
}
206
245
}
207
246
0 commit comments