Skip to content

Commit 3ea315a

Browse files
committed
Port some AVR Serial_ (SerialUSB) API's over
1 parent 2211cf1 commit 3ea315a

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

cores/arduino/USB/CDC.cpp

+52
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ static volatile LineInfo _usbLineInfo = {
5858
0x00 // lineState
5959
};
6060

61+
static volatile int32_t breakValue = -1;
62+
6163
static CDCDescriptor _cdcInterface = {
6264
D_IAD(0, 2, CDC_COMMUNICATION_INTERFACE_CLASS, CDC_ABSTRACT_CONTROL_MODEL, 0),
6365

@@ -134,6 +136,12 @@ bool CDC_Setup(USBSetup& setup)
134136
}
135137
return false;
136138
}
139+
140+
if (CDC_SEND_BREAK == r)
141+
{
142+
breakValue = ((uint16_t)setup.wValueH << 8) | setup.wValueL;
143+
return false;
144+
}
137145
}
138146
return false;
139147
}
@@ -289,6 +297,50 @@ Serial_::operator bool()
289297
return result;
290298
}
291299

300+
int32_t Serial_::readBreak() {
301+
uint8_t enableInterrupts = ((__get_PRIMASK() & 0x1) == 0);
302+
303+
// disable interrupts,
304+
// to avoid clearing a breakValue that might occur
305+
// while processing the current break value
306+
__disable_irq();
307+
308+
int32_t ret = breakValue;
309+
310+
breakValue = -1;
311+
312+
if (enableInterrupts) {
313+
// re-enable the interrupts
314+
__enable_irq();
315+
}
316+
317+
return ret;
318+
}
319+
320+
unsigned long Serial_::baud() {
321+
return _usbLineInfo.dwDTERate;
322+
}
323+
324+
uint8_t Serial_::stopbits() {
325+
return _usbLineInfo.bCharFormat;
326+
}
327+
328+
uint8_t Serial_::paritytype() {
329+
return _usbLineInfo.bParityType;
330+
}
331+
332+
uint8_t Serial_::numbits() {
333+
return _usbLineInfo.bDataBits;
334+
}
335+
336+
bool Serial_::dtr() {
337+
return _usbLineInfo.lineState & 0x1;
338+
}
339+
340+
bool Serial_::rts() {
341+
return _usbLineInfo.lineState & 0x2;
342+
}
343+
292344
Serial_ SerialUSB(USBDevice);
293345

294346
#endif

cores/arduino/USB/USBAPI.h

+40
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,46 @@ class Serial_ : public Stream
126126
virtual size_t write(const uint8_t *buffer, size_t size);
127127
using Print::write; // pull in write(str) from Print
128128
operator bool();
129+
130+
// This method allows processing "SEND_BREAK" requests sent by
131+
// the USB host. Those requests indicate that the host wants to
132+
// send a BREAK signal and are accompanied by a single uint16_t
133+
// value, specifying the duration of the break. The value 0
134+
// means to end any current break, while the value 0xffff means
135+
// to start an indefinite break.
136+
// readBreak() will return the value of the most recent break
137+
// request, but will return it at most once, returning -1 when
138+
// readBreak() is called again (until another break request is
139+
// received, which is again returned once).
140+
// This also mean that if two break requests are received
141+
// without readBreak() being called in between, the value of the
142+
// first request is lost.
143+
// Note that the value returned is a long, so it can return
144+
// 0-0xffff as well as -1.
145+
int32_t readBreak();
146+
147+
// These return the settings specified by the USB host for the
148+
// serial port. These aren't really used, but are offered here
149+
// in case a sketch wants to act on these settings.
150+
uint32_t baud();
151+
uint8_t stopbits();
152+
uint8_t paritytype();
153+
uint8_t numbits();
154+
bool dtr();
155+
bool rts();
156+
enum {
157+
ONE_STOP_BIT = 0,
158+
ONE_AND_HALF_STOP_BIT = 1,
159+
TWO_STOP_BITS = 2,
160+
};
161+
enum {
162+
NO_PARITY = 0,
163+
ODD_PARITY = 1,
164+
EVEN_PARITY = 2,
165+
MARK_PARITY = 3,
166+
SPACE_PARITY = 4,
167+
};
168+
129169
private:
130170
USBDeviceClass &usb;
131171
RingBuffer *_cdc_rx_buffer;

cores/arduino/USB/USBCore.h

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
#define CDC_SET_LINE_CODING 0x20
6969
#define CDC_GET_LINE_CODING 0x21
7070
#define CDC_SET_CONTROL_LINE_STATE 0x22
71+
#define CDC_SEND_BREAK 0x23
7172

7273
#define MSC_RESET 0xFF
7374
#define MSC_GET_MAX_LUN 0xFE

0 commit comments

Comments
 (0)