I'm using Arduino Zero (SAMD21) and Serial NOR flash memory (W25Q128JV).
Software-side, I write into the flash memory via Adafruit SPIFlash + SdFat libraries. File system is FAT16.
My application needs to periodically write into a log file (1 time per second). Also, the final hardware will be exposed to unexpected power-off even in normal conditions.
I've noticed it is very easy to corrupt the file system by powering off the board. When this happens, the opening of the log file fails and, sometimes, also other files in the memory get unaccessible... even if I did not write into them (is it expected?)
I'm looking to suggestions on how to protect from these conditions. I've tried to list some ideas:
Try to reduce the write operations over time, using buffer instead of immediate writing every seconds --> this only reduces the probabilities
Change the file system with another that supports journaling --> I have not found any Arduino/SAMD libraries apart from FAT
Keep track of the voltage supplied to the board and close+stop any file writing when this goes down to a threshold --> this might require some work on the hardware as the voltage must not drop down quickly.
Does the flash memory itself support somekind of corruption protection systems?
Have you thought of building a simple "uninterrupted power supply" solution? This could be as simple as a circuit that includes a large capacitor and diode arrangement. Enough to keep the Arduino running for ~1s will probably be enough. The Arduino would use an input pin to monitor the incoming power and if it detects a failure, your code can automatically close any open files and whatever other shutdown procedure is needed to avoid corrupting the file system.
Yes, this is more or less the idea in the 3rd bulled of my list. The drawback is that it requires additional elements in the hardware and I have some cost/space limitations. I would like to keep it as a last chance. Thanks anyway for the suggestion.
Maybe choose a different file system? There are file systems specifically designed for this sort of thing.
Second bullet of my list. I have not found much on this, do you have suggestions?
I know there is NTFS, uBIFS... but are there compatible libraries for Arduino Zero?
as already mentioned, a diode and a capacitor and measure for power loss.
if you have solved your question already, please provide your solution and/or mark this thread as solved.