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