Page 1 of 1

Setting up USB device on ESP32-S3

Posted: Wed Oct 04, 2023 7:36 pm
by JambonLeVrai
Hi there, I’m attempting to use an ESP32-S3 board as device, the exact board I’m using being esp32-s3-devkitc-1-N8R2. What I want to do is to use it as a composite USB device, combining keyboard and CDC ; the cdc serial port being used to configure macros on the keyboard through a software on the computer.
I have managed to make it work as a keyboard, but I can’t seem to be able to configure the CDC as I wish. Since to detect the right USB device to communicate to, I have to scan for the right VID/PID but I can’t change this. Here is the code I am working with (which is a simplified code taken from the ESP32 GitHub) :
  1. #include <Arduino.h>
  2. #include "USB.h"
  3. #include "USBCDC.h"
  4. #define HWSerial Serial0
  5. #define USBSerial Serial
  6.  
  7. void setup() {
  8.   USB.VID(0x1234);
  9.   USB.PID(0x5678);
  10.   USB.productName("Test1");
  11.   USB.manufacturerName("Test2");
  12.   HWSerial.begin(115200);
  13.  
  14.   USBSerial.begin(115200);
  15.   USB.begin();
  16. }
  17.  
  18. void loop() {
  19.   USBSerial.println(USB.VID());
  20.   USBSerial.println(USB.PID());
  21.   USBSerial.println(USB.productName());
  22.   USBSerial.println(USB.manufacturerName());
  23.  
  24.   while(HWSerial.available()){
  25.     size_t l = HWSerial.available();
  26.     uint8_t b[l];
  27.     l = HWSerial.read(b, l);
  28.     USBSerial.write(b, l);
  29.   }
  30.   delay(100);
  31. }
The serial works correctly, I can write things on the hardware serial and see the bytes coming on the usb serial just right. The only issue here is that the USB descriptor is kept as the original one on my device manager. The VID, PID, Product Name and Manufacturer Name that are debugged to USBSerial are the default ones, though I should have changed them in setup.
I am using the Platformio framework, but I believe everything is setup correctly : I have to unset the flag

Code: Select all

ARDUINO_USB_MODE
and set

Code: Select all

ARDUINO_USB_CDC_ON_BOOT
on the build flags, so here is the platformio.ini file :
  1. ; PlatformIO Project Configuration File
  2. ;
  3. ;   Build options: build flags, source filter
  4. ;   Upload options: custom upload port, speed and extra flags
  5. ;   Library options: dependencies, extra library storages
  6. ;   Advanced options: extra scripting
  7. ;
  8. ; Please visit documentation for the other options and examples
  9. ; https://docs.platformio.org/page/projectconf.html
  10.  
  11. [env:esp32-c3-devkitm-1]
  12. platform = espressif32
  13. board = esp32-s3-devkitc-1
  14.  
  15. ; change microcontroller
  16. board_build.mcu = esp32s3
  17.  
  18. ; change MCU frequency
  19. board_build.f_cpu = 240000000L
  20.  
  21. build_unflags = -D ARDUINO_USB_MODE
  22. build_flags = -D ARDUINO_USB_CDC_ON_BOOT=1
  23.  
  24. framework = arduino
  25. monitor_port = COM7
  26. upload_port = COM7
  27. monitor_speed = 115200
  28. monitor_rts = 0
  29. monitor_dtr = 0
Has anyone been able to perform this ? Or is the hardware simply not supporting changing the USB descriptor ?

Re: Setting up USB device on ESP32-S3

Posted: Thu Oct 05, 2023 1:33 am
by lbernstone
USB_CDC_ON_BOOT is a hardware device. It is not configurable. If you want to have custom settings, you will need to use the USB-OTG/tinyUSB settings, and then you should be able to use the USBCDC object.