-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathHWSerial_Events.ino
69 lines (63 loc) · 2.18 KB
/
HWSerial_Events.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
* This Example demonstrates how to receive Hardware Serial Events
* This USB interface is available for the ESP32-S3, ESP32-C3, ESP32-C6 and ESP32-H2
*
* It will log all events and USB status (plugged/unplugged) into UART0
* Any data read from UART0 will be sent to the USB CDC
* Any data read from USB CDC will be sent to the UART0
*
* A suggestion is to use Arduino Serial Monitor for the UART0 port
* and some other serial monitor application for the USB CDC port
* in order to see the exchanged data and the Hardware Serial Events
*
*/
#ifndef ARDUINO_USB_MODE
#error This ESP32 SoC has no Native USB interface
#elif ARDUINO_USB_MODE == 0
#warning This sketch should be used when USB is in Hardware CDC and JTAG mode
void setup(){}
void loop(){}
#else
// USB Event Callback Function that will log CDC events into UART0
static void usbEventCallback(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
if (event_base == ARDUINO_HW_CDC_EVENTS) {
switch (event_id) {
case ARDUINO_HW_CDC_CONNECTED_EVENT:
Serial0.println("CDC EVENT:: ARDUINO_HW_CDC_CONNECTED_EVENT");
break;
case ARDUINO_HW_CDC_BUS_RESET_EVENT:
Serial0.println("CDC EVENT:: ARDUINO_HW_CDC_BUS_RESET_EVENT");
break;
case ARDUINO_HW_CDC_RX_EVENT:
Serial0.println("\nCDC EVENT:: ARDUINO_HW_CDC_RX_EVENT");
// sends all bytes read from USB Hardware Serial to UART0
while (HWCDCSerial.available()) Serial0.write(HWCDCSerial.read());
break;
case ARDUINO_HW_CDC_TX_EVENT:
Serial0.println("CDC EVENT:: ARDUINO_HW_CDC_TX_EVENT");
break;
default:
break;
}
}
}
void setup() {
Serial0.begin(115200);
Serial0.setDebugOutput(true);
HWCDCSerial.begin();
HWCDCSerial.onEvent(usbEventCallback);
Serial0.println("Starting...");
}
void loop() {
static uint32_t counter = 0;
Serial0.print(counter++);
if (HWCDCSerial) {
Serial0.println(" +++ USB CDC JTAG Plugged");
} else {
Serial0.println(" --- USB CDC JTAG Unplugged");
}
// sends all bytes read from UART0 to USB Hardware Serial
while (Serial0.available()) HWCDCSerial.write(Serial0.read());
delay(1000);
}
#endif