2 FreeRTOS V8.2.0rc1 - Copyright (C) 2014 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 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
14 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
15 >>! obliged to provide the source code for proprietary components !<<
\r
16 >>! outside of the FreeRTOS kernel. !<<
\r
18 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
19 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
20 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
21 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * Having a problem? Start by reading the FAQ "My application does *
\r
28 * not run, what could be wrong?". Have you defined configASSERT()? *
\r
30 * http://www.FreeRTOS.org/FAQHelp.html *
\r
32 ***************************************************************************
\r
34 ***************************************************************************
\r
36 * FreeRTOS provides completely free yet professionally developed, *
\r
37 * robust, strictly quality controlled, supported, and cross *
\r
38 * platform software that is more than just the market leader, it *
\r
39 * is the industry's de facto standard. *
\r
41 * Help yourself get started quickly while simultaneously helping *
\r
42 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
43 * tutorial book, reference manual, or both: *
\r
44 * http://www.FreeRTOS.org/Documentation *
\r
46 ***************************************************************************
\r
48 ***************************************************************************
\r
50 * Investing in training allows your team to be as productive as *
\r
51 * possible as early as possible, lowering your overall development *
\r
52 * cost, and enabling you to bring a more robust product to market *
\r
53 * earlier than would otherwise be possible. Richard Barry is both *
\r
54 * the architect and key author of FreeRTOS, and so also the world's *
\r
55 * leading authority on what is the world's most popular real time *
\r
56 * kernel for deeply embedded MCU designs. Obtaining your training *
\r
57 * from Richard ensures your team will gain directly from his in-depth *
\r
58 * product knowledge and years of usage experience. Contact Real Time *
\r
59 * Engineers Ltd to enquire about the FreeRTOS Masterclass, presented *
\r
60 * by Richard Barry: http://www.FreeRTOS.org/contact
\r
62 ***************************************************************************
\r
64 ***************************************************************************
\r
66 * You are receiving this top quality software for free. Please play *
\r
67 * fair and reciprocate by reporting any suspected issues and *
\r
68 * participating in the community forum: *
\r
69 * http://www.FreeRTOS.org/support *
\r
73 ***************************************************************************
\r
75 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
76 license and Real Time Engineers Ltd. contact details.
\r
78 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
79 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
80 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
82 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
83 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
85 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
86 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
87 licenses offer ticketed support, indemnification and commercial middleware.
\r
89 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
90 engineered and independently SIL3 certified version for use in safety and
\r
91 mission critical applications that require provable dependability.
\r
96 /*******************************************************************************
\r
97 * See the URL in the comments within main.c for the location of the online
\r
99 ******************************************************************************/
\r
101 /* Standard includes. */
\r
103 #include <string.h>
\r
105 /* FreeRTOS includes. */
\r
106 #include "FreeRTOS.h"
\r
109 /* File system includes. */
\r
110 #include "fat_sl.h"
\r
111 #include "api_mdriver_ram.h"
\r
113 /* 8.3 format, plus null terminator. */
\r
114 #define fsMAX_FILE_NAME_LEN 13
\r
116 /* The number of bytes read/written to the example files at a time. */
\r
117 #define fsRAM_BUFFER_SIZE 200
\r
119 /* The number of bytes written to the file that uses f_putc() and f_getc(). */
\r
120 #define fsPUTC_FILE_SIZE 100
\r
122 /* The number of files created in root. */
\r
123 #define fsROOT_FILES 5
\r
125 /*-----------------------------------------------------------*/
\r
128 * Creates and verifies different files on the volume, demonstrating the use of
\r
129 * various different API functions.
\r
131 void vCreateAndVerifySampleFiles( void );
\r
134 * Create a set of example files in the root directory of the volume using
\r
137 static void prvCreateDemoFilesUsing_f_write( void );
\r
140 * Use f_read() to read back and verify the files that were created by
\r
141 * prvCreateDemoFilesUsing_f_write().
\r
143 static void prvVerifyDemoFileUsing_f_read( void );
\r
146 * Create an example file in a sub-directory using f_putc().
\r
148 static void prvCreateDemoFileUsing_f_putc( void );
\r
151 * Use f_getc() to read back and verify the file that was created by
\r
152 * prvCreateDemoFileUsing_f_putc().
\r
154 static void prvVerifyDemoFileUsing_f_getc( void );
\r
156 /*-----------------------------------------------------------*/
\r
158 /* A buffer used to both create content to write to disk, and read content back
\r
159 from a disk. Note there is no mutual exclusion on this buffer. */
\r
160 static char cRAMBuffer[ fsRAM_BUFFER_SIZE ];
\r
162 /* Names of directories that are created. */
\r
163 static const char *pcRoot = "/", *pcDirectory1 = "SUB1", *pcDirectory2 = "SUB2", *pcFullPath = "/SUB1/SUB2";
\r
165 /*-----------------------------------------------------------*/
\r
167 void vCreateAndVerifySampleFiles( void )
\r
169 unsigned char ucStatus;
\r
171 /* First create the volume. */
\r
172 ucStatus = f_initvolume( ram_initfunc );
\r
174 /* It is expected that the volume is not formatted. */
\r
175 if( ucStatus == F_ERR_NOTFORMATTED )
\r
177 /* Format the created volume. */
\r
178 ucStatus = f_format( F_FAT12_MEDIA );
\r
181 if( ucStatus == F_NO_ERROR )
\r
183 /* Create a set of files using f_write(). */
\r
184 prvCreateDemoFilesUsing_f_write();
\r
186 /* Read back and verify the files that were created using f_write(). */
\r
187 prvVerifyDemoFileUsing_f_read();
\r
189 /* Create sub directories two deep then create a file using putc. */
\r
190 prvCreateDemoFileUsing_f_putc();
\r
192 /* Read back and verify the file created by
\r
193 prvCreateDemoFileUsing_f_putc(). */
\r
194 prvVerifyDemoFileUsing_f_getc();
\r
197 /*-----------------------------------------------------------*/
\r
199 static void prvCreateDemoFilesUsing_f_write( void )
\r
201 portBASE_TYPE xFileNumber, xWriteNumber;
\r
202 char cFileName[ fsMAX_FILE_NAME_LEN ];
\r
203 long lItemsWritten;
\r
206 /* Create fsROOT_FILES files. Each created file will be
\r
207 ( xFileNumber * fsRAM_BUFFER_SIZE ) bytes in length, and filled
\r
208 with a different repeating character. */
\r
209 for( xFileNumber = 1; xFileNumber <= fsROOT_FILES; xFileNumber++ )
\r
211 /* Generate a file name. */
\r
212 sprintf( cFileName, "root%03d.txt", xFileNumber );
\r
214 /* Obtain the current working directory and print out the file name and
\r
215 the directory into which the file is being written. */
\r
216 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
217 printf( "Creating file %s in %s\r\n", cFileName, cRAMBuffer );
\r
219 /* Open the file, creating the file if it does not already exist. */
\r
220 pxFile = f_open( cFileName, "w" );
\r
221 configASSERT( pxFile );
\r
223 /* Fill the RAM buffer with data that will be written to the file. This
\r
224 is just a repeating ascii character that indicates the file number. */
\r
225 memset( cRAMBuffer, ( int ) ( '0' + xFileNumber ), fsRAM_BUFFER_SIZE );
\r
227 /* Write the RAM buffer to the opened file a number of times. The
\r
228 number of times the RAM buffer is written to the file depends on the
\r
229 file number, so the length of each created file will be different. */
\r
230 for( xWriteNumber = 0; xWriteNumber < xFileNumber; xWriteNumber++ )
\r
232 lItemsWritten = f_write( cRAMBuffer, fsRAM_BUFFER_SIZE, 1, pxFile );
\r
233 configASSERT( lItemsWritten == 1 );
\r
236 /* Close the file so another file can be created. */
\r
240 /*-----------------------------------------------------------*/
\r
242 static void prvVerifyDemoFileUsing_f_read( void )
\r
244 portBASE_TYPE xFileNumber, xReadNumber;
\r
245 char cFileName[ fsMAX_FILE_NAME_LEN ];
\r
246 long lItemsRead, lChar;
\r
249 /* Read back the files that were created by
\r
250 prvCreateDemoFilesUsing_f_write(). */
\r
251 for( xFileNumber = 1; xFileNumber <= fsROOT_FILES; xFileNumber++ )
\r
253 /* Generate the file name. */
\r
254 sprintf( cFileName, "root%03d.txt", xFileNumber );
\r
256 /* Obtain the current working directory and print out the file name and
\r
257 the directory from which the file is being read. */
\r
258 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
259 printf( "Reading file %s from %s\r\n", cFileName, cRAMBuffer );
\r
261 /* Open the file for reading. */
\r
262 pxFile = f_open( cFileName, "r" );
\r
263 configASSERT( pxFile );
\r
265 /* Read the file into the RAM buffer, checking the file contents are as
\r
266 expected. The size of the file depends on the file number. */
\r
267 for( xReadNumber = 0; xReadNumber < xFileNumber; xReadNumber++ )
\r
269 /* Start with the RAM buffer clear. */
\r
270 memset( cRAMBuffer, 0x00, fsRAM_BUFFER_SIZE );
\r
272 lItemsRead = f_read( cRAMBuffer, fsRAM_BUFFER_SIZE, 1, pxFile );
\r
273 configASSERT( lItemsRead == 1 );
\r
275 /* Check the RAM buffer is filled with the expected data. Each
\r
276 file contains a different repeating ascii character that indicates
\r
277 the number of the file. */
\r
278 for( lChar = 0; lChar < fsRAM_BUFFER_SIZE; lChar++ )
\r
280 configASSERT( cRAMBuffer[ lChar ] == ( '0' + ( char ) xFileNumber ) );
\r
284 /* Close the file. */
\r
288 /*-----------------------------------------------------------*/
\r
290 static void prvCreateDemoFileUsing_f_putc( void )
\r
292 unsigned char ucReturn;
\r
293 int iByte, iReturned;
\r
295 char cFileName[ fsMAX_FILE_NAME_LEN ];
\r
297 /* Obtain and print out the working directory. */
\r
298 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
299 printf( "In directory %s\r\n", cRAMBuffer );
\r
301 /* Create a sub directory. */
\r
302 ucReturn = f_mkdir( pcDirectory1 );
\r
303 configASSERT( ucReturn == F_NO_ERROR );
\r
305 /* Move into the created sub-directory. */
\r
306 ucReturn = f_chdir( pcDirectory1 );
\r
307 configASSERT( ucReturn == F_NO_ERROR );
\r
309 /* Obtain and print out the working directory. */
\r
310 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
311 printf( "In directory %s\r\n", cRAMBuffer );
\r
313 /* Create a subdirectory in the new directory. */
\r
314 ucReturn = f_mkdir( pcDirectory2 );
\r
315 configASSERT( ucReturn == F_NO_ERROR );
\r
317 /* Move into the directory just created - now two directories down from
\r
319 ucReturn = f_chdir( pcDirectory2 );
\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( "In directory %s\r\n", cRAMBuffer );
\r
325 configASSERT( strcmp( cRAMBuffer, pcFullPath ) == 0 );
\r
327 /* Generate the file name. */
\r
328 sprintf( cFileName, "%s.txt", pcDirectory2 );
\r
330 /* Print out the file name and the directory into which the file is being
\r
332 printf( "Writing file %s in %s\r\n", cFileName, cRAMBuffer );
\r
334 pxFile = f_open( cFileName, "w" );
\r
336 /* Create a file 1 byte at a time. The file is filled with incrementing
\r
337 ascii characters starting from '0'. */
\r
338 for( iByte = 0; iByte < fsPUTC_FILE_SIZE; iByte++ )
\r
340 iReturned = f_putc( ( ( int ) '0' + iByte ), pxFile );
\r
341 configASSERT( iReturned == ( ( int ) '0' + iByte ) );
\r
344 /* Finished so close the file. */
\r
347 /* Move back to the root directory. */
\r
348 ucReturn = f_chdir( "../.." );
\r
349 configASSERT( ucReturn == F_NO_ERROR );
\r
351 /* Obtain and print out the working directory. */
\r
352 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
353 printf( "Back in root directory %s\r\n", cRAMBuffer );
\r
354 configASSERT( strcmp( cRAMBuffer, pcRoot ) == 0 );
\r
356 /*-----------------------------------------------------------*/
\r
358 static void prvVerifyDemoFileUsing_f_getc( void )
\r
360 unsigned char ucReturn;
\r
361 int iByte, iReturned;
\r
363 char cFileName[ fsMAX_FILE_NAME_LEN ];
\r
365 /* Move into the directory in which the file was created. */
\r
366 ucReturn = f_chdir( pcFullPath );
\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 directory %s\r\n", cRAMBuffer );
\r
372 configASSERT( strcmp( cRAMBuffer, pcFullPath ) == 0 );
\r
374 /* Generate the file name. */
\r
375 sprintf( cFileName, "%s.txt", pcDirectory2 );
\r
377 /* Print out the file name and the directory from which the file is being
\r
379 printf( "Reading file %s in %s\r\n", cFileName, cRAMBuffer );
\r
381 /* This time the file is opened for reading. */
\r
382 pxFile = f_open( cFileName, "r" );
\r
384 /* Read the file 1 byte at a time. */
\r
385 for( iByte = 0; iByte < fsPUTC_FILE_SIZE; iByte++ )
\r
387 iReturned = f_getc( pxFile );
\r
388 configASSERT( iReturned == ( ( int ) '0' + iByte ) );
\r
391 /* Finished so close the file. */
\r
394 /* Move back to the root directory. */
\r
395 ucReturn = f_chdir( "../.." );
\r
396 configASSERT( ucReturn == F_NO_ERROR );
\r
398 /* Obtain and print out the working directory. */
\r
399 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
400 printf( "Back in root directory %s\r\n", cRAMBuffer );
\r