From 8997abe61b1ee12e4e820de96e1f8a2823bcea93 Mon Sep 17 00:00:00 2001 From: Nico Date: Fri, 2 Jan 2015 16:59:24 +0100 Subject: [PATCH] USB-Serial Update --- Readme.md | 1 + .../USB-Serial/USB-Serial.ino} | 36 ++++++++++++------- 2 files changed, 25 insertions(+), 12 deletions(-) rename examples/{HoodLoader2/HoodLoader2_USB-Serial/HoodLoader2_USB-Serial.ino => Projects/USB-Serial/USB-Serial.ino} (57%) diff --git a/Readme.md b/Readme.md index 304c28f..8bc3fba 100644 --- a/Readme.md +++ b/Readme.md @@ -69,6 +69,7 @@ Your Arduino IDE will output errors then if you double click the file and try to * Added USB Wakeup support * Separated USB-Core in its own folder * Added HID Tables +* USB-Serial now fully reprogrammable ``` ``` diff --git a/examples/HoodLoader2/HoodLoader2_USB-Serial/HoodLoader2_USB-Serial.ino b/examples/Projects/USB-Serial/USB-Serial.ino similarity index 57% rename from examples/HoodLoader2/HoodLoader2_USB-Serial/HoodLoader2_USB-Serial.ino rename to examples/Projects/USB-Serial/USB-Serial.ino index 1dce7ff..5c8dbac 100644 --- a/examples/HoodLoader2/HoodLoader2_USB-Serial/HoodLoader2_USB-Serial.ino +++ b/examples/Projects/USB-Serial/USB-Serial.ino @@ -2,16 +2,21 @@ Copyright (c) 2014 NicoHood See the readme for credit to other people. - HoodLoader2 USB-Serial + USB-Serial Transferes from USB to HW Serial and vice versa. It also resets the main MCU on a DTR rise. */ +// define the reset pin to reset the destination MCU. +// this definition is made for HoodLoader2 (pin 20) +// but you still can use it with any other USB MCU or pin +const int resetPin = MAIN_MCU_RESET_PIN; + void setup() { // set main MCU by default active - pinMode(MAIN_MCU_RESET_PIN, OUTPUT); - digitalWrite(MAIN_MCU_RESET_PIN, HIGH); + pinMode(resetPin, OUTPUT); + digitalWrite(resetPin, HIGH); // Start USB and HW Serial Serial.begin(115200); @@ -31,21 +36,28 @@ void loop() { if (Serial1.available()) { Serial.flush(); // send maximum one EP_SIZE to give the usb some time to flush the buffer - uint8_t buff[USB_EP_SIZE-1]; + uint8_t buff[USB_EP_SIZE - 1]; int i = 0; - for (i = 0; i < USB_EP_SIZE-1; i++) { + for (i = 0; i < USB_EP_SIZE - 1; i++) { if (Serial1.available()) buff[i] = Serial1.read(); else break; } Serial.write(buff, i); } - - // reset the main mcu if DTR goes HIGH - static bool lastDTR = 0; - bool newDTR = (Serial.lineState()&CDC_CONTROL_LINE_OUT_DTR) ? 1 : 0; - if (lastDTR ^ newDTR) - digitalWrite(MAIN_MCU_RESET_PIN, lastDTR); - lastDTR = newDTR; } +void CDC_LineEncodingEvent(void) +{ + // start HW Serial with new baud rate + Serial1.end(); + Serial1.begin(Serial.baud()); +} + +void CDC_LineStateEvent(void) { + // reset the main mcu if DTR goes HIGH + if (Serial.dtr()) + digitalWrite(resetPin, LOW); + else + digitalWrite(resetPin, HIGH); +}