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
78 /* FreeRTOS includes. */
\r
79 #include "FreeRTOS.h"
\r
81 /* File system includes. */
\r
82 #include <redposix.h>
\r
84 /* The number of bytes read/written to the example files at a time. */
\r
85 #define fsRAM_BUFFER_SIZE 200
\r
87 /* The volume prefix is an empty string, for convenience since there is only one
\r
88 volume in this demo.
\r
90 #define fsVOLUME_NAME ""
\r
92 /*-----------------------------------------------------------*/
\r
95 * Creates and verifies different files on the volume, demonstrating the use of
\r
96 * various different API functions.
\r
98 void vCreateAndVerifySampleFiles( void );
\r
101 * Create a set of example files in the root directory of the volume using
\r
104 static void prvCreateDemoFiles( void );
\r
107 * Use f_read() to read back and verify the files that were created by
\r
108 * prvCreateDemoFiles().
\r
110 static void prvVerifyDemoFiles( void );
\r
112 /*-----------------------------------------------------------*/
\r
114 /* A buffer used to both create content to write to disk, and read content back
\r
115 from a disk. Note there is no mutual exclusion on this buffer. */
\r
116 static char cRAMBuffer[ fsRAM_BUFFER_SIZE ];
\r
118 /* Names of directories that are created. */
\r
119 static const char *pcDirectory1 = "/SUB1", *pcDirectory2 = "/SUB1/SUB2";
\r
121 /*-----------------------------------------------------------*/
\r
123 void vCreateAndVerifySampleFiles( void )
\r
127 /* First initialize the Reliance Edge driver. */
\r
128 lStatus = red_init();
\r
130 /* Format the volume. */
\r
133 lStatus = red_format( fsVOLUME_NAME );
\r
136 /* Mount the volume. */
\r
139 lStatus = red_mount( fsVOLUME_NAME );
\r
144 /* Create a set of files using red_write(). */
\r
145 prvCreateDemoFiles();
\r
147 /* Read back and verify the files that were created using red_write(). */
\r
148 prvVerifyDemoFiles();
\r
151 /*-----------------------------------------------------------*/
\r
153 static void prvCreateDemoFiles( void )
\r
155 BaseType_t xFileNumber, xWriteNumber;
\r
156 char cFilePath[ 64 ];
\r
157 const BaseType_t xMaxFiles = 5;
\r
158 uint32_t ulEventMask;
\r
159 int32_t lBytesWritten, lFildes, lStatus;
\r
162 /* Save the current transaction point settings. */
\r
163 lStatus = red_gettransmask( fsVOLUME_NAME, &ulEventMask );
\r
164 configASSERT( lStatus == 0 );
\r
166 /* Disable automatic transaction points so that all of the files can be
\r
167 created in one atomic operation. */
\r
168 lStatus = red_settransmask( fsVOLUME_NAME, RED_TRANSACT_MANUAL );
\r
169 configASSERT( lStatus == 0 );
\r
171 /* Create xMaxFiles files. Each created file will be
\r
172 ( xFileNumber * fsRAM_BUFFER_SIZE ) bytes in length, and filled
\r
173 with a different repeating character. */
\r
174 for( xFileNumber = 1; xFileNumber <= xMaxFiles; xFileNumber++ )
\r
176 /* Generate a file name. */
\r
177 sprintf( cFilePath, "/root%03d.txt", xFileNumber );
\r
179 /* Print out the file name and the directory into which the file is
\r
181 printf( "Creating file %s\r\n", cFilePath );
\r
183 /* Open the file, creating the file if it does not already exist. */
\r
184 lFildes = red_open( cFilePath, RED_O_CREAT|RED_O_TRUNC|RED_O_WRONLY );
\r
185 configASSERT( lFildes != -1 );
\r
187 /* Fill the RAM buffer with data that will be written to the file. This
\r
188 is just a repeating ascii character that indicates the file number. */
\r
189 memset( cRAMBuffer, ( int ) ( '0' + xFileNumber ), fsRAM_BUFFER_SIZE );
\r
191 /* Write the RAM buffer to the opened file a number of times. The
\r
192 number of times the RAM buffer is written to the file depends on the
\r
193 file number, so the length of each created file will be different. */
\r
194 for( xWriteNumber = 0; xWriteNumber < xFileNumber; xWriteNumber++ )
\r
196 lBytesWritten = red_write( lFildes, cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
197 configASSERT( lBytesWritten == fsRAM_BUFFER_SIZE );
\r
200 /* Close the file so another file can be created. */
\r
201 lStatus = red_close( lFildes );
\r
202 configASSERT( lStatus == 0 );
\r
205 /* Commit a transaction point, atomically adding the set of files to the
\r
206 transacted state. */
\r
207 lStatus = red_transact( fsVOLUME_NAME );
\r
208 configASSERT( lStatus == 0 );
\r
210 /* Create a sub directory. */
\r
211 printf( "Creating directory %s\r\n", pcDirectory1 );
\r
213 lStatus = red_mkdir( pcDirectory1 );
\r
214 configASSERT( lStatus == 0 );
\r
216 /* Create a subdirectory in the new directory. */
\r
217 printf( "Creating directory %s\r\n", pcDirectory2 );
\r
219 lStatus = red_mkdir( pcDirectory2 );
\r
220 configASSERT( lStatus == 0 );
\r
222 /* Generate the file name. */
\r
223 sprintf( cFilePath, "%s/file.txt", pcDirectory2 );
\r
225 /* Print out the file name and the directory into which the file is being
\r
227 printf( "Writing file %s\r\n", cFilePath );
\r
229 lFildes = red_open( cFilePath, RED_O_CREAT|RED_O_TRUNC|RED_O_WRONLY );
\r
231 /* Write the file. It is filled with incrementing ascii characters starting
\r
233 for( iByte = 0; iByte < fsRAM_BUFFER_SIZE; iByte++ )
\r
235 cRAMBuffer[ iByte ] = ( char ) ( ( int ) '0' + iByte );
\r
238 lBytesWritten = red_write( lFildes, cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
239 configASSERT( lBytesWritten == fsRAM_BUFFER_SIZE );
\r
241 /* Finished so close the file. */
\r
242 lStatus = red_close( lFildes );
\r
243 configASSERT( lStatus == 0 );
\r
245 /* Commit a transaction point, atomically adding the set of files and
\r
246 directories to the transacted state. */
\r
247 lStatus = red_transact( fsVOLUME_NAME );
\r
248 configASSERT( lStatus == 0 );
\r
250 /* Restore previous transaction point settings. */
\r
251 lStatus = red_settransmask( fsVOLUME_NAME, ulEventMask );
\r
252 configASSERT( lStatus == 0 );
\r
254 /*-----------------------------------------------------------*/
\r
256 static void prvVerifyDemoFiles( void )
\r
258 BaseType_t xFileNumber, xReadNumber;
\r
259 char cFilePath[ 64 ];
\r
260 const BaseType_t xMaxFiles = 5;
\r
262 int32_t lBytesRead, lFildes, lStatus;
\r
265 /* Read back the files that were created by prvCreateDemoFiles(). */
\r
266 for( xFileNumber = 1; xFileNumber <= xMaxFiles; xFileNumber++ )
\r
268 /* Generate the file name. */
\r
269 sprintf( cFilePath, "/root%03d.txt", xFileNumber );
\r
271 /* Print out the file name and the directory from which the file is
\r
273 printf( "Reading file %s\r\n", cFilePath );
\r
275 /* Open the file for reading. */
\r
276 lFildes = red_open( cFilePath, RED_O_RDONLY );
\r
277 configASSERT( lFildes != -1 );
\r
279 /* Read the file into the RAM buffer, checking the file contents are as
\r
280 expected. The size of the file depends on the file number. */
\r
281 for( xReadNumber = 0; xReadNumber < xFileNumber; xReadNumber++ )
\r
283 /* Start with the RAM buffer clear. */
\r
284 memset( cRAMBuffer, 0x00, fsRAM_BUFFER_SIZE );
\r
286 lBytesRead = red_read( lFildes, cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
287 configASSERT( lBytesRead == fsRAM_BUFFER_SIZE );
\r
289 /* Check the RAM buffer is filled with the expected data. Each
\r
290 file contains a different repeating ascii character that indicates
\r
291 the number of the file. */
\r
292 for( lChar = 0; lChar < fsRAM_BUFFER_SIZE; lChar++ )
\r
294 configASSERT( cRAMBuffer[ lChar ] == ( '0' + ( char ) xFileNumber ) );
\r
298 /* Close the file. */
\r
299 lStatus = red_close( lFildes );
\r
300 configASSERT( lStatus == 0 );
\r
303 /* Generate the file name. */
\r
304 sprintf( cFilePath, "%s/file.txt", pcDirectory2 );
\r
306 /* Print out the file name and the directory from which the file is being
\r
308 printf( "Reading file %s\r\n", cFilePath );
\r
310 /* This time the file is opened for reading. */
\r
311 lFildes = red_open( cFilePath, RED_O_RDONLY );
\r
312 configASSERT( lFildes != -1 );
\r
314 /* Read the file. */
\r
315 lBytesRead = red_read( lFildes, cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
316 configASSERT( lBytesRead == fsRAM_BUFFER_SIZE );
\r
318 /* Verify the file 1 byte at a time. */
\r
319 for( iByte = 0; iByte < fsRAM_BUFFER_SIZE; iByte++ )
\r
321 configASSERT( cRAMBuffer[ iByte ] == ( char ) ( ( int ) '0' + iByte ) );
\r
324 /* Finished so close the file. */
\r
325 lStatus = red_close( lFildes );
\r
326 configASSERT( lStatus == 0 );
\r