There has been a known issue for quite a while that cassette writes can cause filesystem corruption on the SD card. Usually, I approach bugs from a 'surely it's something I did' standpoint (and in truth, I prefer if it's something I did, because then I'm in a position to fix it), but sometimes it's not, and you have to be prepared that you might have to prove it one way or the other.
Long story short: there appears to be a bug in the Microchip MDD library specifically when opening a file in the "r+" mode, and when the file length is 0. If you do, you will incur file system corruption upon subsequent writing. If the file is of non-zero length, then "r+" works as expected (and thank goodness for that).
I am using the latest libraries, to wit 1.4.4, which interestingly contains a fix to "r+" mode (though apparently unrelated to this issue).
Anyway, because of the nature of the problem (only with 0 length files), what I do is translate "r+" to "w+" when the file length is zero. This incurs some otherwise unnecessary file operations on opening, and has some consequences regarding position in the filesystem and timestamps (because in "w+" mode the file is deleted and then re-created), but I think this is minor enough to disregard - especially relative to alternative: filesystem corruption.
So hopefully this is the end of this issue. It was a long and painful journey.
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.