SD card - random write delays

ghost07
Posts: 36
Joined: Mon Oct 03, 2022 11:47 am

SD card - random write delays

Postby ghost07 » Wed Oct 04, 2023 2:43 pm

Hello, I've noticed random/unpredictable longer write time onto SD card and would like to know why it happens.

Normally, to write 4096 byte block usually takes 4-5 ms, so the speed is 0.70 MB/s in average.
However, sometimes it takes up to 50 ms.

At first I thought it is because FAT library is allocating new sector for file, but it occurs for raw sdmmc_write_sectors() too.

Anyone know what could cause these long delays?
Could it be not-well optimized SDSPI driver, or more like SD card controller performing some erase cycles?

Is there any way to prevent it? Like slow format SD card or something?

Write test results:

512 byte blocks:

Code: Select all

Test write (2MB | 512 bytes chunks) | min: 1.475 ms | max: 52.959 ms | total: 9025 ms (0.23 MB/s)
Test write (2MB | 512 bytes chunks) | min: 1.479 ms | max: 53.115 ms | total: 8249 ms (0.25 MB/s)
Test write (2MB | 512 bytes chunks) | min: 1.499 ms | max: 53.282 ms | total: 8861 ms (0.24 MB/s)
Test write (2MB | 512 bytes chunks) | min: 1.503 ms | max: 52.962 ms | total: 8861 ms (0.24 MB/s)
2048 byte blocks:

Code: Select all

Test write (2MB | 2048 bytes chunks) | min: 2.684 ms | max: 29.506 ms | total: 3447 ms (0.61 MB/s)
Test write (2MB | 2048 bytes chunks) | min: 2.663 ms | max: 33.304 ms | total: 3718 ms (0.56 MB/s)
Test write (2MB | 2048 bytes chunks) | min: 2.687 ms | max: 21.590 ms | total: 3676 ms (0.57 MB/s)
Test write (2MB | 2048 bytes chunks) | min: 2.670 ms | max: 20.062 ms | total: 3496 ms (0.60 MB/s)
4096 byte blocks:

Code: Select all

Test write (2MB | 4096 bytes chunks) | min: 4.219 ms | max: 12.968 ms | total: 2703 ms (0.78 MB/s)
Test write (2MB | 4096 bytes chunks) | min: 4.233 ms | max: 21.813 ms | total: 2827 ms (0.74 MB/s)
Test write (2MB | 4096 bytes chunks) | min: 4.236 ms | max: 29.091 ms | total: 2722 ms (0.77 MB/s)
Test write (2MB | 4096 bytes chunks) | min: 4.234 ms | max: 11.089 ms | total: 2671 ms (0.79 MB/s)
8192 byte blocks:

Code: Select all

Test write (2MB | 8192 bytes chunks) | min: 7.381 ms | max: 34.616 ms | total: 2473 ms (0.85 MB/s)
Test write (2MB | 8192 bytes chunks) | min: 7.353 ms | max: 34.327 ms | total: 2375 ms (0.88 MB/s)
Test write (2MB | 8192 bytes chunks) | min: 7.360 ms | max: 17.250 ms | total: 2330 ms (0.90 MB/s)
Test write (2MB | 8192 bytes chunks) | min: 7.358 ms | max: 27.664 ms | total: 2331 ms (0.90 MB/s)
SDMMC sector write (bypass FatFS):

Code: Select all

Test write (2MB | 4096 bytes chunks) | min: 4.089 ms | max: 20.760 ms | total: 2363 ms (0.89 MB/s)
Test write (2MB | 4096 bytes chunks) | min: 4.075 ms | max: 8.001 ms | total: 2325 ms (0.90 MB/s)
Test write (2MB | 4096 bytes chunks) | min: 4.075 ms | max: 20.277 ms | total: 2341 ms (0.90 MB/s)
Test write (2MB | 4096 bytes chunks) | min: 4.082 ms | max: 11.014 ms | total: 2342 ms (0.90 MB/s)
Btw, on PC the write speed to the same SD card is about 10 MB/s, so there is a room for improvement on ESP32 ;-)
But, yeah, thats probably because PC uses 4-bit mode or something...

MicroController
Posts: 1552
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: SD card - random write delays

Postby MicroController » Wed Oct 04, 2023 6:35 pm

or more like SD card controller performing some erase cycles?
That may be the case.
Is there any way to prevent it? Like slow format SD card or something?
Preemptively performing an ERASE command (CMD32+CMD33+CMD38) on all (unallocated) sectors of the card may mitigate the issue. It's what the TRIM command is for SSDs. However, if and how the card will honor ERASE commands is at the discretion of the card's controller.

ghost07
Posts: 36
Joined: Mon Oct 03, 2022 11:47 am

Re: SD card - random write delays

Postby ghost07 » Wed Oct 11, 2023 3:01 pm

So... there is probably no guaranteed method to prevent these delays.

After some googling I've found a few posts, they call them "wear-leveling delays", and they are SD card inherent - each SD card can handle it differently.

So the only solution seems to be a buffer big enough to outlast accidental delays while new samples keep arrive.

However it would be nice to at least know how long it can be at max and how often in can happen - worst case scenario. But yeah - SD card inherent - and manufactures usually don't provide detailed datasheets with such information.

Thanks anyway

Who is online

Users browsing this forum: Bing [Bot], ShinyGlossy and 314 guests