56 lines
1.8 KiB
C
56 lines
1.8 KiB
C
#include <sys/stat.h>
|
|
#include "esp_vfs_fat.h"
|
|
#include "sdmmc_cmd.h"
|
|
#include "driver/sdmmc_host.h"
|
|
#include "esp_log.h"
|
|
|
|
static const char *TAG = "dr_sdmmc";
|
|
#define MOUNT_POINT "/sdcard"
|
|
|
|
esp_err_t sdmmc_init(void)
|
|
{
|
|
esp_err_t ret;
|
|
|
|
// Options for mounting the filesystem.
|
|
// If format_if_mount_failed is set to true, SD card will be partitioned and
|
|
// formatted in case when mounting fails.
|
|
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
|
|
.format_if_mount_failed = false,
|
|
.max_files = 5,
|
|
.allocation_unit_size = 1024 * 1024 *2
|
|
};
|
|
sdmmc_card_t *card;
|
|
const char mount_point[] = MOUNT_POINT;
|
|
ESP_LOGI(TAG, "Initializing SD card");
|
|
|
|
ESP_LOGI(TAG, "Using SDMMC peripheral");
|
|
sdmmc_host_t host = SDMMC_HOST_DEFAULT();
|
|
sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
|
|
|
|
slot_config.width = 4;
|
|
slot_config.clk = 7;
|
|
slot_config.cmd = 2;
|
|
slot_config.d0 = 6;
|
|
slot_config.d1 = 5;
|
|
slot_config.d2 = 39;
|
|
slot_config.d3 = 38;
|
|
slot_config.gpio_cd = 33;
|
|
slot_config.flags |= SDMMC_SLOT_FLAG_INTERNAL_PULLUP;
|
|
|
|
ESP_LOGI(TAG, "Mounting filesystem");
|
|
ret = esp_vfs_fat_sdmmc_mount(mount_point, &host, &slot_config, &mount_config, &card);
|
|
|
|
if (ret != ESP_OK) {
|
|
if (ret == ESP_FAIL) {
|
|
ESP_LOGE(TAG, "Failed to mount filesystem. "
|
|
"If you want the card to be formatted, set the EXAMPLE_FORMAT_IF_MOUNT_FAILED menuconfig option.");
|
|
} else {
|
|
ESP_LOGE(TAG, "Failed to initialize the card (%s). "
|
|
"Make sure SD card lines have pull-up resistors in place.", esp_err_to_name(ret));
|
|
}
|
|
return ESP_FAIL;
|
|
}
|
|
ESP_LOGI(TAG, "Filesystem mounted");
|
|
sdmmc_card_print_info(stdout, card);
|
|
return ESP_OK;
|
|
} |