SD Card File System Interfacing

When we come across smaller microcontrollers, where the memory is limited and the RAM size is pretty small when compared to the size of a sector, creating a file system to operate an SD card becomes inconvenient. In order to overcome this issue in smaller microcontrollers, the FatFs (FAT FS) is broken down into a smaller, less complex and more convenient file system known as the Petit FatFs (petit fat file system).

In this section, we will learn how to incorporate the file system that we created to perform raw read/write, into the Petit FatFs.
The Petit file system consumes a modest 44 bytes of RAM space with a further more required in the stack. The code will fit within the 2K – 4K bytes size. A lot of considerations were given in order to minimize the size of the file system and hence there will be a few restrictions for the user.
Before we go forward with the details of this system, we will first look into the functions provided by the file system.

As you can see, the functions provided in the system covers the application program layer, the logical file system, the file-organization and the basic file system. The user must provide the layer to interface the basic file system with the device. In other words, the I/O control.

Disk I/O File

The Petit file system comes with a file named diskio.c with the following functions.

disk_initialize (void)
disk_readp ( . . . )
disk_writep ( . . . )

The user must construct the I/O control using these functions and its respective arguments.

The disk_initialize function performs the basic SD card set up. It resets the card first and starts the communication between the SD card and the microcontroller. It further sets the block size.

The disk_readp function contains the commands to read the sector table as well as the contents written on the SD card. It takes a pointer as an argument to point to the destination object. It uses the sector number and the offset to locate the data to be read from the sector and an argument to take note of the byte count.

The disk_writep function takes in the pointer containing the data to be written and its byte count as the arguments and performs the sector write.

For further details, regarding the initialization and read/write functions on the SD card, refer the following link:

Read Program Sequence

Before we start the whole process, a file must be created in the SD card, containing the text file that has to be read by the microcontroller.
Once the initial set up is complete, the file system begins the mounting process of the SD card. First, it checks whether the card has been inserted and then performs a software reset and then kick starts the initialization process. This process has to be made available by the user. The next process is performed by the Petit FatFs, to read sector 0 to check whether the card is formatted precisely and identify the file system variant to which it has been formatted to.

After successfully mounting the SD card, the next procedure is to open the file that we created. The pf_open function takes in the file name as its argument and checks for the given path in the SD card. Once the path is identified, we must prepare the system to read the text file inside the path.

Due to memory constraints, only 31 bytes of data should be read. Once the data read is completed, the read data must be displayed and the SD card is unmounted.

Write Program Sequence

As specified in the beginning, due to memory constraints, the Petit FatFs has a lot of restrictions. This is particularly in the case of writing data to the SD card. Data can be written only on an existing file. A new file cannot be created. Additionally, the size of the existing file cannot be increased and writing starts and stops on a sector boundary. Therefore, only a very basic data write can be performed using the Petit FatFs.

To understand this, it is important to analyze the sector write operation performed in the pf_write( ) function.
It first initiates a sector write transaction and then begins to start writing the data to the sector. Since writing starts and stops on the sector boundary, data can be written up to 512 bytes. If the written data is less than 512 bytes, the rest of the space in the sector will be filled with zeros.

In the code, the writing sequence is performed soon after the file open operation is done. After the writing has been performed the read sequence is done, the process is completed by un-mounting the SD card.

Be the first to get latest updates and exclusive content straight to your email inbox.
Stay Updated
Subscribe, it is quick.
We hate spam too. Give it a try, you can unsubscribe anytime.
Spread the love, share this