turgu1 / ESP-IDF-InkPlate

A porting effort to the ESP-IDF framework for the e-Radionica InkPlate software.
15 stars 9 forks source link

Setup SD Card fails on stable version of ESP-IDF 4.2 #1

Closed mzyy94 closed 3 years ago

mzyy94 commented 3 years ago

Summary

SD card initialization fails with an error code ESP_ERR_INVALID_STATE on Inkplate 6.

Environment

PACKAGES: 
 - framework-espidf 3.40200.210118 (4.2.0) 
 - tool-cmake 3.16.4 
 - tool-esptoolpy 1.30000.201119 (3.0.0) 
 - tool-ninja 1.9.0 
 - toolchain-esp32ulp 1.22851.191205 (2.28.51) 
 - toolchain-xtensa32 2.80400.210211 (8.4.0)

Reproduction Code

All code using this library with ESP-IDF 4.2, such as examples/Others/Inkplate_Maze_Generator

Reproduction step

  1. Insert microSD Card into Inkplate 6.
  2. Open Inkplate_Maze_Generator with PlatformIO.
  3. Build and upload the example.
  4. Monitor serial console.
  5. An SD Card setup error has occurred at startup.
I (1553) SDCard: Setup SD card
E (1553) vfs_fat_sdmmc: slot init failed (0x103).
E (1553) SDCard: Failed to setup the SD card (ESP_ERR_INVALID_STATE).
Log
> Executing task: pio device monitor --environment inkplate-6 <

--- Available filters and text transformations: colorize, debug, default, direct, esp32_exception_decoder, hexlify, log2file, nocontrol, printable, send_on_enter, time
--- More details at http://bit.ly/pio-monitor-filters
--- Miniterm on /dev/cu.usbserial-1440  115200,8,N,1 ---
--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
ets Jun  8 2016 00:22:57

rst:0x1 (POWERON_RESET),boot:0x33 (SPI_FAST_FLASH_BOOT)
flash read err, 1000
ets_main.c 371 
ets Jun  8 2016 00:22:57

rst:0x10 (RTCWDT_RTC_RESET),boot:0x33 (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:4
load:0x3fff0034,len:7280
ho 0 tail 12 room 4
load:0x40078000,len:13664
load:0x40080400,len:4632
entry 0x400806f4
[0;32mI (31) boot: ESP-IDF 3.40200.210118 2nd stage bootloader[0m
I (31) boot: compile time 18:47:22
I (31) boot: chip revision: 1
0;32mI (35) boot_comm: chip revision: 1, min. bootloader chip revision: 0
I (44) boot.esp32: SPI Speed      : 40MHz[0m
I (47) boot.esp32: SPI Mode       : DIO
I (51) boot.esp32: SPI Flash Size : 4MB
I (56) boot: Enabling RNG early entropy source...
I (61) boot: Partition Table:
I (65) boot: ## Label            Usage          Type ST Offset   Length
I (72) boot:  0 nvs              WiFi data        01 02 00009000 00004000
[0;32mI (79) boot:  1 otadata          OTA data         01 00 0000d000 00002000
I (87) boot:  2 phy_init         RF data          01 01 0000f000 00001000
I (94) boot:  3 factory          factory app      00 00 00010000 00150000
I (102) boot:  4 ota_0            OTA app          00 10 00160000 00150000
I (109) boot:  5 ota_1            OTA app          00 11 002b0000 00150000
I (117) boot: End of partition table
[0;32mI (121) boot: Defaulting to factory image
I (126) boot_comm: chip revision: 1, min. application chip revision: 0
I (133) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x22b64 (142180) map
[0;32mI (201) esp_image: segment 1: paddr=0x00032b8c vaddr=0x3ffb0000 size=0x02870 ( 10352) load
I (206) esp_image: segment 2: paddr=0x00035404 vaddr=0x40080000 size=0x00404 (  1028) load
I (209) esp_image: segment 3: paddr=0x00035810 vaddr=0x40080404 size=0x0a808 ( 43016) load
[0;32mI (238) esp_image: segment 4: paddr=0x00040020 vaddr=0x400d0020 size=0x557a8 (350120) map[0m
[0;32mI (384) esp_image: segment 5: paddr=0x000957d0 vaddr=0x4008ac0c size=0x08b00 ( 35584) load
[0;32mI (412) boot: Loaded app from partition at offset 0x10000[0m
I (412) boot: Disabling RNG early entropy source...[0m
I (413) psram: This chip is ESP32-D0WD
I (418) spiram: Found 64MBit SPI RAM device
I (422) spiram: SPI RAM mode: flash 40m sram 40m
I (427) spiram: PSRAM initialized, cache is in low/high (2-core) mode.
I (434) cpu_start: Pro cpu up.
I (438) cpu_start: Application information:0m
I (443) cpu_start: Project name:     Inkplate_Maze_Generator
I (449) cpu_start: App version:      7004c73-dirty
I (455) cpu_start: Compile time:     Mar  2 2021 18:46:25
I (461) cpu_start: ELF file SHA256:  52cfd1b0a0bff91e...
I (467) cpu_start: ESP-IDF:          3.40200.210118
I (472) cpu_start: Starting app cpu, entry point is 0x40082b18
I (0) cpu_start: App cpu up.
[0;32mI (1364) spiram: SPI SRAM memory test OK
I (1365) heap_init: Initializing. RAM available for dynamic allocation:
I (1365) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (1371) heap_init: At 3FFB5D88 len 0002A278 (168 KiB): DRAM
I (1377) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAMm
I (1384) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (1390) heap_init: At 4009370C len 0000C8F4 (50 KiB): IRAM
I (1397) cpu_start: Pro cpu start user code
I (1401) spiram: Adding pool of 4096K of external SPI memory to heap allocator
[0;32mI (1464) spi_flash: detected chip: gd
I (1465) spi_flash: flash io: dio
[0;32mI (1465) cpu_start: Starting scheduler on PRO CPU.
[0;32mI (0) cpu_start: Starting scheduler on APP CPU.
2mI (1473) spiram: Reserving pool of 32K of internal memory for DMA/internal allocations
D (1483) Wire: Initializing...
D (1483) EInk6: Initializing...
D (1483) Wire: Initializing...
D (1513) EInk6: MCP initialized.[0m
D (1513) EInk6: Memory allocation for frame/bitmap buffers.
D (1523) EInk6: d_memory_new: 3f849424 p_buffer: 3f857e98.
[0;32mI (1553) SDCard: Setup SD card
E (1553) vfs_fat_sdmmc: slot init failed (0x103).
E (1553) SDCard: Failed to setup the SD card (ESP_ERR_INVALID_STATE).
D (1653) Graphics: Update 1Bit frame buffer
D (1653) EInk6: 1bit Update...
[0;32mI (3103) Maze: Completed...

Details

An error vfs_fat_sdmmc: slot init failed (0x103). is caused by init_sdmmc_host() API call in sdmmc initialization code block at line https://github.com/espressif/esp-idf/blob/v4.2/components/fatfs/vfs/vfs_fat_sdmmc.c#L233 .

    if (host_config->flags == SDMMC_HOST_FLAG_SPI) {
        //Deprecated API
        //the init() function is usually empty, doesn't require any deinit to revert it
        err = (*host_config->init)();
        CHECK_EXECUTE_RESULT(err, "host init failed");
        err = init_sdspi_host_deprecated(host_config->slot, slot_config, &card_handle);
        CHECK_EXECUTE_RESULT(err, "slot init failed");
        //Set `host_inited` to true to indicate that host_config->deinit() needs
        //to be called to revert `init_sdspi_host_deprecated`; set `card_handle`
        //to -1 to indicate that no other deinit is required.
        host_inited = true;
        card_handle = -1;
    } else {
        err = (*host_config->init)();
        CHECK_EXECUTE_RESULT(err, "host init failed");
        //deinit() needs to be called to revert the init
        host_inited = true;
        //If this failed (indicated by card_handle != -1), slot deinit needs to called()
        //leave card_handle as is to indicate that (though slot deinit not implemented yet.
        err = init_sdmmc_host(host_config->slot, slot_config, &card_handle);
        CHECK_EXECUTE_RESULT(err, "slot init failed");
    }

The API is not for sdspi but for sdmmc, but the default value of host_config->flags set by SDSPI_HOST_DEFAULT() has been changed in ESP-IDF 4.2, so it is called and failed. The block is not for sdspi but for sdmmc, so modifying the code not to reach here will solve the issue. One solution is to add a single line of code in order to re-set the same value as in ESP-IDF 4.1, like below:

host.flags = SDMMC_HOST_FLAG_SPI;
turgu1 commented 3 years ago

Hello Yuki,

Thanks for the info and the patch. I've not tried ESP-IDF 4.2 yet, but I will in the following hours.