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