Summary – Examples using FreeRTOS FAT SL
In the previous several articles I have written about the FreeRTOS FAT SL Filesystem. This included using the Cypress FM24V10 FRAM, creating an FRAM media driver and building an example project. This article will show you the C-functions in “extestfs.c” that are used to actually test the filesystem. Notice that I named all of the functions starting with “ex” which I adopted from the FreeRTOS FAT SL examples. The functions in this article are all called by the command line interpreter that I built in the previous post. They include
- exInit – Initializing the Filesystem
- exCreateFile – Creating a file in the FileSystem
- exReadFile – Reading the data from a file
- exFormat – Formatting the disk
- exDriveInfo – Printing the drive information
- exDirectory – Print an “ls” or “dir”
exInit
Before the FreeRTOS FAT SL Filesystem can do anything, you must initialize it. All this function does is call the function that I created to turn on the file system in the media port. One thing that is interesting is that it returns an error code that tells you if the file system is formatted or not. It figured this out by looking at the data in the first sector of the FRAM.
// This function initalize the filesystem void exInit(void) { unsigned char ucStatus; /* First create the volume. */ ucStatus = f_initvolume( fram_initfunc ); UART_UartPutString("Initialized sucessfully\n"); /* It is expected that the volume is not formatted. */ if( ucStatus == F_ERR_NOTFORMATTED ) { UART_UartPutString("Filesystem Unformatted\n"); } else { UART_UartPutString("Filesystem Formatted\n"); } }
exCreateFile
This functiom creates a file called “afile.txt” in the FreeRTOS FAT SL filesystem with the ASCII characters for 0-9. I did something that was probably not helpful in that I made a loop (on line 43) that started a ‘0’ which is also known as 49 (go look at the ASCII table).
This function calls f_open which will create a new file when it is passed the argument “w”. I also use the function f_putc to output characters to the file.
As I am looking at the code to write this article I realized that I didnt f_close the file (which I have now fixed).
#define FILENAME "afile.txt" // exCreateFile - this function creates a file called "afile.txt" an void exCreateFile(void) { F_FILE *pxFile; UART_UartPutString("Attempting Create & Write File\n"); pxFile = f_open(FILENAME, "w" ); if(pxFile) { for(int i='0' ; i< '9' ; i++) { f_putc(i,pxFile); } f_close(pxFile); } else { UART_UartPutString("Failed File Create\n"); } }
exReadFile
The exReadFile functions reads all of the characters out of the “afile.txt” and prints them to the screen. It uses the function f_getc which reads one character from the file. Notice that I open the file in read mode by using the “r” option.
void exReadFile(void) { UART_UartPutString("Attempting Read\n"); F_FILE *pxFile; pxFile = f_open( FILENAME, "r" ); if(pxFile) { while(!f_eof(pxFile)) { UART_UartPutChar(f_getc(pxFile)); } f_close(pxFile); } else { UART_UartPutString("File not found\n"); } }
exFormat
The exFormat function calles f_format with the FAT12 option. It is impossible to use FAT16 and FAT32 which require a much larger media to use. FAT12 was originally created for use on smallish floppy disk.
void exFormat(void) { char buff[128]; /* Format the created volume. */ unsigned char ucStatus; ucStatus = f_format( F_FAT12_MEDIA ); if( ucStatus == F_NO_ERROR ) { UART_UartPutString("Formatted sucessfully\n"); } else { sprintf(buff,"Error = %d\n",ucStatus); UART_UartPutString(buff); UART_UartPutString("Format Error\n"); } }
exDriveInfo
The exDriveInfo function calls the f_getfreespace function to find out how much space is free for use on the FRAM FreeRTOS FAT SL Filesystem.
void exDriveInfo( void ) { char buff[128]; F_SPACE xSpace; unsigned char ucReturned; /* Get space information on current embedded FAT file system drive. */ ucReturned = f_getfreespace( &xSpace ); if( ucReturned == F_NO_ERROR ) { /* xSpace.total holds the total drive size, xSpace.free holds the free space on the drive, xSpace.used holds the size of the used space on the drive, xSpace.bad holds the size of unusable space on the drive. */ sprintf(buff,"Free Space = %lu\nUsed Space = %lu\nTotal = %lu\n",xSpace.free,xSpace.used,xSpace.total); UART_UartPutString(buff); } else { /* xSpace could not be completed. ucReturned holds the error code. */ UART_UartPutString("Free space failed\n"); } }
exDirectory
The exDirectory function calls the f_findfirst and f_findnext functions to iterate all of the files and directories on the top level of the file system. The f_findfirst function uses a wildcard regular expression to match filenames. When it finds a file it prints information about that file.
void exDirectory( void ) { char buff[128]; F_FIND xFindStruct; /* Print out information on every file in the subdirectory "subdir". */ if( f_findfirst( "*.*", &xFindStruct ) == F_NO_ERROR ) { do { sprintf(buff,"filename:%s ", xFindStruct.filename ); UART_UartPutString(buff); if( ( xFindStruct.attr & F_ATTR_DIR ) != 0 ) { UART_UartPutString ( "is a directory directory\n" ); } else { sprintf ( buff, "is a file of size %lu\n", xFindStruct.filesize ); UART_UartPutString(buff); } } while( f_findnext( &xFindStruct ) == F_NO_ERROR ); } }
No comment yet, add your voice below!