problem with sd in esp32

souler
Posts: 4
Joined: Sun May 26, 2024 12:01 am

problem with sd in esp32

Postby souler » Thu Jun 20, 2024 12:33 am

Hello, I have a problem connecting a microsd to an esp32.

I have these declaration at the beginning of my code

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

#include "esp_log.h"
#include "sdmmc_cmd.h"
#include "esp_vfs_fat.h"
#include "driver/sdmmc_host.h"
#include "driver/sdspi_host.h"
#include "driver/sdmmc_types.h"
#include "esp_err.h" 
#include "hal/spi_types.h"
#include "driver/spi_common.h" 
#include "driver/spi_master.h"
#include "driver/gpio.h"



#define PIN_MISO 12
#define PIN_MOSI 13
#define PIN_SCK 14
#define PIN_CS 15

#define MAX_CHAR_SIZE 100
#define TAG "SD_CARD"
#define TARJETA_SD "/sdcard"



esp_err_t initSD();

and sd funtion

Code: Select all

esp_err_t initSD(){

  sdmmc_card_t* card;
   esp_err_t ret;


  esp_vfs_fat_sdmmc_mount_config_t mount_conf = {

    .format_if_mount_failed = false,
    .max_files = 5,
    .allocation_unit_size =  16 * 1024

  };

 sdmmc_host_t host = SDSPI_HOST_DEFAULT();


    spi_bus_config_t buscfg = {
        .miso_io_num = PIN_MISO,
        .mosi_io_num = PIN_MOSI,
        .sclk_io_num = PIN_SCK,
        .quadwp_io_num = -1,
        .quadhd_io_num = -1,
        .max_transfer_sz = 4096
    };


     ret = spi_bus_initialize(host.slot, &buscfg, SDSPI_DEFAULT_DMA);
    if (ret != ESP_OK) {
        printf("\nERROR SPI BUS\n");
        return ret;
    }  
    

  
    sdspi_device_config_t slot_config = {
        .host_id = host.slot,
        .gpio_cs = PIN_CS,
        .gpio_cd = -1,
        .gpio_wp = -1,
        .gpio_int = -1,
    };
 
     
 
  ret = esp_vfs_fat_sdspi_mount("/sdcard", &host, &slot_config, &mount_conf, &card);

  if(ret != ESP_OK){

    if(ret == ESP_FAIL){
      printf("\nESP_FAIL ERROR: 0x%x\n", ret);

    }
    else
    {
        printf("\nERROR ret: 0x%x\n", ret);


    }
    return ESP_FAIL;

  }
sdmmc_card_print_info(stdout, card);

  return ESP_OK;

}

I am using the wokwi simulator, I don't know if it is a problem with the simulator or something is wrong with me, I get these errors:

Code: Select all

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:1156
load:0x40078000,len:11456
ho 0 tail 12 room 4
load:0x40080400,len:2972
entry 0x400805dc
[0;31mE (876) sdmmc_sd: sdmmc_init_sd_if_cond: send_if_cond (1) returned 0x108[0m
[0;31mE (876) vfs_fat_sdmmc: sdmmc_card_init failed (0x108).[0m

ERROR ret: 0x108

 >Failed to boot sd<

Failed to open file for writing
Error file create

Failed to open file for reading
Error read
imagen_2024-06-19_171834328.png
imagen_2024-06-19_171834328.png (57.82 KiB) Viewed 326 times
I know I need to add to SD library in Wokwi Library Manager, my project already has it.
I used to have a project similar to this that did work but I don't know if it's a problem now because of wokwi or if I need to change something.

In espressif the error 0x108 is: ESP_ERR_INVALID_RESPONSE (0x108): Received response was invalid

Horvat
Posts: 11
Joined: Thu May 02, 2024 8:40 am

Re: problem with sd in esp32

Postby Horvat » Fri Jun 21, 2024 11:26 am

I don't see any obvious errors in your code, therefore probably a Wokvi issue.

If you have been using Wokvi for a while, can you give us comparison of Wokvi and real HW for development?
- compilation time?
- error messages?
- debugging?
- etc?
Thx.

Who is online

Users browsing this forum: No registered users and 61 guests