Arduino ESP32 USB CDC功能使用介绍

发布时间:2024年01月22日

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()循环中处理。
/* ESP32 USB CDC介绍文档:
https://espressif-docs.readthedocs-hosted.com/projects/arduino-esp32/en/latest/api/usb_cdc.html


*/

#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:
        // Serial.printf("CDC RX [%u]:", data->rx.len);
        // {
        //     uint8_t buf[data->rx.len];
        //     size_t len = USBSerial.read(buf, data->rx.len);
        //     Serial.write(buf, len);
        // }
        // Serial.println();
        {
          String USBData = USBSerial.readString();  // 将接收到的信息使用readString()存储于USBData变量
          Serial0.print("Received USBCDC Data: ");  // 然后通过硬件串口0监视器输出USBData变量内容
          Serial0.println(USBData);                 // 以便查看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);
  //Serial.println("USB CDC communication");
  USB.onEvent(usbEventCallback);
  USBSerial.onEvent(usbEventCallback);

  USBSerial.begin();//在USB CDC On Boot使能情况下,这里是USBCDC
  USB.begin();
//  USBSerial.println("USB CDC communication");
}

void loop() {
  if (Serial0.available()) {
    // size_t l = Serial.available();
    // uint8_t b[l];
    // l = Serial.read(b, l);
    // USBSerial.write(b, l);
    USBSerial.println("Serial Data Available...");  // 通过USBCDC串口监视器通知用户

    String SerialData = Serial0.readString();   // 将接收到的信息使用readString()存储于serialData变量
    USBSerial.print("Received Serial Data: ");  // 然后通过USBCDC串口监视器输出serialData变量内容
    USBSerial.println(SerialData);              // 以便查看serialData变量的信息
  }
  /*
 //   if(USBSerial.available()){
    if(Serial.available()){
    
    //  Serial0.println("USBCDC Data Available..."); // 通过串口监视器通知用户

    String USBData = USBSerial.readString();    // 将接收到的信息使用readString()存储于serialData变量
    Serial0.print("Received USBCDC Data: ");     // 然后通过串口监视器输出serialData变量内容
    Serial0.println(USBData);                 // 以便查看serialData变量的信息
  }
  */
}
#endif /* ARDUINO_USB_MODE */

  • 📜通讯效果:

  • 在这里插入图片描述

USB CDC 仅作为调试输出使用例程

  • 🔖测试对象:ESP32S3
  • 🔧配置选项:
    在这里插入图片描述
/*
  Blink

  Turns an LED on for one second, then off for one second, repeatedly.

  Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
  it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
  the correct LED pin independent of which board is used.
  If you want to know what pin the on-board LED is connected to on your Arduino
  model, check the Technical Specs of your board at:
  https://www.arduino.cc/en/Main/Products

  modified 8 May 2014
  by Scott Fitzgerald
  modified 2 Sep 2016
  by Arturo Guadalupi
  modified 8 Sep 2016
  by Colby Newman

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/Blink
*/

// the setup function runs once when you press reset or power the board
void setup() {
  Serial.begin();//在USB CDC On Boot使能(Enabled)情况下,这里是USBCDC
  // Serial.begin(115200);  // 在USB CDC On Boot没有使能(Disabled)情况下,启动的是硬件串口0通信,波特率为115200
   Serial.setDebugOutput(true);
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level)
  delay(1000);                      // wait for a second
  digitalWrite(LED_BUILTIN, LOW);   // turn the LED off by making the voltage LOW
  delay(1000);                      // wait for a second
  Serial.println("USB CDC Data");     
}

  • 🔖串口信息打印:
    在这里插入图片描述

在这里插入图片描述

文章来源:https://blog.csdn.net/weixin_42880082/article/details/135745843
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。