Serial as a #define (#8798)
* Serial as a #define * Typo * fixes USBCDC declaration * Fixes Examples * simplifies examples * Adds USB Serial Events * adds error msg when ESP32 is used with USB
This commit is contained in:
parent
e8f82286b5
commit
9dd9bde682
23 changed files with 194 additions and 189 deletions
|
|
@ -436,12 +436,9 @@ void HWCDC::setDebugOutput(bool en)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#if ARDUINO_USB_MODE
|
#if ARDUINO_USB_MODE // Hardware JTAG CDC selected
|
||||||
#if ARDUINO_USB_CDC_ON_BOOT//Serial used for USB CDC
|
// USBSerial is always available to be used
|
||||||
HWCDC Serial;
|
HWCDC HWCDCSerial;
|
||||||
#else
|
|
||||||
HWCDC USBSerial;
|
|
||||||
#endif
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif /* SOC_USB_SERIAL_JTAG_SUPPORTED */
|
#endif /* SOC_USB_SERIAL_JTAG_SUPPORTED */
|
||||||
|
|
|
||||||
|
|
@ -102,13 +102,12 @@ public:
|
||||||
uint32_t baudRate(){return 115200;}
|
uint32_t baudRate(){return 115200;}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
#if ARDUINO_USB_MODE // Hardware JTAG CDC selected
|
||||||
#if ARDUINO_USB_MODE
|
#ifndef HWCDC_SERIAL_IS_DEFINED
|
||||||
#if ARDUINO_USB_CDC_ON_BOOT//Serial used for USB CDC
|
#define HWCDC_SERIAL_IS_DEFINED 1
|
||||||
extern HWCDC Serial;
|
|
||||||
#else
|
|
||||||
extern HWCDC USBSerial;
|
|
||||||
#endif
|
#endif
|
||||||
|
// HWCDCSerial is always available to be used
|
||||||
|
extern HWCDC HWCDCSerial;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif /* CONFIG_IDF_TARGET_ESP32C3 */
|
#endif /* SOC_USB_SERIAL_JTAG_SUPPORTED */
|
||||||
|
|
|
||||||
|
|
@ -113,11 +113,8 @@ void serialEvent2(void) {}
|
||||||
#endif /* SOC_UART_NUM > 2 */
|
#endif /* SOC_UART_NUM > 2 */
|
||||||
|
|
||||||
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL)
|
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL)
|
||||||
#if ARDUINO_USB_CDC_ON_BOOT //Serial used for USB CDC
|
// There is always Seria0 for UART0
|
||||||
HardwareSerial Serial0(0);
|
HardwareSerial Serial0(0);
|
||||||
#else
|
|
||||||
HardwareSerial Serial(0);
|
|
||||||
#endif
|
|
||||||
#if SOC_UART_NUM > 1
|
#if SOC_UART_NUM > 1
|
||||||
HardwareSerial Serial1(1);
|
HardwareSerial Serial1(1);
|
||||||
#endif
|
#endif
|
||||||
|
|
@ -125,13 +122,27 @@ HardwareSerial Serial1(1);
|
||||||
HardwareSerial Serial2(2);
|
HardwareSerial Serial2(2);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if HWCDC_SERIAL_IS_DEFINED == 1 // Hardware JTAG CDC Event
|
||||||
|
extern void HWCDCSerialEvent (void)__attribute__((weak));
|
||||||
|
void HWCDCSerialEvent(void) {}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if USB_SERIAL_IS_DEFINED == 1 // Native USB CDC Event
|
||||||
|
// Used by Hardware Serial for USB CDC events
|
||||||
|
extern void USBSerialEvent (void)__attribute__((weak));
|
||||||
|
void USBSerialEvent(void) {}
|
||||||
|
#endif
|
||||||
|
|
||||||
void serialEventRun(void)
|
void serialEventRun(void)
|
||||||
{
|
{
|
||||||
#if ARDUINO_USB_CDC_ON_BOOT //Serial used for USB CDC
|
#if HWCDC_SERIAL_IS_DEFINED == 1 // Hardware JTAG CDC Event
|
||||||
|
if(HWCDCSerial.available()) HWCDCSerialEvent();
|
||||||
|
#endif
|
||||||
|
#if USB_SERIAL_IS_DEFINED == 1 // Native USB CDC Event
|
||||||
|
if(USBSerial.available()) USBSerialEvent();
|
||||||
|
#endif
|
||||||
|
// UART0 is default serialEvent()
|
||||||
if(Serial0.available()) serialEvent();
|
if(Serial0.available()) serialEvent();
|
||||||
#else
|
|
||||||
if(Serial.available()) serialEvent();
|
|
||||||
#endif
|
|
||||||
#if SOC_UART_NUM > 1
|
#if SOC_UART_NUM > 1
|
||||||
if(Serial1.available()) serialEvent1();
|
if(Serial1.available()) serialEvent1();
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -51,6 +51,7 @@
|
||||||
#include "esp32-hal.h"
|
#include "esp32-hal.h"
|
||||||
#include "soc/soc_caps.h"
|
#include "soc/soc_caps.h"
|
||||||
#include "HWCDC.h"
|
#include "HWCDC.h"
|
||||||
|
#include "USBCDC.h"
|
||||||
|
|
||||||
#include "freertos/FreeRTOS.h"
|
#include "freertos/FreeRTOS.h"
|
||||||
#include "freertos/task.h"
|
#include "freertos/task.h"
|
||||||
|
|
@ -240,21 +241,26 @@ extern void serialEventRun(void) __attribute__((weak));
|
||||||
#ifndef ARDUINO_USB_CDC_ON_BOOT
|
#ifndef ARDUINO_USB_CDC_ON_BOOT
|
||||||
#define ARDUINO_USB_CDC_ON_BOOT 0
|
#define ARDUINO_USB_CDC_ON_BOOT 0
|
||||||
#endif
|
#endif
|
||||||
#if ARDUINO_USB_CDC_ON_BOOT //Serial used for USB CDC
|
#if ARDUINO_USB_CDC_ON_BOOT //Serial used from Native_USB_CDC | HW_CDC_JTAG
|
||||||
#if !ARDUINO_USB_MODE
|
#if ARDUINO_USB_MODE // Hardware CDC mode
|
||||||
#include "USB.h"
|
// Arduino Serial is the HW JTAG CDC device
|
||||||
#include "USBCDC.h"
|
#define Serial HWCDCSerial
|
||||||
#endif
|
#else // !ARDUINO_USB_MODE -- Native USB Mode
|
||||||
|
// Arduino Serial is the Native USB CDC device
|
||||||
|
#define Serial USBSerial
|
||||||
|
#endif // ARDUINO_USB_MODE
|
||||||
|
#else // !ARDUINO_USB_CDC_ON_BOOT -- Serial is used from UART0
|
||||||
|
// if not using CDC on Boot, Arduino Serial is the UART0 device
|
||||||
|
#define Serial Serial0
|
||||||
|
#endif // ARDUINO_USB_CDC_ON_BOOT
|
||||||
|
// There is always Seria0 for UART0
|
||||||
extern HardwareSerial Serial0;
|
extern HardwareSerial Serial0;
|
||||||
#else
|
|
||||||
extern HardwareSerial Serial;
|
|
||||||
#endif
|
|
||||||
#if SOC_UART_NUM > 1
|
#if SOC_UART_NUM > 1
|
||||||
extern HardwareSerial Serial1;
|
extern HardwareSerial Serial1;
|
||||||
#endif
|
#endif
|
||||||
#if SOC_UART_NUM > 2
|
#if SOC_UART_NUM > 2
|
||||||
extern HardwareSerial Serial2;
|
extern HardwareSerial Serial2;
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif //!defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL)
|
||||||
|
|
||||||
#endif // HardwareSerial_h
|
#endif // HardwareSerial_h
|
||||||
|
|
|
||||||
|
|
@ -455,8 +455,9 @@ USBCDC::operator bool() const
|
||||||
return connected;
|
return connected;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if ARDUINO_USB_CDC_ON_BOOT && !ARDUINO_USB_MODE //Serial used for USB CDC
|
#if !ARDUINO_USB_MODE // Native USB CDC selected
|
||||||
USBCDC Serial(0);
|
// USBSerial is always available to be used
|
||||||
|
USBCDC USBSerial(0);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif /* CONFIG_TINYUSB_CDC_ENABLED */
|
#endif /* CONFIG_TINYUSB_CDC_ENABLED */
|
||||||
|
|
|
||||||
|
|
@ -141,9 +141,14 @@ protected:
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#if ARDUINO_USB_CDC_ON_BOOT && !ARDUINO_USB_MODE //Serial used for USB CDC
|
#if !ARDUINO_USB_MODE // Native USB CDC selected
|
||||||
extern USBCDC Serial;
|
#ifndef USB_SERIAL_IS_DEFINED
|
||||||
|
#define USB_SERIAL_IS_DEFINED 1
|
||||||
|
#endif
|
||||||
|
// USBSerial is always available to be used
|
||||||
|
extern USBCDC USBSerial;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#endif /* CONFIG_TINYUSB_CDC_ENABLED */
|
#endif /* CONFIG_TINYUSB_CDC_ENABLED */
|
||||||
#endif /* SOC_USB_OTG_SUPPORTED */
|
#endif /* SOC_USB_OTG_SUPPORTED */
|
||||||
|
|
|
||||||
|
|
@ -16,16 +16,6 @@
|
||||||
// This makes the code transparent to what SoC is used.
|
// This makes the code transparent to what SoC is used.
|
||||||
#include "soc/soc_caps.h"
|
#include "soc/soc_caps.h"
|
||||||
|
|
||||||
// In case that the target has USB CDC and it has being selected to be enable on boot,
|
|
||||||
// the console output will into USB (Serial).
|
|
||||||
// Otherwise the output will be sent to UART0 (Serial) and we have to redefine Serial0
|
|
||||||
#ifndef ARDUINO_USB_CDC_ON_BOOT
|
|
||||||
#define ARDUINO_USB_CDC_ON_BOOT 0
|
|
||||||
#endif
|
|
||||||
#if ARDUINO_USB_CDC_ON_BOOT == 0 // No USB CDC
|
|
||||||
#define Serial0 Serial // redefine the symbol Serial0 to the default Arduino
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// This example shall use UART1 or UART2 for testing and UART0 for console messages
|
// This example shall use UART1 or UART2 for testing and UART0 for console messages
|
||||||
// If UART0 is used for testing, it is necessary to manually send data to it, using the Serial Monitor/Terminal
|
// If UART0 is used for testing, it is necessary to manually send data to it, using the Serial Monitor/Terminal
|
||||||
// In case that USB CDC is available, it may be used as console for messages.
|
// In case that USB CDC is available, it may be used as console for messages.
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
#if ARDUINO_USB_MODE
|
#ifndef ARDUINO_USB_MODE
|
||||||
|
#error This ESP32 SoC has no Native USB interface
|
||||||
|
#elif ARDUINO_USB_MODE == 1
|
||||||
#warning This sketch should be used when USB is in OTG mode
|
#warning This sketch should be used when USB is in OTG mode
|
||||||
void setup(){}
|
void setup(){}
|
||||||
void loop(){}
|
void loop(){}
|
||||||
|
|
@ -15,13 +17,6 @@ void loop(){}
|
||||||
#if !ARDUINO_USB_MSC_ON_BOOT
|
#if !ARDUINO_USB_MSC_ON_BOOT
|
||||||
FirmwareMSC MSC_Update;
|
FirmwareMSC MSC_Update;
|
||||||
#endif
|
#endif
|
||||||
#if ARDUINO_USB_CDC_ON_BOOT
|
|
||||||
#define HWSerial Serial0
|
|
||||||
#define USBSerial Serial
|
|
||||||
#else
|
|
||||||
#define HWSerial Serial
|
|
||||||
USBCDC USBSerial;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
USBHID HID;
|
USBHID HID;
|
||||||
USBHIDKeyboard Keyboard;
|
USBHIDKeyboard Keyboard;
|
||||||
|
|
@ -39,16 +34,16 @@ static void usbEventCallback(void* arg, esp_event_base_t event_base, int32_t eve
|
||||||
arduino_usb_event_data_t * data = (arduino_usb_event_data_t*)event_data;
|
arduino_usb_event_data_t * data = (arduino_usb_event_data_t*)event_data;
|
||||||
switch (event_id){
|
switch (event_id){
|
||||||
case ARDUINO_USB_STARTED_EVENT:
|
case ARDUINO_USB_STARTED_EVENT:
|
||||||
HWSerial.println("USB PLUGGED");
|
Serial.println("USB PLUGGED");
|
||||||
break;
|
break;
|
||||||
case ARDUINO_USB_STOPPED_EVENT:
|
case ARDUINO_USB_STOPPED_EVENT:
|
||||||
HWSerial.println("USB UNPLUGGED");
|
Serial.println("USB UNPLUGGED");
|
||||||
break;
|
break;
|
||||||
case ARDUINO_USB_SUSPEND_EVENT:
|
case ARDUINO_USB_SUSPEND_EVENT:
|
||||||
HWSerial.printf("USB SUSPENDED: remote_wakeup_en: %u\n", data->suspend.remote_wakeup_en);
|
Serial.printf("USB SUSPENDED: remote_wakeup_en: %u\n", data->suspend.remote_wakeup_en);
|
||||||
break;
|
break;
|
||||||
case ARDUINO_USB_RESUME_EVENT:
|
case ARDUINO_USB_RESUME_EVENT:
|
||||||
HWSerial.println("USB RESUMED");
|
Serial.println("USB RESUMED");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
@ -58,28 +53,28 @@ 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;
|
arduino_usb_cdc_event_data_t * data = (arduino_usb_cdc_event_data_t*)event_data;
|
||||||
switch (event_id){
|
switch (event_id){
|
||||||
case ARDUINO_USB_CDC_CONNECTED_EVENT:
|
case ARDUINO_USB_CDC_CONNECTED_EVENT:
|
||||||
HWSerial.println("CDC CONNECTED");
|
Serial.println("CDC CONNECTED");
|
||||||
break;
|
break;
|
||||||
case ARDUINO_USB_CDC_DISCONNECTED_EVENT:
|
case ARDUINO_USB_CDC_DISCONNECTED_EVENT:
|
||||||
HWSerial.println("CDC DISCONNECTED");
|
Serial.println("CDC DISCONNECTED");
|
||||||
break;
|
break;
|
||||||
case ARDUINO_USB_CDC_LINE_STATE_EVENT:
|
case ARDUINO_USB_CDC_LINE_STATE_EVENT:
|
||||||
HWSerial.printf("CDC LINE STATE: dtr: %u, rts: %u\n", data->line_state.dtr, data->line_state.rts);
|
Serial.printf("CDC LINE STATE: dtr: %u, rts: %u\n", data->line_state.dtr, data->line_state.rts);
|
||||||
break;
|
break;
|
||||||
case ARDUINO_USB_CDC_LINE_CODING_EVENT:
|
case ARDUINO_USB_CDC_LINE_CODING_EVENT:
|
||||||
HWSerial.printf("CDC LINE CODING: bit_rate: %lu, 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);
|
Serial.printf("CDC LINE CODING: bit_rate: %lu, 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;
|
break;
|
||||||
case ARDUINO_USB_CDC_RX_EVENT:
|
case ARDUINO_USB_CDC_RX_EVENT:
|
||||||
HWSerial.printf("CDC RX [%u]:", data->rx.len);
|
Serial.printf("CDC RX [%u]:", data->rx.len);
|
||||||
{
|
{
|
||||||
uint8_t buf[data->rx.len];
|
uint8_t buf[data->rx.len];
|
||||||
size_t len = USBSerial.read(buf, data->rx.len);
|
size_t len = USBSerial.read(buf, data->rx.len);
|
||||||
HWSerial.write(buf, len);
|
Serial.write(buf, len);
|
||||||
}
|
}
|
||||||
HWSerial.println();
|
Serial.println();
|
||||||
break;
|
break;
|
||||||
case ARDUINO_USB_CDC_RX_OVERFLOW_EVENT:
|
case ARDUINO_USB_CDC_RX_OVERFLOW_EVENT:
|
||||||
HWSerial.printf("CDC RX Overflow of %d bytes", data->rx_overflow.dropped_bytes);
|
Serial.printf("CDC RX Overflow of %d bytes", data->rx_overflow.dropped_bytes);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
@ -89,20 +84,20 @@ static void usbEventCallback(void* arg, esp_event_base_t event_base, int32_t eve
|
||||||
arduino_firmware_msc_event_data_t * data = (arduino_firmware_msc_event_data_t*)event_data;
|
arduino_firmware_msc_event_data_t * data = (arduino_firmware_msc_event_data_t*)event_data;
|
||||||
switch (event_id){
|
switch (event_id){
|
||||||
case ARDUINO_FIRMWARE_MSC_START_EVENT:
|
case ARDUINO_FIRMWARE_MSC_START_EVENT:
|
||||||
HWSerial.println("MSC Update Start");
|
Serial.println("MSC Update Start");
|
||||||
break;
|
break;
|
||||||
case ARDUINO_FIRMWARE_MSC_WRITE_EVENT:
|
case ARDUINO_FIRMWARE_MSC_WRITE_EVENT:
|
||||||
//HWSerial.printf("MSC Update Write %u bytes at offset %u\n", data->write.size, data->write.offset);
|
//Serial.printf("MSC Update Write %u bytes at offset %u\n", data->write.size, data->write.offset);
|
||||||
HWSerial.print(".");
|
Serial.print(".");
|
||||||
break;
|
break;
|
||||||
case ARDUINO_FIRMWARE_MSC_END_EVENT:
|
case ARDUINO_FIRMWARE_MSC_END_EVENT:
|
||||||
HWSerial.printf("\nMSC Update End: %u bytes\n", data->end.size);
|
Serial.printf("\nMSC Update End: %u bytes\n", data->end.size);
|
||||||
break;
|
break;
|
||||||
case ARDUINO_FIRMWARE_MSC_ERROR_EVENT:
|
case ARDUINO_FIRMWARE_MSC_ERROR_EVENT:
|
||||||
HWSerial.printf("MSC Update ERROR! Progress: %u bytes\n", data->error.size);
|
Serial.printf("MSC Update ERROR! Progress: %u bytes\n", data->error.size);
|
||||||
break;
|
break;
|
||||||
case ARDUINO_FIRMWARE_MSC_POWER_EVENT:
|
case ARDUINO_FIRMWARE_MSC_POWER_EVENT:
|
||||||
HWSerial.printf("MSC Update Power: power: %u, start: %u, eject: %u\n", data->power.power_condition, data->power.start, data->power.load_eject);
|
Serial.printf("MSC Update Power: power: %u, start: %u, eject: %u\n", data->power.power_condition, data->power.start, data->power.load_eject);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
@ -112,10 +107,10 @@ static void usbEventCallback(void* arg, esp_event_base_t event_base, int32_t eve
|
||||||
arduino_usb_hid_event_data_t * data = (arduino_usb_hid_event_data_t*)event_data;
|
arduino_usb_hid_event_data_t * data = (arduino_usb_hid_event_data_t*)event_data;
|
||||||
switch (event_id){
|
switch (event_id){
|
||||||
case ARDUINO_USB_HID_SET_PROTOCOL_EVENT:
|
case ARDUINO_USB_HID_SET_PROTOCOL_EVENT:
|
||||||
HWSerial.printf("HID SET PROTOCOL: %s\n", data->set_protocol.protocol?"REPORT":"BOOT");
|
Serial.printf("HID SET PROTOCOL: %s\n", data->set_protocol.protocol?"REPORT":"BOOT");
|
||||||
break;
|
break;
|
||||||
case ARDUINO_USB_HID_SET_IDLE_EVENT:
|
case ARDUINO_USB_HID_SET_IDLE_EVENT:
|
||||||
HWSerial.printf("HID SET IDLE: %u\n", data->set_idle.idle_rate);
|
Serial.printf("HID SET IDLE: %u\n", data->set_idle.idle_rate);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
@ -125,7 +120,7 @@ static void usbEventCallback(void* arg, esp_event_base_t event_base, int32_t eve
|
||||||
arduino_usb_hid_keyboard_event_data_t * data = (arduino_usb_hid_keyboard_event_data_t*)event_data;
|
arduino_usb_hid_keyboard_event_data_t * data = (arduino_usb_hid_keyboard_event_data_t*)event_data;
|
||||||
switch (event_id){
|
switch (event_id){
|
||||||
case ARDUINO_USB_HID_KEYBOARD_LED_EVENT:
|
case ARDUINO_USB_HID_KEYBOARD_LED_EVENT:
|
||||||
HWSerial.printf("HID KEYBOARD LED: NumLock:%u, CapsLock:%u, ScrollLock:%u\n", data->numlock, data->capslock, data->scrolllock);
|
Serial.printf("HID KEYBOARD LED: NumLock:%u, CapsLock:%u, ScrollLock:%u\n", data->numlock, data->capslock, data->scrolllock);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
@ -135,25 +130,25 @@ static void usbEventCallback(void* arg, esp_event_base_t event_base, int32_t eve
|
||||||
arduino_usb_hid_vendor_event_data_t * data = (arduino_usb_hid_vendor_event_data_t*)event_data;
|
arduino_usb_hid_vendor_event_data_t * data = (arduino_usb_hid_vendor_event_data_t*)event_data;
|
||||||
switch (event_id){
|
switch (event_id){
|
||||||
case ARDUINO_USB_HID_VENDOR_GET_FEATURE_EVENT:
|
case ARDUINO_USB_HID_VENDOR_GET_FEATURE_EVENT:
|
||||||
HWSerial.printf("HID VENDOR GET FEATURE: len:%u\n", data->len);
|
Serial.printf("HID VENDOR GET FEATURE: len:%u\n", data->len);
|
||||||
for(uint16_t i=0; i<data->len; i++){
|
for(uint16_t i=0; i<data->len; i++){
|
||||||
HWSerial.write(data->buffer[i]?data->buffer[i]:'.');
|
Serial.write(data->buffer[i]?data->buffer[i]:'.');
|
||||||
}
|
}
|
||||||
HWSerial.println();
|
Serial.println();
|
||||||
break;
|
break;
|
||||||
case ARDUINO_USB_HID_VENDOR_SET_FEATURE_EVENT:
|
case ARDUINO_USB_HID_VENDOR_SET_FEATURE_EVENT:
|
||||||
HWSerial.printf("HID VENDOR SET FEATURE: len:%u\n", data->len);
|
Serial.printf("HID VENDOR SET FEATURE: len:%u\n", data->len);
|
||||||
for(uint16_t i=0; i<data->len; i++){
|
for(uint16_t i=0; i<data->len; i++){
|
||||||
HWSerial.write(data->buffer[i]?data->buffer[i]:'.');
|
Serial.write(data->buffer[i]?data->buffer[i]:'.');
|
||||||
}
|
}
|
||||||
HWSerial.println();
|
Serial.println();
|
||||||
break;
|
break;
|
||||||
case ARDUINO_USB_HID_VENDOR_OUTPUT_EVENT:
|
case ARDUINO_USB_HID_VENDOR_OUTPUT_EVENT:
|
||||||
HWSerial.printf("HID VENDOR OUTPUT: len:%u\n", data->len);
|
Serial.printf("HID VENDOR OUTPUT: len:%u\n", data->len);
|
||||||
for(uint16_t i=0; i<data->len; i++){
|
for(uint16_t i=0; i<data->len; i++){
|
||||||
HWSerial.write(Vendor.read());
|
Serial.write(Vendor.read());
|
||||||
}
|
}
|
||||||
HWSerial.println();
|
Serial.println();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
@ -163,8 +158,8 @@ static void usbEventCallback(void* arg, esp_event_base_t event_base, int32_t eve
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
HWSerial.begin(115200);
|
Serial.begin(115200);
|
||||||
HWSerial.setDebugOutput(true);
|
Serial.setDebugOutput(true);
|
||||||
|
|
||||||
pinMode(buttonPin, INPUT_PULLUP);
|
pinMode(buttonPin, INPUT_PULLUP);
|
||||||
|
|
||||||
|
|
@ -191,7 +186,7 @@ void loop() {
|
||||||
if (HID.ready() && buttonState != previousButtonState) {
|
if (HID.ready() && buttonState != previousButtonState) {
|
||||||
previousButtonState = buttonState;
|
previousButtonState = buttonState;
|
||||||
if (buttonState == LOW) {
|
if (buttonState == LOW) {
|
||||||
HWSerial.println("Button Pressed");
|
if (Serial != USBSerial) Serial.println("Button Pressed");
|
||||||
USBSerial.println("Button Pressed");
|
USBSerial.println("Button Pressed");
|
||||||
Vendor.println("Button Pressed");
|
Vendor.println("Button Pressed");
|
||||||
Mouse.move(10,10);
|
Mouse.move(10,10);
|
||||||
|
|
@ -206,15 +201,15 @@ void loop() {
|
||||||
//SystemControl.release();
|
//SystemControl.release();
|
||||||
Vendor.println("Button Released");
|
Vendor.println("Button Released");
|
||||||
USBSerial.println("Button Released");
|
USBSerial.println("Button Released");
|
||||||
HWSerial.println("Button Released");
|
if (Serial != USBSerial) Serial.println("Button Released");
|
||||||
}
|
}
|
||||||
delay(100);
|
delay(100);
|
||||||
}
|
}
|
||||||
|
|
||||||
while(HWSerial.available()){
|
while(Serial.available()){
|
||||||
size_t l = HWSerial.available();
|
size_t l = Serial.available();
|
||||||
uint8_t b[l];
|
uint8_t b[l];
|
||||||
l = HWSerial.read(b, l);
|
l = Serial.read(b, l);
|
||||||
USBSerial.write(b, l);
|
USBSerial.write(b, l);
|
||||||
if(HID.ready()){
|
if(HID.ready()){
|
||||||
Vendor.write(b,l);
|
Vendor.write(b,l);
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
#if ARDUINO_USB_MODE
|
#ifndef ARDUINO_USB_MODE
|
||||||
|
#error This ESP32 SoC has no Native USB interface
|
||||||
|
#elif ARDUINO_USB_MODE == 1
|
||||||
#warning This sketch should be used when USB is in OTG mode
|
#warning This sketch should be used when USB is in OTG mode
|
||||||
void setup(){}
|
void setup(){}
|
||||||
void loop(){}
|
void loop(){}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
#if ARDUINO_USB_MODE
|
#ifndef ARDUINO_USB_MODE
|
||||||
#warning This sketch should be used when USB is in OTG mode
|
#error This ESP32 SoC has no Native USB interface
|
||||||
|
#elif ARDUINO_USB_MODE == 1
|
||||||
|
#warning This sketch should be used when USB is in OTG mode
|
||||||
void setup(){}
|
void setup(){}
|
||||||
void loop(){}
|
void loop(){}
|
||||||
#else
|
#else
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
#if ARDUINO_USB_MODE
|
#ifndef ARDUINO_USB_MODE
|
||||||
|
#error This ESP32 SoC has no Native USB interface
|
||||||
|
#elif ARDUINO_USB_MODE == 1
|
||||||
#warning This sketch should be used when USB is in OTG mode
|
#warning This sketch should be used when USB is in OTG mode
|
||||||
void setup(){}
|
void setup(){}
|
||||||
void loop(){}
|
void loop(){}
|
||||||
|
|
@ -9,29 +11,22 @@ void loop(){}
|
||||||
#if !ARDUINO_USB_MSC_ON_BOOT
|
#if !ARDUINO_USB_MSC_ON_BOOT
|
||||||
FirmwareMSC MSC_Update;
|
FirmwareMSC MSC_Update;
|
||||||
#endif
|
#endif
|
||||||
#if ARDUINO_USB_CDC_ON_BOOT
|
|
||||||
#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){
|
static void usbEventCallback(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data){
|
||||||
if(event_base == ARDUINO_USB_EVENTS){
|
if(event_base == ARDUINO_USB_EVENTS){
|
||||||
arduino_usb_event_data_t * data = (arduino_usb_event_data_t*)event_data;
|
arduino_usb_event_data_t * data = (arduino_usb_event_data_t*)event_data;
|
||||||
switch (event_id){
|
switch (event_id){
|
||||||
case ARDUINO_USB_STARTED_EVENT:
|
case ARDUINO_USB_STARTED_EVENT:
|
||||||
HWSerial.println("USB PLUGGED");
|
Serial.println("USB PLUGGED");
|
||||||
break;
|
break;
|
||||||
case ARDUINO_USB_STOPPED_EVENT:
|
case ARDUINO_USB_STOPPED_EVENT:
|
||||||
HWSerial.println("USB UNPLUGGED");
|
Serial.println("USB UNPLUGGED");
|
||||||
break;
|
break;
|
||||||
case ARDUINO_USB_SUSPEND_EVENT:
|
case ARDUINO_USB_SUSPEND_EVENT:
|
||||||
HWSerial.printf("USB SUSPENDED: remote_wakeup_en: %u\n", data->suspend.remote_wakeup_en);
|
Serial.printf("USB SUSPENDED: remote_wakeup_en: %u\n", data->suspend.remote_wakeup_en);
|
||||||
break;
|
break;
|
||||||
case ARDUINO_USB_RESUME_EVENT:
|
case ARDUINO_USB_RESUME_EVENT:
|
||||||
HWSerial.println("USB RESUMED");
|
Serial.println("USB RESUMED");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
@ -41,20 +36,20 @@ static void usbEventCallback(void* arg, esp_event_base_t event_base, int32_t eve
|
||||||
arduino_firmware_msc_event_data_t * data = (arduino_firmware_msc_event_data_t*)event_data;
|
arduino_firmware_msc_event_data_t * data = (arduino_firmware_msc_event_data_t*)event_data;
|
||||||
switch (event_id){
|
switch (event_id){
|
||||||
case ARDUINO_FIRMWARE_MSC_START_EVENT:
|
case ARDUINO_FIRMWARE_MSC_START_EVENT:
|
||||||
HWSerial.println("MSC Update Start");
|
Serial.println("MSC Update Start");
|
||||||
break;
|
break;
|
||||||
case ARDUINO_FIRMWARE_MSC_WRITE_EVENT:
|
case ARDUINO_FIRMWARE_MSC_WRITE_EVENT:
|
||||||
//HWSerial.printf("MSC Update Write %u bytes at offset %u\n", data->write.size, data->write.offset);
|
//Serial.printf("MSC Update Write %u bytes at offset %u\n", data->write.size, data->write.offset);
|
||||||
HWSerial.print(".");
|
Serial.print(".");
|
||||||
break;
|
break;
|
||||||
case ARDUINO_FIRMWARE_MSC_END_EVENT:
|
case ARDUINO_FIRMWARE_MSC_END_EVENT:
|
||||||
HWSerial.printf("\nMSC Update End: %u bytes\n", data->end.size);
|
Serial.printf("\nMSC Update End: %u bytes\n", data->end.size);
|
||||||
break;
|
break;
|
||||||
case ARDUINO_FIRMWARE_MSC_ERROR_EVENT:
|
case ARDUINO_FIRMWARE_MSC_ERROR_EVENT:
|
||||||
HWSerial.printf("MSC Update ERROR! Progress: %u bytes\n", data->error.size);
|
Serial.printf("MSC Update ERROR! Progress: %u bytes\n", data->error.size);
|
||||||
break;
|
break;
|
||||||
case ARDUINO_FIRMWARE_MSC_POWER_EVENT:
|
case ARDUINO_FIRMWARE_MSC_POWER_EVENT:
|
||||||
HWSerial.printf("MSC Update Power: power: %u, start: %u, eject: %u", data->power.power_condition, data->power.start, data->power.load_eject);
|
Serial.printf("MSC Update Power: power: %u, start: %u, eject: %u", data->power.power_condition, data->power.start, data->power.load_eject);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
@ -64,8 +59,8 @@ static void usbEventCallback(void* arg, esp_event_base_t event_base, int32_t eve
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
HWSerial.begin(115200);
|
Serial.begin(115200);
|
||||||
HWSerial.setDebugOutput(true);
|
Serial.setDebugOutput(true);
|
||||||
|
|
||||||
USB.onEvent(usbEventCallback);
|
USB.onEvent(usbEventCallback);
|
||||||
MSC_Update.onEvent(usbEventCallback);
|
MSC_Update.onEvent(usbEventCallback);
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
#if ARDUINO_USB_MODE
|
#ifndef ARDUINO_USB_MODE
|
||||||
|
#error This ESP32 SoC has no Native USB interface
|
||||||
|
#elif ARDUINO_USB_MODE == 1
|
||||||
#warning This sketch should be used when USB is in OTG mode
|
#warning This sketch should be used when USB is in OTG mode
|
||||||
void setup(){}
|
void setup(){}
|
||||||
void loop(){}
|
void loop(){}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
#if ARDUINO_USB_MODE
|
#ifndef ARDUINO_USB_MODE
|
||||||
|
#error This ESP32 SoC has no Native USB interface
|
||||||
|
#elif ARDUINO_USB_MODE == 1
|
||||||
#warning This sketch should be used when USB is in OTG mode
|
#warning This sketch should be used when USB is in OTG mode
|
||||||
void setup(){}
|
void setup(){}
|
||||||
void loop(){}
|
void loop(){}
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,9 @@
|
||||||
|
|
||||||
http://www.arduino.cc/en/Tutorial/KeyboardLogout
|
http://www.arduino.cc/en/Tutorial/KeyboardLogout
|
||||||
*/
|
*/
|
||||||
#if ARDUINO_USB_MODE
|
#ifndef ARDUINO_USB_MODE
|
||||||
|
#error This ESP32 SoC has no Native USB interface
|
||||||
|
#elif ARDUINO_USB_MODE == 1
|
||||||
#warning This sketch should be used when USB is in OTG mode
|
#warning This sketch should be used when USB is in OTG mode
|
||||||
void setup(){}
|
void setup(){}
|
||||||
void loop(){}
|
void loop(){}
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,9 @@
|
||||||
|
|
||||||
http://www.arduino.cc/en/Tutorial/KeyboardMessage
|
http://www.arduino.cc/en/Tutorial/KeyboardMessage
|
||||||
*/
|
*/
|
||||||
#if ARDUINO_USB_MODE
|
#ifndef ARDUINO_USB_MODE
|
||||||
|
#error This ESP32 SoC has no Native USB interface
|
||||||
|
#elif ARDUINO_USB_MODE == 1
|
||||||
#warning This sketch should be used when USB is in OTG mode
|
#warning This sketch should be used when USB is in OTG mode
|
||||||
void setup(){}
|
void setup(){}
|
||||||
void loop(){}
|
void loop(){}
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,9 @@
|
||||||
|
|
||||||
http://www.arduino.cc/en/Tutorial/KeyboardReprogram
|
http://www.arduino.cc/en/Tutorial/KeyboardReprogram
|
||||||
*/
|
*/
|
||||||
#if ARDUINO_USB_MODE
|
#ifndef ARDUINO_USB_MODE
|
||||||
|
#error This ESP32 SoC has no Native USB interface
|
||||||
|
#elif ARDUINO_USB_MODE == 1
|
||||||
#warning This sketch should be used when USB is in OTG mode
|
#warning This sketch should be used when USB is in OTG mode
|
||||||
void setup(){}
|
void setup(){}
|
||||||
void loop(){}
|
void loop(){}
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,9 @@
|
||||||
|
|
||||||
http://www.arduino.cc/en/Tutorial/KeyboardSerial
|
http://www.arduino.cc/en/Tutorial/KeyboardSerial
|
||||||
*/
|
*/
|
||||||
#if ARDUINO_USB_MODE
|
#ifndef ARDUINO_USB_MODE
|
||||||
|
#error This ESP32 SoC has no Native USB interface
|
||||||
|
#elif ARDUINO_USB_MODE == 1
|
||||||
#warning This sketch should be used when USB is in OTG mode
|
#warning This sketch should be used when USB is in OTG mode
|
||||||
void setup(){}
|
void setup(){}
|
||||||
void loop(){}
|
void loop(){}
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,9 @@
|
||||||
|
|
||||||
http://www.arduino.cc/en/Tutorial/KeyboardAndMouseControl
|
http://www.arduino.cc/en/Tutorial/KeyboardAndMouseControl
|
||||||
*/
|
*/
|
||||||
#if ARDUINO_USB_MODE
|
#ifndef ARDUINO_USB_MODE
|
||||||
|
#error This ESP32 SoC has no Native USB interface
|
||||||
|
#elif ARDUINO_USB_MODE == 1
|
||||||
#warning This sketch should be used when USB is in OTG mode
|
#warning This sketch should be used when USB is in OTG mode
|
||||||
void setup(){}
|
void setup(){}
|
||||||
void loop(){}
|
void loop(){}
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,9 @@
|
||||||
|
|
||||||
http://www.arduino.cc/en/Tutorial/ButtonMouseControl
|
http://www.arduino.cc/en/Tutorial/ButtonMouseControl
|
||||||
*/
|
*/
|
||||||
#if ARDUINO_USB_MODE
|
#ifndef ARDUINO_USB_MODE
|
||||||
|
#error This ESP32 SoC has no Native USB interface
|
||||||
|
#elif ARDUINO_USB_MODE == 1
|
||||||
#warning This sketch should be used when USB is in OTG mode
|
#warning This sketch should be used when USB is in OTG mode
|
||||||
void setup(){}
|
void setup(){}
|
||||||
void loop(){}
|
void loop(){}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
#if ARDUINO_USB_MODE
|
#ifndef ARDUINO_USB_MODE
|
||||||
|
#error This ESP32 SoC has no Native USB interface
|
||||||
|
#elif ARDUINO_USB_MODE == 1
|
||||||
#warning This sketch should be used when USB is in OTG mode
|
#warning This sketch should be used when USB is in OTG mode
|
||||||
void setup(){}
|
void setup(){}
|
||||||
void loop(){}
|
void loop(){}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
#if ARDUINO_USB_MODE
|
#ifndef ARDUINO_USB_MODE
|
||||||
|
#error This ESP32 SoC has no Native USB interface
|
||||||
|
#elif ARDUINO_USB_MODE == 1
|
||||||
#warning This sketch should be used when USB is in OTG mode
|
#warning This sketch should be used when USB is in OTG mode
|
||||||
void setup(){}
|
void setup(){}
|
||||||
void loop(){}
|
void loop(){}
|
||||||
|
|
@ -6,14 +8,6 @@ void loop(){}
|
||||||
#include "USB.h"
|
#include "USB.h"
|
||||||
#include "USBMSC.h"
|
#include "USBMSC.h"
|
||||||
|
|
||||||
#if ARDUINO_USB_CDC_ON_BOOT
|
|
||||||
#define HWSerial Serial0
|
|
||||||
#define USBSerial Serial
|
|
||||||
#else
|
|
||||||
#define HWSerial Serial
|
|
||||||
USBCDC USBSerial;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
USBMSC MSC;
|
USBMSC MSC;
|
||||||
|
|
||||||
#define FAT_U8(v) ((v) & 0xFF)
|
#define FAT_U8(v) ((v) & 0xFF)
|
||||||
|
|
@ -136,19 +130,19 @@ static uint8_t msc_disk[DISK_SECTOR_COUNT][DISK_SECTOR_SIZE] =
|
||||||
};
|
};
|
||||||
|
|
||||||
static int32_t onWrite(uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsize){
|
static int32_t onWrite(uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsize){
|
||||||
HWSerial.printf("MSC WRITE: lba: %lu, offset: %lu, bufsize: %lu\n", lba, offset, bufsize);
|
Serial.printf("MSC WRITE: lba: %lu, offset: %lu, bufsize: %lu\n", lba, offset, bufsize);
|
||||||
memcpy(msc_disk[lba] + offset, buffer, bufsize);
|
memcpy(msc_disk[lba] + offset, buffer, bufsize);
|
||||||
return bufsize;
|
return bufsize;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int32_t onRead(uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize){
|
static int32_t onRead(uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize){
|
||||||
HWSerial.printf("MSC READ: lba: %lu, offset: %lu, bufsize: %lu\n", lba, offset, bufsize);
|
Serial.printf("MSC READ: lba: %lu, offset: %lu, bufsize: %lu\n", lba, offset, bufsize);
|
||||||
memcpy(buffer, msc_disk[lba] + offset, bufsize);
|
memcpy(buffer, msc_disk[lba] + offset, bufsize);
|
||||||
return bufsize;
|
return bufsize;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool onStartStop(uint8_t power_condition, bool start, bool load_eject){
|
static bool onStartStop(uint8_t power_condition, bool start, bool load_eject){
|
||||||
HWSerial.printf("MSC START/STOP: power: %u, start: %u, eject: %u\n", power_condition, start, load_eject);
|
Serial.printf("MSC START/STOP: power: %u, start: %u, eject: %u\n", power_condition, start, load_eject);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -157,16 +151,16 @@ static void usbEventCallback(void* arg, esp_event_base_t event_base, int32_t eve
|
||||||
arduino_usb_event_data_t * data = (arduino_usb_event_data_t*)event_data;
|
arduino_usb_event_data_t * data = (arduino_usb_event_data_t*)event_data;
|
||||||
switch (event_id){
|
switch (event_id){
|
||||||
case ARDUINO_USB_STARTED_EVENT:
|
case ARDUINO_USB_STARTED_EVENT:
|
||||||
HWSerial.println("USB PLUGGED");
|
Serial.println("USB PLUGGED");
|
||||||
break;
|
break;
|
||||||
case ARDUINO_USB_STOPPED_EVENT:
|
case ARDUINO_USB_STOPPED_EVENT:
|
||||||
HWSerial.println("USB UNPLUGGED");
|
Serial.println("USB UNPLUGGED");
|
||||||
break;
|
break;
|
||||||
case ARDUINO_USB_SUSPEND_EVENT:
|
case ARDUINO_USB_SUSPEND_EVENT:
|
||||||
HWSerial.printf("USB SUSPENDED: remote_wakeup_en: %u\n", data->suspend.remote_wakeup_en);
|
Serial.printf("USB SUSPENDED: remote_wakeup_en: %u\n", data->suspend.remote_wakeup_en);
|
||||||
break;
|
break;
|
||||||
case ARDUINO_USB_RESUME_EVENT:
|
case ARDUINO_USB_RESUME_EVENT:
|
||||||
HWSerial.println("USB RESUMED");
|
Serial.println("USB RESUMED");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
@ -176,8 +170,8 @@ static void usbEventCallback(void* arg, esp_event_base_t event_base, int32_t eve
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
HWSerial.begin(115200);
|
Serial.begin(115200);
|
||||||
HWSerial.setDebugOutput(true);
|
Serial.setDebugOutput(true);
|
||||||
|
|
||||||
USB.onEvent(usbEventCallback);
|
USB.onEvent(usbEventCallback);
|
||||||
MSC.vendorID("ESP32");//max 8 chars
|
MSC.vendorID("ESP32");//max 8 chars
|
||||||
|
|
|
||||||
|
|
@ -1,33 +1,27 @@
|
||||||
#if ARDUINO_USB_MODE
|
#ifndef ARDUINO_USB_MODE
|
||||||
|
#error This ESP32 SoC has no Native USB interface
|
||||||
|
#elif ARDUINO_USB_MODE == 1
|
||||||
#warning This sketch should be used when USB is in OTG mode
|
#warning This sketch should be used when USB is in OTG mode
|
||||||
void setup(){}
|
void setup(){}
|
||||||
void loop(){}
|
void loop(){}
|
||||||
#else
|
#else
|
||||||
#include "USB.h"
|
#include "USB.h"
|
||||||
|
|
||||||
#if ARDUINO_USB_CDC_ON_BOOT
|
|
||||||
#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){
|
static void usbEventCallback(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data){
|
||||||
if(event_base == ARDUINO_USB_EVENTS){
|
if(event_base == ARDUINO_USB_EVENTS){
|
||||||
arduino_usb_event_data_t * data = (arduino_usb_event_data_t*)event_data;
|
arduino_usb_event_data_t * data = (arduino_usb_event_data_t*)event_data;
|
||||||
switch (event_id){
|
switch (event_id){
|
||||||
case ARDUINO_USB_STARTED_EVENT:
|
case ARDUINO_USB_STARTED_EVENT:
|
||||||
HWSerial.println("USB PLUGGED");
|
Serial.println("USB PLUGGED");
|
||||||
break;
|
break;
|
||||||
case ARDUINO_USB_STOPPED_EVENT:
|
case ARDUINO_USB_STOPPED_EVENT:
|
||||||
HWSerial.println("USB UNPLUGGED");
|
Serial.println("USB UNPLUGGED");
|
||||||
break;
|
break;
|
||||||
case ARDUINO_USB_SUSPEND_EVENT:
|
case ARDUINO_USB_SUSPEND_EVENT:
|
||||||
HWSerial.printf("USB SUSPENDED: remote_wakeup_en: %u\n", data->suspend.remote_wakeup_en);
|
Serial.printf("USB SUSPENDED: remote_wakeup_en: %u\n", data->suspend.remote_wakeup_en);
|
||||||
break;
|
break;
|
||||||
case ARDUINO_USB_RESUME_EVENT:
|
case ARDUINO_USB_RESUME_EVENT:
|
||||||
HWSerial.println("USB RESUMED");
|
Serial.println("USB RESUMED");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
@ -37,28 +31,28 @@ 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;
|
arduino_usb_cdc_event_data_t * data = (arduino_usb_cdc_event_data_t*)event_data;
|
||||||
switch (event_id){
|
switch (event_id){
|
||||||
case ARDUINO_USB_CDC_CONNECTED_EVENT:
|
case ARDUINO_USB_CDC_CONNECTED_EVENT:
|
||||||
HWSerial.println("CDC CONNECTED");
|
Serial.println("CDC CONNECTED");
|
||||||
break;
|
break;
|
||||||
case ARDUINO_USB_CDC_DISCONNECTED_EVENT:
|
case ARDUINO_USB_CDC_DISCONNECTED_EVENT:
|
||||||
HWSerial.println("CDC DISCONNECTED");
|
Serial.println("CDC DISCONNECTED");
|
||||||
break;
|
break;
|
||||||
case ARDUINO_USB_CDC_LINE_STATE_EVENT:
|
case ARDUINO_USB_CDC_LINE_STATE_EVENT:
|
||||||
HWSerial.printf("CDC LINE STATE: dtr: %u, rts: %u\n", data->line_state.dtr, data->line_state.rts);
|
Serial.printf("CDC LINE STATE: dtr: %u, rts: %u\n", data->line_state.dtr, data->line_state.rts);
|
||||||
break;
|
break;
|
||||||
case ARDUINO_USB_CDC_LINE_CODING_EVENT:
|
case ARDUINO_USB_CDC_LINE_CODING_EVENT:
|
||||||
HWSerial.printf("CDC LINE CODING: bit_rate: %lu, 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);
|
Serial.printf("CDC LINE CODING: bit_rate: %lu, 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;
|
break;
|
||||||
case ARDUINO_USB_CDC_RX_EVENT:
|
case ARDUINO_USB_CDC_RX_EVENT:
|
||||||
HWSerial.printf("CDC RX [%u]:", data->rx.len);
|
Serial.printf("CDC RX [%u]:", data->rx.len);
|
||||||
{
|
{
|
||||||
uint8_t buf[data->rx.len];
|
uint8_t buf[data->rx.len];
|
||||||
size_t len = USBSerial.read(buf, data->rx.len);
|
size_t len = USBSerial.read(buf, data->rx.len);
|
||||||
HWSerial.write(buf, len);
|
Serial.write(buf, len);
|
||||||
}
|
}
|
||||||
HWSerial.println();
|
Serial.println();
|
||||||
break;
|
break;
|
||||||
case ARDUINO_USB_CDC_RX_OVERFLOW_EVENT:
|
case ARDUINO_USB_CDC_RX_OVERFLOW_EVENT:
|
||||||
HWSerial.printf("CDC RX Overflow of %d bytes", data->rx_overflow.dropped_bytes);
|
Serial.printf("CDC RX Overflow of %d bytes", data->rx_overflow.dropped_bytes);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
@ -68,8 +62,8 @@ static void usbEventCallback(void* arg, esp_event_base_t event_base, int32_t eve
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
HWSerial.begin(115200);
|
Serial.begin(115200);
|
||||||
HWSerial.setDebugOutput(true);
|
Serial.setDebugOutput(true);
|
||||||
|
|
||||||
USB.onEvent(usbEventCallback);
|
USB.onEvent(usbEventCallback);
|
||||||
USBSerial.onEvent(usbEventCallback);
|
USBSerial.onEvent(usbEventCallback);
|
||||||
|
|
@ -79,10 +73,10 @@ void setup() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
while(HWSerial.available()){
|
while(Serial.available()){
|
||||||
size_t l = HWSerial.available();
|
size_t l = Serial.available();
|
||||||
uint8_t b[l];
|
uint8_t b[l];
|
||||||
l = HWSerial.read(b, l);
|
l = Serial.read(b, l);
|
||||||
USBSerial.write(b, l);
|
USBSerial.write(b, l);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
#if ARDUINO_USB_MODE
|
#ifndef ARDUINO_USB_MODE
|
||||||
|
#error This ESP32 SoC has no Native USB interface
|
||||||
|
#elif ARDUINO_USB_MODE == 1
|
||||||
#warning This sketch should be used when USB is in OTG mode
|
#warning This sketch should be used when USB is in OTG mode
|
||||||
void setup(){}
|
void setup(){}
|
||||||
void loop(){}
|
void loop(){}
|
||||||
|
|
@ -6,12 +8,6 @@ void loop(){}
|
||||||
#include "USB.h"
|
#include "USB.h"
|
||||||
#include "USBVendor.h"
|
#include "USBVendor.h"
|
||||||
|
|
||||||
#if ARDUINO_USB_CDC_ON_BOOT
|
|
||||||
#define HWSerial Serial0
|
|
||||||
#else
|
|
||||||
#define HWSerial Serial
|
|
||||||
#endif
|
|
||||||
|
|
||||||
USBVendor Vendor;
|
USBVendor Vendor;
|
||||||
const int buttonPin = 0;
|
const int buttonPin = 0;
|
||||||
|
|
||||||
|
|
@ -39,16 +35,16 @@ static void usbEventCallback(void* arg, esp_event_base_t event_base, int32_t eve
|
||||||
arduino_usb_event_data_t * data = (arduino_usb_event_data_t*)event_data;
|
arduino_usb_event_data_t * data = (arduino_usb_event_data_t*)event_data;
|
||||||
switch (event_id) {
|
switch (event_id) {
|
||||||
case ARDUINO_USB_STARTED_EVENT:
|
case ARDUINO_USB_STARTED_EVENT:
|
||||||
HWSerial.println("USB PLUGGED");
|
Serial.println("USB PLUGGED");
|
||||||
break;
|
break;
|
||||||
case ARDUINO_USB_STOPPED_EVENT:
|
case ARDUINO_USB_STOPPED_EVENT:
|
||||||
HWSerial.println("USB UNPLUGGED");
|
Serial.println("USB UNPLUGGED");
|
||||||
break;
|
break;
|
||||||
case ARDUINO_USB_SUSPEND_EVENT:
|
case ARDUINO_USB_SUSPEND_EVENT:
|
||||||
HWSerial.printf("USB SUSPENDED: remote_wakeup_en: %u\n", data->suspend.remote_wakeup_en);
|
Serial.printf("USB SUSPENDED: remote_wakeup_en: %u\n", data->suspend.remote_wakeup_en);
|
||||||
break;
|
break;
|
||||||
case ARDUINO_USB_RESUME_EVENT:
|
case ARDUINO_USB_RESUME_EVENT:
|
||||||
HWSerial.println("USB RESUMED");
|
Serial.println("USB RESUMED");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
@ -58,11 +54,11 @@ static void usbEventCallback(void* arg, esp_event_base_t event_base, int32_t eve
|
||||||
arduino_usb_vendor_event_data_t * data = (arduino_usb_vendor_event_data_t*)event_data;
|
arduino_usb_vendor_event_data_t * data = (arduino_usb_vendor_event_data_t*)event_data;
|
||||||
switch (event_id) {
|
switch (event_id) {
|
||||||
case ARDUINO_USB_VENDOR_DATA_EVENT:
|
case ARDUINO_USB_VENDOR_DATA_EVENT:
|
||||||
HWSerial.printf("Vendor RX: len:%u\n", data->data.len);
|
Serial.printf("Vendor RX: len:%u\n", data->data.len);
|
||||||
for (uint16_t i = 0; i < data->data.len; i++) {
|
for (uint16_t i = 0; i < data->data.len; i++) {
|
||||||
HWSerial.write(Vendor.read());
|
Serial.write(Vendor.read());
|
||||||
}
|
}
|
||||||
HWSerial.println();
|
Serial.println();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
@ -78,7 +74,7 @@ static const char * strRequestStages[] = {"SETUP", "DATA", "ACK"};
|
||||||
|
|
||||||
//Handle USB requests to the vendor interface
|
//Handle USB requests to the vendor interface
|
||||||
bool vendorRequestCallback(uint8_t rhport, uint8_t requestStage, arduino_usb_control_request_t const * request) {
|
bool vendorRequestCallback(uint8_t rhport, uint8_t requestStage, arduino_usb_control_request_t const * request) {
|
||||||
HWSerial.printf("Vendor Request: Stage: %5s, Direction: %3s, Type: %8s, Recipient: %9s, bRequest: 0x%02x, wValue: 0x%04x, wIndex: %u, wLength: %u\n",
|
Serial.printf("Vendor Request: Stage: %5s, Direction: %3s, Type: %8s, Recipient: %9s, bRequest: 0x%02x, wValue: 0x%04x, wIndex: %u, wLength: %u\n",
|
||||||
strRequestStages[requestStage],
|
strRequestStages[requestStage],
|
||||||
strRequestDirections[request->bmRequestDirection],
|
strRequestDirections[request->bmRequestDirection],
|
||||||
strRequestTypes[request->bmRequestType],
|
strRequestTypes[request->bmRequestType],
|
||||||
|
|
@ -113,7 +109,7 @@ bool vendorRequestCallback(uint8_t rhport, uint8_t requestStage, arduino_usb_con
|
||||||
result = Vendor.sendResponse(rhport, request, (void*) &vendor_line_coding, sizeof(request_line_coding_t));
|
result = Vendor.sendResponse(rhport, request, (void*) &vendor_line_coding, sizeof(request_line_coding_t));
|
||||||
} else if (requestStage == REQUEST_STAGE_ACK) {
|
} else if (requestStage == REQUEST_STAGE_ACK) {
|
||||||
//In the ACK stage the response is complete
|
//In the ACK stage the response is complete
|
||||||
HWSerial.printf("Vendor Line Coding: bit_rate: %lu, data_bits: %u, stop_bits: %u, parity: %u\n", vendor_line_coding.bit_rate, vendor_line_coding.data_bits, vendor_line_coding.stop_bits, vendor_line_coding.parity);
|
Serial.printf("Vendor Line Coding: bit_rate: %lu, data_bits: %u, stop_bits: %u, parity: %u\n", vendor_line_coding.bit_rate, vendor_line_coding.data_bits, vendor_line_coding.stop_bits, vendor_line_coding.parity);
|
||||||
}
|
}
|
||||||
result = true;
|
result = true;
|
||||||
break;
|
break;
|
||||||
|
|
@ -143,7 +139,7 @@ bool vendorRequestCallback(uint8_t rhport, uint8_t requestStage, arduino_usb_con
|
||||||
//In the ACK stage the response is complete
|
//In the ACK stage the response is complete
|
||||||
bool dtr = (vendor_line_state & 1) != 0;
|
bool dtr = (vendor_line_state & 1) != 0;
|
||||||
bool rts = (vendor_line_state & 2) != 0;
|
bool rts = (vendor_line_state & 2) != 0;
|
||||||
HWSerial.printf("Vendor Line State: dtr: %u, rts: %u\n", dtr, rts);
|
Serial.printf("Vendor Line State: dtr: %u, rts: %u\n", dtr, rts);
|
||||||
}
|
}
|
||||||
result = true;
|
result = true;
|
||||||
break;
|
break;
|
||||||
|
|
@ -159,8 +155,8 @@ bool vendorRequestCallback(uint8_t rhport, uint8_t requestStage, arduino_usb_con
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
pinMode(buttonPin, INPUT_PULLUP);
|
pinMode(buttonPin, INPUT_PULLUP);
|
||||||
HWSerial.begin(115200);
|
Serial.begin(115200);
|
||||||
HWSerial.setDebugOutput(true);
|
Serial.setDebugOutput(true);
|
||||||
|
|
||||||
Vendor.onEvent(usbEventCallback);
|
Vendor.onEvent(usbEventCallback);
|
||||||
Vendor.onRequest(vendorRequestCallback);
|
Vendor.onRequest(vendorRequestCallback);
|
||||||
|
|
@ -178,19 +174,19 @@ void loop() {
|
||||||
if (buttonState != previousButtonState) {
|
if (buttonState != previousButtonState) {
|
||||||
previousButtonState = buttonState;
|
previousButtonState = buttonState;
|
||||||
if (buttonState == LOW) {
|
if (buttonState == LOW) {
|
||||||
HWSerial.println("Button Pressed");
|
Serial.println("Button Pressed");
|
||||||
Vendor.println("Button Pressed");
|
Vendor.println("Button Pressed");
|
||||||
} else {
|
} else {
|
||||||
Vendor.println("Button Released");
|
Vendor.println("Button Released");
|
||||||
HWSerial.println("Button Released");
|
Serial.println("Button Released");
|
||||||
}
|
}
|
||||||
delay(100);
|
delay(100);
|
||||||
}
|
}
|
||||||
|
|
||||||
while (HWSerial.available()) {
|
while (Serial.available()) {
|
||||||
size_t l = HWSerial.available();
|
size_t l = Serial.available();
|
||||||
uint8_t b[l];
|
uint8_t b[l];
|
||||||
l = HWSerial.read(b, l);
|
l = Serial.read(b, l);
|
||||||
Vendor.write(b, l);
|
Vendor.write(b, l);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue