From f4bee40834e0858c5c78a5ccf6dc6a4a788a40dd Mon Sep 17 00:00:00 2001 From: Nico Date: Sat, 29 Nov 2014 20:27:32 +0100 Subject: [PATCH] Added USB-Serial function Its now possible to also reprogram the main MCU with the HoodLoader2 in the sketch itself. It might have some bugs/errors since the USB implementation isn't that reliable. --- .../HoodLoader2_USB-Serial.ino | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 examples/HoodLoader2/HoodLoader2_USB-Serial/HoodLoader2_USB-Serial.ino diff --git a/examples/HoodLoader2/HoodLoader2_USB-Serial/HoodLoader2_USB-Serial.ino b/examples/HoodLoader2/HoodLoader2_USB-Serial/HoodLoader2_USB-Serial.ino new file mode 100644 index 0000000..1dce7ff --- /dev/null +++ b/examples/HoodLoader2/HoodLoader2_USB-Serial/HoodLoader2_USB-Serial.ino @@ -0,0 +1,51 @@ +/* + Copyright (c) 2014 NicoHood + See the readme for credit to other people. + + HoodLoader2 USB-Serial + + Transferes from USB to HW Serial and vice versa. + It also resets the main MCU on a DTR rise. + */ + +void setup() { + // set main MCU by default active + pinMode(MAIN_MCU_RESET_PIN, OUTPUT); + digitalWrite(MAIN_MCU_RESET_PIN, HIGH); + + // Start USB and HW Serial + Serial.begin(115200); + Serial1.begin(115200); +} + +void loop() { + // USB -> Serial + for (int i = 0; i < USB_EP_SIZE; i++) { + // read maximum one EP_SIZE to not block + if (Serial.available()) + Serial1.write(Serial.read()); + else break; + } + + // Serial -> USB + 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]; + int i = 0; + 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; +} +