]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/FreeRTOS_Plus_FAT_SL_and_CLI_Windows_Simulator/File-system-demo.c
Update version number to 8.1.2 after moving the defaulting of configUSE_PORT_OPTIMISE...
[freertos] / FreeRTOS-Plus / Demo / FreeRTOS_Plus_FAT_SL_and_CLI_Windows_Simulator / File-system-demo.c
1 /*\r
2     FreeRTOS V8.1.2 - Copyright (C) 2014 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS kernel.                                   !<<\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /*******************************************************************************\r
67  * See the URL in the comments within main.c for the location of the online\r
68  * documentation.\r
69  ******************************************************************************/\r
70 \r
71 /* Standard includes. */\r
72 #include <stdio.h>\r
73 \r
74 /* FreeRTOS includes. */\r
75 #include "FreeRTOS.h"\r
76 \r
77 /* File system includes. */\r
78 #include "fat_sl.h"\r
79 #include "api_mdriver_ram.h"\r
80 \r
81 /* 8.3 format, plus null terminator. */\r
82 #define fsMAX_FILE_NAME_LEN                             13\r
83 \r
84 /* The number of bytes read/written to the example files at a time. */\r
85 #define fsRAM_BUFFER_SIZE                               200\r
86 \r
87 /* The number of bytes written to the file that uses f_putc() and f_getc(). */\r
88 #define fsPUTC_FILE_SIZE                                100\r
89 \r
90 /*-----------------------------------------------------------*/\r
91 \r
92 /*\r
93  * Creates and verifies different files on the volume, demonstrating the use of\r
94  * various different API functions.\r
95  */\r
96 void vCreateAndVerifySampleFiles( void );\r
97 \r
98 /*\r
99  * Create a set of example files in the root directory of the volume using\r
100  * f_write().\r
101  */\r
102 static void prvCreateDemoFilesUsing_f_write( void );\r
103 \r
104 /*\r
105  * Use f_read() to read back and verify the files that were created by\r
106  * prvCreateDemoFilesUsing_f_write().\r
107  */\r
108 static void prvVerifyDemoFileUsing_f_read( void );\r
109 \r
110 /*\r
111  * Create an example file in a sub-directory using f_putc().\r
112  */\r
113 static void prvCreateDemoFileUsing_f_putc( void );\r
114 \r
115 /*\r
116  * Use f_getc() to read back and verify the file that was created by\r
117  * prvCreateDemoFileUsing_f_putc().\r
118  */\r
119 static void prvVerifyDemoFileUsing_f_getc( void );\r
120 \r
121 /*-----------------------------------------------------------*/\r
122 \r
123 /* A buffer used to both create content to write to disk, and read content back\r
124 from a disk.  Note there is no mutual exclusion on this buffer. */\r
125 static char cRAMBuffer[ fsRAM_BUFFER_SIZE ];\r
126 \r
127 /* Names of directories that are created. */\r
128 static const char *pcRoot = "/", *pcDirectory1 = "SUB1", *pcDirectory2 = "SUB2", *pcFullPath = "/SUB1/SUB2";\r
129 \r
130 /*-----------------------------------------------------------*/\r
131 \r
132 void vCreateAndVerifySampleFiles( void )\r
133 {\r
134 unsigned char ucStatus;\r
135 \r
136         /* First create the volume. */\r
137         ucStatus = f_initvolume( ram_initfunc );\r
138 \r
139         /* It is expected that the volume is not formatted. */\r
140         if( ucStatus == F_ERR_NOTFORMATTED )\r
141         {\r
142                 /* Format the created volume. */\r
143                 ucStatus = f_format( F_FAT12_MEDIA );\r
144         }\r
145 \r
146         if( ucStatus == F_NO_ERROR )\r
147         {\r
148                 /* Create a set of files using f_write(). */\r
149                 prvCreateDemoFilesUsing_f_write();\r
150 \r
151                 /* Read back and verify the files that were created using f_write(). */\r
152                 prvVerifyDemoFileUsing_f_read();\r
153 \r
154                 /* Create sub directories two deep then create a file using putc. */\r
155                 prvCreateDemoFileUsing_f_putc();\r
156 \r
157                 /* Read back and verify the file created by\r
158                 prvCreateDemoFileUsing_f_putc(). */\r
159                 prvVerifyDemoFileUsing_f_getc();\r
160         }\r
161 }\r
162 /*-----------------------------------------------------------*/\r
163 \r
164 static void prvCreateDemoFilesUsing_f_write( void )\r
165 {\r
166 BaseType_t xFileNumber, xWriteNumber;\r
167 char cFileName[ fsMAX_FILE_NAME_LEN ];\r
168 const BaseType_t xMaxFiles = 5;\r
169 long lItemsWritten;\r
170 F_FILE *pxFile;\r
171 \r
172         /* Create xMaxFiles files.  Each created file will be\r
173         ( xFileNumber * fsRAM_BUFFER_SIZE ) bytes in length, and filled\r
174         with a different repeating character. */\r
175         for( xFileNumber = 1; xFileNumber <= xMaxFiles; xFileNumber++ )\r
176         {\r
177                 /* Generate a file name. */\r
178                 sprintf( cFileName, "root%03d.txt", xFileNumber );\r
179 \r
180                 /* Obtain the current working directory and print out the file name and\r
181                 the     directory into which the file is being written. */\r
182                 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );\r
183                 printf( "Creating file %s in %s\r\n", cFileName, cRAMBuffer );\r
184 \r
185                 /* Open the file, creating the file if it does not already exist. */\r
186                 pxFile = f_open( cFileName, "w" );\r
187                 configASSERT( pxFile );\r
188 \r
189                 /* Fill the RAM buffer with data that will be written to the file.  This\r
190                 is just a repeating ascii character that indicates the file number. */\r
191                 memset( cRAMBuffer, ( int ) ( '0' + xFileNumber ), fsRAM_BUFFER_SIZE );\r
192 \r
193                 /* Write the RAM buffer to the opened file a number of times.  The\r
194                 number of times the RAM buffer is written to the file depends on the\r
195                 file number, so the length of each created file will be different. */\r
196                 for( xWriteNumber = 0; xWriteNumber < xFileNumber; xWriteNumber++ )\r
197                 {\r
198                         lItemsWritten = f_write( cRAMBuffer, fsRAM_BUFFER_SIZE, 1, pxFile );\r
199                         configASSERT( lItemsWritten == 1 );\r
200                 }\r
201 \r
202                 /* Close the file so another file can be created. */\r
203                 f_close( pxFile );\r
204         }\r
205 }\r
206 /*-----------------------------------------------------------*/\r
207 \r
208 static void prvVerifyDemoFileUsing_f_read( void )\r
209 {\r
210 BaseType_t xFileNumber, xReadNumber;\r
211 char cFileName[ fsMAX_FILE_NAME_LEN ];\r
212 const BaseType_t xMaxFiles = 5;\r
213 long lItemsRead, lChar;\r
214 F_FILE *pxFile;\r
215 \r
216         /* Read back the files that were created by\r
217         prvCreateDemoFilesUsing_f_write(). */\r
218         for( xFileNumber = 1; xFileNumber <= xMaxFiles; xFileNumber++ )\r
219         {\r
220                 /* Generate the file name. */\r
221                 sprintf( cFileName, "root%03d.txt", xFileNumber );\r
222 \r
223                 /* Obtain the current working directory and print out the file name and\r
224                 the     directory from which the file is being read. */\r
225                 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );\r
226                 printf( "Reading file %s from %s\r\n", cFileName, cRAMBuffer );\r
227 \r
228                 /* Open the file for reading. */\r
229                 pxFile = f_open( cFileName, "r" );\r
230                 configASSERT( pxFile );\r
231 \r
232                 /* Read the file into the RAM buffer, checking the file contents are as\r
233                 expected.  The size of the file depends on the file number. */\r
234                 for( xReadNumber = 0; xReadNumber < xFileNumber; xReadNumber++ )\r
235                 {\r
236                         /* Start with the RAM buffer clear. */\r
237                         memset( cRAMBuffer, 0x00, fsRAM_BUFFER_SIZE );\r
238 \r
239                         lItemsRead = f_read( cRAMBuffer, fsRAM_BUFFER_SIZE, 1, pxFile );\r
240                         configASSERT( lItemsRead == 1 );\r
241 \r
242                         /* Check the RAM buffer is filled with the expected data.  Each\r
243                         file contains a different repeating ascii character that indicates\r
244                         the number of the file. */\r
245                         for( lChar = 0; lChar < fsRAM_BUFFER_SIZE; lChar++ )\r
246                         {\r
247                                 configASSERT( cRAMBuffer[ lChar ] == ( '0' + ( char ) xFileNumber ) );\r
248                         }\r
249                 }\r
250 \r
251                 /* Close the file. */\r
252                 f_close( pxFile );\r
253         }\r
254 }\r
255 /*-----------------------------------------------------------*/\r
256 \r
257 static void prvCreateDemoFileUsing_f_putc( void )\r
258 {\r
259 unsigned char ucReturn;\r
260 int iByte, iReturned;\r
261 F_FILE *pxFile;\r
262 char cFileName[ fsMAX_FILE_NAME_LEN ];\r
263 \r
264         /* Obtain and print out the working directory. */\r
265         f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );\r
266         printf( "In directory %s\r\n", cRAMBuffer );\r
267 \r
268         /* Create a sub directory. */\r
269         ucReturn = f_mkdir( pcDirectory1 );\r
270         configASSERT( ucReturn == F_NO_ERROR );\r
271 \r
272         /* Move into the created sub-directory. */\r
273         ucReturn = f_chdir( pcDirectory1 );\r
274         configASSERT( ucReturn == F_NO_ERROR );\r
275 \r
276         /* Obtain and print out the working directory. */\r
277         f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );\r
278         printf( "In directory %s\r\n", cRAMBuffer );\r
279 \r
280         /* Create a subdirectory in the new directory. */\r
281         ucReturn = f_mkdir( pcDirectory2 );\r
282         configASSERT( ucReturn == F_NO_ERROR );\r
283 \r
284         /* Move into the directory just created - now two directories down from\r
285         the root. */\r
286         ucReturn = f_chdir( pcDirectory2 );\r
287         configASSERT( ucReturn == F_NO_ERROR );\r
288 \r
289         /* Obtain and print out the working directory. */\r
290         f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );\r
291         printf( "In directory %s\r\n", cRAMBuffer );\r
292         configASSERT( strcmp( cRAMBuffer, pcFullPath ) == 0 );\r
293 \r
294         /* Generate the file name. */\r
295         sprintf( cFileName, "%s.txt", pcDirectory2 );\r
296 \r
297         /* Print out the file name and the directory into which the file is being\r
298         written. */\r
299         printf( "Writing file %s in %s\r\n", cFileName, cRAMBuffer );\r
300 \r
301         pxFile = f_open( cFileName, "w" );\r
302 \r
303         /* Create a file 1 byte at a time.  The file is filled with incrementing\r
304         ascii characters starting from '0'. */\r
305         for( iByte = 0; iByte < fsPUTC_FILE_SIZE; iByte++ )\r
306         {\r
307                 iReturned = f_putc( ( ( int ) '0' + iByte ), pxFile );\r
308                 configASSERT( iReturned ==  ( ( int ) '0' + iByte ) );\r
309         }\r
310 \r
311         /* Finished so close the file. */\r
312         f_close( pxFile );\r
313 \r
314         /* Move back to the root directory. */\r
315         ucReturn = f_chdir( "../.." );\r
316         configASSERT( ucReturn == F_NO_ERROR );\r
317 \r
318         /* Obtain and print out the working directory. */\r
319         f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );\r
320         printf( "Back in root directory %s\r\n", cRAMBuffer );\r
321         configASSERT( strcmp( cRAMBuffer, pcRoot ) == 0 );\r
322 }\r
323 /*-----------------------------------------------------------*/\r
324 \r
325 static void prvVerifyDemoFileUsing_f_getc( void )\r
326 {\r
327 unsigned char ucReturn;\r
328 int iByte, iReturned;\r
329 F_FILE *pxFile;\r
330 char cFileName[ fsMAX_FILE_NAME_LEN ];\r
331 \r
332         /* Move into the directory in which the file was created. */\r
333         ucReturn = f_chdir( pcFullPath );\r
334         configASSERT( ucReturn == F_NO_ERROR );\r
335 \r
336         /* Obtain and print out the working directory. */\r
337         f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );\r
338         printf( "Back in directory %s\r\n", cRAMBuffer );\r
339         configASSERT( strcmp( cRAMBuffer, pcFullPath ) == 0 );\r
340 \r
341         /* Generate the file name. */\r
342         sprintf( cFileName, "%s.txt", pcDirectory2 );\r
343 \r
344         /* Print out the file name and the directory from which the file is being\r
345         read. */\r
346         printf( "Reading file %s in %s\r\n", cFileName, cRAMBuffer );\r
347 \r
348         /* This time the file is opened for reading. */\r
349         pxFile = f_open( cFileName, "r" );\r
350 \r
351         /* Read the file 1 byte at a time. */\r
352         for( iByte = 0; iByte < fsPUTC_FILE_SIZE; iByte++ )\r
353         {\r
354                 iReturned = f_getc( pxFile );\r
355                 configASSERT( iReturned ==  ( ( int ) '0' + iByte ) );\r
356         }\r
357 \r
358         /* Finished so close the file. */\r
359         f_close( pxFile );\r
360 \r
361         /* Move back to the root directory. */\r
362         ucReturn = f_chdir( "../.." );\r
363         configASSERT( ucReturn == F_NO_ERROR );\r
364 \r
365         /* Obtain and print out the working directory. */\r
366         f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );\r
367         printf( "Back in root directory %s\r\n", cRAMBuffer );\r
368 }\r
369 \r
370 \r
371 \r
372 \r