cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F10x + FATFS + SDIO

Niks
Associate II
Posted on September 19, 2016 at 10:22

Hi,

I am working on SDIO(Sandisk 32Gb class 10 sd card) + FATFS. My application code contains two loops one is 50Hz and other is of 1Hz. I am writing 512 bytes in 50Hz loop(i.e. after 20ms delay, flag is becoming true in sysTick handler) and f_sync at 1Hz (calling f_sync to avoid any accidental data loss).

Normally loop rate is fine 46-50 but exactly after 660 seconds my 50Hz loop is dropping to 20/23/17 etc(i have used one counter in 50Hz and the same is stored as one of the parameter on SD card, which is plotted to see the results).

I formatted the SD card with 8k,16k,32k cluster size but i am getting the same results.

Even i tried to increase the number of bytes to 1024,2048 (multiple sectors ) etc and called f_write after 40ms / 100ms. But results are same with loop rates such as 18/20/22 etc exactly after 660 seconds.

Along with this i am also logging the f_sync and f_write timings in milliseconds on SD card, normally it is 7-9ms which is acceptable as it is not hindering my 50Hz loop but exactly after 660 seconds its write time is going to 400ms-450ms.

For exercise purpose i wrote the 512Bytes at 25Hz which gave me the loop rate around 48-50 which is really very good, But then i tried 1024 at 40ms,with this loop rate was dropped to 20/18/23 etc.

I want to write 1024 bytes at 50Hz. Is it possible with the kind of behavior and the kind of application which is explained above?

I have attached the resulting image for reference.

Thanks...!!!

4 REPLIES 4
Posted on September 19, 2016 at 16:31

Continuous use of f_sync is going to be brutal to performance. Decide what a reasonable rate is for data loss not to be an issue, and ideally provide a method for the user to sync rather than assume it is alway sudden disconnection of the card. ie a button

Don't do writes under interrupt, buffer the data, ideally to some aligned level like 32KB, and flush that in it's own thread, with your critical interrupts capable of preempting the SDIO/DMA interrupts.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Niks
Associate II
Posted on September 20, 2016 at 14:11

Hi clive1,

Thanks for reply.

I used 16K buffer to store the data and wrote it to sd card after filling it and did f_sync on user command.

Attached is the result image of the same.

50Hz loop rate drops in between 45- 48 which is fine (f_write takes around 40ms to write 16K data ) but again some times it drops to 20/22 (which is for around 1 second). My application is critical and missing the loop more than 10 will create lot of problems.

One more thing is i can use max buffer of 16K due to limited RAM size of controller. Application itself requires more RAM including global variables, stack and heap.

As buffer size is increasing the time required for writing it to sd card is increasing which in turn reduces my 50Hz loop rate.

while debugging the code i found that f_write in turn disk_write make call to the singleblockwrite() function defined in diskio file. I tried to modify it so that call will be made to multiblockwrite() function, but it works only for some time like 4-5 minutes after that writing to sd card stops.if i stop the writing manually within 4-5 minutes, i can see the written data in sd card(2-3Mb). If i do not stop writing manually, it automatically stops after 4-5 minutes and no any data is written to sd card (0Kb).

I am not able to figure it out through debuggingwhy this is happening.

Thanks

Nikhil

________________

Attachments :

16K_Block.jpg : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HzOf&d=%2Fa%2F0X0000000bLV%2Fc0s7ilPC5QHjX.xkpg51nLRn8PwulbhuWKWEPBAvBAE&asPdf=false
Posted on September 20, 2016 at 17:34

FATFS is going to read/write FAT sectors and directory sectors periodically, and when flushed with f_sync(). You'd want to instrument the read/write to see how these were interacting with respect to your spikes, you'd also want to look at the DMA/SDIO interrupts, and where it is waiting for the FIFO, etc. I generally use F2/F4 parts so don't have recent F1 tests. Make sure it's not timing out loops.

FATFS will also try to group consecutive sectors through f_write, but if you do unaligned accesses to sectors/clusters this is likely precipitate reads/writes to deblock things. But the writing is a pass-thru thing, as it's not holding vast buffers or caching for you.

You must separate the data creation from the data writing, there isn't much reason you can't just capture data in the 50 Hz and always hit that periodicity, writing data to a buffer. I'd probably opt for a ping-pong, or rolling buffer that provided enough headroom for the card writes, and push the f_sync out to some longer periodicity, or size. I would do this outside of the capture task.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Niks
Associate II
Posted on September 23, 2016 at 09:27

I tried to find the reason for stopping the SD MultiBlockwrite() after some time (around 4-5minutes).

It can be DMA error or timeout error as i discussed in some previous posts.

I tried with increasing timeout period (SDIO_TIMOUT*Num_Blocks), then i tried disabling the timeout flag initialized in SD_ITConfig function.

The results are same, writing stops after some time.

One more thing to notice is, the SD_CMD_SET_BLOCK_COUNT (CMD23)command defined in header file of sd card says that its not supported by sd card(meaning it supports mmc cards only).image named with captureis attached here for reference.

I saw this comment in front of that command defined in header file of sd cardin number of files written by different people.

Does it mean that multiblockwrite defined for SDIO is not supported by micro sd cards or it only sopports mmc cards?

I saw in 4-5 minutes of log that with multiblockwrite i could write 16Kb buffer in 10ms which is excellent.But it stops after some time.

Please help me out for this if you find any solution.

Thanks

________________

Attachments :

Capture.JPG : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HtqI&d=%2Fa%2F0X0000000aXh%2Fyh_KTHsvFaApEn7Hfh3kXfVwKSDD.m7wZr9kvuRG8XQ&asPdf=false

en.CD00197763.pdf : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HtqN&d=%2Fa%2F0X0000000aXk%2FiSj6EvjnNXQpFJX_3_FmVpHBFS5ujXVVXehFu0uE2OU&asPdf=false