🛠 Using the USB-A host port for USB flash drive reading

Adafruit Metro ESP32-P4 Hydra · avance · Exploitez la puissance du bus USB High-Speed 480 Mbps de la Metro ESP32-P4 Hydra pour accéder à des clés de stockage externes.

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

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.

  1. 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.

  1. Hardware Configuration

Create a new sketch. Include necessary libraries: ```cpp #include "Adafruit_TinyUSB.h" #include "SdFat.h"

  1. 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.

  1. 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

Code examples for this board

← Back to the board 🛠 All tutorials