Skip to content

Commit 8665c03

Browse files
committed
add Serial.attachInterrupt_Send()
add Serial.attachInterrupt_Send() rename Serial.attachInterrupt() to Serial.attachInterrupt_Receive()
1 parent e5fd810 commit 8665c03

7 files changed

+91
-20
lines changed

Diff for: cores/arduino/HardwareSerial.cpp

+29-2
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,14 @@ void HardwareSerial::_tx_udr_empty_irq(void)
114114
}
115115
}
116116

117+
void HardwareSerial::_tx_complete_irq(void)
118+
{
119+
// user send function was attached -> call it
120+
if (_isrTx) {
121+
_isrTx();
122+
}
123+
}
124+
117125
// Public Methods //////////////////////////////////////////////////////////////
118126

119127
void HardwareSerial::begin(unsigned long baud, byte config)
@@ -160,6 +168,7 @@ void HardwareSerial::end()
160168
cbi(*_ucsrb, TXEN0);
161169
cbi(*_ucsrb, RXCIE0);
162170
cbi(*_ucsrb, UDRIE0);
171+
cbi(*_ucsrb, TXCIE0);
163172

164173
// clear any received data
165174
_rx_buffer_head = _rx_buffer_tail;
@@ -280,11 +289,29 @@ size_t HardwareSerial::write(uint8_t c)
280289
return 1;
281290
}
282291

283-
void HardwareSerial::attachInterrupt( isr_t fn )
292+
void HardwareSerial::attachInterrupt_Receive( isrRx_t fn )
293+
{
294+
uint8_t oldSREG = SREG;
295+
cli();
296+
_isrRx = fn;
297+
SREG = oldSREG;
298+
}
299+
300+
void HardwareSerial::attachInterrupt_Send( isrTx_t fn )
301+
{
302+
uint8_t oldSREG = SREG;
303+
cli();
304+
_isrTx = fn;
305+
sbi(*_ucsrb, TXCIE0);
306+
SREG = oldSREG;
307+
}
308+
309+
void HardwareSerial::detachInterrupt_Send()
284310
{
285311
uint8_t oldSREG = SREG;
286312
cli();
287-
_isr = fn;
313+
_isrTx = NULL;
314+
cbi(*_ucsrb, TXCIE0);
288315
SREG = oldSREG;
289316
}
290317

Diff for: cores/arduino/HardwareSerial.h

+10-5
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,17 @@ class HardwareSerial : public Stream
139139
// Interrupt handlers - Not intended to be called externally
140140
inline void _rx_complete_irq(void);
141141
void _tx_udr_empty_irq(void);
142-
143-
typedef void (* isr_t)( uint8_t d, uint8_t s );
144-
void attachInterrupt( isr_t fn );
145-
void detachInterrupt() { attachInterrupt( (isr_t) NULL ); };
142+
void _tx_complete_irq(void);
143+
144+
typedef void (* isrRx_t)( uint8_t d, uint8_t s );
145+
void attachInterrupt_Receive( isrRx_t fn );
146+
void detachInterrupt_Receive() { attachInterrupt_Receive( (isrRx_t) NULL ); };
147+
typedef void (* isrTx_t)( void );
148+
void attachInterrupt_Send( isrTx_t fn );
149+
void detachInterrupt_Send( void );
146150
private:
147-
isr_t _isr;
151+
isrRx_t _isrRx;
152+
isrTx_t _isrTx;
148153

149154
HardwareSerial( const HardwareSerial & );
150155
HardwareSerial & operator =( const HardwareSerial &);

Diff for: cores/arduino/HardwareSerial0.cpp

+14-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
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
23+
Modified 17 November 2019 by Georg Icking-Konert
2524
*/
2625

2726
#include "Arduino.h"
@@ -66,6 +65,19 @@ ISR(USART_UDRE_vect)
6665
Serial._tx_udr_empty_irq();
6766
}
6867

68+
#if defined(UART0_TX_vect)
69+
ISR(UART0_TX_vect)
70+
#elif defined(USART0_TX_vect)
71+
ISR(USART0_TX_vect)
72+
#elif defined(USART_TX_vect)
73+
ISR(USART_TX_vect)
74+
#else
75+
#error "Don't know what the Transmission Complete vector is called for Serial"
76+
#endif
77+
{
78+
Serial._tx_complete_irq();
79+
}
80+
6981
#if defined(UBRRH) && defined(UBRRL)
7082
HardwareSerial Serial(&UBRRH, &UBRRL, &UCSRA, &UCSRB, &UCSRC, &UDR);
7183
#else

Diff for: cores/arduino/HardwareSerial1.cpp

+12-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
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
23+
Modified 17 November 2019 by Georg Icking-Konert
2524
*/
2625

2726
#include "Arduino.h"
@@ -60,6 +59,17 @@ ISR(USART1_UDRE_vect)
6059
Serial1._tx_udr_empty_irq();
6160
}
6261

62+
#if defined(UART1_TX_vect)
63+
ISR(UART1_TX_vect)
64+
#elif defined(USART1_TX_vect)
65+
ISR(USART1_TX_vect)
66+
#else
67+
#error "Don't know what the Transmission Complete vector is called for Serial1"
68+
#endif
69+
{
70+
Serial1._tx_complete_irq();
71+
}
72+
6373
HardwareSerial Serial1(&UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UCSR1C, &UDR1);
6474

6575
// Function that can be weakly referenced by serialEventRun to prevent

Diff for: cores/arduino/HardwareSerial2.cpp

+15-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
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
23+
Modified 17 November 2019 by Georg Icking-Konert
2524
*/
2625

2726
#include "Arduino.h"
@@ -38,16 +37,30 @@
3837

3938
#if defined(HAVE_HWSERIAL2)
4039

40+
// xxx
41+
extern int count;
42+
4143
ISR(USART2_RX_vect)
4244
{
4345
Serial2._rx_complete_irq();
4446
}
4547

4648
ISR(USART2_UDRE_vect)
4749
{
50+
// xxx
51+
Serial.println(++count);
52+
digitalWrite(LED_BUILTIN, HIGH);
53+
delayMicroseconds(50);
54+
digitalWrite(LED_BUILTIN, LOW);
55+
4856
Serial2._tx_udr_empty_irq();
4957
}
5058

59+
ISR(USART2_TX_vect)
60+
{
61+
Serial2._tx_complete_irq();
62+
}
63+
5164
HardwareSerial Serial2(&UBRR2H, &UBRR2L, &UCSR2A, &UCSR2B, &UCSR2C, &UDR2);
5265

5366
// Function that can be weakly referenced by serialEventRun to prevent

Diff for: cores/arduino/HardwareSerial3.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
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
23+
Modified 17 November 2019 by Georg Icking-Konert
2524
*/
2625

2726
#include "Arduino.h"
@@ -48,6 +47,11 @@ ISR(USART3_UDRE_vect)
4847
Serial3._tx_udr_empty_irq();
4948
}
5049

50+
ISR(USART3_TX_vect)
51+
{
52+
Serial3._tx_complete_irq();
53+
}
54+
5155
HardwareSerial Serial3(&UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UCSR3C, &UDR3);
5256

5357
// Function that can be weakly referenced by serialEventRun to prevent

Diff for: cores/arduino/HardwareSerial_private.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
Modified 28 September 2010 by Mark Sproul
2121
Modified 14 August 2012 by Alarus
2222
Modified 2 November 2015 by SlashDev
23-
Modified 7 November 2019 by Georg Icking-Konert
23+
Modified 17 November 2019 by Georg Icking-Konert
2424
*/
2525

2626
#include "wiring_private.h"
@@ -95,19 +95,19 @@ HardwareSerial::HardwareSerial(
9595
_udr(udr),
9696
_rx_buffer_head(0), _rx_buffer_tail(0),
9797
_tx_buffer_head(0), _tx_buffer_tail(0),
98-
_isr(0)
98+
_isrRx(0), _isrTx(0)
9999
{
100100
}
101101

102102
// Actual interrupt handlers //////////////////////////////////////////////////////////////
103103

104104
void HardwareSerial::_rx_complete_irq(void)
105105
{
106-
// user function was attached -> call it with data and status byte
107-
if (_isr) {
106+
// user receive function was attached -> call it with data and status byte
107+
if (_isrRx) {
108108
unsigned char status = *_ucsra;
109109
unsigned char data = *_udr;
110-
_isr( data, status );
110+
_isrRx( data, status );
111111
}
112112

113113
// default: save data in ring buffer

0 commit comments

Comments
 (0)