HID/examples/Projects/USB-Serial/USB-Serial.ino

62 lines
1.3 KiB
Arduino
Raw Normal View History

2015-01-02 15:23:27 +00:00
/*
Copyright (c) 2014 NicoHood
See the readme for credit to other people.
2015-01-02 15:59:24 +00:00
USB-Serial
2015-01-02 15:23:27 +00:00
Transferes from USB to HW Serial and vice versa.
It also resets the main MCU on a DTR rise.
*/
2015-01-02 15:59:24 +00:00
// 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;
2015-01-02 15:23:27 +00:00
void setup() {
// set main MCU by default active
2015-01-02 15:59:24 +00:00
pinMode(resetPin, OUTPUT);
digitalWrite(resetPin, HIGH);
2015-01-02 15:23:27 +00:00
2015-01-02 16:28:13 +00:00
// Start USB Serial
2015-01-02 15:23:27 +00:00
Serial.begin(115200);
}
void loop() {
// USB -> Serial
uint8_t i;
for (i = 0; i < USB_EP_SIZE; i++) {
2015-01-02 15:23:27 +00:00
// read maximum one EP_SIZE to not block
int b = Serial.read();
if (b < 0)
break;
Serial1.write(b);
2015-01-02 15:23:27 +00:00
}
// Serial -> USB
uint8_t buff[USB_EP_SIZE];
for (i = 0; i < sizeof(buff); i++) {
// read maximum one EP_SIZE to not block
int b = Serial1.read();
if (b < 0)
break;
buff[i] = b;
2015-01-02 15:23:27 +00:00
}
// send maximum one EP_SIZE to give the usb some time to flush the buffer
Serial.write(buff, i);
2015-01-02 15:59:24 +00:00
}
2015-01-02 15:23:27 +00:00
void CDC_LineEncodingEvent(void) {
2015-01-02 15:59:24 +00:00
// start HW Serial with new baud rate
Serial1.end();
Serial1.begin(Serial.baud());
2015-01-02 15:23:27 +00:00
}
2015-01-02 15:59:24 +00:00
void CDC_LineStateEvent(void) {
// reset the main mcu if DTR goes HIGH
if (Serial.dtr())
digitalWrite(resetPin, LOW);
else
digitalWrite(resetPin, HIGH);
}