2 FreeRTOS V8.1.1 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
28 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
29 >>! obliged to provide the source code for proprietary components !<<
\r
30 >>! outside of the FreeRTOS kernel. !<<
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
66 /*******************************************************************************
\r
67 * See the URL in the comments within main.c for the location of the online
\r
69 ******************************************************************************/
\r
71 /* Standard includes. */
\r
74 /* FreeRTOS includes. */
\r
75 #include "FreeRTOS.h"
\r
77 /* File system includes. */
\r
79 #include "api_mdriver_ram.h"
\r
81 /* 8.3 format, plus null terminator. */
\r
82 #define fsMAX_FILE_NAME_LEN 13
\r
84 /* The number of bytes read/written to the example files at a time. */
\r
85 #define fsRAM_BUFFER_SIZE 200
\r
87 /* The number of bytes written to the file that uses f_putc() and f_getc(). */
\r
88 #define fsPUTC_FILE_SIZE 100
\r
90 /*-----------------------------------------------------------*/
\r
93 * Creates and verifies different files on the volume, demonstrating the use of
\r
94 * various different API functions.
\r
96 void vCreateAndVerifySampleFiles( void );
\r
99 * Create a set of example files in the root directory of the volume using
\r
102 static void prvCreateDemoFilesUsing_f_write( void );
\r
105 * Use f_read() to read back and verify the files that were created by
\r
106 * prvCreateDemoFilesUsing_f_write().
\r
108 static void prvVerifyDemoFileUsing_f_read( void );
\r
111 * Create an example file in a sub-directory using f_putc().
\r
113 static void prvCreateDemoFileUsing_f_putc( void );
\r
116 * Use f_getc() to read back and verify the file that was created by
\r
117 * prvCreateDemoFileUsing_f_putc().
\r
119 static void prvVerifyDemoFileUsing_f_getc( void );
\r
121 /*-----------------------------------------------------------*/
\r
123 /* A buffer used to both create content to write to disk, and read content back
\r
124 from a disk. Note there is no mutual exclusion on this buffer. */
\r
125 static char cRAMBuffer[ fsRAM_BUFFER_SIZE ];
\r
127 /* Names of directories that are created. */
\r
128 static const char *pcRoot = "/", *pcDirectory1 = "SUB1", *pcDirectory2 = "SUB2", *pcFullPath = "/SUB1/SUB2";
\r
130 /*-----------------------------------------------------------*/
\r
132 void vCreateAndVerifySampleFiles( void )
\r
134 unsigned char ucStatus;
\r
136 /* First create the volume. */
\r
137 ucStatus = f_initvolume( ram_initfunc );
\r
139 /* It is expected that the volume is not formatted. */
\r
140 if( ucStatus == F_ERR_NOTFORMATTED )
\r
142 /* Format the created volume. */
\r
143 ucStatus = f_format( F_FAT12_MEDIA );
\r
146 if( ucStatus == F_NO_ERROR )
\r
148 /* Create a set of files using f_write(). */
\r
149 prvCreateDemoFilesUsing_f_write();
\r
151 /* Read back and verify the files that were created using f_write(). */
\r
152 prvVerifyDemoFileUsing_f_read();
\r
154 /* Create sub directories two deep then create a file using putc. */
\r
155 prvCreateDemoFileUsing_f_putc();
\r
157 /* Read back and verify the file created by
\r
158 prvCreateDemoFileUsing_f_putc(). */
\r
159 prvVerifyDemoFileUsing_f_getc();
\r
162 /*-----------------------------------------------------------*/
\r
164 static void prvCreateDemoFilesUsing_f_write( void )
\r
166 BaseType_t xFileNumber, xWriteNumber;
\r
167 char cFileName[ fsMAX_FILE_NAME_LEN ];
\r
168 const BaseType_t xMaxFiles = 5;
\r
169 long lItemsWritten;
\r
172 /* Create xMaxFiles files. Each created file will be
\r
173 ( xFileNumber * fsRAM_BUFFER_SIZE ) bytes in length, and filled
\r
174 with a different repeating character. */
\r
175 for( xFileNumber = 1; xFileNumber <= xMaxFiles; xFileNumber++ )
\r
177 /* Generate a file name. */
\r
178 sprintf( cFileName, "root%03d.txt", xFileNumber );
\r
180 /* Obtain the current working directory and print out the file name and
\r
181 the directory into which the file is being written. */
\r
182 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
183 printf( "Creating file %s in %s\r\n", cFileName, cRAMBuffer );
\r
185 /* Open the file, creating the file if it does not already exist. */
\r
186 pxFile = f_open( cFileName, "w" );
\r
187 configASSERT( pxFile );
\r
189 /* Fill the RAM buffer with data that will be written to the file. This
\r
190 is just a repeating ascii character that indicates the file number. */
\r
191 memset( cRAMBuffer, ( int ) ( '0' + xFileNumber ), fsRAM_BUFFER_SIZE );
\r
193 /* Write the RAM buffer to the opened file a number of times. The
\r
194 number of times the RAM buffer is written to the file depends on the
\r
195 file number, so the length of each created file will be different. */
\r
196 for( xWriteNumber = 0; xWriteNumber < xFileNumber; xWriteNumber++ )
\r
198 lItemsWritten = f_write( cRAMBuffer, fsRAM_BUFFER_SIZE, 1, pxFile );
\r
199 configASSERT( lItemsWritten == 1 );
\r
202 /* Close the file so another file can be created. */
\r
206 /*-----------------------------------------------------------*/
\r
208 static void prvVerifyDemoFileUsing_f_read( void )
\r
210 BaseType_t xFileNumber, xReadNumber;
\r
211 char cFileName[ fsMAX_FILE_NAME_LEN ];
\r
212 const BaseType_t xMaxFiles = 5;
\r
213 long lItemsRead, lChar;
\r
216 /* Read back the files that were created by
\r
217 prvCreateDemoFilesUsing_f_write(). */
\r
218 for( xFileNumber = 1; xFileNumber <= xMaxFiles; xFileNumber++ )
\r
220 /* Generate the file name. */
\r
221 sprintf( cFileName, "root%03d.txt", xFileNumber );
\r
223 /* Obtain the current working directory and print out the file name and
\r
224 the directory from which the file is being read. */
\r
225 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
226 printf( "Reading file %s from %s\r\n", cFileName, cRAMBuffer );
\r
228 /* Open the file for reading. */
\r
229 pxFile = f_open( cFileName, "r" );
\r
230 configASSERT( pxFile );
\r
232 /* Read the file into the RAM buffer, checking the file contents are as
\r
233 expected. The size of the file depends on the file number. */
\r
234 for( xReadNumber = 0; xReadNumber < xFileNumber; xReadNumber++ )
\r
236 /* Start with the RAM buffer clear. */
\r
237 memset( cRAMBuffer, 0x00, fsRAM_BUFFER_SIZE );
\r
239 lItemsRead = f_read( cRAMBuffer, fsRAM_BUFFER_SIZE, 1, pxFile );
\r
240 configASSERT( lItemsRead == 1 );
\r
242 /* Check the RAM buffer is filled with the expected data. Each
\r
243 file contains a different repeating ascii character that indicates
\r
244 the number of the file. */
\r
245 for( lChar = 0; lChar < fsRAM_BUFFER_SIZE; lChar++ )
\r
247 configASSERT( cRAMBuffer[ lChar ] == ( '0' + ( char ) xFileNumber ) );
\r
251 /* Close the file. */
\r
255 /*-----------------------------------------------------------*/
\r
257 static void prvCreateDemoFileUsing_f_putc( void )
\r
259 unsigned char ucReturn;
\r
260 int iByte, iReturned;
\r
262 char cFileName[ fsMAX_FILE_NAME_LEN ];
\r
264 /* Obtain and print out the working directory. */
\r
265 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
266 printf( "In directory %s\r\n", cRAMBuffer );
\r
268 /* Create a sub directory. */
\r
269 ucReturn = f_mkdir( pcDirectory1 );
\r
270 configASSERT( ucReturn == F_NO_ERROR );
\r
272 /* Move into the created sub-directory. */
\r
273 ucReturn = f_chdir( pcDirectory1 );
\r
274 configASSERT( ucReturn == F_NO_ERROR );
\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 subdirectory in the new directory. */
\r
281 ucReturn = f_mkdir( pcDirectory2 );
\r
282 configASSERT( ucReturn == F_NO_ERROR );
\r
284 /* Move into the directory just created - now two directories down from
\r
286 ucReturn = f_chdir( pcDirectory2 );
\r
287 configASSERT( ucReturn == F_NO_ERROR );
\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 configASSERT( strcmp( cRAMBuffer, pcFullPath ) == 0 );
\r
294 /* Generate the file name. */
\r
295 sprintf( cFileName, "%s.txt", pcDirectory2 );
\r
297 /* Print out the file name and the directory into which the file is being
\r
299 printf( "Writing file %s in %s\r\n", cFileName, cRAMBuffer );
\r
301 pxFile = f_open( cFileName, "w" );
\r
303 /* Create a file 1 byte at a time. The file is filled with incrementing
\r
304 ascii characters starting from '0'. */
\r
305 for( iByte = 0; iByte < fsPUTC_FILE_SIZE; iByte++ )
\r
307 iReturned = f_putc( ( ( int ) '0' + iByte ), pxFile );
\r
308 configASSERT( iReturned == ( ( int ) '0' + iByte ) );
\r
311 /* Finished so close the file. */
\r
314 /* Move back to the root directory. */
\r
315 ucReturn = f_chdir( "../.." );
\r
316 configASSERT( ucReturn == F_NO_ERROR );
\r
318 /* Obtain and print out the working directory. */
\r
319 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
320 printf( "Back in root directory %s\r\n", cRAMBuffer );
\r
321 configASSERT( strcmp( cRAMBuffer, pcRoot ) == 0 );
\r
323 /*-----------------------------------------------------------*/
\r
325 static void prvVerifyDemoFileUsing_f_getc( void )
\r
327 unsigned char ucReturn;
\r
328 int iByte, iReturned;
\r
330 char cFileName[ fsMAX_FILE_NAME_LEN ];
\r
332 /* Move into the directory in which the file was created. */
\r
333 ucReturn = f_chdir( pcFullPath );
\r
334 configASSERT( ucReturn == F_NO_ERROR );
\r
336 /* Obtain and print out the working directory. */
\r
337 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
338 printf( "Back in directory %s\r\n", cRAMBuffer );
\r
339 configASSERT( strcmp( cRAMBuffer, pcFullPath ) == 0 );
\r
341 /* Generate the file name. */
\r
342 sprintf( cFileName, "%s.txt", pcDirectory2 );
\r
344 /* Print out the file name and the directory from which the file is being
\r
346 printf( "Reading file %s in %s\r\n", cFileName, cRAMBuffer );
\r
348 /* This time the file is opened for reading. */
\r
349 pxFile = f_open( cFileName, "r" );
\r
351 /* Read the file 1 byte at a time. */
\r
352 for( iByte = 0; iByte < fsPUTC_FILE_SIZE; iByte++ )
\r
354 iReturned = f_getc( pxFile );
\r
355 configASSERT( iReturned == ( ( int ) '0' + iByte ) );
\r
358 /* Finished so close the file. */
\r
361 /* Move back to the root directory. */
\r
362 ucReturn = f_chdir( "../.." );
\r
363 configASSERT( ucReturn == F_NO_ERROR );
\r
365 /* Obtain and print out the working directory. */
\r
366 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
367 printf( "Back in root directory %s\r\n", cRAMBuffer );
\r