Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

problem with SPI and SD card initialization / deinitialization (AUD-5435) #1207

Closed
combalafra01 opened this issue May 28, 2024 · 2 comments
Closed

Comments

@combalafra01
Copy link

Environment

  • Audio development kit: custom
  • [Required] Module or chip used: ESP32-S3-WROOM-1
  • [Required] IDF version v5.2.1
  • [Required] ADF version v2.6-86-g1a03fe6a
  • Build system: CMake
  • Operating system: Windows
  • Using an IDE?: VsCode
  • Power supply: USB

Problem Description

in sdcard.c, function sdcard_mount calls spi_bus_initialize and fails if the SPI bus is already initialized
but sdcard_unmount does not call spi_bus_free to close the SPI bus
so if when sdcard_mount is called again after sdcard_unmount, it fails and it is not possible to access the sdcard anymore (because SPI but is already initialized and sdcard_mount fails).
this is a common use case when we create a first pipeline and it stops when the song is finished and we create a new one few minutes later to play an other song.

Expected Behavior

sdcard_unmount calls spi_bus_free or if impossible sdcard_mount does not fail if spi_bus_initialize returns an error

Actual Behavior

cannot call sdcard_mount after sdcard_unmount

@github-actions github-actions bot changed the title problem with SPI and SD card initialization / deinitialization problem with SPI and SD card initialization / deinitialization (AUD-5435) May 28, 2024
@hbler99
Copy link

hbler99 commented Jul 16, 2024

You're right. sdcard_unmounted and periph_sdcard_unmount functions need to be changed as follows:

esp_err_t sdcard_unmount(const char *base_path, periph_sdcard_mode_t mode)
{
#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 0))
    esp_err_t ret = esp_vfs_fat_sdcard_unmount(base_path, card);
#else
    esp_err_t ret = esp_vfs_fat_sdmmc_unmount();
#endif
    if(mode == SD_MODE_SPI)
    {
        sdmmc_host_t host = SDSPI_HOST_DEFAULT();
        spi_bus_free(host.slot);
    }
    if (ret == ESP_ERR_INVALID_STATE) {
        ESP_LOGE(TAG, "File system not mounted");
    }
    return ret;
}
esp_err_t periph_sdcard_unmount(esp_periph_handle_t periph)
{
    VALIDATE_SDCARD(periph, ESP_FAIL);
    periph_sdcard_t *sdcard = esp_periph_get_data(periph);
    int ret = sdcard_unmount(sdcard->root, sdcard->sd_mode);
    // ESP_LOGW(TAG, "unmount sdcard ret = %d", ret);
    if (ret == ESP_OK) {
        ESP_LOGD(TAG, "UnMount SDCARD success");
        sdcard->is_mounted = false;
        return esp_periph_send_event(periph, SDCARD_STATUS_UNMOUNTED, NULL, 0);
    } else {
        esp_periph_send_event(periph, SDCARD_STATUS_UNMOUNT_ERROR, NULL, 0);
        ESP_LOGE(TAG, "unmount sdcard error!");
        sdcard->is_mounted = false;
        return ESP_FAIL;
    }
    return ESP_OK;
}

@jason-mao
Copy link
Collaborator

It's fixed on faabc34

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants