Skip to content

Commit 1278144

Browse files
committed
Moving serialEvent() calls from RX interrupts to main for() loop (after loop()).
http://code.google.com/p/arduino/issues/detail?id=584
1 parent 61b33f1 commit 1278144

File tree

4 files changed

+69
-17
lines changed

4 files changed

+69
-17
lines changed

build/shared/examples/4.Communication/SerialEvent/SerialEvent.ino

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,23 @@ void loop() {
3838
}
3939

4040
/*
41-
SerialEvent occurs whenever a new byte comes in the
42-
hardware serial RX. Don't do complex things here, as the
43-
processor halts the regular program to run this routine:
41+
SerialEvent occurs whenever a new data comes in the
42+
hardware serial RX. This routine is run between each
43+
time loop() runs, so using delay inside loop can delay
44+
response. Multiple bytes of data may be available.
4445
*/
4546
void serialEvent() {
46-
// get the new byte:
47-
char inChar = (char)Serial.read();
48-
// add it to the inputString:
49-
inputString += inChar;
50-
// if the incoming character is a newline, set a flag
51-
// so the main loop can do something about it:
52-
if (inChar == '\n') {
53-
stringComplete = true;
54-
}
47+
while (Serial.available()) {
48+
// get the new byte:
49+
char inChar = (char)Serial.read();
50+
// add it to the inputString:
51+
inputString += inChar;
52+
// if the incoming character is a newline, set a flag
53+
// so the main loop can do something about it:
54+
if (inChar == '\n') {
55+
stringComplete = true;
56+
}
57+
}
5558
}
5659

5760

hardware/arduino/cores/arduino/HardwareSerial.cpp

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ inline void store_char(unsigned char c, ring_buffer *buffer)
8888
#else
8989
void serialEvent() __attribute__((weak));
9090
void serialEvent() {}
91+
volatile static unsigned char serialEvent_flag = 0;
92+
#define serialEvent_implemented
9193
#if defined(USART_RX_vect)
9294
SIGNAL(USART_RX_vect)
9395
#elif defined(SIG_USART0_RECV)
@@ -108,18 +110,20 @@ inline void store_char(unsigned char c, ring_buffer *buffer)
108110
#error UDR not defined
109111
#endif
110112
store_char(c, &rx_buffer);
111-
serialEvent();
113+
serialEvent_flag = 1;
112114
}
113115
#endif
114116

115117
#if defined(USART1_RX_vect)
116118
void serialEvent1() __attribute__((weak));
117119
void serialEvent1() {}
120+
volatile static unsigned char serialEvent1_flag = 0;
121+
#define serialEvent1_implemented
118122
SIGNAL(USART1_RX_vect)
119123
{
120124
unsigned char c = UDR1;
121125
store_char(c, &rx_buffer1);
122-
serialEvent1();
126+
serialEvent1_flag = 1;
123127
}
124128
#elif defined(SIG_USART1_RECV)
125129
#error SIG_USART1_RECV
@@ -128,11 +132,13 @@ inline void store_char(unsigned char c, ring_buffer *buffer)
128132
#if defined(USART2_RX_vect) && defined(UDR2)
129133
void serialEvent2() __attribute__((weak));
130134
void serialEvent2() {}
135+
volatile static unsigned char serialEvent2_flag = 0;
136+
#define serialEvent2_implemented
131137
SIGNAL(USART2_RX_vect)
132138
{
133139
unsigned char c = UDR2;
134140
store_char(c, &rx_buffer2);
135-
serialEvent2();
141+
serialEvent2_flag = 1;
136142
}
137143
#elif defined(SIG_USART2_RECV)
138144
#error SIG_USART2_RECV
@@ -141,16 +147,55 @@ inline void store_char(unsigned char c, ring_buffer *buffer)
141147
#if defined(USART3_RX_vect) && defined(UDR3)
142148
void serialEvent3() __attribute__((weak));
143149
void serialEvent3() {}
150+
volatile static unsigned char serialEvent3_flag = 0;
151+
#define serialEvent3_implemented
144152
SIGNAL(USART3_RX_vect)
145153
{
146154
unsigned char c = UDR3;
147155
store_char(c, &rx_buffer3);
148-
serialEvent3();
156+
serialEvent3_flag = 1;
149157
}
150158
#elif defined(SIG_USART3_RECV)
151159
#error SIG_USART3_RECV
152160
#endif
153161

162+
void serialEventRun(void)
163+
{
164+
unsigned char flag, oldSREG;
165+
#ifdef serialEvent_implemented
166+
oldSREG = SREG;
167+
noInterrupts();
168+
flag = serialEvent_flag;
169+
serialEvent_flag = 0;
170+
SREG = oldSREG;
171+
if (flag) serialEvent();
172+
#endif
173+
#ifdef serialEvent1_implemented
174+
oldSREG = SREG;
175+
noInterrupts();
176+
flag = serialEvent1_flag;
177+
serialEvent1_flag = 0;
178+
SREG = oldSREG;
179+
if (flag) serialEvent1();
180+
#endif
181+
#ifdef serialEvent2_implemented
182+
oldSREG = SREG;
183+
noInterrupts();
184+
flag = serialEvent2_flag;
185+
serialEvent2_flag = 0;
186+
SREG = oldSREG;
187+
if (flag) serialEvent2();
188+
#endif
189+
#ifdef serialEvent3_implemented
190+
oldSREG = SREG;
191+
noInterrupts();
192+
flag = serialEvent3_flag;
193+
serialEvent3_flag = 0;
194+
SREG = oldSREG;
195+
if (flag) serialEvent3();
196+
#endif
197+
}
198+
154199

155200
#if !defined(UART0_UDRE_vect) && !defined(UART_UDRE_vect) && !defined(USART0_UDRE_vect) && !defined(USART_UDRE_vect)
156201
#error Don't know what the Data Register Empty vector is called for the first UART

hardware/arduino/cores/arduino/HardwareSerial.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,6 @@ class HardwareSerial : public Stream
7474
extern HardwareSerial Serial3;
7575
#endif
7676

77+
extern void serialEventRun(void);
78+
7779
#endif

hardware/arduino/cores/arduino/main.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ int main(void)
77

88
setup();
99

10-
for (;;)
10+
for (;;) {
1111
loop();
12+
serialEventRun();
13+
}
1214

1315
return 0;
1416
}

0 commit comments

Comments
 (0)