2 FreeRTOS V7.6.0 - Copyright (C) 2013 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 distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\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
75 /* FreeRTOS includes. */
\r
76 #include "FreeRTOS.h"
\r
78 /* File system includes. */
\r
80 #include "api_mdriver_ram.h"
\r
82 /* 8.3 format, plus null terminator. */
\r
83 #define fsMAX_FILE_NAME_LEN 13
\r
85 /* The number of bytes read/written to the example files at a time. */
\r
86 #define fsRAM_BUFFER_SIZE 200
\r
88 /* The number of bytes written to the file that uses f_putc() and f_getc(). */
\r
89 #define fsPUTC_FILE_SIZE 100
\r
91 /*-----------------------------------------------------------*/
\r
94 * Creates and verifies different files on the volume, demonstrating the use of
\r
95 * various different API functions.
\r
97 void vCreateAndVerifySampleFiles( void );
\r
100 * Create a set of example files in the root directory of the volume using
\r
103 static void prvCreateDemoFilesUsing_f_write( void );
\r
106 * Use f_read() to read back and verify the files that were created by
\r
107 * prvCreateDemoFilesUsing_f_write().
\r
109 static void prvVerifyDemoFileUsing_f_read( void );
\r
112 * Create an example file in a sub-directory using f_putc().
\r
114 static void prvCreateDemoFileUsing_f_putc( void );
\r
117 * Use f_getc() to read back and verify the file that was created by
\r
118 * prvCreateDemoFileUsing_f_putc().
\r
120 static void prvVerifyDemoFileUsing_f_getc( void );
\r
122 /*-----------------------------------------------------------*/
\r
124 /* A buffer used to both create content to write to disk, and read content back
\r
125 from a disk. Note there is no mutual exclusion on this buffer. */
\r
126 static char cRAMBuffer[ fsRAM_BUFFER_SIZE ];
\r
128 /* Names of directories that are created. */
\r
129 static const char *pcRoot = "/", *pcDirectory1 = "SUB1", *pcDirectory2 = "SUB2", *pcFullPath = "/SUB1/SUB2";
\r
131 /*-----------------------------------------------------------*/
\r
133 void vCreateAndVerifySampleFiles( void )
\r
135 unsigned char ucStatus;
\r
137 /* First create the volume. */
\r
138 ucStatus = f_initvolume( ram_initfunc );
\r
140 /* It is expected that the volume is not formatted. */
\r
141 if( ucStatus == F_ERR_NOTFORMATTED )
\r
143 /* Format the created volume. */
\r
144 ucStatus = f_format( F_FAT12_MEDIA );
\r
147 if( ucStatus == F_NO_ERROR )
\r
149 /* Create a set of files using f_write(). */
\r
150 prvCreateDemoFilesUsing_f_write();
\r
152 /* Read back and verify the files that were created using f_write(). */
\r
153 prvVerifyDemoFileUsing_f_read();
\r
155 /* Create sub directories two deep then create a file using putc. */
\r
156 prvCreateDemoFileUsing_f_putc();
\r
158 /* Read back and verify the file created by
\r
159 prvCreateDemoFileUsing_f_putc(). */
\r
160 prvVerifyDemoFileUsing_f_getc();
\r
163 /*-----------------------------------------------------------*/
\r
165 static void prvCreateDemoFilesUsing_f_write( void )
\r
167 portBASE_TYPE xFileNumber, xWriteNumber;
\r
168 char cFileName[ fsMAX_FILE_NAME_LEN ];
\r
169 const portBASE_TYPE xMaxFiles = 5;
\r
170 long lItemsWritten;
\r
173 /* Create xMaxFiles files. Each created file will be
\r
174 ( xFileNumber * fsRAM_BUFFER_SIZE ) bytes in length, and filled
\r
175 with a different repeating character. */
\r
176 for( xFileNumber = 1; xFileNumber <= xMaxFiles; xFileNumber++ )
\r
178 /* Generate a file name. */
\r
179 sprintf( cFileName, "root%03d.txt", ( int ) xFileNumber );
\r
181 /* Obtain the current working directory and print out the file name and
\r
182 the directory into which the file is being written. */
\r
183 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\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 portBASE_TYPE xFileNumber, xReadNumber;
\r
211 char cFileName[ fsMAX_FILE_NAME_LEN ];
\r
212 const portBASE_TYPE 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", ( int ) 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
227 /* Open the file for reading. */
\r
228 pxFile = f_open( cFileName, "r" );
\r
229 configASSERT( pxFile );
\r
231 /* Read the file into the RAM buffer, checking the file contents are as
\r
232 expected. The size of the file depends on the file number. */
\r
233 for( xReadNumber = 0; xReadNumber < xFileNumber; xReadNumber++ )
\r
235 /* Start with the RAM buffer clear. */
\r
236 memset( cRAMBuffer, 0x00, fsRAM_BUFFER_SIZE );
\r
238 lItemsRead = f_read( cRAMBuffer, fsRAM_BUFFER_SIZE, 1, pxFile );
\r
239 configASSERT( lItemsRead == 1 );
\r
241 /* Check the RAM buffer is filled with the expected data. Each
\r
242 file contains a different repeating ascii character that indicates
\r
243 the number of the file. */
\r
244 for( lChar = 0; lChar < fsRAM_BUFFER_SIZE; lChar++ )
\r
246 configASSERT( cRAMBuffer[ lChar ] == ( '0' + ( char ) xFileNumber ) );
\r
250 /* Close the file. */
\r
254 /*-----------------------------------------------------------*/
\r
256 static void prvCreateDemoFileUsing_f_putc( void )
\r
258 unsigned char ucReturn;
\r
259 int iByte, iReturned;
\r
261 char cFileName[ fsMAX_FILE_NAME_LEN ];
\r
263 /* Obtain and print out the working directory. */
\r
264 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
266 /* Create a sub directory. */
\r
267 ucReturn = f_mkdir( pcDirectory1 );
\r
268 configASSERT( ucReturn == F_NO_ERROR );
\r
270 /* Move into the created sub-directory. */
\r
271 ucReturn = f_chdir( pcDirectory1 );
\r
272 configASSERT( ucReturn == F_NO_ERROR );
\r
274 /* Obtain and print out the working directory. */
\r
275 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
277 /* Create a subdirectory in the new directory. */
\r
278 ucReturn = f_mkdir( pcDirectory2 );
\r
279 configASSERT( ucReturn == F_NO_ERROR );
\r
281 /* Move into the directory just created - now two directories down from
\r
283 ucReturn = f_chdir( pcDirectory2 );
\r
284 configASSERT( ucReturn == F_NO_ERROR );
\r
286 /* Obtain and print out the working directory. */
\r
287 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
288 configASSERT( strcmp( ( const char * ) cRAMBuffer, pcFullPath ) == 0 );
\r
290 /* Generate the file name. */
\r
291 sprintf( cFileName, "%s.txt", pcDirectory2 );
\r
293 /* Print out the file name and the directory into which the file is being
\r
295 pxFile = f_open( cFileName, "w" );
\r
297 /* Create a file 1 byte at a time. The file is filled with incrementing
\r
298 ascii characters starting from '0'. */
\r
299 for( iByte = 0; iByte < fsPUTC_FILE_SIZE; iByte++ )
\r
301 iReturned = f_putc( ( ( int ) '0' + iByte ), pxFile );
\r
302 configASSERT( iReturned == ( ( int ) '0' + iByte ) );
\r
305 /* Finished so close the file. */
\r
308 /* Move back to the root directory. */
\r
309 ucReturn = f_chdir( "../.." );
\r
310 configASSERT( ucReturn == F_NO_ERROR );
\r
312 /* Obtain and print out the working directory. */
\r
313 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
314 configASSERT( strcmp( ( const char * ) cRAMBuffer, pcRoot ) == 0 );
\r
316 /*-----------------------------------------------------------*/
\r
318 static void prvVerifyDemoFileUsing_f_getc( void )
\r
320 unsigned char ucReturn;
\r
321 int iByte, iReturned;
\r
323 char cFileName[ fsMAX_FILE_NAME_LEN ];
\r
325 /* Move into the directory in which the file was created. */
\r
326 ucReturn = f_chdir( pcFullPath );
\r
327 configASSERT( ucReturn == F_NO_ERROR );
\r
329 /* Obtain and print out the working directory. */
\r
330 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
331 configASSERT( strcmp( ( const char * ) cRAMBuffer, pcFullPath ) == 0 );
\r
333 /* Generate the file name. */
\r
334 sprintf( cFileName, "%s.txt", pcDirectory2 );
\r
336 /* This time the file is opened for reading. */
\r
337 pxFile = f_open( cFileName, "r" );
\r
339 /* Read the file 1 byte at a time. */
\r
340 for( iByte = 0; iByte < fsPUTC_FILE_SIZE; iByte++ )
\r
342 iReturned = f_getc( pxFile );
\r
343 configASSERT( iReturned == ( ( int ) '0' + iByte ) );
\r
346 /* Finished so close the file. */
\r
349 /* Move back to the root directory. */
\r
350 ucReturn = f_chdir( "../.." );
\r
351 configASSERT( ucReturn == F_NO_ERROR );
\r
353 /* Obtain and print out the working directory. */
\r
354 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r