cancel
Showing results for 
Search instead for 
Did you mean: 

How to write data efficiently to an SD card ?

johngj
Senior

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):

  • Tick time from HAL_GetTick()  (32 bits)
  • Temperature (8 bits)
  • Voltage high byte (8 bits)
  • Voltage low byte (8 bits)
  • Current high byte (8 bits)
  • Current low byte (8 bits)
  • Consumption high byte (8 bits)
  • Consumption low byte (8 bits)
  • Speed high byte (8 bits)
  • Speed low byte (8 bits)
  • CRC (8 bits)

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.

johngj_2-1707419795099.png

 

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...

  • 36 samples would be 508 bytes i.e. 4 bytes (UNIX time) + 14 bytes (data packet) * 36 = 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 ?

johngj_3-1707421018110.png

Eventually I want to use larger SD cards so will need to use the exFAT format.

My code is attached for reference

 

 

11 REPLIES 11
lodwar
Associate

Hello!

Interesting Subject...

Any progress?

Well the button push interrupt needs to be denounced, and the closing needs to occur before restarting.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..