forked from espressif/arduino-esp32
Various USB fixes (#5422)
* Fix compile archive arguments for the new toolchain * Add menu to S2 for picking through which port to upload Internal USB CDC requires to reset and wait for the new port (because persistence is not yet stable) * USB CDC should also be started in main * Fix URL and USB version for WebUSB * Update vendor callback API * Update CDC::write to use TX_DONE semaphore * Update USB_Serial example
This commit is contained in:
@ -1,21 +1,28 @@
|
||||
#include "USB.h"
|
||||
|
||||
#if ARDUINO_SERIAL_PORT
|
||||
#define HWSerial Serial0
|
||||
#define USBSerial Serial
|
||||
#else
|
||||
#define HWSerial Serial
|
||||
USBCDC USBSerial;
|
||||
#endif
|
||||
|
||||
static void usbEventCallback(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data){
|
||||
if(event_base == ARDUINO_USB_EVENTS){
|
||||
arduino_usb_event_data_t * data = (arduino_usb_event_data_t*)event_data;
|
||||
switch (event_id){
|
||||
case ARDUINO_USB_STARTED_EVENT:
|
||||
Serial.println("USB PLUGGED");
|
||||
HWSerial.println("USB PLUGGED");
|
||||
break;
|
||||
case ARDUINO_USB_STOPPED_EVENT:
|
||||
Serial.println("USB UNPLUGGED");
|
||||
HWSerial.println("USB UNPLUGGED");
|
||||
break;
|
||||
case ARDUINO_USB_SUSPEND_EVENT:
|
||||
Serial.printf("USB SUSPENDED: remote_wakeup_en: %u\n", data->suspend.remote_wakeup_en);
|
||||
HWSerial.printf("USB SUSPENDED: remote_wakeup_en: %u\n", data->suspend.remote_wakeup_en);
|
||||
break;
|
||||
case ARDUINO_USB_RESUME_EVENT:
|
||||
Serial.println("USB RESUMED");
|
||||
HWSerial.println("USB RESUMED");
|
||||
break;
|
||||
|
||||
default:
|
||||
@ -25,24 +32,25 @@ static void usbEventCallback(void* arg, esp_event_base_t event_base, int32_t eve
|
||||
arduino_usb_cdc_event_data_t * data = (arduino_usb_cdc_event_data_t*)event_data;
|
||||
switch (event_id){
|
||||
case ARDUINO_USB_CDC_CONNECTED_EVENT:
|
||||
Serial.println("CDC CONNECTED");
|
||||
HWSerial.println("CDC CONNECTED");
|
||||
break;
|
||||
case ARDUINO_USB_CDC_DISCONNECTED_EVENT:
|
||||
Serial.println("CDC DISCONNECTED");
|
||||
HWSerial.println("CDC DISCONNECTED");
|
||||
break;
|
||||
case ARDUINO_USB_CDC_LINE_STATE_EVENT:
|
||||
Serial.printf("CDC LINE STATE: dtr: %u, rts: %u\n", data->line_state.dtr, data->line_state.rts);
|
||||
HWSerial.printf("CDC LINE STATE: dtr: %u, rts: %u\n", data->line_state.dtr, data->line_state.rts);
|
||||
break;
|
||||
case ARDUINO_USB_CDC_LINE_CODING_EVENT:
|
||||
Serial.printf("CDC LINE CODING: bit_rate: %u, data_bits: %u, stop_bits: %u, parity: %u\n", data->line_coding.bit_rate, data->line_coding.data_bits, data->line_coding.stop_bits, data->line_coding.parity);
|
||||
HWSerial.printf("CDC LINE CODING: bit_rate: %u, data_bits: %u, stop_bits: %u, parity: %u\n", data->line_coding.bit_rate, data->line_coding.data_bits, data->line_coding.stop_bits, data->line_coding.parity);
|
||||
break;
|
||||
case ARDUINO_USB_CDC_RX_EVENT:
|
||||
Serial.printf("CDC RX: %u\n", data->rx.len);
|
||||
HWSerial.printf("CDC RX [%u]:", data->rx.len);
|
||||
{
|
||||
uint8_t buf[data->rx.len];
|
||||
size_t len = USBSerial.read(buf, data->rx.len);
|
||||
Serial.write(buf, len);
|
||||
HWSerial.write(buf, len);
|
||||
}
|
||||
HWSerial.println();
|
||||
break;
|
||||
|
||||
default:
|
||||
@ -51,24 +59,28 @@ static void usbEventCallback(void* arg, esp_event_base_t event_base, int32_t eve
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
Serial.setDebugOutput(true);
|
||||
HWSerial.begin(115200);
|
||||
HWSerial.setDebugOutput(true);
|
||||
|
||||
USB.onEvent(usbEventCallback);
|
||||
USBSerial.onEvent(usbEventCallback);
|
||||
|
||||
#if !ARDUINO_SERIAL_PORT
|
||||
USB.enableDFU();
|
||||
USB.webUSB(true);
|
||||
USB.webUSBURL("http://localhost/webusb");
|
||||
USB.productName("ESP32S2-USB");
|
||||
USB.begin();
|
||||
|
||||
USBSerial.onEvent(usbEventCallback);
|
||||
USBSerial.begin();
|
||||
#endif
|
||||
}
|
||||
|
||||
void loop() {
|
||||
while(Serial.available()){
|
||||
size_t l = Serial.available();
|
||||
while(HWSerial.available()){
|
||||
size_t l = HWSerial.available();
|
||||
uint8_t b[l];
|
||||
l = Serial.read(b, l);
|
||||
l = HWSerial.read(b, l);
|
||||
USBSerial.write(b, l);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user