espressif / esp-idf

Espressif IoT Development Framework. Official development framework for Espressif SoCs.
Apache License 2.0
13.34k stars 7.21k forks source link

In USB Host mode, can the ESP32 format the Udisk? (IDFGH-12283) #13325

Open rtek1000 opened 6 months ago

rtek1000 commented 6 months ago

Is your feature request related to a problem?

Hello, I did a project with STM32F407VGT6, but I'm interested in trying the ESP32-S3 instead of the STM32.

For this I need 2 USB Host ports, so I thought about using 2 ESP32-S3, or some converter like CH376 (but it seems that this IC does not have Udisk formatting in the internal firmware).

One USB port for keyboard with integrated touchpad, which the ESP32-S3 can already operate. And another port for Udisk, but I also need the formatting function.

Describe the solution you'd like.

If there is already an option to format Udisk, is there any specific documentation or example code?

Describe alternatives you've considered.

Perhaps the Udisk formatting functions of the STM32 code (FatLib) could be adapted for the ESP32-S3

Additional context.

Code I used for STM32F4:

HAL_StatusTypeDef Udisk_format(uint8_t fs_type) {
    if (Appli_state_HS != APPLICATION_READY) {
        UsrLog("Appli_state_HS Not APPLICATION_READY");
        return HAL_BUSY;
    }

    if ((fs_type != FM_FAT32) && (fs_type != FM_EXFAT)) {
        return HAL_ERROR;
    }

    //if (lcd_screen == LcdScreenUsbFormat) {
    uint16_t FF_MAX_SS = 4096;
    BYTE work[FF_MAX_SS];

    uint8_t fres = 0;

    DWORD myLba[4] = { 100, 0, 0, 0 };

    UsrLog("f_fdisk Start...");

    fres = f_fdisk(0, myLba, (void*) work);

    if (fres != FR_OK) {
        UsrLog("f_fdisk ERROR: %u", fres);
        return HAL_ERROR;
    } else {
        UsrLog("f_fdisk OK");
    }

    UsrLog("f_mkfs Start...");

    // fs_type: FM_EXFAT, FM_FAT32
    fres = f_mkfs((const TCHAR*) USBDISKPath, fs_type, 0, work, sizeof work);

    if (fres != FR_OK) {
        UsrLog("f_mkfs ERROR: %u", fres);
        return HAL_ERROR;
    } else {
        UsrLog("f_mkfs OK");
    }
    //}

    return HAL_OK;
}
leeebo commented 6 months ago

@rtek1000 By default, if you set esp_vfs_fat_mount_config_t->format_if_mount_failed = true, the driver will internally format the udisk only when mounts failed (happens when udisk FAT files is bronken)

If you want to force trigger the format operate, please refer msc_format_storage

leeebo commented 6 months ago

For the 2 USB Host ports requirement, you may get interests of the USB Hub support. https://github.com/espressif/esp-idf/issues/12554