veiner.eu
SD CARDDISK PARTITIONS AND FAT32WIRING AND COMMUNCATIONINIT, READ AND WRITETHE CARD READING
HOW TO USE THE CARD
SOURCE CODES

SD card - how to use the card

9.6.2018

The SD card can be used in many kinds of projects as the storage medium because of its quite large capacity. I will not discuss the cases when some kind of ready made solution is used. I will stick with the basic principles we have learned.

Imagine some simple project where the data from some sensor like the DS18B20 digital temperature sensor has to be stored. The output of this sensor are two bytes with temperature information. So I will just be writing 2 bytes to the card in some time intervals. The complexity of the software necessary to accomplish this task depends on whether I need other devices to read the data from the card. I so then I have to use some standard file system such as FAT32. In other case no file system is needed. Let's explore these two variants.

SD card without file system

In case I do not need other devices (e.g. PC) to be able to read the data stored on the card then it is completely up to me to choose the format of the data.

Say that the device is continually storing data in regular time intervals until all the card's memory is full. After that I will transmit the stored data to PC via some interface - e.g. USB. The storing of the data can be done this way:

  1. Ther is no need for the file system to be present in the card.
  2. Data would be written simply one byte after another. In case of the DS18B20 512 / 2 = 256 records can be written to one sector. The current address of the written bytes would be stored in some kind of permanent memory such as EEPROM.
  3. After the data was copied to PC the pointer to the card memory address would be set to zero without need to erase the card.

Using FAT32

The principle here is similar to the previous mode. The question here is how to store the data. Shall I create directories? How big files shall I use? The fact is that FAT32 does not limit the size of the root directory so the files can be stored in the root. I will decide on the files size with respect to effectiveness of the space usage. The file size should be the whole number multiple of the cluster size. When these parameters are set then I know how large the root directory after the whole space is used will be. Because it is only my device that is writing data to the card I can simplify the way I will treat FAT32:

  1. The files will be written one after another, cluster after cluster.
  2. The device keeps the current cluster number.
  3. After the card is full the data will be copied to the PC. Then the root directory will be erased in some effective way and data can be written from start again.