espressif / esp-idf

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

host example msc_host_vfs_register error (IDFGH-11541) #12662

Closed RomanYakhnenko closed 10 months ago

RomanYakhnenko commented 10 months ago

Answers checklist.

IDF version.

v5.1.1

Espressif SoC revision.

ESP32-S3

Operating System used.

Windows

How did you build your project?

VS Code IDE

If you are using Windows, please specify command line type.

PowerShell

Development Kit.

ESP32-S3-WROOM-1U

Power Supply used.

USB

What is the expected behavior?

Flash is mounted

What is the actual behavior?

Error mount flash in ESP_ERROR_CHECK( msc_host_vfs_register(msc_device, "/usb", &mount_config, &vfs_handle) );

Steps to reproduce.

  1. Take example from IDF 5.1.1 examples\peripherals\usb\host\msc
  2. Set My chip
  3. Set port
  4. Run compile
  5. Run...

Debug Logs.

ESP-ROM:esp32s3-20210327
Build:Mar 27 2021
rst:0x1 (POWERON),boot:0x8 (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fce3818,len:0x186c
load:0x403c9700,len:0x4
load:0x403c9704,len:0xda4
load:0x403cc700,len:0x3130
entry 0x403c9934
I (27) boot: ESP-IDF v5.1.1 2nd stage bootloader
I (27) boot: compile time Nov 24 2023 12:34:29  
I (27) boot: Multicore bootloader
I (30) boot: chip revision: v0.1
I (34) qio_mode: Enabling default flash chip QIO
I (39) boot.esp32s3: Boot SPI Speed : 80MHz     
I (44) boot.esp32s3: SPI Mode       : QIO        
I (49) boot.esp32s3: SPI Flash Size : 8MB        
I (53) boot: Enabling RNG early entropy source...
I (59) boot: Partition Table:
I (62) boot: ## Label            Usage          Type ST Offset   Length  
I (70) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (77) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (85) boot:  2 factory          factory app      00 00 00010000 00100000
I (92) boot:  3 storage          Unknown data     01 82 00110000 002f0000
I (100) boot: End of partition table
I (104) esp_image: segment 0: paddr=00010020 vaddr=3c040020 size=0ef64h ( 61284) map
I (122) esp_image: segment 1: paddr=0001ef8c vaddr=3fc92900 size=0108ch (  4236) load
I (123) esp_image: segment 2: paddr=00020020 vaddr=42000020 size=3629ch (221852) map
I (163) esp_image: segment 3: paddr=000562c4 vaddr=3fc9398c size=01854h (  6228) load
I (164) esp_image: segment 4: paddr=00057b20 vaddr=40374000 size=0e854h ( 59476) load
I (186) boot: Loaded app from partition at offset 0x10000
I (186) boot: Disabling RNG early entropy source...
I (198) cpu_start: Multicore app
I (198) cpu_start: Pro cpu up.
I (198) cpu_start: Starting app cpu, entry point is 0x40375304
0x40375304: call_start_cpu1 at C:/Espressif/frameworks/esp-idf/components/esp_system/port/cpu_start.c:154

I (0) cpu_start: App cpu up.
I (216) cpu_start: Pro cpu start user code
I (216) cpu_start: cpu freq: 160000000 Hz
I (216) cpu_start: Application information:
I (219) cpu_start: Project name:     usb-msc
I (224) cpu_start: App version:      1
I (228) cpu_start: Compile time:     Nov 24 2023 12:37:58
I (234) cpu_start: ELF file SHA256:  b5a3f2892118bee4...
I (240) cpu_start: ESP-IDF:          v5.1.1
I (245) cpu_start: Min chip rev:     v0.0
I (250) cpu_start: Max chip rev:     v0.99 
I (255) cpu_start: Chip rev:         v0.1
I (260) heap_init: Initializing. RAM available for dynamic allocation:
I (267) heap_init: At 3FC95AB0 len 00053C60 (335 KiB): DRAM
I (273) heap_init: At 3FCE9710 len 00005724 (21 KiB): STACK/DRAM
I (280) heap_init: At 3FCF0000 len 00008000 (32 KiB): DRAM
I (286) heap_init: At 600FE010 len 00001FD8 (7 KiB): RTCRAM
I (293) spi_flash: detected chip: generic
I (297) spi_flash: flash io: qio
I (301) sleep: Configure to isolate all GPIO pins in sleep state
I (308) sleep: Enable automatic switching of GPIO sleep configuration
I (315) app_start: Starting scheduler on CPU0
I (320) app_start: Starting scheduler on CPU1
I (320) main_task: Started on CPU0
I (330) main_task: Calling app_main()
I (610) example: Waiting for USB stick to be connected
I (1000) example: MSC device connected
I (1000) example: connection...
*** Device descriptor ***
bLength 18
bDescriptorType 1
bcdUSB 2.00
bDeviceClass 0x0
bDeviceSubClass 0x0
bDeviceProtocol 0x0
bMaxPacketSize0 64
idVendor 0x930
idProduct 0x6545
bcdDevice 1.10
iManufacturer 1
iProduct 2
iSerialNumber 3
bNumConfigurations 1
*** Configuration descriptor ***
bLength 9
bDescriptorType 2
wTotalLength 32
bNumInterfaces 1
bConfigurationValue 1
iConfiguration 0
bmAttributes 0x80
bMaxPower 150mA
        *** Interface descriptor ***
        bLength 9
        bDescriptorType 4
        bInterfaceNumber 0
        bAlternateSetting 0
        bNumEndpoints 2
        bInterfaceClass 0x8
        bInterfaceSubClass 0x6
        bInterfaceProtocol 0x50
        iInterface 0
                *** Endpoint descriptor ***
                bLength 7
                bDescriptorType 5
                bEndpointAddress 0x81   EP 1 IN
                bmAttributes 0x2        BULK
                wMaxPacketSize 64
                bInterval 0
                *** Endpoint descriptor ***
                bLength 7
                bDescriptorType 5
                bEndpointAddress 0x2    EP 2 OUT
                bmAttributes 0x2        BULK
                wMaxPacketSize 64
                bInterval 0
Device info:
         Capacity: 7445 MB
         Sector size: 512
         Sector count: 15249407
         PID: 0x6545
         VID: 0x0930
         iProduct: USB Flash Memory
         iManufacturer:
         iSerialNumber: 00187D1174FEE190111E6C89 
ESP_ERROR_CHECK failed: esp_err_t 0x1701 (ERROR) at 0x4200958b
0x4200958b: app_main at C:/REPOSITARY/PU110/Tests/Storage/msc_ESPIDF_clean/main/msc_example_main.c:212 (discriminator 1)

file: "./main/msc_example_main.c" line 212
func: app_main
expression: msc_host_vfs_register(msc_device, "/usb", &mount_config, &vfs_handle)

abort() was called at PC 0x4037a2fb on core 0
0x4037a2fb: _esp_error_check_failed at C:/Espressif/frameworks/esp-idf/components/esp_system/esp_err.c:50

Backtrace: 0x4037584e:0x3fc993d0 0x4037a305:0x3fc993f0 0x403806be:0x3fc99410 0x4037a2fb:0x3fc99480 0x4200958b:0x3fc994b0 0x42035ac3:0x3fc99600 0x4037ca09:0x3fc99630
0x4037584e: panic_abort at C:/Espressif/frameworks/esp-idf/components/esp_system/panic.c:452

0x4037a305: esp_system_abort at C:/Espressif/frameworks/esp-idf/components/esp_system/port/esp_system_chip.c:84

0x403806be: abort at C:/Espressif/frameworks/esp-idf/components/newlib/abort.c:38

0x4037a2fb: _esp_error_check_failed at C:/Espressif/frameworks/esp-idf/components/esp_system/esp_err.c:50

0x4200958b: app_main at C:/REPOSITARY/PU110/Tests/Storage/msc_ESPIDF_clean/main/msc_example_main.c:212 (discriminator 1)

0x42035ac3: main_task at C:/Espressif/frameworks/esp-idf/components/freertos/app_startup.c:208 (discriminator 13)

0x4037ca09: vPortTaskWrapper at C:/Espressif/frameworks/esp-idf/components/freertos/FreeRTOS-Kernel/portable/xtensa/port.c:162

More Information.

Try with 3 different usb flash stick. Try change in here

RomanYakhnenko commented 10 months ago

espressif/usb_host_msc version 1.1.1 I look theme with this problem - writes all ok - must work. But all last version - and not working(

RomanYakhnenko commented 10 months ago

if need more: full code msc_example_main.c ` /*

define USB_DISCONNECT_PIN GPIO_NUM_10

define READY_TO_UNINSTALL (HOST_NO_CLIENT | HOST_ALL_FREE)

typedef enum { HOST_NO_CLIENT = 0x1, HOST_ALL_FREE = 0x2, DEVICE_CONNECTED = 0x4, DEVICE_DISCONNECTED = 0x8, DEVICE_ADDRESS_MASK = 0xFF0, } app_event_t;

static const char *TAG = "example"; static EventGroupHandle_t usb_flags;

static void msc_event_cb(const msc_host_event_t event, void arg) { if (event->event == MSC_DEVICE_CONNECTED) { ESP_LOGI(TAG, "MSC device connected"); // Obtained USB device address is placed after application events xEventGroupSetBits(usb_flags, DEVICE_CONNECTED | (event->device.address << 4)); } else if (event->event == MSC_DEVICE_DISCONNECTED) { xEventGroupSetBits(usb_flags, DEVICE_DISCONNECTED); ESP_LOGI(TAG, "MSC device disconnected"); } }

static void print_device_info(msc_host_device_info_t info) { const size_t megabyte = 1024 1024; uint64_t capacity = ((uint64_t)info->sector_size * info->sector_count) / megabyte;

printf("Device info:\n");
printf("\t Capacity: %llu MB\n", capacity);
printf("\t Sector size: %"PRIu32"\n", info->sector_size);
printf("\t Sector count: %"PRIu32"\n", info->sector_count);
printf("\t PID: 0x%04X \n", info->idProduct);
printf("\t VID: 0x%04X \n", info->idVendor);
wprintf(L"\t iProduct: %S \n", info->iProduct);
wprintf(L"\t iManufacturer: %S \n", info->iManufacturer);
wprintf(L"\t iSerialNumber: %S \n", info->iSerialNumber);

} / static bool file_exists(const char file_path) { struct stat buffer; return stat(file_path, &buffer) == 0; }

static void file_operations(void) { const char directory = "/usb/esp"; const char file_path = "/usb/esp/test.txt";

struct stat s = {0};
bool directory_exists = stat(directory, &s) == 0;
if (!directory_exists) {
    if (mkdir(directory, 0775) != 0) {
        ESP_LOGE(TAG, "mkdir failed with errno: %s\n", strerror(errno));
    }
}

if (!file_exists(file_path)) {
    ESP_LOGI(TAG, "Creating file");
    FILE *f = fopen(file_path, "w");
    if (f == NULL) {
        ESP_LOGE(TAG, "Failed to open file for writing");
        return;
    }
    fprintf(f, "Hello World!\n");
    fclose(f);
}

FILE *f;
ESP_LOGI(TAG, "Reading file");
f = fopen(file_path, "r");
if (f == NULL) {
    ESP_LOGE(TAG, "Failed to open file for reading");
    return;
}
char line[64];
fgets(line, sizeof(line), f);
fclose(f);
// strip newline
char *pos = strchr(line, '\n');
if (pos) {
    *pos = '\0';
}
ESP_LOGI(TAG, "Read from file: '%s'", line);

} / // Handles common USB host library events static void handle_usb_events(void args) { while (1) { uint32_t event_flags; usb_host_lib_handle_events(portMAX_DELAY, &event_flags);

    // Release devices once all clients has deregistered
    if (event_flags & USB_HOST_LIB_EVENT_FLAGS_NO_CLIENTS) {
        usb_host_device_free_all();
        xEventGroupSetBits(usb_flags, HOST_NO_CLIENT);
    }
    // Give ready_to_uninstall_usb semaphore to indicate that USB Host library
    // can be deinitialized, and terminate this task.
    if (event_flags & USB_HOST_LIB_EVENT_FLAGS_ALL_FREE) {
        xEventGroupSetBits(usb_flags, HOST_ALL_FREE);
    }
}

vTaskDelete(NULL);

}

static uint8_t wait_for_msc_device(void) { EventBits_t event;

ESP_LOGI(TAG, "Waiting for USB stick to be connected");
event = xEventGroupWaitBits(usb_flags, DEVICE_CONNECTED | DEVICE_ADDRESS_MASK,
                            pdTRUE, pdFALSE, portMAX_DELAY);
ESP_LOGI(TAG, "connection...");
// Extract USB device address from event group bits
return (event & DEVICE_ADDRESS_MASK) >> 4;

}

static bool wait_for_event(EventBits_t event, TickType_t timeout) { return xEventGroupWaitBits(usb_flags, event, pdTRUE, pdTRUE, timeout) & event; }

void app_main(void) { msc_host_device_handle_t msc_device; msc_host_vfs_handle_t vfs_handle; msc_host_device_info_t info; BaseType_t task_created;

const esp_vfs_spiffs_conf_t spiffs_conf = {
    .base_path = FS_MNT_PATH,
    .partition_label = NULL,
    .max_files = 5,
    .format_if_mount_failed = false
};
/* Initialize SPI file system */
ESP_ERROR_CHECK( esp_vfs_spiffs_register(&spiffs_conf) );

usb_flags = xEventGroupCreate();
assert(usb_flags);
const usb_host_config_t host_config = { .intr_flags = ESP_INTR_FLAG_LEVEL1 };
ESP_ERROR_CHECK( usb_host_install(&host_config) );
task_created = xTaskCreate(handle_usb_events, "usb_events", 2 * 2048, NULL, 2, NULL); //2048
assert(task_created);

const msc_host_driver_config_t msc_config = {
    .create_backround_task = true,
    .task_priority = 5,
    .stack_size = 4096,
    .callback = msc_event_cb,
};
ESP_ERROR_CHECK( msc_host_install(&msc_config) );

const esp_vfs_fat_mount_config_t mount_config = {
    .format_if_mount_failed = false,
    .max_files = 3,
    .allocation_unit_size = 1024,
};

do {
    uint8_t device_address = wait_for_msc_device();
    ESP_ERROR_CHECK( msc_host_install_device(device_address, &msc_device) );
    msc_host_print_descriptors(msc_device);
    ESP_ERROR_CHECK( msc_host_get_device_info(msc_device, &info) );
    print_device_info(&info);

    //msc_host_vfs_register(msc_device, "/usb", &mount_config, &vfs_handle);
    ESP_ERROR_CHECK( msc_host_vfs_register(msc_device, "/usb", &mount_config, &vfs_handle) );

    ESP_LOGI(TAG, "Device connected ok, writing binary!");

    FILE *original = fopen("/spiffs/readme.txt", "r");
    FILE *copy = fopen("/usb/readme.txt", "w");
    assert(original);
    assert(copy);

    int ch;
    while( ( ch = fgetc(original) ) != EOF ) {
        fputc(ch, copy);
    }
    fclose(original);
    fclose(copy);

    ESP_LOGI(TAG, "Copy finished");

    while (!wait_for_event(DEVICE_DISCONNECTED, 200)) {}

    xEventGroupClearBits(usb_flags, READY_TO_UNINSTALL);
    ESP_ERROR_CHECK( msc_host_vfs_unregister(vfs_handle) );
    ESP_ERROR_CHECK( msc_host_uninstall_device(msc_device) );

} while (gpio_get_level(USB_DISCONNECT_PIN) != 0);

ESP_LOGI(TAG, "Uninitializing USB ...");
ESP_ERROR_CHECK( msc_host_uninstall() );
wait_for_event(READY_TO_UNINSTALL, portMAX_DELAY);
ESP_ERROR_CHECK( usb_host_uninstall() );
ESP_LOGI(TAG, "Done");

} `

RomanYakhnenko commented 10 months ago

When i make active

const esp_vfs_fat_mount_config_t mount_config = { .format_if_mount_failed = **true**, flash format and mount - now testing

RomanYakhnenko commented 10 months ago

In this moment after format from esp32 all work. Not undestand - try format from ciomputer with fifferent parameters. If need open again

tore-espressif commented 10 months ago

Hi @RomanYakhnenko , the MSC driver supports only FAT file format. New USB flash disks with great size (>8GB) usually use exFAT. That is why reformatting to FAT helped.

fozzzgate commented 3 months ago

@RomanYakhnenko Were you ever able to get this working with Windows FAT32 formatted USB drives? I am having this same problem and I can't figure out what is causing it. Windows formatted USB drives are not accepted (0x1701 - ESP_ERR_MSC_MOUNT_FAILED) and USB drives formatted by usb_host_msc can be read fine on ESP32, but are corrupt and not accepted on Windows.

I have downgraded to ESP IDF 5.0.4 & 4.4.7 / usb_host_msc 1.0.0 and it is still happening. Everything was working fine yesterday with Windows-formatted USB drives until I upgraded to ESP IDF 5.2.2 and now everything is broken and I can't even go back to a working version.