USB OTG
The ESP32-P4 includes a USB 2.0 OTG controller capable of operating in both host and device modes at High Speed (480 Mbps). The board provides two USB connectors and a jumper for mode selection.
USB ports on the board
Section titled “USB ports on the board”| Connector | Type | Purpose |
|---|---|---|
| USB-A | Standard Type-A socket | HOST mode — connect USB peripherals (keyboards, flash drives, etc.) |
| Type-C USB (OTG) | Type-C receptacle | DEVICE mode — board acts as a USB device to a host computer |
| Type-C UART | Type-C receptacle | Programming and serial debug (not part of USB OTG) |
Jumper configuration
Section titled “Jumper configuration”The mode-switching jumper (J1) controls which USB port is active for the OTG controller.
| Jumper position | Active port | Mode | Use case |
|---|---|---|---|
| HOST | USB-A socket | Host | Connect external USB devices |
| DEVICE | Type-C OTG port | Device | Board appears as USB device to your computer |
-
Power off the board before changing the jumper position.
-
Locate jumper J1 near the USB connectors.
-
Move the jumper to the desired position:
- HOST: Pins toward the USB-A connector
- DEVICE: Pins toward the Type-C OTG connector
-
Power the board back on. The selected USB port is now active.
Host mode
Section titled “Host mode”In host mode, the ESP32-P4 can enumerate and communicate with standard USB devices through the USB-A port.
Supported device classes
Section titled “Supported device classes”The ESP-IDF USB Host library supports several standard device classes:
- HID: Keyboards, mice, gamepads
- MSC: USB flash drives, external storage
- CDC-ACM: Serial devices, modems
- Custom: Vendor-specific protocols via raw transfers
Host mode example (MSC — USB flash drive)
Section titled “Host mode example (MSC — USB flash drive)”#include "usb/usb_host.h"#include "usb/msc_host.h"
void app_main(void){ // Install USB Host library usb_host_config_t host_config = { .skip_phy_setup = false, .intr_flags = ESP_INTR_FLAG_LEVEL1, }; usb_host_install(&host_config);
// Initialize MSC host driver msc_host_driver_config_t msc_config = { .create_backround_task = true, .task_priority = 5, .stack_size = 4096, }; msc_host_install(&msc_config);
// The driver will detect and mount USB flash drives automatically. // Access files via standard VFS paths once mounted.}Device mode
Section titled “Device mode”In device mode, the board presents itself as a USB device when connected to a host computer via the Type-C OTG port.
Common device class implementations
Section titled “Common device class implementations”- CDC-ACM: Virtual serial port (useful for custom communication protocols)
- MSC: Present the microSD card or flash as a mass storage device
- HID: Emulate a keyboard or mouse
- DFU: Device firmware update over USB
Device mode example (CDC-ACM — virtual serial port)
Section titled “Device mode example (CDC-ACM — virtual serial port)”#include "tinyusb.h"#include "tusb_cdc_acm.h"
void app_main(void){ tinyusb_config_t tusb_cfg = { .device_descriptor = NULL, // Use default .configuration_descriptor = NULL, }; tinyusb_driver_install(&tusb_cfg);
tinyusb_config_cdcacm_t acm_cfg = { .usb_dev = TINYUSB_USBDEV_0, .cdc_port = TINYUSB_CDC_ACM_0, .callback_rx = NULL, // Set your RX callback }; tusb_cdc_acm_init(&acm_cfg);
// The board now appears as a COM port on the host computer}Troubleshooting
Section titled “Troubleshooting”| Symptom | Cause | Fix |
|---|---|---|
| USB device not detected (host mode) | Jumper not set to HOST | Power off, move J1 to HOST position |
| Board not recognized by computer (device mode) | Jumper not set to DEVICE | Power off, move J1 to DEVICE position |
| ”Device not recognized” on Windows | Missing TinyUSB driver | Install the WinUSB driver via Zadig |
| USB flash drive not mounting | Insufficient power | Some drives draw too much current; try a powered hub |
| Intermittent disconnections | Poor cable quality | Use a short, high-quality USB cable |
Further reading
Section titled “Further reading”- ESP-IDF USB Host documentation
- ESP-IDF TinyUSB (device mode)
- Waveshare demo package — USB host and device examples