aboutsummaryrefslogtreecommitdiff
path: root/cores
diff options
context:
space:
mode:
authorMatthijs Kooijman <matthijs@stdin.nl>2015-06-15 21:17:35 +0200
committerMatthijs Kooijman <matthijs@stdin.nl>2015-07-31 13:40:43 +0200
commitacb1a47a78ef96a5fcfbae9013dcc7d39e30292e (patch)
treee1181afbde193357d73c1cb97c7aafa32a8ec65a /cores
parent99a27d0bd8555f4b380e07ff2b7334ab6ae32841 (diff)
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). The Teensy core already supported these methods. This code was independently developed, but the method names were chosen to match the Teensy code, for compatibility (except that `dtr()` and `rtr()` return `bool`, while the Teensy version return a `uint8_t`). This change is applied to both the avr and sam cores, which have a very similar CDC implementation.
Diffstat (limited to 'cores')
-rw-r--r--cores/arduino/CDC.cpp27
-rw-r--r--cores/arduino/USBAPI.h23
2 files changed, 50 insertions, 0 deletions
diff --git a/cores/arduino/CDC.cpp b/cores/arduino/CDC.cpp
index d694a2d..90a1698 100644
--- a/cores/arduino/CDC.cpp
+++ b/cores/arduino/CDC.cpp
@@ -18,6 +18,7 @@
#include "USBAPI.h"
#include <avr/wdt.h>
+#include <util/atomic.h>
#if defined(USBCON)
@@ -205,6 +206,32 @@ Serial_::operator bool() {
return result;
}
+unsigned long Serial_::baud() {
+ ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
+ return _usbLineInfo.dwDTERate;
+ }
+}
+
+uint8_t Serial_::stopbits() {
+ return _usbLineInfo.bCharFormat;
+}
+
+uint8_t Serial_::paritytype() {
+ return _usbLineInfo.bParityType;
+}
+
+uint8_t Serial_::numbits() {
+ return _usbLineInfo.bDataBits;
+}
+
+bool Serial_::dtr() {
+ return _usbLineInfo.lineState & 0x1;
+}
+
+bool Serial_::rts() {
+ return _usbLineInfo.lineState & 0x2;
+}
+
Serial_ Serial;
#endif /* if defined(USBCON) */
diff --git a/cores/arduino/USBAPI.h b/cores/arduino/USBAPI.h
index 4abd961..ada73b0 100644
--- a/cores/arduino/USBAPI.h
+++ b/cores/arduino/USBAPI.h
@@ -101,6 +101,29 @@ public:
volatile uint8_t _rx_buffer_head;
volatile uint8_t _rx_buffer_tail;
unsigned char _rx_buffer[SERIAL_BUFFER_SIZE];
+
+ // These return the settings specified by the USB host for the
+ // serial port. These aren't really used, but are offered here
+ // in case a sketch wants to act on these settings.
+ uint32_t baud();
+ uint8_t stopbits();
+ uint8_t paritytype();
+ uint8_t numbits();
+ bool dtr();
+ bool rts();
+ enum {
+ ONE_STOP_BIT = 0,
+ ONE_AND_HALF_STOP_BIT = 1,
+ TWO_STOP_BITS = 2,
+ };
+ enum {
+ NO_PARITY = 0,
+ ODD_PARITY = 1,
+ EVEN_PARITY = 2,
+ MARK_PARITY = 3,
+ SPACE_PARITY = 4,
+ };
+
};
extern Serial_ Serial;