If the hardware is working with arduino framework, but not idf, then you are correct that it is likely software-related.
I don't know if this helps, but here is my sd-card init routine:
Code: Untitled.c Select all
//------------------------------------------------------------------------------
/**
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;
}
and support files:
Code: Untitled.c Select all
//------------------------------------------------------------------------------
static void print_sdcard_info(const sdmmc_card_t* card)
{
#define SD_OCR_SDHC_CAP (1<<30) // Card Capacity Status bit in OCR
bool print_scr = false;
bool print_csd = false;
const char *type;
printf("\n SD-Card Info:\n");
printf(" SDCard Name: %s\n", card->cid.name);
if (card->is_sdio)
{
type = "SDIO";
print_scr = true;
print_csd = true;
}
else if (card->is_mmc)
{
type = "MMC";
print_csd = true;
}
else
{
type = (card->ocr & SD_OCR_SDHC_CAP) ? "SDHC/SDXC" : "SDSC";
print_csd = true;
}
printf(" Card Type: %s\n", type);
if (card->real_freq_khz == 0) {
printf(" Speed: N/A\n");
} else {
const char *freq_unit = card->real_freq_khz < 1000 ? "kHz" : "MHz";
const float freq = card->real_freq_khz < 1000 ? card->real_freq_khz : card->real_freq_khz / 1000.0;
const char *max_freq_unit = card->max_freq_khz < 1000 ? "kHz" : "MHz";
const float max_freq = card->max_freq_khz < 1000 ? card->max_freq_khz : card->max_freq_khz / 1000.0;
printf(" Speed: %.2f %s (limit: %.2f %s)%s\n", freq, freq_unit, max_freq, max_freq_unit, card->is_ddr ? ", DDR" : "");
}
printf(" Size: %llu MB\n", ((uint64_t) card->csd.capacity) * card->csd.sector_size / (1024 * 1024));
if (print_csd) {
printf(" CSD: ver=%d, sector_size=%d, capacity=%d read_bl_len=%d\n",
(int) (card->is_mmc ? card->csd.csd_ver : card->csd.csd_ver + 1),
card->csd.sector_size, card->csd.capacity, card->csd.read_block_len);
if (card->is_mmc) {
printf(" EXT CSD: bus_width=%" PRIu32 "\n", (uint32_t) (1 << card->log_bus_width));
} else if (!card->is_sdio){ // make sure card is SD
printf(" SSR: bus_width=%" PRIu32 "\n", (uint32_t) (card->ssr.cur_bus_width ? 4 : 1));
}
}
if (print_scr) {
printf(" SCR: sd_spec=%d, bus_width=%d\n", card->scr.sd_spec, card->scr.bus_width);
}
}
Code: Untitled.c Select all
//------------------------------------------------------------------------------
void print_sdcard_dir(void)
{
DIR *directory;
struct dirent *entry;
struct stat filestat;
int mplen = strlen(SD_MOUNT_POINT);
int fnlen;
directory = opendir(SD_MOUNT_POINT);
if (directory)
{
printf("\n SD-Card Dir (size/filename/datetime):\n");
while ((entry = readdir(directory)) != NULL)
{
// build full-path filename:
fnlen = strlen(entry->d_name);
char *filename = malloc(mplen + fnlen + 2);
sprintf(filename, "%s/%s", SD_MOUNT_POINT, entry->d_name);
// get file stats:
stat(filename, &filestat);
// need to reuse this variable-len string:
free(filename);
// print dir or file size:
if( S_ISDIR(filestat.st_mode) )
{ printf("%12s ","<DIR>"); }
else
{ printf("%12ld ", filestat.st_size); }
// print filename:
printf("%-64s", entry->d_name);
// file datetime:
printf("%s", ctime(&filestat.st_mtime));
}
closedir(directory);
printf("\n");
}
else
{
printf(" SD-Card Dir Failed\n");
}
vTaskDelay(pdMS_TO_TICKS(1000));
}
I have been having no problem with:
- 8GB level-4 Kingston
- 8GB level-4 PNY
- 32 GB level-10 PNY
and various others