Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b4dc14a

Browse files
authoredApr 11, 2024
Create HWCDC_Events.ino
1 parent 93448d7 commit b4dc14a

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* This Example demonstrates how to receive Hardware Serial Events
3+
* This USB interface is available for the ESP32-S3, ESP32-C3, ESP32-C6 and ESP32-H2
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+
#if !ARDUINO_USB_CDC_ON_BOOT
24+
HWCDC HWCDCSerial;
25+
#endif
26+
27+
// USB Event Callback Function that will log CDC events into UART0
28+
static void usbEventCallback(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
29+
if (event_base == ARDUINO_HW_CDC_EVENTS) {
30+
switch (event_id) {
31+
case ARDUINO_HW_CDC_CONNECTED_EVENT:
32+
Serial0.println("CDC EVENT:: ARDUINO_HW_CDC_CONNECTED_EVENT");
33+
break;
34+
case ARDUINO_HW_CDC_BUS_RESET_EVENT:
35+
Serial0.println("CDC EVENT:: ARDUINO_HW_CDC_BUS_RESET_EVENT");
36+
break;
37+
case ARDUINO_HW_CDC_RX_EVENT:
38+
Serial0.println("\nCDC EVENT:: ARDUINO_HW_CDC_RX_EVENT");
39+
// sends all bytes read from USB Hardware Serial to UART0
40+
while (HWCDCSerial.available()) Serial0.write(HWCDCSerial.read());
41+
break;
42+
case ARDUINO_HW_CDC_TX_EVENT:
43+
Serial0.println("CDC EVENT:: ARDUINO_HW_CDC_TX_EVENT");
44+
break;
45+
46+
default:
47+
break;
48+
}
49+
}
50+
}
51+
52+
const char* _hwcdc_status[] = {
53+
" USB Plugged but CDC is NOT connected\r\n",
54+
" USB Plugged and CDC is connected\r\n",
55+
" USB Unplugged and CDC NOT connected\r\n",
56+
" USB Unplugged BUT CDC is connected :: PROBLEM\r\n",
57+
};
58+
59+
const char* HWCDC_Status() {
60+
int i = HWCDCSerial.isPlugged() ? 0 : 2;
61+
if(HWCDCSerial.isConnected()) i += 1;
62+
return _hwcdc_status[i];
63+
}
64+
65+
void setup() {
66+
USBSerial.onEvent(usbEventCallback);
67+
USBSerial.begin();
68+
69+
Serial0.begin(115200);
70+
Serial0.setDebugOutput(true);
71+
Serial0.println("Starting...");
72+
}
73+
74+
void loop() {
75+
static uint32_t counter = 0;
76+
77+
Serial0.print(counter);
78+
Serial0.print(HWCDC_Status());
79+
80+
if (HWCDCSerial) {
81+
HWCDCSerial.printf(" [%ld] connected\n\r", counter);
82+
}
83+
// sends all bytes read from UART0 to USB Hardware Serial
84+
while (Serial0.available()) HWCDCSerial.write(Serial0.read());
85+
delay(1000);
86+
counter++;
87+
}
88+
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.