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
Post a Comment