Skip to content

Commit cf5fe3f

Browse files
authored
Create HWCDC_Events.ino
1 parent fd43355 commit cf5fe3f

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* This Example demonstrates how to receive Hardware Serial Events
3+
* This USB interface is available for the ESP32-S3 and ESP32-C3
4+
*
5+
* It will log all events and USB status (plugged/unplugged) into UART0
6+
* Any data read from UART0 will be sent to the USB CDC
7+
* Any data read from USB CDC will be sent to the UART0
8+
*
9+
* A suggestion is to use Arduino Serial Monitor for the UART0 port
10+
* and some other serial monitor application for the USB CDC port
11+
* in order to see the exchanged data and the Hardware Serial Events
12+
*
13+
*/
14+
15+
#ifndef ARDUINO_USB_MODE
16+
#error This ESP32 SoC has no Native USB interface
17+
#elif ARDUINO_USB_MODE == 0
18+
#warning This sketch should be used when USB is in Hardware CDC and JTAG mode
19+
void setup(){}
20+
void loop(){}
21+
#else
22+
23+
// Makes it work always using Serial0 as UART0 and USBSerial as the HW Serial USB CDC port
24+
#if ARDUINO_USB_CDC_ON_BOOT
25+
// HardwareSerial::Serial0 is declared but HWCDC::USBSerial not
26+
// Serial is the HWCDC USB CDC port
27+
#define USBSerial Serial
28+
#else
29+
// HWCDC::USBSerial is declared but HardwareSerial::Serial0 not
30+
// Serial is HardwareSerial UART0
31+
#define Serial0 Serial // redefine the symbol Serial0 to the default Arduino
32+
#endif
33+
34+
// USB Event Callback Function that will log CDC events into UART0
35+
static void usbEventCallback(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
36+
if (event_base == ARDUINO_HW_CDC_EVENTS) {
37+
switch (event_id) {
38+
case ARDUINO_HW_CDC_CONNECTED_EVENT:
39+
Serial0.println("CDC EVENT:: ARDUINO_HW_CDC_CONNECTED_EVENT");
40+
break;
41+
case ARDUINO_HW_CDC_BUS_RESET_EVENT:
42+
Serial0.println("CDC EVENT:: ARDUINO_HW_CDC_BUS_RESET_EVENT");
43+
break;
44+
case ARDUINO_HW_CDC_RX_EVENT:
45+
Serial0.println("\nCDC EVENT:: ARDUINO_HW_CDC_RX_EVENT");
46+
// sends all bytes read from USB Hardware Serial to UART0
47+
while (USBSerial.available()) Serial0.write(USBSerial.read());
48+
break;
49+
case ARDUINO_HW_CDC_TX_EVENT:
50+
Serial0.println("CDC EVENT:: ARDUINO_HW_CDC_TX_EVENT");
51+
break;
52+
53+
default:
54+
break;
55+
}
56+
}
57+
}
58+
59+
const char* _hwcdc_status[] = {
60+
" USB Plugged but CDC is NOT connected\r\n",
61+
" USB Plugged and CDC is connected\r\n",
62+
" USB Unplugged and CDC is NOT connected\r\n",
63+
" USB Unplugged BUT CDC is connected :: PROBLEM\r\n",
64+
};
65+
66+
const char* HWCDC_Status() {
67+
int i = USBSerial.isPlugged() ? 0 : 2;
68+
if(USBSerial.isConnected()) i += 1;
69+
return _hwcdc_status[i];
70+
}
71+
72+
void setup() {
73+
USBSerial.onEvent(usbEventCallback);
74+
USBSerial.begin();
75+
76+
Serial0.begin(115200);
77+
Serial0.setDebugOutput(true);
78+
Serial0.println("Starting...");
79+
}
80+
81+
void loop() {
82+
static uint32_t counter = 0;
83+
84+
Serial0.print(counter);
85+
Serial0.print(HWCDC_Status());
86+
87+
if (USBSerial) {
88+
USBSerial.printf(" [%ld] connected\n\r", counter);
89+
}
90+
// sends all bytes read from UART0 to USB Hardware Serial
91+
while (Serial0.available()) USBSerial.write(Serial0.read());
92+
delay(1000);
93+
counter++;
94+
}
95+
#endif

0 commit comments

Comments
 (0)