Read / write binary data to SD using Arduino

I am working on a project with Arduino and I would like to keep the data persistent. I already use an Ethernet screen that has a microSD reader.

The data I save will be incredibly small. For now, I will just save 3 bytes at a time. I would really like to open the SD card for writing, starting at byte x , and then write y bytes of data. When I want to read it, I just read y bytes, starting at byte x .

However, all the code I saw is related to working with the file system, which seems like an unnecessary overhead. I do not need this data to read on any other system, data storage space is not a problem, and there is no other data on the card that I need to worry about. Is there a way to simply write binary data directly to an SD card?

+5
source share
1 answer

You can write raw binary data to an SD card. Most people do this using the 4-pin SPI interface supported by the SD card. Unfortunately, the data is not addressed to bytes, but block addresses (the block size is usually 512 bytes).

This means that if you want to write 4 bytes in byte 516, you will need to read in block 0x00000001 (second block), and then calculate the offset, write data, and then write the entire block back. (I can’t say that this restriction applies to the SD interface using more contacts, I have no experience with it)

This complication is why many people choose to use libraries that include “unnecessary overhead”.

With that said, I should have done this in the past because I need a way to log data that was reliable before power failures. I found the following resource very useful:

http://elm-chan.org/docs/mmc/mmc_e.html

It will probably be easier for you to make your smaller entries into the memory buffer and upload them to the SD card when you have enough data to make it useful.

If you look around, you will find many open source code related to the SP SPI interface to use directly or as a reference to implement your own system.

+5
source

Source: https://habr.com/ru/post/1202554/


All Articles