Problem writing to card in SPI mode

frankiPL
Posts: 9
Joined: Thu Nov 16, 2017 8:59 am

Problem writing to card in SPI mode

Postby frankiPL » Sat Nov 18, 2017 9:49 am

Hi

We have problem writing file to SD card, small files up to 1MB usually writes well, bigger files up to 10MB sometimes fails during fwrite.
I have seen this issues: https://github.com/espressif/esp-idf/issues/1089 and https://github.com/espressif/esp-idf/issues/1093 and I have new esp-idf with write timeout for SD card set to:

Code: Select all

#define SDMMC_DEFAULT_CMD_TIMEOUT_MS  1000   // Max timeout of ordinary commands
#define SDMMC_WRITE_CMD_TIMEOUT_MS    5000   // Max timeout of write commands
We receive the file from tcp connection and write it to SD card with a code:

Code: Select all

int total =	10*1024;
char *data = malloc(total);
while(1) {
            bzero(data,total);
            ssize_t sizeRead = recv(clientSock, data , 1024, 0);
            if (sizeRead < 0) {
                break;
            }
            sd_write_file(data,sizeRead);
}
bool sd_write_file(char *buff,int size) {
    int ret=fwrite(buff,1,size,sd_file);
    if(ret!=size) {
        ESP_LOGE(sd_tag,"Write error:%d ret:%d size:%d",ferror(sd_file),ret,size);
        return false;
    }
    else {
        ESP_LOGI(sd_tag,"File write size:%d\n",size);
        return true;
    }
    fflush(sd_file);
}
I have tested card initialization with different options:

Code: Select all

host.max_freq_khz=SDMMC_FREQ_PROBING;
host.max_freq_khz=SDMMC_FREQ_DEFAULT;
Card is recognized as:

Code: Select all

I (49) gpio: GPIO[5]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0 
Name: SA08G
Type: SDHC/SDXC
Speed: default speed
Size: 7386MB
CSD: ver=1, sector_size=512, capacity=15126528 read_bl_len=9
SCR: sd_spec=2, bus_width=5
card is: GOODRAM microSD 8GB cl4 lifetime warranty. We were using this card with esp8266 in earlier project and no problem.

Our GPIO setup is:

Code: Select all

#define PIN_NUM_MISO 19
#define PIN_NUM_MOSI 23
#define PIN_NUM_CLK  18
#define PIN_NUM_CS   5
for testing we are using https://www.dfrobot.com/product-1590.html with http://www.geeetech.com/wiki/index.php/ ... ard_Module also with additional 10nF 100nF 1uF on input 3.3V
We were also adding different pull up resistors between MISO MOSI

It looks like ESP32 is very picky about writing into microSD card in SPI mode.
What should be the correct setup ? I mean software SD card initialization parameters and also resistors and capacitors for SD card module?

Below are a few errors from multiple testes. At the moment files about 6,3MB sometimes writes well, sometimes they fail at the end.
I can provide more data if needed.

Best Regards
Marek

Code: Select all

I (18689) socket_server: Total bytes:463872 left file_size:5868594
E (18769) sdspi_host: sdspi_host_start_command: cmd=24 error=0x107
E (18769) sdmmc_cmd: sdmmc_write_sectors_dma: sdmmc_send_cmd returned 0x107
E (18769) diskio_sdmmc: sdmmc_write_blocks failed (263)
E (18779) sd_card:: Write error:1 ret:0 size:512
I (18779) socket_server: Total bytes:464384 left file_size:5868082

I (167149) socket_server: Total bytes:6259344 left file_size:73122
E (167219) sdspi_host: sdspi_host_start_command: cmd=24 error=0x107
E (167219) sdmmc_cmd: sdmmc_write_sectors_dma: sdmmc_send_cmd returned 0x107
E (167229) diskio_sdmmc: sdmmc_write_blocks failed (263)
E (167229) sd_card:: Write error:1 ret:368 size:512
I (167239) socket_server: Total bytes:6259856 left file_size:72610

I (92199) socket_server: Total bytes:6258712 left file_size:74266
E (92349) sdspi_host: sdspi_host_start_command: cmd=24 error=0x107
E (92349) sdmmc_cmd: sdmmc_write_sectors_dma: sdmmc_send_cmd returned 0x107
E (92359) diskio_sdmmc: sdmmc_write_blocks failed (263)
E (92359) sd_card:: Write error:1 ret:488 size:1024
I (92369) socket_server: Total bytes:6259736 left file_size:73242
E (92369) sd_card:: Write error:1 ret:0 size:1024

I (19919) socket_server: Total bytes:1198080 left file_size:5134898
E (19989) sdspi_host: sdspi_host_start_command: cmd=25 error=0x107
E (19999) sdmmc_cmd: sdmmc_write_sectors_dma: sdmmc_send_cmd returned 0x107
E (19999) diskio_sdmmc: sdmmc_write_blocks failed (263)
E (19999) sd_card:: Write error:1 ret:0 size:1024

I (94569) socket_server: Total bytes:6259636 left file_size:73342
E (94639) sdspi_host: sdspi_host_start_command: cmd=24 error=0x107
E (94639) sdmmc_cmd: sdmmc_write_sectors_dma: sdmmc_send_cmd returned 0x107
E (94649) diskio_sdmmc: sdmmc_write_blocks failed (263)
E (94649) sd_card:: Write error:1 ret:76 size:1024

JanDupal
Posts: 1
Joined: Thu Feb 15, 2018 9:11 am

Re: Problem writing to card in SPI mode

Postby JanDupal » Thu Feb 15, 2018 9:29 am

Hi Marek,

we you able to resolve the issue? We’re having very similar issue — timeouts or CRC fails when writing larger files (about 2 MB) to microSD card in SPI mode. Decreasing write size (1 kB in your case) to 256 B and decreasing SPI clock (via sdmmc_host_t.max_freq_khz) seems to help a bit — still the writes fail in 30 % of our tests.

However this behaviour seems to be related to card model — two Kingston Class 10 HC cards (4 GB and 32 GB) fail, while SanDisk Extreme V3 XC 128 GB card performs flawlessly.

Best regards,
Jan

UPDATE: In the end, we switched from SD SPI to SD/MMC driver and haven’t encountered any issues since then.

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], ok-home and 186 guests