57 lines
1.6 KiB
C
57 lines
1.6 KiB
C
#include "driver/sdspi_host.h"
|
|
#include "driver/spi_common.h"
|
|
#include "esp_err.h"
|
|
#include "esp_log.h"
|
|
#include "esp_vfs_fat.h"
|
|
#include "sdmmc_cmd.h"
|
|
#include "pins.h"
|
|
|
|
static const char *TAG = "sdspi";
|
|
|
|
static sdmmc_card_t *card = NULL;
|
|
|
|
void initSD(void) {
|
|
esp_err_t ret;
|
|
|
|
// FAT mount config
|
|
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
|
|
.format_if_mount_failed = false,
|
|
.max_files = 5,
|
|
.allocation_unit_size = 16 * 1024};
|
|
|
|
// SPI host (this is correct for SDSPI)
|
|
sdmmc_host_t host = SDSPI_HOST_DEFAULT();
|
|
|
|
// SPI bus config
|
|
spi_bus_config_t bus_cfg = {.mosi_io_num = MICROSD_MOSI,
|
|
.miso_io_num = MICROSD_MISO,
|
|
.sclk_io_num = MICROSD_CLK,
|
|
.quadwp_io_num = -1,
|
|
.quadhd_io_num = -1,
|
|
.max_transfer_sz = 4000};
|
|
|
|
ret = spi_bus_initialize(host.slot, &bus_cfg, SDSPI_DEFAULT_DMA);
|
|
if (ret != ESP_OK) {
|
|
ESP_LOGE(TAG, "SPI bus init failed: %s", esp_err_to_name(ret));
|
|
return;
|
|
}
|
|
|
|
// SD card device config (CS pin lives here)
|
|
sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT();
|
|
slot_config.gpio_cs = MICROSD_CS;
|
|
slot_config.host_id = host.slot;
|
|
|
|
// Mount filesystem
|
|
ret =
|
|
esp_vfs_fat_sdspi_mount("/sd", &host, &slot_config, &mount_config, &card);
|
|
if (ret != ESP_OK) {
|
|
ESP_LOGE(TAG, "SD mount failed: %s", esp_err_to_name(ret));
|
|
spi_bus_free(host.slot);
|
|
return;
|
|
}
|
|
|
|
ESP_LOGI(TAG, "SD card mounted successfully");
|
|
|
|
// Optional: print card info
|
|
sdmmc_card_print_info(stdout, card);
|
|
} |