Skip to content

stuck on tinyusb_driver_uinstall and tinyusb_msc_storage_mount while trying to unexpose and mount back the storage (IEC-340) #219

@earthPerson-001

Description

@earthPerson-001

Answers checklist.

  • I have read the component documentation ESP-IDF Components and the issue is not addressed there.
  • I am using target and esp-idf version as defined in component's idf_component.yml
  • I have searched the issue tracker for a similar issue and not found any related issue.

Which component are you using? If you choose Other, provide details in More Information.

device/esp_tinyusb

ESP-IDF version.

v5.5.0

Development Kit.

Custom Board

Used Component version.

v1.7.6

More Information.

When i repeatedly expose and mount, the execution gets stopped (without any errors) around the mounting point.

The issue is mostly observed when usb is still connected to pc/other device.

I don't have vbus monitoring enabled as the intention is to never plug out the USB cable.

Reproducable code

#include <esp_log.h>
#include <esp_system.h>

#include "SDMMC_USB.hpp"
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

#define TAG "main"

#define MOUNT_POINT_M "/data"

extern "C" void app_main(void)
{
    SDMMC_USB::getInstance().initiate();

    vTaskDelay(1000 / portTICK_PERIOD_MS);

    SDMMC_USB::getInstance().mount();

    char srcFileName[64];
    char dstFileName[64];
    char outFileName[64];
    sprintf(srcFileName, "%s/hello.txt", MOUNT_POINT_M);
    sprintf(dstFileName, "hello.txt");
    sprintf(outFileName, "%s/out.txt", MOUNT_POINT_M);

    for (int i = 0; i < 10; i++) {
        FILE *f = fopen(srcFileName, "w");
        if (f == NULL) {
            ESP_LOGE(TAG, "Failed to open file for writing");
            return;
        }
        fprintf(f, "Hello World!  %d\n", i);
        fclose(f);
        ESP_LOGI(TAG, "Wrote the text on %s", srcFileName);

        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }

    vTaskDelay(2000);
    SDMMC_USB::getInstance().deleteAllFiles();

    while (true) {
        vTaskDelay(5000 / portTICK_PERIOD_MS);
        SDMMC_USB::getInstance().expose();

        vTaskDelay(5000 / portTICK_PERIOD_MS);
        SDMMC_USB::getInstance().mount();
    }
}

The functions used

void SDMMC_USB::mount()
{
    if (!isMountedToESP) {
        ESP_LOGI(TAG, "Unexposing storage from host...");
        tud_disconnect();

        ESP_ERROR_CHECK(tinyusb_driver_uninstall());

        ESP_LOGI(TAG, "Mount storage...");
        ESP_ERROR_CHECK(tinyusb_msc_storage_mount(BASE_PATH));

        // List all the files in this directory
        ESP_LOGI(TAG, "\nls command output:");
        struct dirent *d;
        DIR *dh = opendir(BASE_PATH);
        if (!dh) {
            if (errno == ENOENT) {
                // If the directory is not found
                ESP_LOGE(TAG, "Directory doesn't exist %s", BASE_PATH);
            } else {
                // If the directory is not readable then throw error and exit
                ESP_LOGE(TAG, "Unable to read directory %s", BASE_PATH);
            }
            return;
        }
        // While the next entry is not readable we will print directory files
        while ((d = readdir(dh)) != NULL) {
            printf("%s\n", d->d_name);
        }
        isMountedToESP = true;
        return;
    }
}

void SDMMC_USB::expose()
{
    if (isMountedToESP) {
        ESP_LOGI(TAG, "Exposing storage to host...");
        ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg));
        tud_connect();
        isMountedToESP = false;
    }
}

The initialisation logic is the same as in tusb_msc_main.c example for sdmmc.

Previously, the program got stuck while calling the tinyusb_msc_storage_mount function, but when I tried to collect the log by setting the log level to verbose, it got stuck in tinyusb_driver_uninstall (intr_alloc: esp_intr_free: Disabling int, killing handler).
Log when verbose logging is enabled:

sdmmc_mount_stuck.log

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions