Skip to content

Commit 6a0b5a4

Browse files
committed
Protect processing break value by disabling and re-enabling interrupts
1 parent 6ffc96b commit 6a0b5a4

File tree

1 file changed

+17
-12
lines changed
  • hardware/arduino/sam/cores/arduino/USB

1 file changed

+17
-12
lines changed

hardware/arduino/sam/cores/arduino/USB/CDC.cpp

+17-12
Original file line numberDiff line numberDiff line change
@@ -309,18 +309,23 @@ Serial_::operator bool()
309309
}
310310

311311
int32_t Serial_::readBreak() {
312-
// read the breakValue, using exclusive read
313-
int32_t ret = __LDREXW((volatile uint32_t*)&breakValue);
314-
315-
if (ret == -1) {
316-
// no new break value, clear exclusive access record
317-
__CLREX();
318-
} else {
319-
// only set it to -1 (no break),
320-
// if a new value has not been written in the ISR,
321-
// using exclusive write
322-
__STREXW(-1, (volatile uint32_t*)&breakValue);
323-
}
312+
uint8_t enableInterrupts = ((__get_PRIMASK() & 0x1) == 0 &&
313+
(__get_FAULTMASK() & 0x1) == 0);
314+
315+
// disable interrupts,
316+
// to avoid clearing a breakValue that might occur
317+
// while processing the current break value
318+
__disable_irq();
319+
320+
int ret = breakValue;
321+
322+
breakValue = -1;
323+
324+
if (enableInterrupts)
325+
{
326+
// re-enable the interrupts
327+
__enable_irq();
328+
}
324329

325330
return ret;
326331
}

0 commit comments

Comments
 (0)