Skip to content

Commit 3a3db88

Browse files
authored
Merge pull request #265 from facchinm/serial_protect_ringbuffer
Serial: properly protect ringbuffer access
2 parents a74d0b8 + 2724061 commit 3a3db88

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

cores/arduino/Serial.cpp

+15-4
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,12 @@ void UART::on_rx() {
132132
return;
133133
}
134134
#endif
135-
while(_serial->obj->readable()) {
135+
while(_serial->obj->readable() && rx_buffer.availableForStore()) {
136136
char c;
137+
core_util_critical_section_enter();
137138
_serial->obj->read(&c, 1);
138139
rx_buffer.store_char(c);
140+
core_util_critical_section_exit();
139141
}
140142
}
141143

@@ -160,7 +162,10 @@ int UART::available() {
160162
return _SerialUSB.available();
161163
}
162164
#endif
163-
return rx_buffer.available();
165+
core_util_critical_section_enter();
166+
int c = rx_buffer.available();
167+
core_util_critical_section_exit();
168+
return c;
164169
}
165170

166171
int UART::peek() {
@@ -169,7 +174,10 @@ int UART::peek() {
169174
return _SerialUSB.peek();
170175
}
171176
#endif
172-
return rx_buffer.peek();
177+
core_util_critical_section_enter();
178+
int c = rx_buffer.peek();
179+
core_util_critical_section_exit();
180+
return c;
173181
}
174182

175183
int UART::read() {
@@ -178,7 +186,10 @@ int UART::read() {
178186
return _SerialUSB.read();
179187
}
180188
#endif
181-
return rx_buffer.read_char();
189+
core_util_critical_section_enter();
190+
int c = rx_buffer.read_char();
191+
core_util_critical_section_exit();
192+
return c;
182193
}
183194

184195
void UART::flush() {

0 commit comments

Comments
 (0)