esp_err_t sdcard_mount(const char* base_path) { sdmmc_host_t host = SDMMC_HOST_DEFAULT(); // To use 1-line SD mode, uncomment the following line: host.flags = SDMMC_HOST_FLAG_1BIT; sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT(); slot_config.gpio_cd = g_gpio; slot_config.width = 1; esp_vfs_fat_sdmmc_mount_config_t mount_config = { .format_if_mount_failed = false, .max_files = SD_CARD_OPEN_FILE_NUM_MAX }; sdmmc_card_t* card; ESP_LOGI(TAG, "Trying to mount with base path=%s", base_path); esp_err_t ret = esp_vfs_fat_sdmmc_mount(base_path, &host, &slot_config, &mount_config, &card); switch (ret) { case ESP_OK: // Card has been initialized, print its properties sdmmc_card_print_info(card); ESP_LOGI(TAG, "CID name %s!\n", card->cid.name); break; case ESP_ERR_INVALID_STATE: ESP_LOGE(TAG, "File system already mounted"); break; case ESP_FAIL: ESP_LOGE(TAG, "Failed to mount filesystem. If you want the card to be formatted, set format_if_mount_failed = true."); break; default: ESP_LOGE(TAG, "Failed to initialize the card (%d). Make sure SD card lines have pull-up resistors in place.", ret); break; } return ret; } esp_err_t sdcard_unmount(void) { esp_err_t ret = esp_vfs_fat_sdmmc_unmount(); if (ret == ESP_ERR_INVALID_STATE) { ESP_LOGE(TAG, "File system not mounted"); } return ret; }
static void test_setup(void) { sdmmc_host_t host = SDMMC_HOST_DEFAULT(); sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT(); esp_vfs_fat_sdmmc_mount_config_t mount_config = { .format_if_mount_failed = true, .max_files = 5 }; TEST_ESP_OK(esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, NULL)); } static void test_teardown(void) { TEST_ESP_OK(esp_vfs_fat_sdmmc_unmount()); }