2 FreeRTOS V9.0.0rc1 - 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
82 /* File system includes. */
\r
84 #include "api_mdriver_ram.h"
\r
86 /* 8.3 format, plus null terminator. */
\r
87 #define fsMAX_FILE_NAME_LEN 13
\r
89 /* The number of bytes read/written to the example files at a time. */
\r
90 #define fsRAM_BUFFER_SIZE 200
\r
92 /* The number of bytes written to the file that uses f_putc() and f_getc(). */
\r
93 #define fsPUTC_FILE_SIZE 100
\r
95 /*-----------------------------------------------------------*/
\r
98 * Creates and verifies different files on the volume, demonstrating the use of
\r
99 * various different API functions.
\r
101 void vCreateAndVerifySampleFiles( void );
\r
104 * Create a set of example files in the root directory of the volume using
\r
107 static void prvCreateDemoFilesUsing_f_write( void );
\r
110 * Use f_read() to read back and verify the files that were created by
\r
111 * prvCreateDemoFilesUsing_f_write().
\r
113 static void prvVerifyDemoFileUsing_f_read( void );
\r
116 * Create an example file in a sub-directory using f_putc().
\r
118 static void prvCreateDemoFileUsing_f_putc( void );
\r
121 * Use f_getc() to read back and verify the file that was created by
\r
122 * prvCreateDemoFileUsing_f_putc().
\r
124 static void prvVerifyDemoFileUsing_f_getc( void );
\r
126 /*-----------------------------------------------------------*/
\r
128 /* A buffer used to both create content to write to disk, and read content back
\r
129 from a disk. Note there is no mutual exclusion on this buffer. */
\r
130 static char cRAMBuffer[ fsRAM_BUFFER_SIZE ];
\r
132 /* Names of directories that are created. */
\r
133 static const char *pcRoot = "/", *pcDirectory1 = "SUB1", *pcDirectory2 = "SUB2", *pcFullPath = "/SUB1/SUB2";
\r
135 /*-----------------------------------------------------------*/
\r
137 void vCreateAndVerifySampleFiles( void )
\r
139 unsigned char ucStatus;
\r
141 /* First create the volume. */
\r
142 ucStatus = f_initvolume( ram_initfunc );
\r
144 /* It is expected that the volume is not formatted. */
\r
145 if( ucStatus == F_ERR_NOTFORMATTED )
\r
147 /* Format the created volume. */
\r
148 ucStatus = f_format( F_FAT12_MEDIA );
\r
151 if( ucStatus == F_NO_ERROR )
\r
153 /* Create a set of files using f_write(). */
\r
154 prvCreateDemoFilesUsing_f_write();
\r
156 /* Read back and verify the files that were created using f_write(). */
\r
157 prvVerifyDemoFileUsing_f_read();
\r
159 /* Create sub directories two deep then create a file using putc. */
\r
160 prvCreateDemoFileUsing_f_putc();
\r
162 /* Read back and verify the file created by
\r
163 prvCreateDemoFileUsing_f_putc(). */
\r
164 prvVerifyDemoFileUsing_f_getc();
\r
167 /*-----------------------------------------------------------*/
\r
169 static void prvCreateDemoFilesUsing_f_write( void )
\r
171 BaseType_t xFileNumber, xWriteNumber;
\r
172 char cFileName[ fsMAX_FILE_NAME_LEN ];
\r
173 const BaseType_t xMaxFiles = 5;
\r
174 long lItemsWritten;
\r
177 /* Create xMaxFiles files. Each created file will be
\r
178 ( xFileNumber * fsRAM_BUFFER_SIZE ) bytes in length, and filled
\r
179 with a different repeating character. */
\r
180 for( xFileNumber = 1; xFileNumber <= xMaxFiles; xFileNumber++ )
\r
182 /* Generate a file name. */
\r
183 sprintf( cFileName, "root%03d.txt", ( int ) xFileNumber );
\r
185 /* Obtain the current working directory and print out the file name and
\r
186 the directory into which the file is being written. */
\r
187 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\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", ( int ) 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
231 /* Open the file for reading. */
\r
232 pxFile = f_open( cFileName, "r" );
\r
233 configASSERT( pxFile );
\r
235 /* Read the file into the RAM buffer, checking the file contents are as
\r
236 expected. The size of the file depends on the file number. */
\r
237 for( xReadNumber = 0; xReadNumber < xFileNumber; xReadNumber++ )
\r
239 /* Start with the RAM buffer clear. */
\r
240 memset( cRAMBuffer, 0x00, fsRAM_BUFFER_SIZE );
\r
242 lItemsRead = f_read( cRAMBuffer, fsRAM_BUFFER_SIZE, 1, pxFile );
\r
243 configASSERT( lItemsRead == 1 );
\r
245 /* Check the RAM buffer is filled with the expected data. Each
\r
246 file contains a different repeating ascii character that indicates
\r
247 the number of the file. */
\r
248 for( lChar = 0; lChar < fsRAM_BUFFER_SIZE; lChar++ )
\r
250 configASSERT( cRAMBuffer[ lChar ] == ( '0' + ( char ) xFileNumber ) );
\r
254 /* Close the file. */
\r
258 /*-----------------------------------------------------------*/
\r
260 static void prvCreateDemoFileUsing_f_putc( void )
\r
262 unsigned char ucReturn;
\r
263 int iByte, iReturned;
\r
265 char cFileName[ fsMAX_FILE_NAME_LEN ];
\r
267 /* Obtain and print out the working directory. */
\r
268 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
270 /* Create a sub directory. */
\r
271 ucReturn = f_mkdir( pcDirectory1 );
\r
272 configASSERT( ucReturn == F_NO_ERROR );
\r
274 /* Move into the created sub-directory. */
\r
275 ucReturn = f_chdir( pcDirectory1 );
\r
276 configASSERT( ucReturn == F_NO_ERROR );
\r
278 /* Obtain and print out the working directory. */
\r
279 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
281 /* Create a subdirectory in the new directory. */
\r
282 ucReturn = f_mkdir( pcDirectory2 );
\r
283 configASSERT( ucReturn == F_NO_ERROR );
\r
285 /* Move into the directory just created - now two directories down from
\r
287 ucReturn = f_chdir( pcDirectory2 );
\r
288 configASSERT( ucReturn == F_NO_ERROR );
\r
290 /* Obtain and print out the working directory. */
\r
291 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\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 pxFile = f_open( cFileName, "w" );
\r
301 /* Create a file 1 byte at a time. The file is filled with incrementing
\r
302 ascii characters starting from '0'. */
\r
303 for( iByte = 0; iByte < fsPUTC_FILE_SIZE; iByte++ )
\r
305 iReturned = f_putc( ( ( int ) '0' + iByte ), pxFile );
\r
306 configASSERT( iReturned == ( ( int ) '0' + iByte ) );
\r
309 /* Finished so close the file. */
\r
312 /* Move back to the root directory. */
\r
313 ucReturn = f_chdir( "../.." );
\r
314 configASSERT( ucReturn == F_NO_ERROR );
\r
316 /* Obtain and print out the working directory. */
\r
317 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
318 configASSERT( strcmp( cRAMBuffer, pcRoot ) == 0 );
\r
320 /*-----------------------------------------------------------*/
\r
322 static void prvVerifyDemoFileUsing_f_getc( void )
\r
324 unsigned char ucReturn;
\r
325 int iByte, iReturned;
\r
327 char cFileName[ fsMAX_FILE_NAME_LEN ];
\r
329 /* Move into the directory in which the file was created. */
\r
330 ucReturn = f_chdir( pcFullPath );
\r
331 configASSERT( ucReturn == F_NO_ERROR );
\r
333 /* Obtain and print out the working directory. */
\r
334 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
335 configASSERT( strcmp( cRAMBuffer, pcFullPath ) == 0 );
\r
337 /* Generate the file name. */
\r
338 sprintf( cFileName, "%s.txt", pcDirectory2 );
\r
340 /* This time the file is opened for reading. */
\r
341 pxFile = f_open( cFileName, "r" );
\r
343 /* Read the file 1 byte at a time. */
\r
344 for( iByte = 0; iByte < fsPUTC_FILE_SIZE; iByte++ )
\r
346 iReturned = f_getc( pxFile );
\r
347 configASSERT( iReturned == ( ( int ) '0' + iByte ) );
\r
350 /* Finished so close the file. */
\r
353 /* Move back to the root directory. */
\r
354 ucReturn = f_chdir( "../.." );
\r
355 configASSERT( ucReturn == F_NO_ERROR );
\r
357 /* Obtain and print out the working directory. */
\r
358 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r