SDcard 1-line error in writing file

davdav
Posts: 208
Joined: Thu Nov 17, 2016 2:33 pm

SDcard 1-line error in writing file

Postby davdav » Mon Apr 23, 2018 9:55 am

Hi everybody,

I'm testing sd_card example (1-line mode). My hardware is:

GPIO2->D0
GPIO15->CMD
GPIO14->CLK
I have also GPIO12 used for CD (not configured) without 10Kohm pullup but since I use 1-line mode it should not be a problem.D1-D2-D3 of SD card are pullup with 10kohm.

I modified the code as below.

Code: Select all

    ESP_LOGI(TAG, "Using SDMMC peripheral");
    sdmmc_host_t host = SDMMC_HOST_DEFAULT();

    // To use 1-line SD mode, uncomment the following line:
    host.flags = SDMMC_HOST_FLAG_1BIT;

    // This initializes the slot without card detect (CD) and write protect (WP) signals.
    // Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
    sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
	//slot_config.gpio_cd = GPIO_NUM_12;
	slot_config.width = 1;

    // GPIOs 15, 2, 4, 12, 13 should have external 10k pull-ups.
    // Internal pull-ups are not sufficient. However, enabling internal pull-ups
    // does make a difference some boards, so we do that here.
	gpio_set_pull_mode(GPIO_NUM_2, GPIO_PULLUP_ONLY);   // D0, needed in 4- and 1-line modes
	gpio_set_pull_mode(GPIO_NUM_15, GPIO_PULLUP_ONLY);  // CMD, needed in 4- and 1- line modes

	//gpio_set_pull_mode(GPIO_NUM_12, GPIO_PULLUP_ONLY);  // CD

#else
    ESP_LOGI(TAG, "Using SPI peripheral");

    sdmmc_host_t host = SDSPI_HOST_DEFAULT();
    sdspi_slot_config_t slot_config = SDSPI_SLOT_CONFIG_DEFAULT();
    slot_config.gpio_miso = PIN_NUM_MISO;
    slot_config.gpio_mosi = PIN_NUM_MOSI;
    slot_config.gpio_sck  = PIN_NUM_CLK;
    slot_config.gpio_cs   = PIN_NUM_CS;
    // This initializes the slot without card detect (CD) and write protect (WP) signals.
    // Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
#endif //USE_SPI_MODE

    // Options for mounting the filesystem.
    // If format_if_mount_failed is set to true, SD card will be partitioned and
    // formatted in case when mounting fails.
    esp_vfs_fat_sdmmc_mount_config_t mount_config = {
        .format_if_mount_failed = true,
        .max_files = 3,
        .allocation_unit_size = 0//16 * 1024
    };

    // Use settings defined above to initialize SD card and mount FAT filesystem.
    // Note: esp_vfs_fat_sdmmc_mount is an all-in-one convenience function.
    // Please check its source code and implement error recovery when developing
    // production applications.
    sdmmc_card_t* card;
    err = esp_vfs_fat_sdmmc_mount("/S", &host, &slot_config, &mount_config, &card);

    ESP_LOGI(TAG, "esp_vfs_fat_sdmmc_mount:%d", err);

    if (err != ESP_OK) {
        if (err == ESP_FAIL) {
            ESP_LOGE(TAG, "Failed to mount filesystem. "
                "If you want the card to be formatted, set format_if_mount_failed = true.");
        } else {
            ESP_LOGE(TAG, "Failed to initialize the card (%s). "
                "Make sure SD card lines have pull-up resistors in place.", esp_err_to_name(err));
        }
        return;
    }

    // Card has been initialized, print its properties
    sdmmc_card_print_info(stdout, card);
Mount is OK, but if I write a file

Code: Select all

ESP_LOGI(TAG, "Opening file");
    FILE* f = fopen("/S/hello.txt", "w");
    if (f != NULL)
    {

        int ret = fwrite("Test file", 1, strlen("Test file"), f);
        fclose(f);
        ESP_LOGI(TAG, "File written:%d", ret);
    }
fwrite returns with number of bytes written (9), but I have these errors

Code: Select all

E (290) sdmmc_cmd: sdmmc_write_sectors_dma: sdmmc_send_cmd returned 0x109
E (300) diskio_sdmmc: sdmmc_write_blocks failed (265)
E (300) sdmmc_cmd: sdmmc_write_sectors_dma: sdmmc_send_cmd returned 0x109
E (310) diskio_sdmmc: sdmmc_write_blocks failed (265)
And the file is only created and it is void.

The info reported by "sdmmc_card_print_info(stdout, card)" function are:

Code: Select all

Name: SU02G
Type: SDSC
Speed: default speed
Size: 1886MB
CSD: ver=0, sector_size=512, capacity=3862528 read_bl_len=10
SCR: sd_spec=2, bus_width=5
Am I wrong in some configuration? I attach the source code in case someone want to test it.

Thanks.
Attachments
sd_card.c
(4.66 KiB) Downloaded 782 times

davdav
Posts: 208
Joined: Thu Nov 17, 2016 2:33 pm

Re: SDcard 1-line error in writing file

Postby davdav » Mon Apr 23, 2018 1:38 pm

Update: I have reduced the max_freq_khz to SDMMC_FREQ_PROBING and now it seems to work. Probably the layout of PCB prevents to go at 20MHz, but for our project it is OK to go slower.

Now another question arise:CD (card detect) pin. I'm using GPIO_12 and setup CD as

Code: Select all

slot_config.gpio_cd = GPIO_NUM_12;
gpio_set_pull_mode(GPIO_NUM_12, GPIO_PULLUP_ONLY); 
because the SD card reader switch is grounded.

I expected driver takes care to inform when SD is pulled off, but in reality if I extract and insert the sd card, it didn't do anything.

How to use the CD (in sdmmc mode)?

Thanks

ESP_igrr
Posts: 2067
Joined: Tue Dec 01, 2015 8:37 am

Re: SDcard 1-line error in writing file

Postby ESP_igrr » Mon Apr 23, 2018 2:07 pm

We have some fixes for CD and WP pin handling in review at the moment. Please wait a couple of days, they should fix the issue you see with CD.

davdav
Posts: 208
Joined: Thu Nov 17, 2016 2:33 pm

Re: SDcard 1-line error in writing file

Postby davdav » Mon Apr 23, 2018 2:34 pm

OK @ESP_igrr, I will check commit on github. Thanks.

Who is online

Users browsing this forum: awegel and 122 guests