I want to use an SD card as a ring buffer for a datalogger. If the SD card is never going to be connected to a computer and I just have a single continuous stream of data, is it possible and/or beneficial for simplicity’s sake to write to the card in some “raw” manner that has no file/folder structure/formatting? My main goal is simplicity, but my secondary concern is that if I am constantly writing small files, whichever part of the card is handling the directory listing etc is going to get worn out. Am I naive in thinking that this will simplify the reading/writing of data as well?
SD cards have built-in wear leveling hence by itself you should not expect to prolong your live of a card to much when trying to do it yourself. It may help to understand some “shortcomings” of sd like that the erase block size is normally way bigger then the block write size(this has effect on wear leveling).
Not 100% on topic but well worth the watch is the work from xobs & bunnie on sdcard (and hacking their micro controllers.) The Exploration and Exploitation of an SD Memory Card (Youtube link)
As for you question: I think it is a perfectly good idea to use the sdcard in “raw” mode. Determining where to start writing might prove a little challenging (you will probably need to reserve a block to keep meta-data) and will also probably want to start every block with a magic number as to determine if the block is used. If you are really paranoid or simply want 100% correctness you will need to apply the concept of journaling
- write on a known bloc “I will write block 512”
- write the block
- update known block with I have written block 512
To keep things really simple I would implement a state machine on device startup that reads blocks of the sdcard until it found the first empty block(no magic numer) and then start the normal logic.
There are many example on the SPI protocol to write to sdcard
This one is funny/simple to understand:
One more (nice)… hack it to still create a fat partition and create a 2 gig file on the file system. look into the fat table what sectors are used (this will be a contiguous area ) and write your data in there. This will allow to “pop” the sdcard and get the contents.
You should not worry about wear leveling and should keep a block reserved to meta-data, empty the full card before starting, always write 512 byte blocks and you should be safe.