Very slow write speed in SDMMC 1 bit mode on ESP32 s3

Radu79
Posts: 31
Joined: Tue Nov 17, 2020 9:50 pm

Very slow write speed in SDMMC 1 bit mode on ESP32 s3

Postby Radu79 » Sat May 10, 2025 1:25 am

I have an esp32-s3 wroom camera board. I have an Arduino program that takes a picture and save sit on the SD card. Nothing really fancy. It works well, it saves the picture in 1-2 seconds. I didn't time the write speed, but I would estimate 500KBps or higher. I ported it to esp-idf and I am having some issues. Using esp32-idf 5.5.0, the write speed is VERY slow, around 150 KBps. I tried various things, like using different buffer sizes, different clock speeds (40 and 80 mhz) and so on. Nothing works.

Here is the init code:

Code: Select all

static void init_sdmmc(void) {
    int64_t t_start = esp_timer_get_time();
    esp_err_t err;

    sdmmc_host_t host = SDMMC_HOST_DEFAULT();
    host.max_freq_khz = 80000; // 80 MHz
    host.flags = SDMMC_HOST_FLAG_1BIT; // 1-bit mode

    sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
    slot_config.clk = 39;
    slot_config.cmd = 38;
    slot_config.d0 = 40;
    slot_config.width = 1;

    esp_vfs_fat_sdmmc_mount_config_t mount_config = {
        .format_if_mount_failed = false,
        .max_files = 5,
        .allocation_unit_size = 16 * 1024
    };

    sdmmc_card_t *card;
    err = esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card);
    int64_t t_end = esp_timer_get_time();
    if (err != ESP_OK) {
        printf("[%lld ms] SDMMC mount a dat chix, bă! Eroare: 0x%x\n", t_end / 1000, err);
        return;
    }
    printf("[%lld ms] SDMMC montat, frate! Durată: %lld ms, Frecvență cerută: %d kHz, Frecvență reală: %d kHz\n", 
           t_end / 1000, (t_end - t_start) / 1000, host.max_freq_khz, card->host.max_freq_khz);
}
Any ideas?

ArinaSinH
Posts: 3
Joined: Thu May 08, 2025 8:34 pm

Re: Very slow write speed in SDMMC 1 bit mode on ESP32 s3

Postby ArinaSinH » Sat May 10, 2025 8:01 pm

The config looks ok, no problems there.

First, if you absolutely need the performance and have the pins to spare, I suggest you switch to 4 bit sdio it should 2x to 3x the speed. If you cannot/do not want to, try using SPI instead to see if anything changes.

Still, 150KB/s is really slow, with a good card you should be able to reach the low MB/s. How exaclty are you writting the file? Is it a libraries black magic? are you writting a buffer of pixels yourself?
Its very important for performance to do few, but big operations, so if either you or a library are writting the file in small buffers, that could be the culprit. Also the interface used to interact with the file system can affect performance. In my testing using FILE from the C interface is 10-15% slower than using FatFS and f_write

Radu79
Posts: 31
Joined: Tue Nov 17, 2020 9:50 pm

Re: Very slow write speed in SDMMC 1 bit mode on ESP32 s3

Postby Radu79 » Mon May 12, 2025 9:13 am

Thanks for replying!

I am using fwrite, nothing fancy.
I can't use 4bit mode, the board has an SD slot that is hardwired to 1bit mode. it's a camera board, so not a lot of GPIOs to spare :)

Code: Select all

static void capture_and_save_photo(void) {
    int64_t t_start = esp_timer_get_time();
    camera_fb_t *fb = esp_camera_fb_get();
    int64_t t_capture = esp_timer_get_time();
    if (!fb) {
        printf("[%lld ms] Eroare la captură, pula mea!\n", t_capture / 1000);
        return;
    }
    printf("[%lld ms] Cadru capturat, mărime: %u bytes, Durată captură: %lld ms\n", 
           t_capture / 1000, fb->len, (t_capture - t_start) / 1000);

    int64_t t_save_start = esp_timer_get_time();
    char path[32];
    snprintf(path, sizeof(path), "/sdcard/%05d.jpg", cur_pic);

    FILE *file = fopen(path, "wb");
    if (file) {
        setvbuf(file, NULL, _IOFBF, 16 * 1024); // Buffer 128 KB
        fwrite(fb->buf, 1, fb->len, file);
        fclose(file);
        int64_t t_save_end = esp_timer_get_time();
        printf("[%lld ms] Poză salvată: %s, Durată salvare: %lld ms\n", 
               t_save_end / 1000, path, (t_save_end - t_save_start) / 1000);
    } else {
        printf("[%lld ms] Eroare la salvare pe SD, pula mea!\n", esp_timer_get_time() / 1000);
    }

    esp_camera_fb_return(fb);
    cur_pic++;
}

Who is online

Users browsing this forum: ChatGPT-User, meta-externalagent, Qwantbot and 7 guests