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