esp-idf/examples/peripherals/usb/device/tusb_serial_device
Peter Marcisovsky d8d92c1928 refactor(examples): Refactor rx callbacks in USB device examples with CDC device
- cdcacm_write_queue and cdcacm_write_flush moved from rx callback to main task
    - received data from rx callback are handled by freerots queues
2024-06-06 15:51:23 +02:00
..
main refactor(examples): Refactor rx callbacks in USB device examples with CDC device 2024-06-06 15:51:23 +02:00
CMakeLists.txt feat(esp32p4): Add USB-OTG support for ESP32-P4 2024-01-30 15:07:11 +01:00
pytest_usb_device_serial.py fix(ci): Temporarily disable USB device tests on S3 2024-04-17 11:43:10 +02:00
README.md feat(esp32p4): Add USB-OTG support for ESP32-P4 2024-01-30 15:07:11 +01:00
sdkconfig.ci tinyusb: Add TinyUSB example tests 2022-08-10 14:51:18 +02:00
sdkconfig.ci.dualchannel usb: Extend CDC device with second CDC channel 2022-01-05 08:18:03 +01:00
sdkconfig.defaults tinyusb: Use TinyUSB from component registry 2022-11-02 08:24:43 +01:00

Supported Targets ESP32-P4 ESP32-S2 ESP32-S3

TinyUSB Serial Device Example

(See the README.md file in the upper level 'examples' directory for more information about examples.)

This example shows how to set up ESP chip to work as a USB Serial Device.

As a USB stack, a TinyUSB component is used.

How to use example

This example can also be configured to act as double serial device. Run idf.py menuconfig and in Component config → TinyUSB Stack → Communication Device Class (CDC) → CDC Channel Count select number of serial devices you want to implement.

Hardware Required

Any ESP board that have USB-OTG supported.

Pin Assignment

Note: In case your board doesn't have micro-USB connector connected to USB-OTG peripheral, you may have to DIY a cable and connect D+ and D- to the pins listed below.

See common pin assignments for USB Device examples from upper level.

Build and Flash

Build the project and flash it to the board, then run monitor tool to view serial output:

idf.py -p PORT flash monitor

(Replace PORT with the name of the serial port to use.)

(To exit the serial monitor, type Ctrl-].)

See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.

Example Output

After the flashing you should see this output (for single CDC channel):

I (285) example: USB initialization
I (285) tusb_desc:
┌─────────────────────────────────┐
│  USB Device Descriptor Summary  │
├───────────────────┬─────────────┤
│bDeviceClass       │ 239         │
├───────────────────┼─────────────┤
│bDeviceSubClass    │ 2           │
├───────────────────┼─────────────┤
│bDeviceProtocol    │ 1           │
├───────────────────┼─────────────┤
│bMaxPacketSize0    │ 64          │
├───────────────────┼─────────────┤
│idVendor           │ 0x303a      │
├───────────────────┼─────────────┤
│idProduct          │ 0x4001      │
├───────────────────┼─────────────┤
│bcdDevice          │ 0x100       │
├───────────────────┼─────────────┤
│iManufacturer      │ 0x1         │
├───────────────────┼─────────────┤
│iProduct           │ 0x2         │
├───────────────────┼─────────────┤
│iSerialNumber      │ 0x3         │
├───────────────────┼─────────────┤
│bNumConfigurations │ 0x1         │
└───────────────────┴─────────────┘
I (455) TinyUSB: TinyUSB Driver installed
I (465) example: USB initialization DONE

Connect to the serial port (e.g. on Linux, it should be /dev/ttyACM0) by any terminal application (e.g. picocom /dev/ttyACM0). Now you can send data strings to the device, the device will echo back the same data string.

The monitor tool will also print the communication process:

I (12438) example: Data from channel 0:
I (12438) example: 0x3ffbfea0   45 73 70 72 65 73 73 69  66 0d                    |Espressif.|