2024-02-08 11:51 AM
I am writing data to an SD card for a data logger application, but I have very little experience with writing to SD cards so I want to ensure I am writing the data efficiently.
My understanding is that data is written as a sector of 512 bytes. So even if I write a single byte, it will need to write all 512 bytes in a sector to do so ?
I am using the FatFs driver with an SPI interface running on the NUCLEO-L433RC-P dev board.
At the moment I create a binary file and start the file with the UNIX time, followed by samples (each 14 bytes in length) until the data log is completed.
Each sample consists of a 4-byte tick time and 10-bytes of serial data received via the UART (at a 100ms rate):
The diagram below shows the structure of the binary file, with just 4 samples for simplicity. The number of samples for a real data log would obviously be much higher than could fill the entire SD card.
I currently write to the SD card (using f_write) each time the serial data is received (~100ms), but I believe this is inefficient because it writes a sector of 512 bytes a time and it takes a certain amount of time to write the data. I also think this technique might cause additional wear on the SD card ?
So how should I write this data efficiently ?
Should I wait until I have enough data to fit into a single sector before performing the write ? If so, I could only fit 36 samples in a sector as this would be 508 bytes...
But this would leave 4 spare bytes in each sector, in which case if these bytes are unused it would be inefficient in terms of memory space. Or are the unused spare bytes somehow used when the next write occurs ?
Is the sector size for all SD cards always 512 bytes, or does the sector size depend on how the SD card is formatted ?
I notice that when I format the SD card, there is an allocation unit size. Is this anything to do with the sector size ?
Eventually I want to use larger SD cards so will need to use the exFAT format.
My code is attached for reference
2024-10-02 10:10 PM
Hello!
Interesting Subject...
Any progress?
2024-10-03 12:28 AM
Well the button push interrupt needs to be denounced, and the closing needs to occur before restarting.