Skip to main content

Reading and Writing a File.

With the introduction of  FatFs, we are now able to interface with a file system on an SD card. Note that we can easily initialize the sd card by using fatfs module along with manipulating files and there is no need to manually send any command through SPI like we did in another blog. So, we begin by taking a look at the files included with the module and including them in our build. The module can be downloaded by following the instructions given in the last blog. It will contain two folders namely Documents and Source. All the required source files, as well as header files, are located in the Source directory.

The diskio.h is the low-level disk input/output header file. It basically contains macro definitions as well as function prototypes required to interface with a storage device such as an SD card. The application must specifically provide the definitions for the function prototypes which makes the fatfs module independent of the platform. Example function definitions are provided in the diskio.c and it is specifically mentioned in the file that a working storage control module should be attached to the diskio.h.
ff.h is the main header file which contains the module application interface i.e all the macros and function prototypes required by the application as well as the FAT filesystem whose definitions are in ff.c. It contains the required functions which will be used to manipulate a file such as f_read() and f_write().
The module configuration options are provided in the ffconf.h. By default, fatfs uses ANSI coding scheme but can switch to unicode by changing the configuration options. The ffunicode.c is only used when unicode encoding is activated.
The integer type definitions are provided in the integer.h header file.

All the above-mentioned files are to be included in the project along with the main application file and a low-level storage control file which is the most important part in getting our module ready. The file must contain the disk control function defined in the diskio.h. Since we are going to use SPI protocol to interface with the sd card we need to write the low-level disk control using SPI. The example for many platforms can be downloaded from here.
The disk control module requires a timer which is implemented in disk_timerproc() and should be called by the main application in every 1milli second.
After including all the above mentioned files, we can now start to write the main application.
The module requires a FATFS object along with a working buffer for processing. A FIL object is also needed as we are working with files. A result check is done by declaring FRESULT.

/* Format default drive and create a file */
int main (void)
{
FATFS fs; /* Filesystem object */
FIL fil; /* File object */
FRESULT res; /* API result code */
UINT bw; /* Bytes written */
BYTE work[FF_MAX_SS]; /* Work area (larger is better for processing time) */
/* Create FAT volume */
res = f_mkfs("", FM_ANY, 0, work, sizeof work);
if (res) ...
/* Register work area */
f_mount(&fs, "", 0);
/* Create a file as new */
res = f_open(&fil, "hello.txt", FA_CREATE_NEW | FA_WRITE);
if (res) ...
/* Write a message */
f_write(&fil, "Hello, World!\r\n", 15, &bw);
if (bw != 15) ...
/* Close the file */
f_close(&fil);
/* Unregister work area */
f_mount(0, "", 0);

The code above provides all the necessary information to create a new FAT volume, register it and create a new text file named hello.txt.
If the above code is used in the main application along with all the required inclusions, we will be able to store a file in the SD Card and append it!



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