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