🛠 Using the USB-A Host Port to Read a USB Drive
Objectifs
This tutorial guides you through configuring the High-Speed USB-A Host port on the Adafruit Metro ESP32-P4 Hydra board. Unlike other boards that require an external hub, this port allows direct connection of standard USB peripherals such as storage drives. You will learn how to manage the software control of the port power and mount the filesystem to read data.
Matériel nécessaire
- Adafruit Metro ESP32-P4 Hydra board
- USB-C cable for PC connection (for debugging and main power)
- USB drive formatted in FAT32 (up to 32 GB recommended for initial compatibility)
- Computer with ESP-IDF or Arduino development environment (with USB Host libraries)
- External power supply (Jack 7-12 V) if connecting multiple power-hungry peripherals, though the USB stick alone often works via USB-C.
Étapes
- Environment Preparation: Ensure your development environment is configured to target the ESP32-P4 chip. Managing the USB Host port requires enabling specific components in the project configuration menu (menuconfig). Enable the
USB Host Mass Storage Classoption and ensure High-Speed mode is selected.
- Power Management: The board schematic indicates that the USB-A port power is logically controlled. Before physically connecting your drive, you must initialize the USB Host controller. In your code, call the USB Host driver initialization function. Note that the board has a software control circuit for USB-A port power; without this initialization step, the port remains off to save energy.
- Physical Connection: Insert your FAT32 USB drive into the female USB-A port on the board. Ensure the drive is properly inserted. The USB-C connector remains connected to your computer to allow debugging messages to be displayed on the serial monitor.
- Mounting the Filesystem: Once the driver is initialized, the system detects the mass storage device. Use a filesystem library (like LittleFS or FatFS integrated into ESP-IDF) to mount the drive's filesystem. You will need to allocate sufficient memory buffer, as the 16 MB of PSRAM is available for storing temporary data during reading.
- Reading Data: Browse the root directory of the USB drive. Open a simple text file present on the drive, read its content into a buffer, and display it on the serial console via the USB-C interface. Remember to cleanly unmount the filesystem before unplugging the drive to avoid data corruption.
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)