Arduino ESP32 USB CDC功能使用介绍
- 有关USB CDC原文档介绍:
https://espressif-docs.readthedocs-hosted.com/projects/arduino-esp32/en/latest/api/usb_cdc.html
- 有关ESP32-S/C 系列 USB 外设介绍详情页:
https://espressif-docs.readthedocs-hosted.com/projects/espressif-esp-iot-solution/zh-cn/latest/usb/usb_overview.html
- ✨在Arduino IDE开发环境下,ESP32 USB CDC功能是通过Arduino IDE面板上的功能菜单,配置选项完成设置的。例如:
- ✨在VSCode PIO环境下,需要在
platformio.ini
文件中,添加相应的参数.例如:
build_flags =
-DARDUINO_USB_MODE=1
-DARDUINO_USB_CDC_ON_BOOT=1
- USB CDC相关的API函数大部分属于Stream类,和Serial、WiFiClient使用方法类似。
- 如果只是使用USB CDC作为调试串口,那么仅需要使能
USB CDC On Boot
选项即可(该选项配置为Disabled
,则在代码中如果使用Serial.begin()
则选择的是硬件串口0)
USB CDC 和硬件串口0数据相互转发测试例程
- 测试对象:
ESP32s3
- Arduno IDE 选项配置:
USB CDC On Boot
必须使能,和USB Mode设置:USB-OTG
- 在例程中同时使用了硬件串口0和USB CDC串口功能。
- ✨需要注意,本程序中,USB CDC接收数据的处理,是在相应的事件回调中处理,而不是在loop()循环中处理。
#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
void setup() {}
void loop() {}
#else
#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) {
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");
break;
case ARDUINO_USB_STOPPED_EVENT:
Serial.println("USB UNPLUGGED");
break;
case ARDUINO_USB_SUSPEND_EVENT:
Serial.printf("USB SUSPENDED: remote_wakeup_en: %u\n", data->suspend.remote_wakeup_en);
break;
case ARDUINO_USB_RESUME_EVENT:
Serial.println("USB RESUMED");
break;
default:
break;
}
} else if (event_base == ARDUINO_USB_CDC_EVENTS) {
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");
break;
case ARDUINO_USB_CDC_DISCONNECTED_EVENT:
Serial.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);
break;
case ARDUINO_USB_CDC_LINE_CODING_EVENT:
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;
case ARDUINO_USB_CDC_RX_EVENT:
{
String USBData = USBSerial.readString();
Serial0.print("Received USBCDC Data: ");
Serial0.println(USBData);
}
break;
case ARDUINO_USB_CDC_RX_OVERFLOW_EVENT:
Serial.printf("CDC RX Overflow of %d bytes", data->rx_overflow.dropped_bytes);
break;
default:
break;
}
}
}
void setup() {
Serial0.begin(115200);
Serial.setDebugOutput(true);
delay(3000);
USB.onEvent(usbEventCallback);
USBSerial.onEvent(usbEventCallback);
USBSerial.begin();
USB.begin();
}
void loop() {
if (Serial0.available()) {
USBSerial.println("Serial Data Available...");
String SerialData = Serial0.readString();
USBSerial.print("Received Serial Data: ");
USBSerial.println(SerialData);
}
}
#endif
-
通讯效果:
-
USB CDC 仅作为调试输出使用例程
- 测试对象:ESP32S3
- 配置选项:
void setup() {
Serial.begin();
Serial.setDebugOutput(true);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
Serial.println("USB CDC Data");
}
- 串口信息打印: