In the last post, we were able to get the sd card initialised and hence are one step closer to saving a file in the storage device. We now require a system which can keep track of all the different bits of a file and hence can perform operations on an entity rather than bits or bytes. Here comes the File System, A system used to organize & manage data on any storage. It comprises of different methods as well as structures to keep track of all the files on a given storage device. Here we deal with the FAT filesystem as it is used universally for SD cards. There are various other popular systems such as NTFS, EXT etc developed for different devices as well as operating systems.
FAT stands for File Allocation Table and literally tracks all the files using a table which contains all the information about a file such as its address as well as other metadata like short name etc.
A complete FAT file system is known as a logical volume or logical drive. It consists of three to four areas, each of them with one or more sectors. So a storage device can have multiple volumes and every volume will have its separate areas in the order shown in the figure below:-
The Boot sector lies in the Reserved area along with the superblock which contains info about the filesystem such as block size etc. As the name suggests, the boot sector contains information that the file system uses to access the volume such as Sectors per file allocation table, Physical Disk Number, System ID.
Then comes the FAT area where all the files and directories are organized by keeping track of the clusters. A cluster is an allocation unit used by filesystem to give storage space to files. It can also be defined as a contagious group of sectors where a sector is the smallest unit of a data block that can be read or written. The FAT area contains the information about every cluster such as whether the cluster is unused or whether used by any file and thus acts as a map of the data region. Each cluster contains a pointer to the next cluster location in the file and hence works as a linked list. The last cluster ends with 0xffff or any specific number to mark the end of the file. A file can be accessed by using the address of the first cluster which is stored in the Root Directory. This is followed by the data area where the actual bits are stored. The FAT directory is basically a file whose entries are the metadata about other files.
Since any file system is a software system and all the information in different sectors such as the BOOT sector, can be manipulated by the use of data structures which is an efficient way to map the software on the memory in a specific predefined format.
The BIOS Parameter Block or BPB is the most important data structure in the FAT volume. It stores the configuration parameters of the FAT volume and is located in the boot sector.
As we can see, The FAT file system offers an efficient way to organize the files but if we were to write a program to interface with the filesystem and manually write every piece of code to read/write information to save even one simple file, it would be time-consuming and moreover a bit stupid as we have various open source libraries at our disposal. Here we will be using the very versatile FatFs by chan. It is completely separate from the disk I/O layer and hence is platform independent.
The Fatfs provides us with simple to use API's to create/manipulate any file and hence provides us with complete control over our data. We need to develop a layer to interface the provided disk control API's with our MCU and then we can easily use every other functionality provided by the module.
To Download the source code, we need to visit the website provided above. Go to the download section and click on the latest release. A Zip file will be downloaded which contains all the code for the module in the Source directory. A sample code for different platforms is also available on the website which contains the requires layer to interface the disk control API's.
Now we are ready to start the process of writing the actual code and we need to include the provided source files in the fatfs module along with the provided example code for the platform(stm32f103 here) along with creating the main application file. If all the steps are followed then we should now be able to compile the code and create an executable binary and hence we are able to use the FAT file system in our SD card by the use of FatFs module.
So now, we have successfully devised a way to store our data(file) in a specific format and from the next post, the process of building the code for our music player will begin!

Comments
Post a Comment