Skip to content

Commit 9759f2c

Browse files
committed
Port AVR Serial_::readBreak() API to SAM core
1 parent 86e34b9 commit 9759f2c

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

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

+25
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ static volatile LineInfo _usbLineInfo = {
5555
0x00 // lineState
5656
};
5757

58+
static volatile int32_t breakValue = -1;
59+
5860
_Pragma("pack(1)")
5961
static const CDCDescriptor _cdcInterface =
6062
{
@@ -144,6 +146,7 @@ bool WEAK CDC_Setup(USBSetup& setup)
144146

145147
if (CDC_SEND_BREAK == r)
146148
{
149+
breakValue = ((uint16_t)setup.wValueH << 8) | setup.wValueL;
147150
return true;
148151
}
149152
}
@@ -305,6 +308,28 @@ Serial_::operator bool()
305308
return result;
306309
}
307310

311+
int32_t Serial_::readBreak() {
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+
}
329+
330+
return ret;
331+
}
332+
308333
unsigned long Serial_::baud() {
309334
return _usbLineInfo.dwDTERate;
310335
}

hardware/arduino/sam/cores/arduino/USB/USBAPI.h

+17
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,23 @@ class Serial_ : public Stream
6363
using Print::write; // pull in write(str) from Print
6464
operator bool();
6565

66+
// This method allows processing "SEND_BREAK" requests sent by
67+
// the USB host. Those requests indicate that the host wants to
68+
// send a BREAK signal and are accompanied by a single uint16_t
69+
// value, specifying the duration of the break. The value 0
70+
// means to end any current break, while the value 0xffff means
71+
// to start an indefinite break.
72+
// readBreak() will return the value of the most recent break
73+
// request, but will return it at most once, returning -1 when
74+
// readBreak() is called again (until another break request is
75+
// received, which is again returned once).
76+
// This also mean that if two break requests are received
77+
// without readBreak() being called in between, the value of the
78+
// first request is lost.
79+
// Note that the value returned is a long, so it can return
80+
// 0-0xffff as well as -1.
81+
int32_t readBreak();
82+
6683
// These return the settings specified by the USB host for the
6784
// serial port. These aren't really used, but are offered here
6885
// in case a sketch wants to act on these settings.

0 commit comments

Comments
 (0)