Author Topic: Where does f_write store data  (Read 1886 times)

0 Members and 1 Guest are viewing this topic.

Offline zer0c00lTopic starter

  • Contributor
  • Posts: 27
  • Country: us
Where does f_write store data
« on: February 10, 2021, 05:53:38 pm »
I am using FatFS library with an LPC2368 MCU to store data 8 times per second on an industrial grade 2GB microSD card. I use f_write to store the data with ocassional f_sync every 30 minutes. The file is closed using f_close when the user stops logging. Everything works normally. My question is on the working of f_write. I have read that fwrite (the normal linux equivalent) writes data to a buffer, and only writes to a disk on fsync or fclose. I can understand that model because computer have large amounts of ram and fwrite probably saves data to a buffer in memory and when an fsync is done it stores that data to the harddrive/ssd. But how does this model work with microcontrollers?. Using Elm Chan FatFS library if I keep on writing using f_write, and never f_sync, the file on the sdcard never gets updated with the latest data. Where is the data going in this case?. The MCU don't really have large amounts of RAM to buffer this data. Is it still writing to the sdcard, but not updating the file system to reflect the change?, and only updaing it on f_sync/f_close?. I set up a test where the data being written was also being sent to the terminal, and after recording 200MB of data I stopped but did not f_sync or f_close. The file on the SD card was empty. Where did the 200MB of data go?. Clearly the MCU doesn't have 200MB or ram to buffer it.
 

Offline nctnico

  • Super Contributor
  • ***
  • Posts: 30294
  • Country: nl
    • NCT Developments
Re: Where does f_write store data
« Reply #1 on: February 10, 2021, 06:05:15 pm »
The data is likely on the SD card but the FAT isn't updated. AFAIK FatFS does no buffering at all and so I have implemented my own buffering when using FatFS to write small chunks of data in order to prevent wear of the SD card. If FatFS uses a buffer then it needs some way to allocate it and IIRC it doesn't have a way to do it. Not updating to the FAT for every write is a good precaution because the FAT area is written often and since SDs cards have no wear levelling the sectors containing the FAT will wear out the soonest.
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 

Offline Foxxz

  • Regular Contributor
  • *
  • Posts: 139
  • Country: us
Re: Where does f_write store data
« Reply #2 on: February 10, 2021, 08:03:52 pm »
The data is likely on the SD card but the FAT isn't updated. AFAIK FatFS does no buffering at all and so I have implemented my own buffering when using FatFS to write small chunks of data in order to prevent wear of the SD card. If FatFS uses a buffer then it needs some way to allocate it and IIRC it doesn't have a way to do it. Not updating to the FAT for every write is a good precaution because the FAT area is written often and since SDs cards have no wear levelling the sectors containing the FAT will wear out the soonest.

I was going to suggest this same approach. Allocate your own buffer and keep track of how full it is and if the next write will overflow it. If so, f_write your buffer, reset it to zero, and put the new data into it.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

-->