Forums

Implementing fail/safe method while writing file into SD card

Started by Shubham1006 3 months ago8 replieslatest reply 3 months ago54 views

Hello everyone,

I need a help/guide regarding the file writing in SD card.

I am writing a file into SD card interfaced with TI CC3200 controller which is using FAT FS. The use case is like that, during file write if power is loss, then my data should not corrupt or I can have some mechanism to identify corrupt data in file. So to identify that I need a mechanism that can be implemented through coding.

There are few options in my mind like implementation of  CRC, checksum but it costs controller performance. 

I want to know is there any available method or something from where I can start to implement to identify corrupt data.

Thanks in advance for your suggestions :)

[ - ]
Reply by CustomSargeMay 17, 2019

A simple hardware answer: Schottky diode isolate power of the SD card, CC3200 and any other circuitry in the path. Measure the current draw of that section. Estimate the worst case time to write the file. Size a capacitor to supply power for at least the time required. You may have to add capacitance on the reset line to account for slower power up, if the capacitor affects CC3200 startup.

I designed a position controller that upstream of the diode, power shut off triggered an interrupt. The capacitor supplied enough time to write critical parameters to a flash memory. Next power up caused a check of known flags at start and end of written block to detect for corruption. "Several" power cycle tests found no corruption occurred.

[ - ]
Reply by Shubham1006May 18, 2019

Thanks @CustomSarge,

Hardware solution we have, but currently we are looking for firmware solution.

[ - ]
Reply by mr_banditMay 17, 2019

Simplest method I can think of is write a byte that indicates you started the write sequence, then write a different value to the same byte indicating you have finished. Use a simple circular queue. Only update the index/pointer when you start the write sequence.

This is not foolproof - what happens if you get a power hit during the writing of the values or updating the index. However, that problem is universal to the device.

[ - ]
Reply by SolderdotMay 17, 2019

I guess, there is no need to re-invent the wheel. Use a journaling file system instead of FAT. This will do the trick.

[ - ]
Reply by ivanovpMay 17, 2019

Not only the filesystem matters: commercial SD cards use caches and simply not designed to power cut during write. So they often die if you remove it during write (I killed several SD cards), or not writing the data in the supposed order if power switched off. You need an industrial grade SD card.

Second thing: journaling file systems often protects only the file system's intergrity but not the file integrity. So you can mount your filesystem next time, but your file has 0 size or contains garbage. So check the file system's specification.

[ - ]
Reply by Shubham1006May 18, 2019

Yes, we are using industrial grade SD card.

Hmmm, do you any reference that you can share ?

[ - ]
Reply by ivanovpMay 18, 2019

I recommend SafeFAT which is a journaling filesystem. It protects data integrity too. SafeFAT is compatible with FAT, so the files on SD card can be read/written on PC.

(I'm employee of HCC Embedded, which developed SafeFAT.)

[ - ]
Reply by Shubham1006May 18, 2019

I didn't find support for journaling file system for TI CC3200 controller. Can you please suggest more options ?