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