From a1449d1576ef751683885ea37fbab15fdf95b021 Mon Sep 17 00:00:00 2001 From: Nico Date: Tue, 9 Dec 2014 17:47:25 +0100 Subject: [PATCH] Added Serial line encoding functions + events https://github.com/arduino/Arduino/pull/2469 Weak implementation sounds usefull but takes 6 more bytes of flash/ function (12). Even more, if we would also pass the line states but the user can read this with Serial.dtr() for example. https://github.com/arduino/Arduino/pull/1179 Should we inline the Serial.dtr() functuions etc? But then we need to move the line info to the header as well, also the definition. These optional functions only take flash if they are used. Virtual isnt used, because this would take more flash for no reason. Syntax like this: https://www.pjrc.com/teensy/td_serial.html --- CDC.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ HID_Tests/HID_Tests.ino | 35 +++++++++++++++++++++++++++++++++++ USBAPI.h | 8 ++++++++ USBCore.h | 3 +++ 4 files changed, 87 insertions(+) create mode 100644 HID_Tests/HID_Tests.ino diff --git a/CDC.cpp b/CDC.cpp index 79a790b..4269118 100644 --- a/CDC.cpp +++ b/CDC.cpp @@ -80,11 +80,13 @@ bool WEAK CDC_Setup(Setup& setup) if (CDC_SET_LINE_CODING == r) { USB_RecvControl((void*)&_usbLineInfo, 7); + CDC_LineEncodingEvent(); } if (CDC_SET_CONTROL_LINE_STATE == r) { _usbLineInfo.lineState = setup.wValueL; + CDC_LineStateEvent(); } if (CDC_SET_LINE_CODING == r || CDC_SET_CONTROL_LINE_STATE == r) @@ -146,6 +148,15 @@ bool WEAK CDC_Setup(Setup& setup) return false; } +void WEAK CDC_LineEncodingEvent(void) +{ + // has to be implemented by the user +} + +void WEAK CDC_LineStateEvent(void) +{ + // has to be implemented by the user +} void Serial_::begin(unsigned long /* baud_count */) { @@ -221,6 +232,36 @@ size_t Serial_::write(const uint8_t *buffer, size_t size) return 0; } +uint32_t Serial_::baud(void) +{ + return _usbLineInfo.dwDTERate; +} + +uint8_t Serial_::stopbits(void) +{ + return _usbLineInfo.bCharFormat; +} + +uint8_t Serial_::paritytype(void) +{ + return _usbLineInfo.bParityType; +} + +uint8_t Serial_::numbits(void) +{ + return _usbLineInfo.bDataBits; +} + +bool Serial_::dtr(void) +{ + return (_usbLineInfo.lineState & CDC_CONTROL_LINE_OUT_DTR) ? true : false; +} + +bool Serial_::rts(void) +{ + return (_usbLineInfo.lineState & CDC_CONTROL_LINE_OUT_RTS) ? true : false; +} + // This operator is a convenient way for a sketch to check whether the // port has actually been configured and opened by the host (as opposed // to just being connected to the host). It can be used, for example, in diff --git a/HID_Tests/HID_Tests.ino b/HID_Tests/HID_Tests.ino new file mode 100644 index 0000000..cdba2e4 --- /dev/null +++ b/HID_Tests/HID_Tests.ino @@ -0,0 +1,35 @@ +// simply, ugly code to test the new functions + +void setup() { + Serial.begin(1); +} + +uint32_t eventBaud = 0; + +void loop() { + if (Serial.available()) { + while (Serial.read() != -1); + Serial.println("Serial Port working"); + Keyboard.println("Nico is cool"); + delay(3000); + } + + delay(3000); + Serial.println(Serial.dtr()); + Serial.println(Serial.rts()); + Serial.println(Serial.baud()); + Serial.println(Serial.stopbits()); + Serial.println(Serial.paritytype()); + Serial.println(Serial.numbits()); + + if (eventBaud) { + Serial.println("Event"); + Serial.println(eventBaud); + eventBaud = 0; + } +} + +void CDC_LineEncodingEvent(void) +{ + eventBaud = Serial.baud(); +} diff --git a/USBAPI.h b/USBAPI.h index 325d3ea..049a133 100644 --- a/USBAPI.h +++ b/USBAPI.h @@ -82,6 +82,12 @@ public: virtual size_t write(uint8_t); virtual size_t write(const uint8_t*, size_t); using Print::write; // pull in write(str) and write(buf, size) from Print + uint32_t baud(void); + uint8_t stopbits(void); + uint8_t paritytype(void); + uint8_t numbits(void); + bool dtr(void); + bool rts(void); operator bool(); volatile uint8_t _rx_buffer_head; @@ -222,6 +228,8 @@ bool MSC_Data(uint8_t rx, uint8_t tx); int CDC_GetInterface(uint8_t* interfaceNum); int CDC_GetDescriptor(int i); bool CDC_Setup(Setup& setup); +void CDC_LineEncodingEvent(void); +void CDC_LineStateEvent(void); //================================================================================ //================================================================================ diff --git a/USBCore.h b/USBCore.h index 5df4d5d..4178b75 100644 --- a/USBCore.h +++ b/USBCore.h @@ -56,6 +56,9 @@ #define CDC_GET_LINE_CODING 0x21 #define CDC_SET_CONTROL_LINE_STATE 0x22 +#define CDC_CONTROL_LINE_OUT_DTR (1 << 0) +#define CDC_CONTROL_LINE_OUT_RTS (1 << 1) + #define MSC_RESET 0xFF #define MSC_GET_MAX_LUN 0xFE