Skip to main content

File Systems

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

Popular posts from this blog

Let's Build a Music Player!

Digital Music Player's are one of the many sensational devices that were developed in the 20th century. They have been around for a while now and have become a part of our life. In the last few years, they have been integrated within Smart Phones and hence, have reached the masses. Now, Let us try to dive into knowing how these devices work and I guarantee that we all would be fascinated by the kind of engineering which goes into making these magic boxes. There are various topics which must be covered in order to build our own music player and so there will be several blogs to build a SOUND foundation. Now let us make a list of the required components and then we will walk through each of them in the upcoming blogs. There are 3 basic parts which are required to build a device which can output Audio:- Storage Medium. Micro-controller. Audio Output device. We will store the required music file onto a storage medium as the onboard memory on a microcontroller is low...

SD-CARD

This Post marks the beginning of our quest to develop a simple Music player. SD stands for Secure digital and these small devices have been around for a while now. They contain flash memory and a memory controller which is given instruction by the main MCU. So our main objective here is to understand how sd cards work and how to interface them with a microcontroller. As to understand the working of SD card we need to dive into the world of transistors and bits! The Sd card contains solid-state memory also known as the flash memory which is a type of EEPROM i.e  Electronically Erasable Programmable Read Only Memory .  Flash memory stores information in an array of memory cells made from  floating-gate transistors . These transistors are used in different topologies as to store each bit. We will not program each bit individually as it will be time-consuming as well as an inefficient way to store the data. As to move to a higher level of abstraction, we need a way to ...

Automated Outdoor Lighting

This Project enables a person to make any Outdoor Lighting Automated i.e to switch On/Off according to the sunlight outside. If it is dark outside then the light will turn On and if it is daytime then it will automatically switch Off. This can be very useful for all Outdoor Lighting of various commercial buildings as well as for residential buildings. This can also be implemented in Vehicles which will automatically turn their lights On.  Our aim can be achieved by designing a proper transistor circuit and using a relay with it to control the high voltage network. T he Requirements Are: 1:- Bipolar Transistor(Bc 547) 2:-5/6v Spdt Relay 3:-Resistors(Around 4.5k And 3k Ohm) 4:-Light Dependent Resistor 5:-5v Power Supply 6:-Pcb For Soldering Things Together 7:-Multimeter          CIRCUIT AND EXPLANATION The project involves the use of a transistor as a switch to make the outdoor light on/off. As we know that transistor can be used in three diffe...