Skip to content

added optional custom handler to Serial interrupt #297

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
10 changes: 10 additions & 0 deletions cores/arduino/HardwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
Modified 28 September 2010 by Mark Sproul
Modified 14 August 2012 by Alarus
Modified 3 December 2013 by Matthijs Kooijman
Modified 2 November 2015 by SlashDev
Modified 7 November 2019 by Georg Icking-Konert
*/

#include <stdlib.h>
Expand Down Expand Up @@ -278,4 +280,12 @@ size_t HardwareSerial::write(uint8_t c)
return 1;
}

void HardwareSerial::attachInterrupt( isr_t fn )
{
uint8_t oldSREG = SREG;
cli();
_isr = fn;
SREG = oldSREG;
}

#endif // whole file
11 changes: 11 additions & 0 deletions cores/arduino/HardwareSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
Modified 28 September 2010 by Mark Sproul
Modified 14 August 2012 by Alarus
Modified 3 December 2013 by Matthijs Kooijman
Modified 2 November 2015 by SlashDev
Modified 7 November 2019 by Georg Icking-Konert
*/

#ifndef HardwareSerial_h
Expand Down Expand Up @@ -137,6 +139,15 @@ class HardwareSerial : public Stream
// Interrupt handlers - Not intended to be called externally
inline void _rx_complete_irq(void);
void _tx_udr_empty_irq(void);

typedef void (* isr_t)( uint8_t d, uint8_t s );
void attachInterrupt( isr_t fn );
void detachInterrupt() { attachInterrupt( (isr_t) NULL ); };
private:
isr_t _isr;

HardwareSerial( const HardwareSerial & );
HardwareSerial & operator =( const HardwareSerial &);
};

#if defined(UBRRH) || defined(UBRR0H)
Expand Down
2 changes: 2 additions & 0 deletions cores/arduino/HardwareSerial0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
Modified 28 September 2010 by Mark Sproul
Modified 14 August 2012 by Alarus
Modified 3 December 2013 by Matthijs Kooijman
Modified 2 November 2015 by SlashDev
Modified 7 November 2019 by Georg Icking-Konert
*/

#include "Arduino.h"
Expand Down
2 changes: 2 additions & 0 deletions cores/arduino/HardwareSerial1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
Modified 28 September 2010 by Mark Sproul
Modified 14 August 2012 by Alarus
Modified 3 December 2013 by Matthijs Kooijman
Modified 2 November 2015 by SlashDev
Modified 7 November 2019 by Georg Icking-Konert
*/

#include "Arduino.h"
Expand Down
2 changes: 2 additions & 0 deletions cores/arduino/HardwareSerial2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
Modified 28 September 2010 by Mark Sproul
Modified 14 August 2012 by Alarus
Modified 3 December 2013 by Matthijs Kooijman
Modified 2 November 2015 by SlashDev
Modified 7 November 2019 by Georg Icking-Konert
*/

#include "Arduino.h"
Expand Down
2 changes: 2 additions & 0 deletions cores/arduino/HardwareSerial3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
Modified 28 September 2010 by Mark Sproul
Modified 14 August 2012 by Alarus
Modified 3 December 2013 by Matthijs Kooijman
Modified 2 November 2015 by SlashDev
Modified 7 November 2019 by Georg Icking-Konert
*/

#include "Arduino.h"
Expand Down
50 changes: 32 additions & 18 deletions cores/arduino/HardwareSerial_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
Modified 23 November 2006 by David A. Mellis
Modified 28 September 2010 by Mark Sproul
Modified 14 August 2012 by Alarus
Modified 2 November 2015 by SlashDev
Modified 7 November 2019 by Georg Icking-Konert
*/

#include "wiring_private.h"
Expand Down Expand Up @@ -92,32 +94,44 @@ HardwareSerial::HardwareSerial(
_ucsra(ucsra), _ucsrb(ucsrb), _ucsrc(ucsrc),
_udr(udr),
_rx_buffer_head(0), _rx_buffer_tail(0),
_tx_buffer_head(0), _tx_buffer_tail(0)
_tx_buffer_head(0), _tx_buffer_tail(0),
_isr(0)
{
}

// Actual interrupt handlers //////////////////////////////////////////////////////////////

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

// if we should be storing the received character into the location
// just before the tail (meaning that the head would advance to the
// current location of the tail), we're about to overflow the buffer
// and so we don't write the character or advance the head.
if (i != _rx_buffer_tail) {
_rx_buffer[_rx_buffer_head] = c;
_rx_buffer_head = i;
}
} else {
// Parity error, read byte but discard it
*_udr;
};
// default: save data in ring buffer
else {
if (bit_is_clear(*_ucsra, UPE0)) {
unsigned char c = *_udr;
// No Parity error, read byte and store it in the buffer if there is
// room
rx_buffer_index_t i = (unsigned int)(_rx_buffer_head + 1) % SERIAL_RX_BUFFER_SIZE;

// if we should be storing the received character into the location
// just before the tail (meaning that the head would advance to the
// current location of the tail), we're about to overflow the buffer
// and so we don't write the character or advance the head.
if (i != _rx_buffer_tail) {
_rx_buffer[_rx_buffer_head] = c;
_rx_buffer_head = i;
}
}
else {
// Parity error, read byte but discard it
*_udr;
};
}
}

#endif // whole file