🛠 High-Speed USB Storage Automation on ESP32-P4 Hydra

Adafruit Metro ESP32-P4 Hydra · avance · Configurez le port USB-A hôte pour monter et écrire sur des clés USB à 480 Mbps, exploitant la puissance du RISC-V.

Objectives

This tutorial aims to leverage the advanced capabilities of the Adafruit Metro ESP32-P4 Hydra's High-Speed (480 Mbps) USB-A host port. Unlike classic microcontrollers limited to USB Full-Speed, the ESP32-P4 SoC enables massive data transfers. We will configure the system to automatically detect a USB drive, mount the file system, and perform sequential log writing, illustrating industrial data collection automation.

Required Hardware

Steps

  1. Project Configuration: Create a new ESP-IDF project. In the configuration menu (idf.py menuconfig), enable USB Host High-Speed support under USB Host Stack > USB Host High-Speed. Ensure the USB Mass Storage Class driver is enabled.
  2. Bus Initialization: In your app_main code, initialize the USB bus. The P4 uses dedicated lines for HS. Enable software control of the USB-A port power if necessary to protect the board.
  3. Detection and Mounting: Implement a FreeRTOS task that waits for the USB_HOST_MSC_DEVICE_CONNECTED event. Once detected, call esp_vfs_fat_usb_disk_register to mount the volume.
  4. Data Writing: Open a file in write mode (fopen("/usb/logs.txt", "a")). Write data in 4 KB blocks to saturate the bus and test throughput. Use fflush to force physical writing.
  5. Error Handling: Add a handler for disconnect events (USB_HOST_MSC_DEVICE_DISCONNECTED) to cleanly unmount the file system and prevent data corruption.

Verification

Open the serial monitor (idf.py monitor). You should see USB bus initialization messages, followed by USB drive detection and the "Volume mounted successfully" message. Remove the USB drive after writing and verify on a PC that logs.txt contains the written data. Expected throughput should be significantly higher than 10 MB/s.

Further Reading

Integrate the ESP32-C6 coprocessor to transmit logs via WiFi or Thread before local writing, creating a high-throughput hybrid IoT node.

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials