ESP32 4Bit SD-Mode Init problem

Any bugs you encounter with Flowcode should be discussed here.
stefan.erni
Valued Contributor
Posts: 1025
http://meble-kuchenne.info.pl
Joined: Wed Dec 02, 2020 10:53 am
Has thanked: 194 times
Been thanked: 218 times

Re: ESP32 4Bit SD-Mode Init problem

Post by stefan.erni »

Hi Ben

Here a program to test the SD-Card:
Press a key to start (GPIO19 on my board)
The program write 10'000 line in a .csv file
Its write a string of 128Byte for each line (21 times a fix number 10000,)
If writing is finish it's write the time in line 10'001 in mSec and it send the time to USB
2025-06-10_12-05-58.PNG
2025-06-10_12-05-58.PNG (5 KiB) Viewed 965 times
Test_post_sd_0525.fcfx
(33.22 KiB) Downloaded 16 times
I tested with different setting for the card
the setting from the clock to 1Mhz, 20Mhz, 80Mhz is no difference
the setting 1Bit or 4Bit mode=> 1Bit=2935mSec 4Bit=2551mSec :)

My wish is that the writing of 128Byte always takes the same time or that there is not such a big difference to write the string

test shows the time to write for 128Byte, you van see every 4te string is much longer ( with a 1mSec delay on)
here the 4te string
2025-06-10_11-28-39.PNG
2025-06-10_11-28-39.PNG (126.49 KiB) Viewed 965 times
and sometimes it's much longer...
2025-06-10_11-29-44.PNG
2025-06-10_11-29-44.PNG (196.96 KiB) Viewed 965 times

mnfisher
Valued Contributor
Posts: 1550
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 138 times
Been thanked: 737 times

Re: ESP32 4Bit SD-Mode Init problem

Post by mnfisher »

Hi Stefan,

Could you try 'buffering' the output and writing in larger blocks - probably a multiple of a sector size would be best.
Try 1 sector 10x and 100x for example and see how the times look - there should be less 'overhead' for a larger buffer size, but whether it will translate to a meaningful speed increase is another matter?

Martin

stefan.erni
Valued Contributor
Posts: 1025
Joined: Wed Dec 02, 2020 10:53 am
Has thanked: 194 times
Been thanked: 218 times

Re: ESP32 4Bit SD-Mode Init problem

Post by stefan.erni »

Hi Martin, Hi Ben

Yes I have a second program to write two buffer (array) of 12000 integer to wave file. Its working
The problem is in the csv Version to write csv file.
It's very easy to sample and save if I reduce the samplerate.
but if this different time would be less, the csv version would just work without buffer and second task/core

compare:
csv version
2025-06-13_11-59-06.PNG
2025-06-13_11-59-06.PNG (159.61 KiB) Viewed 923 times

wave version
2025-06-13_12-19-01.PNG
2025-06-13_12-19-01.PNG (462.28 KiB) Viewed 923 times

mnfisher
Valued Contributor
Posts: 1550
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 138 times
Been thanked: 737 times

Re: ESP32 4Bit SD-Mode Init problem

Post by mnfisher »

Just tested on the p4 - and writing 512bytes (1 sector takes) ~20ms. This is fairly consistent - with very occasional 'longer' writes. However - I found I needed a delay between notifications to the 'Write' task - of 40ms (with blocks being missed - so Write outputs 01010101(....) if writing correctly and 01101011(...) if blocks are 'missed' (and notifications missed)

Changing to 1 bit mode - a sector takes ~19ms (!) - but there are more 'slow' blocks (and they are longer)

This is using a class 10 micro SD card - so it seems rather slow. Its writing ~12.5KB per second. I'm not sure the speed of the p4 is conferring any advantage over a standard esp32 (but I hadn't managed to get 1 or 4 wire working on this - perhaps need something with an SD slot built in like the esp32-cam?) There is an 'ADC' mode in the sdmmc driver- I haven't tested this - and not sure what benefits (if any) it would give.

I also tried a 2048 byte buffer - but it took 4 x as long to write :-)

The pins here are probably specific (and are in supplementary code) - and I had to enable LDO power to get things to work. Another oddity - opening a file fails if it already exists - hence the odd file name!

The easiest speed up would be to write the data as binary - rather than a string - and use the minimum data size possible (for example if a value can only be 0..99 (%) - then use a byte rather than a word or long word - and then decode the data on the host.
Attachments
ESP_SD_TEST.fcfx
(22.35 KiB) Downloaded 11 times

mnfisher
Valued Contributor
Posts: 1550
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 138 times
Been thanked: 737 times

Re: ESP32 4Bit SD-Mode Init problem

Post by mnfisher »

Writing to the card in the main loop seems to write much more rapidly - with a sector taking between 1 nd 9ms - and obviously no delay between writes - and writing 1000 sectors taking 1.05 s - which is better!

mnfisher
Valued Contributor
Posts: 1550
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 138 times
Been thanked: 737 times

Re: ESP32 4Bit SD-Mode Init problem

Post by mnfisher »

Just tested cranking up the priority of the separate task 'writer' - and it seems to work much more reliably - with a sector taking 1.3ms to write.

I'd also reformatted the card (fully - not quick) - which solved the issues with overwriting files, and might also be something to do with the sped increase?

Martin

stefan.erni
Valued Contributor
Posts: 1025
Joined: Wed Dec 02, 2020 10:53 am
Has thanked: 194 times
Been thanked: 218 times

Re: ESP32 4Bit SD-Mode Init problem

Post by stefan.erni »

Hi Martin, Hi Ben

If it is not possible to improve the times for block writing,
I'll test the version with the additional task in more detail.
Maybe I can also find a simple solution to store the strings in two buffer's and then write them to the sd-card alternating.

This is still a problem:
I tested with different clock setting for the card,
from the clock setting 1Mhz, to 80Mhz is no difference in time
Can this be improved?

mnfisher
Valued Contributor
Posts: 1550
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 138 times
Been thanked: 737 times

Re: ESP32 4Bit SD-Mode Init problem

Post by mnfisher »

How fast is your device writing? How long does a sector take to write? I thought the 512kB/s I got was okay - but it's not really matching the cards potential...

Martin

stefan.erni
Valued Contributor
Posts: 1025
Joined: Wed Dec 02, 2020 10:53 am
Has thanked: 194 times
Been thanked: 218 times

Re: ESP32 4Bit SD-Mode Init problem

Post by stefan.erni »

Hi Martin

I testet the time to write one buffer (12000Byte) with the Oscilloscope with pinned to core 0 or core 1


pinned to core
0/1
to write a buffer with 12kByte it need
336mSec/380mSec

per second
35.7kByte/31.5kByte/

power consume Board
75-90mA/75-110mA

C-Code
Attachments
2025-06-17_16-11-32.PNG
2025-06-17_16-11-32.PNG (37.48 KiB) Viewed 802 times

stefan.erni
Valued Contributor
Posts: 1025
Joined: Wed Dec 02, 2020 10:53 am
Has thanked: 194 times
Been thanked: 218 times

Re: ESP32 4Bit SD-Mode Init problem

Post by stefan.erni »

Hi Martin, Hi Ben

I set the clock to 80mHz and it's working. The time from the IRQ is still perfect.
I can save power and it becomes a quite little less warm.

Witch 80Mhz CPU clock to write to the disk is much slower and I can change the clock from the SD-Card but no change in the time.
I suspect that something is still not perfect.

but otherwise good values:
pinned to core 0
to write a buffer with 12kByte it need
934mSec
per second
12.8kByte/
power consume Board
40-49mA

Post Reply