2 FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\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
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
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
25 ***************************************************************************
\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
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
37 ***************************************************************************
\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
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
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
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
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
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
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
70 /*******************************************************************************
\r
71 * See the URL in the comments within main.c for the location of the online
\r
73 ******************************************************************************/
\r
75 /* Standard includes. */
\r
79 /* FreeRTOS includes. */
\r
80 #include "FreeRTOS.h"
\r
83 /* File system includes. */
\r
85 #include "api_mdriver_ram.h"
\r
87 /* 8.3 format, plus null terminator. */
\r
88 #define fsMAX_FILE_NAME_LEN 13
\r
90 /* The number of bytes read/written to the example files at a time. */
\r
91 #define fsRAM_BUFFER_SIZE 200
\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
96 /* The number of files created in root. */
\r
97 #define fsROOT_FILES 5
\r
99 /*-----------------------------------------------------------*/
\r
102 * Creates and verifies different files on the volume, demonstrating the use of
\r
103 * various different API functions.
\r
105 void vCreateAndVerifySampleFiles( void );
\r
108 * Create a set of example files in the root directory of the volume using
\r
111 static void prvCreateDemoFilesUsing_f_write( void );
\r
114 * Use f_read() to read back and verify the files that were created by
\r
115 * prvCreateDemoFilesUsing_f_write().
\r
117 static void prvVerifyDemoFileUsing_f_read( void );
\r
120 * Create an example file in a sub-directory using f_putc().
\r
122 static void prvCreateDemoFileUsing_f_putc( void );
\r
125 * Use f_getc() to read back and verify the file that was created by
\r
126 * prvCreateDemoFileUsing_f_putc().
\r
128 static void prvVerifyDemoFileUsing_f_getc( void );
\r
130 /*-----------------------------------------------------------*/
\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
136 /* Names of directories that are created. */
\r
137 static const char *pcRoot = "/", *pcDirectory1 = "SUB1", *pcDirectory2 = "SUB2", *pcFullPath = "/SUB1/SUB2";
\r
139 /*-----------------------------------------------------------*/
\r
141 void vCreateAndVerifySampleFiles( void )
\r
143 unsigned char ucStatus;
\r
145 /* First create the volume. */
\r
146 ucStatus = f_initvolume( ram_initfunc );
\r
148 /* It is expected that the volume is not formatted. */
\r
149 if( ucStatus == F_ERR_NOTFORMATTED )
\r
151 /* Format the created volume. */
\r
152 ucStatus = f_format( F_FAT12_MEDIA );
\r
155 if( ucStatus == F_NO_ERROR )
\r
157 /* Create a set of files using f_write(). */
\r
158 prvCreateDemoFilesUsing_f_write();
\r
160 /* Read back and verify the files that were created using f_write(). */
\r
161 prvVerifyDemoFileUsing_f_read();
\r
163 /* Create sub directories two deep then create a file using putc. */
\r
164 prvCreateDemoFileUsing_f_putc();
\r
166 /* Read back and verify the file created by
\r
167 prvCreateDemoFileUsing_f_putc(). */
\r
168 prvVerifyDemoFileUsing_f_getc();
\r
171 /*-----------------------------------------------------------*/
\r
173 static void prvCreateDemoFilesUsing_f_write( void )
\r
175 portBASE_TYPE xFileNumber, xWriteNumber;
\r
176 char cFileName[ fsMAX_FILE_NAME_LEN ];
\r
177 long lItemsWritten;
\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
185 /* Generate a file name. */
\r
186 sprintf( cFileName, "root%03d.txt", xFileNumber );
\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
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
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
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
206 lItemsWritten = f_write( cRAMBuffer, fsRAM_BUFFER_SIZE, 1, pxFile );
\r
207 configASSERT( lItemsWritten == 1 );
\r
210 /* Close the file so another file can be created. */
\r
214 /*-----------------------------------------------------------*/
\r
216 static void prvVerifyDemoFileUsing_f_read( void )
\r
218 portBASE_TYPE xFileNumber, xReadNumber;
\r
219 char cFileName[ fsMAX_FILE_NAME_LEN ];
\r
220 long lItemsRead, lChar;
\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
227 /* Generate the file name. */
\r
228 sprintf( cFileName, "root%03d.txt", xFileNumber );
\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
235 /* Open the file for reading. */
\r
236 pxFile = f_open( cFileName, "r" );
\r
237 configASSERT( pxFile );
\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
243 /* Start with the RAM buffer clear. */
\r
244 memset( cRAMBuffer, 0x00, fsRAM_BUFFER_SIZE );
\r
246 lItemsRead = f_read( cRAMBuffer, fsRAM_BUFFER_SIZE, 1, pxFile );
\r
247 configASSERT( lItemsRead == 1 );
\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
254 configASSERT( cRAMBuffer[ lChar ] == ( '0' + ( char ) xFileNumber ) );
\r
258 /* Close the file. */
\r
262 /*-----------------------------------------------------------*/
\r
264 static void prvCreateDemoFileUsing_f_putc( void )
\r
266 unsigned char ucReturn;
\r
267 int iByte, iReturned;
\r
269 char cFileName[ fsMAX_FILE_NAME_LEN ];
\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
275 /* Create a sub directory. */
\r
276 ucReturn = f_mkdir( pcDirectory1 );
\r
277 configASSERT( ucReturn == F_NO_ERROR );
\r
279 /* Move into the created sub-directory. */
\r
280 ucReturn = f_chdir( pcDirectory1 );
\r
281 configASSERT( ucReturn == F_NO_ERROR );
\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
287 /* Create a subdirectory in the new directory. */
\r
288 ucReturn = f_mkdir( pcDirectory2 );
\r
289 configASSERT( ucReturn == F_NO_ERROR );
\r
291 /* Move into the directory just created - now two directories down from
\r
293 ucReturn = f_chdir( pcDirectory2 );
\r
294 configASSERT( ucReturn == F_NO_ERROR );
\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
301 /* Generate the file name. */
\r
302 sprintf( cFileName, "%s.txt", pcDirectory2 );
\r
304 /* Print out the file name and the directory into which the file is being
\r
306 printf( "Writing file %s in %s\r\n", cFileName, cRAMBuffer );
\r
308 pxFile = f_open( cFileName, "w" );
\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
314 iReturned = f_putc( ( ( int ) '0' + iByte ), pxFile );
\r
315 configASSERT( iReturned == ( ( int ) '0' + iByte ) );
\r
318 /* Finished so close the file. */
\r
321 /* Move back to the root directory. */
\r
322 ucReturn = f_chdir( "../.." );
\r
323 configASSERT( ucReturn == F_NO_ERROR );
\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
330 /*-----------------------------------------------------------*/
\r
332 static void prvVerifyDemoFileUsing_f_getc( void )
\r
334 unsigned char ucReturn;
\r
335 int iByte, iReturned;
\r
337 char cFileName[ fsMAX_FILE_NAME_LEN ];
\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
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
348 /* Generate the file name. */
\r
349 sprintf( cFileName, "%s.txt", pcDirectory2 );
\r
351 /* Print out the file name and the directory from which the file is being
\r
353 printf( "Reading file %s in %s\r\n", cFileName, cRAMBuffer );
\r
355 /* This time the file is opened for reading. */
\r
356 pxFile = f_open( cFileName, "r" );
\r
358 /* Read the file 1 byte at a time. */
\r
359 for( iByte = 0; iByte < fsPUTC_FILE_SIZE; iByte++ )
\r
361 iReturned = f_getc( pxFile );
\r
362 configASSERT( iReturned == ( ( int ) '0' + iByte ) );
\r
365 /* Finished so close the file. */
\r
368 /* Move back to the root directory. */
\r
369 ucReturn = f_chdir( "../.." );
\r
370 configASSERT( ucReturn == F_NO_ERROR );
\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