2 FreeRTOS V7.5.2 - Copyright (C) 2013 Real Time Engineers Ltd.
\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
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\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
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\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
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
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
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\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
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
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
75 /*******************************************************************************
\r
76 * See the URL in the comments within main.c for the location of the online
\r
78 ******************************************************************************/
\r
80 /* Standard includes. */
\r
84 /* FreeRTOS includes. */
\r
85 #include "FreeRTOS.h"
\r
88 /* File system includes. */
\r
90 #include "api_mdriver_ram.h"
\r
92 /* 8.3 format, plus null terminator. */
\r
93 #define fsMAX_FILE_NAME_LEN 13
\r
95 /* The number of bytes read/written to the example files at a time. */
\r
96 #define fsRAM_BUFFER_SIZE 200
\r
98 /* The number of bytes written to the file that uses f_putc() and f_getc(). */
\r
99 #define fsPUTC_FILE_SIZE 100
\r
101 /* The number of files created in root. */
\r
102 #define fsROOT_FILES 3
\r
104 /*-----------------------------------------------------------*/
\r
107 * Creates and verifies different files on the volume, demonstrating the use of
\r
108 * various different API functions.
\r
110 void vCreateAndVerifySampleFiles( void );
\r
113 * Create a set of example files in the root directory of the volume using
\r
116 static void prvCreateDemoFilesUsing_f_write( void );
\r
119 * Use f_read() to read back and verify the files that were created by
\r
120 * prvCreateDemoFilesUsing_f_write().
\r
122 static void prvVerifyDemoFileUsing_f_read( void );
\r
125 * Create an example file in a sub-directory using f_putc().
\r
127 static void prvCreateDemoFileUsing_f_putc( void );
\r
130 * Use f_getc() to read back and verify the file that was created by
\r
131 * prvCreateDemoFileUsing_f_putc().
\r
133 static void prvVerifyDemoFileUsing_f_getc( void );
\r
135 /*-----------------------------------------------------------*/
\r
137 /* A buffer used to both create content to write to disk, and read content back
\r
138 from a disk. Note there is no mutual exclusion on this buffer. */
\r
139 static char cRAMBuffer[ fsRAM_BUFFER_SIZE ];
\r
141 /* Names of directories that are created. */
\r
142 static const char *pcRoot = "/", *pcDirectory1 = "SUB1", *pcDirectory2 = "SUB2", *pcFullPath = "/SUB1/SUB2";
\r
144 /*-----------------------------------------------------------*/
\r
146 void vCreateAndVerifySampleFiles( void )
\r
148 unsigned char ucStatus;
\r
150 /* First create the volume. */
\r
151 ucStatus = f_initvolume( ram_initfunc );
\r
153 /* It is expected that the volume is not formatted. */
\r
154 if( ucStatus == F_ERR_NOTFORMATTED )
\r
156 /* Format the created volume. */
\r
157 ucStatus = f_format( F_FAT12_MEDIA );
\r
160 if( ucStatus == F_NO_ERROR )
\r
162 /* Create a set of files using f_write(). */
\r
163 prvCreateDemoFilesUsing_f_write();
\r
165 /* Read back and verify the files that were created using f_write(). */
\r
166 prvVerifyDemoFileUsing_f_read();
\r
168 /* Create sub directories two deep then create a file using putc. */
\r
169 prvCreateDemoFileUsing_f_putc();
\r
171 /* Read back and verify the file created by
\r
172 prvCreateDemoFileUsing_f_putc(). */
\r
173 prvVerifyDemoFileUsing_f_getc();
\r
176 /*-----------------------------------------------------------*/
\r
178 static void prvCreateDemoFilesUsing_f_write( void )
\r
180 portBASE_TYPE xFileNumber, xWriteNumber;
\r
181 char cFileName[ fsMAX_FILE_NAME_LEN ];
\r
182 long lItemsWritten;
\r
185 /* Create fsROOT_FILES files. Each created file will be
\r
186 ( xFileNumber * fsRAM_BUFFER_SIZE ) bytes in length, and filled
\r
187 with a different repeating character. */
\r
188 for( xFileNumber = 1; xFileNumber <= fsROOT_FILES; xFileNumber++ )
\r
190 /* Generate a file name. */
\r
191 sprintf( cFileName, "root%03d.txt", ( int ) xFileNumber );
\r
193 /* Obtain the current working directory and print out the file name and
\r
194 the directory into which the file is being written. */
\r
195 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
196 printf( "Creating file %s in %s\r\n", cFileName, cRAMBuffer );
\r
198 /* Open the file, creating the file if it does not already exist. */
\r
199 pxFile = f_open( cFileName, "w" );
\r
200 configASSERT( pxFile );
\r
202 /* Fill the RAM buffer with data that will be written to the file. This
\r
203 is just a repeating ascii character that indicates the file number. */
\r
204 memset( cRAMBuffer, ( int ) ( '0' + xFileNumber ), fsRAM_BUFFER_SIZE );
\r
206 /* Write the RAM buffer to the opened file a number of times. The
\r
207 number of times the RAM buffer is written to the file depends on the
\r
208 file number, so the length of each created file will be different. */
\r
209 for( xWriteNumber = 0; xWriteNumber < xFileNumber; xWriteNumber++ )
\r
211 lItemsWritten = f_write( cRAMBuffer, fsRAM_BUFFER_SIZE, 1, pxFile );
\r
212 configASSERT( lItemsWritten == 1 );
\r
215 /* Close the file so another file can be created. */
\r
219 /*-----------------------------------------------------------*/
\r
221 static void prvVerifyDemoFileUsing_f_read( void )
\r
223 portBASE_TYPE xFileNumber, xReadNumber;
\r
224 char cFileName[ fsMAX_FILE_NAME_LEN ];
\r
225 long lItemsRead, lChar;
\r
228 /* Read back the files that were created by
\r
229 prvCreateDemoFilesUsing_f_write(). */
\r
230 for( xFileNumber = 1; xFileNumber <= fsROOT_FILES; xFileNumber++ )
\r
232 /* Generate the file name. */
\r
233 sprintf( cFileName, "root%03d.txt", ( int ) xFileNumber );
\r
235 /* Obtain the current working directory and print out the file name and
\r
236 the directory from which the file is being read. */
\r
237 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
238 printf( "Reading file %s from %s\r\n", cFileName, cRAMBuffer );
\r
240 /* Open the file for reading. */
\r
241 pxFile = f_open( cFileName, "r" );
\r
242 configASSERT( pxFile );
\r
244 /* Read the file into the RAM buffer, checking the file contents are as
\r
245 expected. The size of the file depends on the file number. */
\r
246 for( xReadNumber = 0; xReadNumber < xFileNumber; xReadNumber++ )
\r
248 /* Start with the RAM buffer clear. */
\r
249 memset( cRAMBuffer, 0x00, fsRAM_BUFFER_SIZE );
\r
251 lItemsRead = f_read( cRAMBuffer, fsRAM_BUFFER_SIZE, 1, pxFile );
\r
252 configASSERT( lItemsRead == 1 );
\r
254 /* Check the RAM buffer is filled with the expected data. Each
\r
255 file contains a different repeating ascii character that indicates
\r
256 the number of the file. */
\r
257 for( lChar = 0; lChar < fsRAM_BUFFER_SIZE; lChar++ )
\r
259 configASSERT( cRAMBuffer[ lChar ] == ( '0' + ( char ) xFileNumber ) );
\r
263 /* Close the file. */
\r
267 /*-----------------------------------------------------------*/
\r
269 static void prvCreateDemoFileUsing_f_putc( void )
\r
271 unsigned char ucReturn;
\r
272 int iByte, iReturned;
\r
274 char cFileName[ fsMAX_FILE_NAME_LEN ];
\r
276 /* Obtain and print out the working directory. */
\r
277 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
278 printf( "In directory %s\r\n", cRAMBuffer );
\r
280 /* Create a sub directory. */
\r
281 ucReturn = f_mkdir( pcDirectory1 );
\r
282 configASSERT( ucReturn == F_NO_ERROR );
\r
284 /* Move into the created sub-directory. */
\r
285 ucReturn = f_chdir( pcDirectory1 );
\r
286 configASSERT( ucReturn == F_NO_ERROR );
\r
288 /* Obtain and print out the working directory. */
\r
289 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
290 printf( "In directory %s\r\n", cRAMBuffer );
\r
292 /* Create a subdirectory in the new directory. */
\r
293 ucReturn = f_mkdir( pcDirectory2 );
\r
294 configASSERT( ucReturn == F_NO_ERROR );
\r
296 /* Move into the directory just created - now two directories down from
\r
298 ucReturn = f_chdir( pcDirectory2 );
\r
299 configASSERT( ucReturn == F_NO_ERROR );
\r
301 /* Obtain and print out the working directory. */
\r
302 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
303 printf( "In directory %s\r\n", cRAMBuffer );
\r
304 configASSERT( strcmp( ( const char * ) cRAMBuffer, pcFullPath ) == 0 );
\r
306 /* Generate the file name. */
\r
307 sprintf( cFileName, "%s.txt", pcDirectory2 );
\r
309 /* Print out the file name and the directory into which the file is being
\r
311 printf( "Writing file %s in %s\r\n", cFileName, cRAMBuffer );
\r
313 pxFile = f_open( cFileName, "w" );
\r
315 /* Create a file 1 byte at a time. The file is filled with incrementing
\r
316 ascii characters starting from '0'. */
\r
317 for( iByte = 0; iByte < fsPUTC_FILE_SIZE; iByte++ )
\r
319 iReturned = f_putc( ( ( int ) '0' + iByte ), pxFile );
\r
320 configASSERT( iReturned == ( ( int ) '0' + iByte ) );
\r
323 /* Finished so close the file. */
\r
326 /* Move back to the root directory. */
\r
327 ucReturn = f_chdir( "../.." );
\r
328 configASSERT( ucReturn == F_NO_ERROR );
\r
330 /* Obtain and print out the working directory. */
\r
331 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
332 printf( "Back in root directory %s\r\n", cRAMBuffer );
\r
333 configASSERT( strcmp( ( const char * ) cRAMBuffer, pcRoot ) == 0 );
\r
335 /*-----------------------------------------------------------*/
\r
337 static void prvVerifyDemoFileUsing_f_getc( void )
\r
339 unsigned char ucReturn;
\r
340 int iByte, iReturned;
\r
342 char cFileName[ fsMAX_FILE_NAME_LEN ];
\r
344 /* Move into the directory in which the file was created. */
\r
345 ucReturn = f_chdir( pcFullPath );
\r
346 configASSERT( ucReturn == F_NO_ERROR );
\r
348 /* Obtain and print out the working directory. */
\r
349 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
350 printf( "Back in directory %s\r\n", cRAMBuffer );
\r
351 configASSERT( strcmp( ( const char * ) cRAMBuffer, pcFullPath ) == 0 );
\r
353 /* Generate the file name. */
\r
354 sprintf( cFileName, "%s.txt", pcDirectory2 );
\r
356 /* Print out the file name and the directory from which the file is being
\r
358 printf( "Reading file %s in %s\r\n", cFileName, cRAMBuffer );
\r
360 /* This time the file is opened for reading. */
\r
361 pxFile = f_open( cFileName, "r" );
\r
363 /* Read the file 1 byte at a time. */
\r
364 for( iByte = 0; iByte < fsPUTC_FILE_SIZE; iByte++ )
\r
366 iReturned = f_getc( pxFile );
\r
367 configASSERT( iReturned == ( ( int ) '0' + iByte ) );
\r
370 /* Finished so close the file. */
\r
373 /* Move back to the root directory. */
\r
374 ucReturn = f_chdir( "../.." );
\r
375 configASSERT( ucReturn == F_NO_ERROR );
\r
377 /* Obtain and print out the working directory. */
\r
378 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
379 printf( "Back in root directory %s\r\n", cRAMBuffer );
\r