Skip to content

Commit 291b7f7

Browse files
committed
added optional custom handler to Serial interrupt
added optional custom handler to Serial interrupt. Is minor enhancement of NeoHWSerial by SlashDevin (https://github.com/SlashDevin/NeoHWSerial), as user function also gets UART status as parameter to e.g. detect LIN break in user routine. Added as core extension instead of library due to similar extension for Due (https://github.com/gicking/ArduinoCore-sam) which AFAIK cannot be implemented as library.
1 parent c8d6aef commit 291b7f7

7 files changed

+61
-18
lines changed

cores/arduino/HardwareSerial.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
Modified 28 September 2010 by Mark Sproul
2121
Modified 14 August 2012 by Alarus
2222
Modified 3 December 2013 by Matthijs Kooijman
23+
Modified 2 November 2015 by SlashDev
24+
Modified 7 November 2019 by Georg Icking-Konert
2325
*/
2426

2527
#include <stdlib.h>
@@ -278,4 +280,12 @@ size_t HardwareSerial::write(uint8_t c)
278280
return 1;
279281
}
280282

283+
void HardwareSerial::attachInterrupt( isr_t fn )
284+
{
285+
uint8_t oldSREG = SREG;
286+
cli();
287+
_isr = fn;
288+
SREG = oldSREG;
289+
}
290+
281291
#endif // whole file

cores/arduino/HardwareSerial.h

+11
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
Modified 28 September 2010 by Mark Sproul
2020
Modified 14 August 2012 by Alarus
2121
Modified 3 December 2013 by Matthijs Kooijman
22+
Modified 2 November 2015 by SlashDev
23+
Modified 7 November 2019 by Georg Icking-Konert
2224
*/
2325

2426
#ifndef HardwareSerial_h
@@ -137,6 +139,15 @@ class HardwareSerial : public Stream
137139
// Interrupt handlers - Not intended to be called externally
138140
inline void _rx_complete_irq(void);
139141
void _tx_udr_empty_irq(void);
142+
143+
typedef void (* isr_t)( uint8_t d=0x00, uint8_t s=0x00 );
144+
void attachInterrupt( isr_t fn );
145+
void detachInterrupt() { attachInterrupt( (isr_t) NULL ); };
146+
private:
147+
isr_t _isr;
148+
149+
HardwareSerial( const HardwareSerial & );
150+
HardwareSerial & operator =( const HardwareSerial &);
140151
};
141152

142153
#if defined(UBRRH) || defined(UBRR0H)

cores/arduino/HardwareSerial0.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
Modified 28 September 2010 by Mark Sproul
2121
Modified 14 August 2012 by Alarus
2222
Modified 3 December 2013 by Matthijs Kooijman
23+
Modified 2 November 2015 by SlashDev
24+
Modified 7 November 2019 by Georg Icking-Konert
2325
*/
2426

2527
#include "Arduino.h"

cores/arduino/HardwareSerial1.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
Modified 28 September 2010 by Mark Sproul
2121
Modified 14 August 2012 by Alarus
2222
Modified 3 December 2013 by Matthijs Kooijman
23+
Modified 2 November 2015 by SlashDev
24+
Modified 7 November 2019 by Georg Icking-Konert
2325
*/
2426

2527
#include "Arduino.h"

cores/arduino/HardwareSerial2.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
Modified 28 September 2010 by Mark Sproul
2121
Modified 14 August 2012 by Alarus
2222
Modified 3 December 2013 by Matthijs Kooijman
23+
Modified 2 November 2015 by SlashDev
24+
Modified 7 November 2019 by Georg Icking-Konert
2325
*/
2426

2527
#include "Arduino.h"

cores/arduino/HardwareSerial3.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
Modified 28 September 2010 by Mark Sproul
2121
Modified 14 August 2012 by Alarus
2222
Modified 3 December 2013 by Matthijs Kooijman
23+
Modified 2 November 2015 by SlashDev
24+
Modified 7 November 2019 by Georg Icking-Konert
2325
*/
2426

2527
#include "Arduino.h"

cores/arduino/HardwareSerial_private.h

+32-18
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
Modified 23 November 2006 by David A. Mellis
2020
Modified 28 September 2010 by Mark Sproul
2121
Modified 14 August 2012 by Alarus
22+
Modified 2 November 2015 by SlashDev
23+
Modified 7 November 2019 by Georg Icking-Konert
2224
*/
2325

2426
#include "wiring_private.h"
@@ -92,32 +94,44 @@ HardwareSerial::HardwareSerial(
9294
_ucsra(ucsra), _ucsrb(ucsrb), _ucsrc(ucsrc),
9395
_udr(udr),
9496
_rx_buffer_head(0), _rx_buffer_tail(0),
95-
_tx_buffer_head(0), _tx_buffer_tail(0)
97+
_tx_buffer_head(0), _tx_buffer_tail(0),
98+
_isr(0)
9699
{
97100
}
98101

99102
// Actual interrupt handlers //////////////////////////////////////////////////////////////
100103

101104
void HardwareSerial::_rx_complete_irq(void)
102105
{
103-
if (bit_is_clear(*_ucsra, UPE0)) {
104-
// No Parity error, read byte and store it in the buffer if there is
105-
// room
106-
unsigned char c = *_udr;
107-
rx_buffer_index_t i = (unsigned int)(_rx_buffer_head + 1) % SERIAL_RX_BUFFER_SIZE;
106+
// user function was attached -> call it with data and status byte
107+
if (_isr) {
108+
unsigned char status = *_ucsra;
109+
unsigned char data = *_udr;
110+
_isr( data, status );
111+
}
108112

109-
// if we should be storing the received character into the location
110-
// just before the tail (meaning that the head would advance to the
111-
// current location of the tail), we're about to overflow the buffer
112-
// and so we don't write the character or advance the head.
113-
if (i != _rx_buffer_tail) {
114-
_rx_buffer[_rx_buffer_head] = c;
115-
_rx_buffer_head = i;
116-
}
117-
} else {
118-
// Parity error, read byte but discard it
119-
*_udr;
120-
};
113+
// default: save data in ring buffer
114+
else {
115+
if (bit_is_clear(*_ucsra, UPE0)) {
116+
unsigned char c = *_udr;
117+
// No Parity error, read byte and store it in the buffer if there is
118+
// room
119+
rx_buffer_index_t i = (unsigned int)(_rx_buffer_head + 1) % SERIAL_RX_BUFFER_SIZE;
120+
121+
// if we should be storing the received character into the location
122+
// just before the tail (meaning that the head would advance to the
123+
// current location of the tail), we're about to overflow the buffer
124+
// and so we don't write the character or advance the head.
125+
if (i != _rx_buffer_tail) {
126+
_rx_buffer[_rx_buffer_head] = c;
127+
_rx_buffer_head = i;
128+
}
129+
}
130+
else {
131+
// Parity error, read byte but discard it
132+
*_udr;
133+
};
134+
}
121135
}
122136

123137
#endif // whole file

0 commit comments

Comments
 (0)