]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/Common/FreeRTOS_Plus_FAT_SL_Demos/CreateExampleFiles/File-system-demo.c
8d1f165b04b6b19e26ccf6f4de4c7b04f2580c60
[freertos] / FreeRTOS-Plus / Demo / Common / FreeRTOS_Plus_FAT_SL_Demos / CreateExampleFiles / File-system-demo.c
1 /*\r
2     FreeRTOS V8.0.1 - 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 #include <string.h>\r
74 \r
75 /* FreeRTOS includes. */\r
76 #include "FreeRTOS.h"\r
77 \r
78 /* File system includes. */\r
79 #include "fat_sl.h"\r
80 #include "api_mdriver_ram.h"\r
81 \r
82 /* 8.3 format, plus null terminator. */\r
83 #define fsMAX_FILE_NAME_LEN                             13\r
84 \r
85 /* The number of bytes read/written to the example files at a time. */\r
86 #define fsRAM_BUFFER_SIZE                               200\r
87 \r
88 /* The number of bytes written to the file that uses f_putc() and f_getc(). */\r
89 #define fsPUTC_FILE_SIZE                                100\r
90 \r
91 /*-----------------------------------------------------------*/\r
92 \r
93 /*\r
94  * Creates and verifies different files on the volume, demonstrating the use of\r
95  * various different API functions.\r
96  */\r
97 void vCreateAndVerifySampleFiles( void );\r
98 \r
99 /*\r
100  * Create a set of example files in the root directory of the volume using\r
101  * f_write().\r
102  */\r
103 static void prvCreateDemoFilesUsing_f_write( void );\r
104 \r
105 /*\r
106  * Use f_read() to read back and verify the files that were created by\r
107  * prvCreateDemoFilesUsing_f_write().\r
108  */\r
109 static void prvVerifyDemoFileUsing_f_read( void );\r
110 \r
111 /*\r
112  * Create an example file in a sub-directory using f_putc().\r
113  */\r
114 static void prvCreateDemoFileUsing_f_putc( void );\r
115 \r
116 /*\r
117  * Use f_getc() to read back and verify the file that was created by\r
118  * prvCreateDemoFileUsing_f_putc().\r
119  */\r
120 static void prvVerifyDemoFileUsing_f_getc( void );\r
121 \r
122 /*-----------------------------------------------------------*/\r
123 \r
124 /* A buffer used to both create content to write to disk, and read content back\r
125 from a disk.  Note there is no mutual exclusion on this buffer. */\r
126 static char cRAMBuffer[ fsRAM_BUFFER_SIZE ];\r
127 \r
128 /* Names of directories that are created. */\r
129 static const char *pcRoot = "/", *pcDirectory1 = "SUB1", *pcDirectory2 = "SUB2", *pcFullPath = "/SUB1/SUB2";\r
130 \r
131 /*-----------------------------------------------------------*/\r
132 \r
133 void vCreateAndVerifySampleFiles( void )\r
134 {\r
135 unsigned char ucStatus;\r
136 \r
137         /* First create the volume. */\r
138         ucStatus = f_initvolume( ram_initfunc );\r
139 \r
140         /* It is expected that the volume is not formatted. */\r
141         if( ucStatus == F_ERR_NOTFORMATTED )\r
142         {\r
143                 /* Format the created volume. */\r
144                 ucStatus = f_format( F_FAT12_MEDIA );\r
145         }\r
146 \r
147         if( ucStatus == F_NO_ERROR )\r
148         {\r
149                 /* Create a set of files using f_write(). */\r
150                 prvCreateDemoFilesUsing_f_write();\r
151 \r
152                 /* Read back and verify the files that were created using f_write(). */\r
153                 prvVerifyDemoFileUsing_f_read();\r
154 \r
155                 /* Create sub directories two deep then create a file using putc. */\r
156                 prvCreateDemoFileUsing_f_putc();\r
157 \r
158                 /* Read back and verify the file created by\r
159                 prvCreateDemoFileUsing_f_putc(). */\r
160                 prvVerifyDemoFileUsing_f_getc();\r
161         }\r
162 }\r
163 /*-----------------------------------------------------------*/\r
164 \r
165 static void prvCreateDemoFilesUsing_f_write( void )\r
166 {\r
167 portBASE_TYPE xFileNumber, xWriteNumber;\r
168 char cFileName[ fsMAX_FILE_NAME_LEN ];\r
169 const portBASE_TYPE xMaxFiles = 5;\r
170 long lItemsWritten;\r
171 F_FILE *pxFile;\r
172 \r
173         /* Create xMaxFiles files.  Each created file will be\r
174         ( xFileNumber * fsRAM_BUFFER_SIZE ) bytes in length, and filled\r
175         with a different repeating character. */\r
176         for( xFileNumber = 1; xFileNumber <= xMaxFiles; xFileNumber++ )\r
177         {\r
178                 /* Generate a file name. */\r
179                 sprintf( cFileName, "root%03d.txt", ( int ) xFileNumber );\r
180 \r
181                 /* Obtain the current working directory and print out the file name and\r
182                 the     directory into which the file is being written. */\r
183                 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );\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 portBASE_TYPE xFileNumber, xReadNumber;\r
211 char cFileName[ fsMAX_FILE_NAME_LEN ];\r
212 const portBASE_TYPE 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", ( int ) 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 \r
227                 /* Open the file for reading. */\r
228                 pxFile = f_open( cFileName, "r" );\r
229                 configASSERT( pxFile );\r
230 \r
231                 /* Read the file into the RAM buffer, checking the file contents are as\r
232                 expected.  The size of the file depends on the file number. */\r
233                 for( xReadNumber = 0; xReadNumber < xFileNumber; xReadNumber++ )\r
234                 {\r
235                         /* Start with the RAM buffer clear. */\r
236                         memset( cRAMBuffer, 0x00, fsRAM_BUFFER_SIZE );\r
237 \r
238                         lItemsRead = f_read( cRAMBuffer, fsRAM_BUFFER_SIZE, 1, pxFile );\r
239                         configASSERT( lItemsRead == 1 );\r
240 \r
241                         /* Check the RAM buffer is filled with the expected data.  Each\r
242                         file contains a different repeating ascii character that indicates\r
243                         the number of the file. */\r
244                         for( lChar = 0; lChar < fsRAM_BUFFER_SIZE; lChar++ )\r
245                         {\r
246                                 configASSERT( cRAMBuffer[ lChar ] == ( '0' + ( char ) xFileNumber ) );\r
247                         }\r
248                 }\r
249 \r
250                 /* Close the file. */\r
251                 f_close( pxFile );\r
252         }\r
253 }\r
254 /*-----------------------------------------------------------*/\r
255 \r
256 static void prvCreateDemoFileUsing_f_putc( void )\r
257 {\r
258 unsigned char ucReturn;\r
259 int iByte, iReturned;\r
260 F_FILE *pxFile;\r
261 char cFileName[ fsMAX_FILE_NAME_LEN ];\r
262 \r
263         /* Obtain and print out the working directory. */\r
264         f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );\r
265 \r
266         /* Create a sub directory. */\r
267         ucReturn = f_mkdir( pcDirectory1 );\r
268         configASSERT( ucReturn == F_NO_ERROR );\r
269 \r
270         /* Move into the created sub-directory. */\r
271         ucReturn = f_chdir( pcDirectory1 );\r
272         configASSERT( ucReturn == F_NO_ERROR );\r
273 \r
274         /* Obtain and print out the working directory. */\r
275         f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );\r
276 \r
277         /* Create a subdirectory in the new directory. */\r
278         ucReturn = f_mkdir( pcDirectory2 );\r
279         configASSERT( ucReturn == F_NO_ERROR );\r
280 \r
281         /* Move into the directory just created - now two directories down from\r
282         the root. */\r
283         ucReturn = f_chdir( pcDirectory2 );\r
284         configASSERT( ucReturn == F_NO_ERROR );\r
285 \r
286         /* Obtain and print out the working directory. */\r
287         f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );\r
288         configASSERT( strcmp( cRAMBuffer, pcFullPath ) == 0 );\r
289 \r
290         /* Generate the file name. */\r
291         sprintf( cFileName, "%s.txt", pcDirectory2 );\r
292 \r
293         /* Print out the file name and the directory into which the file is being\r
294         written. */\r
295         pxFile = f_open( cFileName, "w" );\r
296 \r
297         /* Create a file 1 byte at a time.  The file is filled with incrementing\r
298         ascii characters starting from '0'. */\r
299         for( iByte = 0; iByte < fsPUTC_FILE_SIZE; iByte++ )\r
300         {\r
301                 iReturned = f_putc( ( ( int ) '0' + iByte ), pxFile );\r
302                 configASSERT( iReturned ==  ( ( int ) '0' + iByte ) );\r
303         }\r
304 \r
305         /* Finished so close the file. */\r
306         f_close( pxFile );\r
307 \r
308         /* Move back to the root directory. */\r
309         ucReturn = f_chdir( "../.." );\r
310         configASSERT( ucReturn == F_NO_ERROR );\r
311 \r
312         /* Obtain and print out the working directory. */\r
313         f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );\r
314         configASSERT( strcmp( cRAMBuffer, pcRoot ) == 0 );\r
315 }\r
316 /*-----------------------------------------------------------*/\r
317 \r
318 static void prvVerifyDemoFileUsing_f_getc( void )\r
319 {\r
320 unsigned char ucReturn;\r
321 int iByte, iReturned;\r
322 F_FILE *pxFile;\r
323 char cFileName[ fsMAX_FILE_NAME_LEN ];\r
324 \r
325         /* Move into the directory in which the file was created. */\r
326         ucReturn = f_chdir( pcFullPath );\r
327         configASSERT( ucReturn == F_NO_ERROR );\r
328 \r
329         /* Obtain and print out the working directory. */\r
330         f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );\r
331         configASSERT( strcmp( cRAMBuffer, pcFullPath ) == 0 );\r
332 \r
333         /* Generate the file name. */\r
334         sprintf( cFileName, "%s.txt", pcDirectory2 );\r
335 \r
336         /* This time the file is opened for reading. */\r
337         pxFile = f_open( cFileName, "r" );\r
338 \r
339         /* Read the file 1 byte at a time. */\r
340         for( iByte = 0; iByte < fsPUTC_FILE_SIZE; iByte++ )\r
341         {\r
342                 iReturned = f_getc( pxFile );\r
343                 configASSERT( iReturned ==  ( ( int ) '0' + iByte ) );\r
344         }\r
345 \r
346         /* Finished so close the file. */\r
347         f_close( pxFile );\r
348 \r
349         /* Move back to the root directory. */\r
350         ucReturn = f_chdir( "../.." );\r
351         configASSERT( ucReturn == F_NO_ERROR );\r
352 \r
353         /* Obtain and print out the working directory. */\r
354         f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );\r
355 }\r
356 \r
357 \r
358 \r
359 \r