Skip to content

Poor SD read speed of fread() (IDFGH-903) #3249

Description

@keelung-yang

I'm using the last master branch since fopen() failed in v3.3 while opening large file which more than 7MB.

Test results on Kingston 32GB 90MB/s TF(Micro SD) Class10 UHS-I:

esp_err_t sd_connect()
{
    // On esp-idf v4.0-dev-225-g2f8b6cfc7
    //      SPI SDMMC_FREQ_DEFAULT   :   585 KB/s
    //      SPI SDMMC_FREQ_HIGHSPEED :   594 KB/s
    //      MMC(1-line) SDMMC_FREQ_DEFAULT   :   673 KB/s
    //      MMC(1-line) SDMMC_FREQ_HIGHSPEED :   775 KB/s
#if 0
    ESP_LOGI(LOG_TAG, "Using SPI peripheral");
    sdmmc_host_t host = SDSPI_HOST_DEFAULT();
    sdspi_slot_config_t slot_config = SDSPI_SLOT_CONFIG_DEFAULT();
#else
    ESP_LOGI(LOG_TAG, "Using SDMMC peripheral");
    sdmmc_host_t host = SDMMC_HOST_DEFAULT();
    sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
    slot_config.width = 1;
#endif
    // host.max_freq_khz = SDMMC_FREQ_HIGHSPEED;
    // ...
}

And my test code:

int64_t sd_read_file(const char * path, uint8_t * buf, long int pos, size_t size)
{
    FILE * file = NULL;
    int ret = sd_cached_file(path, &file);  // To avoid call fopen() frequently
    if (ret != 0)
        return ret;
    
    ret = fseek(file, pos, SEEK_SET);
    if (ret != 0)
        return ret > 0 ? -ret : ret;
    else
        return fread(buf, 1, size, file);
}

double sd_test_read(const char * path, size_t blocksize)
{
    uint8_t * buf = (uint8_t *)malloc(blocksize);
    if (buf == NULL)
        return -1.0;
    
    int64_t bytes = 0;
    int64_t from = esp_timer_get_time();
    for (int64_t i = 0; ; i += blocksize)
    {
        int64_t ret = sd_read_file(path, buf, i, blocksize);
        if (ret > 0)
            bytes += ret;
        else
            break;
    }
    int64_t to = esp_timer_get_time();

    free(buf);
    
    return 1000000.0 * bytes / (to - from);
}

double speed = sd_test_read("/sdcard/37m.zip", 64*1024);
printf("---------------SD SPEED: %.2f KB/s\n", speed/1000);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions