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