//------------------------------------------------------------------------------
/**
Inits SD Card
@param void
@return sdcard-ready or error code
* the handy-dandy esp_vfs_fat_sdspi_mount() function:
* - initializes an SPI Master device based on the SPI Master driver with configuration in
* slot_config, and attach it to an initialized SPI bus.
* - initializes SD card with configuration in host_config_input
* - mounts FAT partition on SD card using FATFS library, with configuration in mount_config
* - registers FATFS library with VFS, with prefix given by base_prefix variable
* @param base_path path where partition should be registered (e.g. "/sdcard")
* @param host_config_input Pointer to structure describing SDMMC host. This structure can be
* initialized using SDSPI_HOST_DEFAULT() macro.
* @param slot_config Pointer to structure with slot configuration.
* For SPI peripheral, pass a pointer to sdspi_device_config_t
* structure initialized using SDSPI_DEVICE_CONFIG_DEFAULT().
* @param mount_config pointer to structure with extra parameters for mounting FATFS
* @param[out] out_card If not NULL, pointer to the card information structure will be returned via
* this argument. It is suggested to hold this handle and use it to unmount the card.
*/
sdcard_return_t sdcard_init(void)
{
static const char *TAG = "sdcard_init";
g_log(TAG, "----- Init SPI bus for SD-Card and Mount fat filesystem");
esp_err_t return_esp;
sdmmc_host_t host = SDSPI_HOST_DEFAULT(); // init host to various defaults, max spi freq of 20MHz...
host.slot = SDCARD_PORT; // ...and use spi port defined in hwconfig.h
spi_bus_config_t buscfg = {
.sclk_io_num = SDCARD_SPI_SCK, // pins defined in hwconfig.h
.mosi_io_num = SDCARD_SPI_MOSI,
.miso_io_num = SDCARD_SPI_MISO,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
.max_transfer_sz = 4000 };
return_esp = spi_bus_initialize(host.slot, &buscfg, SDSPI_DEFAULT_DMA);
if (return_esp != ESP_OK)
{
g_logm(TAG, ">>> Failed to initialize SPI bus (%s)", esp_err_to_name(return_esp));
return SDCARD_ERR_SPI_BUS;
}
sdspi_device_config_t slot_config = {
.host_id = host.slot,
.gpio_cs = SDCARD_SPI_CS, // pins defined in hwconfig.h
.gpio_cd = SDSPI_SLOT_NO_CD,
.gpio_wp = SDSPI_SLOT_NO_WP,
.gpio_int = GPIO_NUM_NC,
.gpio_wp_polarity = SDSPI_IO_ACTIVE_LOW };
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
.format_if_mount_failed = FORMAT_IF_MOUNT_FAILS,
.max_files = 5,
.allocation_unit_size = (16 * 1024) }; // a good size?
const char mount_point[] = SD_MOUNT_POINT;
sdmmc_card_t *card;
return_esp = esp_vfs_fat_sdspi_mount(mount_point, &host, &slot_config, &mount_config, &card);
if (return_esp != ESP_OK)
{
sdcard_available_g = false;
if (return_esp == ESP_FAIL)
{
g_logm(TAG, ">>> Failed to mount filesystem");
return SDCARD_ERR_FAT_MOUNT_FAIL;
}
else
{
g_logm(TAG, ">>> Failed to initialize the card (%s)", esp_err_to_name(return_esp));
return SDCARD_ERR_FAT_MOUNT_ERR;
}
}
g_logb(TAG, "--- Filesystem mounted");
sdcard_available_g = true;
// --- card has been initialized, print card statistics and directory:
print_sdcard_info(card);
print_sdcard_dir();
return READY;
}