🛠 Using the USB-A host port for USB flash drive reading
Objectives
This advanced tutorial aims to master the High-Speed USB Host interface available on the Adafruit Metro ESP32-P4 Hydra. Unlike classic microcontrollers limited to USB Full-Speed, this board offers a 480 Mbps throughput, enabling rapid reading of large datasets. The goal is to configure the USB bus, mount a file system on a USB flash drive via the USB Mass Storage Class (MSC), and read text or binary files.
Required Hardware
- Adafruit Metro ESP32-P4 Hydra board.
- USB flash drive formatted in FAT32 (under 32 GB recommended for compatibility).
- USB-A to USB-A cable (or adapter if needed).
- Computer with Arduino IDE 2.x or PlatformIO installed.
- Libraries:
Adafruit TinyUSB(ESP32-P4 compatible version) andFatFs.
Steps
Open Arduino IDE and ensure the board manager includes ESP32-P4 support. Install the Adafruit TinyUSB library via the library manager. Note that the USB-C port is for debugging (UART), while the USB-A port is dedicated to Host mode.
- Environment Setup
Insert the FAT32 USB drive into the board's USB-A port. Ensure sufficient power supply (via USB-C or DC jack) as the USB bus may draw up to 500 mA. Power control is handled by software.
- Hardware Configuration
Create a new sketch. Include necessary libraries: ```cpp #include "Adafruit_TinyUSB.h" #include "SdFat.h"
- Code Writing
#define CFG_TUD_HOST_MSC 1
// Define USB power control pin if required by schematic // const int USB_VBUS_PIN = 4; `` In setup(), initialize the USB Host: ``cpp void setup() { Serial.begin(115200); while (!Serial) delay(10);
usbh_msc_init(); Serial.println("USB Host MSC Initialized"); } `` In loop(), check for device presence and mount the volume: `cpp void loop() { if (usbh_msc_connected()) { // FatFs mounting and file reading logic // Simplified example of printing filename Serial.println("USB Drive Detected"); } delay(500); } ` *Note: Exact calls depend on the TinyUSB version for ESP-IDF. Refer to usbh_msc` examples in the library.*
Select "Adafruit Metro ESP32-P4" in the board menu. Compile and upload via the USB-C port. Open the Serial Monitor at 115200 baud.
- Compilation and Upload
Verification
After startup, the Serial Monitor should display the initialization message. Insert a USB drive containing a test.txt file. If the device is detected, the message "USB Drive Detected" (or equivalent based on your reading code) appears. You can extend the code to read the file content and print it to the console, confirming data transmission over the High-Speed bus.
Further Exploration
Explore transferring files to the external PSRAM (16 MB) to process large datasets. You can also develop a Web server that displays USB drive content via the ESP32-C6 coprocessor, combining fast physical storage with wireless network interface.
Other tutorials for this board
- First connection and startup of the Metro ESP32-P4 Hydra
- Controlling GPIOs and a Simple Display on Metro ESP32-P4 Hydra
- Store Data on Adafruit Metro ESP32-P4 Hydra
- First Startup of the Metro ESP32-P4 Hydra
- Controlling an I2C sensor with ESP32-P4 Hydra
- Advanced Startup: Power, USB, and Debugging on ESP32-P4 Hydra
Code examples for this board
esp32-p4-hydra-gpio-blink.inoC++ (Arduino)esp32-p4-hydra-mpu6050-i2c.inoC++ (Arduino)esp32-p4-hydra-wifi-http.inoC++ (Arduino)esp32-p4-hydra-sdcard-write.inoC++ (Arduino)esp32-p4-hydra-usb-host-mouse.inoC++ (Arduino)esp32-p4-hydra-gpio-led-blink.inoC++ (Arduino)