2 FreeRTOS V8.2.1 - Copyright (C) 2015 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
78 /* FreeRTOS includes. */
\r
79 #include "FreeRTOS.h"
\r
81 /* File system includes. */
\r
83 #include "api_mdriver_ram.h"
\r
85 /* 8.3 format, plus null terminator. */
\r
86 #define fsMAX_FILE_NAME_LEN 13
\r
88 /* The number of bytes read/written to the example files at a time. */
\r
89 #define fsRAM_BUFFER_SIZE 200
\r
91 /* The number of bytes written to the file that uses f_putc() and f_getc(). */
\r
92 #define fsPUTC_FILE_SIZE 100
\r
94 /*-----------------------------------------------------------*/
\r
97 * Creates and verifies different files on the volume, demonstrating the use of
\r
98 * various different API functions.
\r
100 void vCreateAndVerifySampleFiles( void );
\r
103 * Create a set of example files in the root directory of the volume using
\r
106 static void prvCreateDemoFilesUsing_f_write( void );
\r
109 * Use f_read() to read back and verify the files that were created by
\r
110 * prvCreateDemoFilesUsing_f_write().
\r
112 static void prvVerifyDemoFileUsing_f_read( void );
\r
115 * Create an example file in a sub-directory using f_putc().
\r
117 static void prvCreateDemoFileUsing_f_putc( void );
\r
120 * Use f_getc() to read back and verify the file that was created by
\r
121 * prvCreateDemoFileUsing_f_putc().
\r
123 static void prvVerifyDemoFileUsing_f_getc( void );
\r
125 /*-----------------------------------------------------------*/
\r
127 /* A buffer used to both create content to write to disk, and read content back
\r
128 from a disk. Note there is no mutual exclusion on this buffer. */
\r
129 static char cRAMBuffer[ fsRAM_BUFFER_SIZE ];
\r
131 /* Names of directories that are created. */
\r
132 static const char *pcRoot = "/", *pcDirectory1 = "SUB1", *pcDirectory2 = "SUB2", *pcFullPath = "/SUB1/SUB2";
\r
134 /*-----------------------------------------------------------*/
\r
136 void vCreateAndVerifySampleFiles( void )
\r
138 unsigned char ucStatus;
\r
140 /* First create the volume. */
\r
141 ucStatus = f_initvolume( ram_initfunc );
\r
143 /* It is expected that the volume is not formatted. */
\r
144 if( ucStatus == F_ERR_NOTFORMATTED )
\r
146 /* Format the created volume. */
\r
147 ucStatus = f_format( F_FAT12_MEDIA );
\r
150 if( ucStatus == F_NO_ERROR )
\r
152 /* Create a set of files using f_write(). */
\r
153 prvCreateDemoFilesUsing_f_write();
\r
155 /* Read back and verify the files that were created using f_write(). */
\r
156 prvVerifyDemoFileUsing_f_read();
\r
158 /* Create sub directories two deep then create a file using putc. */
\r
159 prvCreateDemoFileUsing_f_putc();
\r
161 /* Read back and verify the file created by
\r
162 prvCreateDemoFileUsing_f_putc(). */
\r
163 prvVerifyDemoFileUsing_f_getc();
\r
166 /*-----------------------------------------------------------*/
\r
168 static void prvCreateDemoFilesUsing_f_write( void )
\r
170 BaseType_t xFileNumber, xWriteNumber;
\r
171 char cFileName[ fsMAX_FILE_NAME_LEN ];
\r
172 const BaseType_t xMaxFiles = 5;
\r
173 long lItemsWritten;
\r
176 /* Create xMaxFiles files. Each created file will be
\r
177 ( xFileNumber * fsRAM_BUFFER_SIZE ) bytes in length, and filled
\r
178 with a different repeating character. */
\r
179 for( xFileNumber = 1; xFileNumber <= xMaxFiles; xFileNumber++ )
\r
181 /* Generate a file name. */
\r
182 sprintf( cFileName, "root%03d.txt", xFileNumber );
\r
184 /* Obtain the current working directory and print out the file name and
\r
185 the directory into which the file is being written. */
\r
186 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
187 printf( "Creating file %s in %s\r\n", cFileName, cRAMBuffer );
\r
189 /* Open the file, creating the file if it does not already exist. */
\r
190 pxFile = f_open( cFileName, "w" );
\r
191 configASSERT( pxFile );
\r
193 /* Fill the RAM buffer with data that will be written to the file. This
\r
194 is just a repeating ascii character that indicates the file number. */
\r
195 memset( cRAMBuffer, ( int ) ( '0' + xFileNumber ), fsRAM_BUFFER_SIZE );
\r
197 /* Write the RAM buffer to the opened file a number of times. The
\r
198 number of times the RAM buffer is written to the file depends on the
\r
199 file number, so the length of each created file will be different. */
\r
200 for( xWriteNumber = 0; xWriteNumber < xFileNumber; xWriteNumber++ )
\r
202 lItemsWritten = f_write( cRAMBuffer, fsRAM_BUFFER_SIZE, 1, pxFile );
\r
203 configASSERT( lItemsWritten == 1 );
\r
206 /* Close the file so another file can be created. */
\r
210 /*-----------------------------------------------------------*/
\r
212 static void prvVerifyDemoFileUsing_f_read( void )
\r
214 BaseType_t xFileNumber, xReadNumber;
\r
215 char cFileName[ fsMAX_FILE_NAME_LEN ];
\r
216 const BaseType_t xMaxFiles = 5;
\r
217 long lItemsRead, lChar;
\r
220 /* Read back the files that were created by
\r
221 prvCreateDemoFilesUsing_f_write(). */
\r
222 for( xFileNumber = 1; xFileNumber <= xMaxFiles; xFileNumber++ )
\r
224 /* Generate the file name. */
\r
225 sprintf( cFileName, "root%03d.txt", xFileNumber );
\r
227 /* Obtain the current working directory and print out the file name and
\r
228 the directory from which the file is being read. */
\r
229 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
230 printf( "Reading file %s from %s\r\n", cFileName, cRAMBuffer );
\r
232 /* Open the file for reading. */
\r
233 pxFile = f_open( cFileName, "r" );
\r
234 configASSERT( pxFile );
\r
236 /* Read the file into the RAM buffer, checking the file contents are as
\r
237 expected. The size of the file depends on the file number. */
\r
238 for( xReadNumber = 0; xReadNumber < xFileNumber; xReadNumber++ )
\r
240 /* Start with the RAM buffer clear. */
\r
241 memset( cRAMBuffer, 0x00, fsRAM_BUFFER_SIZE );
\r
243 lItemsRead = f_read( cRAMBuffer, fsRAM_BUFFER_SIZE, 1, pxFile );
\r
244 configASSERT( lItemsRead == 1 );
\r
246 /* Check the RAM buffer is filled with the expected data. Each
\r
247 file contains a different repeating ascii character that indicates
\r
248 the number of the file. */
\r
249 for( lChar = 0; lChar < fsRAM_BUFFER_SIZE; lChar++ )
\r
251 configASSERT( cRAMBuffer[ lChar ] == ( '0' + ( char ) xFileNumber ) );
\r
255 /* Close the file. */
\r
259 /*-----------------------------------------------------------*/
\r
261 static void prvCreateDemoFileUsing_f_putc( void )
\r
263 unsigned char ucReturn;
\r
264 int iByte, iReturned;
\r
266 char cFileName[ fsMAX_FILE_NAME_LEN ];
\r
268 /* Obtain and print out the working directory. */
\r
269 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
270 printf( "In directory %s\r\n", cRAMBuffer );
\r
272 /* Create a sub directory. */
\r
273 ucReturn = f_mkdir( pcDirectory1 );
\r
274 configASSERT( ucReturn == F_NO_ERROR );
\r
276 /* Move into the created sub-directory. */
\r
277 ucReturn = f_chdir( pcDirectory1 );
\r
278 configASSERT( ucReturn == F_NO_ERROR );
\r
280 /* Obtain and print out the working directory. */
\r
281 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
282 printf( "In directory %s\r\n", cRAMBuffer );
\r
284 /* Create a subdirectory in the new directory. */
\r
285 ucReturn = f_mkdir( pcDirectory2 );
\r
286 configASSERT( ucReturn == F_NO_ERROR );
\r
288 /* Move into the directory just created - now two directories down from
\r
290 ucReturn = f_chdir( pcDirectory2 );
\r
291 configASSERT( ucReturn == F_NO_ERROR );
\r
293 /* Obtain and print out the working directory. */
\r
294 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
295 printf( "In directory %s\r\n", cRAMBuffer );
\r
296 configASSERT( strcmp( cRAMBuffer, pcFullPath ) == 0 );
\r
298 /* Generate the file name. */
\r
299 sprintf( cFileName, "%s.txt", pcDirectory2 );
\r
301 /* Print out the file name and the directory into which the file is being
\r
303 printf( "Writing file %s in %s\r\n", cFileName, cRAMBuffer );
\r
305 pxFile = f_open( cFileName, "w" );
\r
307 /* Create a file 1 byte at a time. The file is filled with incrementing
\r
308 ascii characters starting from '0'. */
\r
309 for( iByte = 0; iByte < fsPUTC_FILE_SIZE; iByte++ )
\r
311 iReturned = f_putc( ( ( int ) '0' + iByte ), pxFile );
\r
312 configASSERT( iReturned == ( ( int ) '0' + iByte ) );
\r
315 /* Finished so close the file. */
\r
318 /* Move back to the root directory. */
\r
319 ucReturn = f_chdir( "../.." );
\r
320 configASSERT( ucReturn == F_NO_ERROR );
\r
322 /* Obtain and print out the working directory. */
\r
323 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
324 printf( "Back in root directory %s\r\n", cRAMBuffer );
\r
325 configASSERT( strcmp( cRAMBuffer, pcRoot ) == 0 );
\r
327 /*-----------------------------------------------------------*/
\r
329 static void prvVerifyDemoFileUsing_f_getc( void )
\r
331 unsigned char ucReturn;
\r
332 int iByte, iReturned;
\r
334 char cFileName[ fsMAX_FILE_NAME_LEN ];
\r
336 /* Move into the directory in which the file was created. */
\r
337 ucReturn = f_chdir( pcFullPath );
\r
338 configASSERT( ucReturn == F_NO_ERROR );
\r
340 /* Obtain and print out the working directory. */
\r
341 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
342 printf( "Back in directory %s\r\n", cRAMBuffer );
\r
343 configASSERT( strcmp( cRAMBuffer, pcFullPath ) == 0 );
\r
345 /* Generate the file name. */
\r
346 sprintf( cFileName, "%s.txt", pcDirectory2 );
\r
348 /* Print out the file name and the directory from which the file is being
\r
350 printf( "Reading file %s in %s\r\n", cFileName, cRAMBuffer );
\r
352 /* This time the file is opened for reading. */
\r
353 pxFile = f_open( cFileName, "r" );
\r
355 /* Read the file 1 byte at a time. */
\r
356 for( iByte = 0; iByte < fsPUTC_FILE_SIZE; iByte++ )
\r
358 iReturned = f_getc( pxFile );
\r
359 configASSERT( iReturned == ( ( int ) '0' + iByte ) );
\r
362 /* Finished so close the file. */
\r
365 /* Move back to the root directory. */
\r
366 ucReturn = f_chdir( "../.." );
\r
367 configASSERT( ucReturn == F_NO_ERROR );
\r
369 /* Obtain and print out the working directory. */
\r
370 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
371 printf( "Back in root directory %s\r\n", cRAMBuffer );
\r