Skip to content

Commit 0ef4d87

Browse files
Expose serial settings from CDC virtual serial port
This allows a sketch to find out the settings chosen by the USB host (computer) and act accordingly. Other than reading the DTR flag and checking if the baudrate is 1200, the regular CDC code doesn't actually use any of these settings. By exposing these settings to the sketch, it can for example copy them to the hardware UART, turning the Leonardo into a proper USB-to-serial device. This can be useful to let the computer directly talk to whatever device is connected to the hardware serial port (like an XBee module).
1 parent 9ea3de4 commit 0ef4d87

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

hardware/arduino/avr/cores/arduino/CDC.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,30 @@ Serial_::operator bool() {
205205
return result;
206206
}
207207

208+
unsigned long Serial_::baudrate() {
209+
return _usbLineInfo.dwDTERate;
210+
}
211+
212+
uint8_t Serial_::stopbits() {
213+
return _usbLineInfo.bCharFormat;
214+
}
215+
216+
uint8_t Serial_::parity() {
217+
return _usbLineInfo.bParityType;
218+
}
219+
220+
uint8_t Serial_::databits() {
221+
return _usbLineInfo.bDataBits;
222+
}
223+
224+
bool Serial_::dtr() {
225+
return _usbLineInfo.lineState & 0x1;
226+
}
227+
228+
bool Serial_::rts() {
229+
return _usbLineInfo.lineState & 0x2;
230+
}
231+
208232
Serial_ Serial;
209233

210234
#endif

hardware/arduino/avr/cores/arduino/USBAPI.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,29 @@ class Serial_ : public Stream
9292
volatile uint8_t _rx_buffer_head;
9393
volatile uint8_t _rx_buffer_tail;
9494
unsigned char _rx_buffer[SERIAL_BUFFER_SIZE];
95+
96+
// These return the settings specified by the USB host for the
97+
// serial port. These aren't really used, but are offered here
98+
// in case a sketch wants to act on these settings.
99+
unsigned long baudrate();
100+
uint8_t stopbits();
101+
uint8_t parity();
102+
uint8_t databits();
103+
bool dtr();
104+
bool rts();
105+
enum {
106+
ONE_STOP_BIT = 0,
107+
ONE_AND_HALF_STOP_BIT = 1,
108+
TWO_STOP_BITS = 2,
109+
};
110+
enum {
111+
NO_PARITY = 0,
112+
ODD_PARITY = 1,
113+
EVEN_PARITY = 2,
114+
MARK_PARITY = 3,
115+
SPACE_PARITY = 4,
116+
};
117+
95118
};
96119
extern Serial_ Serial;
97120

0 commit comments

Comments
 (0)