2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
29 /*******************************************************************************
\r
30 * See the URL in the comments within main.c for the location of the online
\r
32 ******************************************************************************/
\r
34 /* Standard includes. */
\r
38 /* FreeRTOS includes. */
\r
39 #include "FreeRTOS.h"
\r
41 /* File system includes. */
\r
43 #include "api_mdriver_ram.h"
\r
45 /* 8.3 format, plus null terminator. */
\r
46 #define fsMAX_FILE_NAME_LEN 13
\r
48 /* The number of bytes read/written to the example files at a time. */
\r
49 #define fsRAM_BUFFER_SIZE 200
\r
51 /* The number of bytes written to the file that uses f_putc() and f_getc(). */
\r
52 #define fsPUTC_FILE_SIZE 100
\r
54 /*-----------------------------------------------------------*/
\r
57 * Creates and verifies different files on the volume, demonstrating the use of
\r
58 * various different API functions.
\r
60 void vCreateAndVerifySampleFiles( void );
\r
63 * Create a set of example files in the root directory of the volume using
\r
66 static void prvCreateDemoFilesUsing_f_write( void );
\r
69 * Use f_read() to read back and verify the files that were created by
\r
70 * prvCreateDemoFilesUsing_f_write().
\r
72 static void prvVerifyDemoFileUsing_f_read( void );
\r
75 * Create an example file in a sub-directory using f_putc().
\r
77 static void prvCreateDemoFileUsing_f_putc( void );
\r
80 * Use f_getc() to read back and verify the file that was created by
\r
81 * prvCreateDemoFileUsing_f_putc().
\r
83 static void prvVerifyDemoFileUsing_f_getc( void );
\r
85 /*-----------------------------------------------------------*/
\r
87 /* A buffer used to both create content to write to disk, and read content back
\r
88 from a disk. Note there is no mutual exclusion on this buffer. */
\r
89 static char cRAMBuffer[ fsRAM_BUFFER_SIZE ];
\r
91 /* Names of directories that are created. */
\r
92 static const char *pcRoot = "/", *pcDirectory1 = "SUB1", *pcDirectory2 = "SUB2", *pcFullPath = "/SUB1/SUB2";
\r
94 /*-----------------------------------------------------------*/
\r
96 void vCreateAndVerifySampleFiles( void )
\r
98 unsigned char ucStatus;
\r
100 /* First create the volume. */
\r
101 ucStatus = f_initvolume( ram_initfunc );
\r
103 /* It is expected that the volume is not formatted. */
\r
104 if( ucStatus == F_ERR_NOTFORMATTED )
\r
106 /* Format the created volume. */
\r
107 ucStatus = f_format( F_FAT12_MEDIA );
\r
110 if( ucStatus == F_NO_ERROR )
\r
112 /* Create a set of files using f_write(). */
\r
113 prvCreateDemoFilesUsing_f_write();
\r
115 /* Read back and verify the files that were created using f_write(). */
\r
116 prvVerifyDemoFileUsing_f_read();
\r
118 /* Create sub directories two deep then create a file using putc. */
\r
119 prvCreateDemoFileUsing_f_putc();
\r
121 /* Read back and verify the file created by
\r
122 prvCreateDemoFileUsing_f_putc(). */
\r
123 prvVerifyDemoFileUsing_f_getc();
\r
126 /*-----------------------------------------------------------*/
\r
128 static void prvCreateDemoFilesUsing_f_write( void )
\r
130 BaseType_t xFileNumber, xWriteNumber;
\r
131 char cFileName[ fsMAX_FILE_NAME_LEN ];
\r
132 const BaseType_t xMaxFiles = 5;
\r
133 long lItemsWritten;
\r
136 /* Create xMaxFiles files. Each created file will be
\r
137 ( xFileNumber * fsRAM_BUFFER_SIZE ) bytes in length, and filled
\r
138 with a different repeating character. */
\r
139 for( xFileNumber = 1; xFileNumber <= xMaxFiles; xFileNumber++ )
\r
141 /* Generate a file name. */
\r
142 sprintf( cFileName, "root%03d.txt", ( int ) xFileNumber );
\r
144 /* Obtain the current working directory and print out the file name and
\r
145 the directory into which the file is being written. */
\r
146 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
148 /* Open the file, creating the file if it does not already exist. */
\r
149 pxFile = f_open( cFileName, "w" );
\r
150 configASSERT( pxFile );
\r
152 /* Fill the RAM buffer with data that will be written to the file. This
\r
153 is just a repeating ascii character that indicates the file number. */
\r
154 memset( cRAMBuffer, ( int ) ( '0' + xFileNumber ), fsRAM_BUFFER_SIZE );
\r
156 /* Write the RAM buffer to the opened file a number of times. The
\r
157 number of times the RAM buffer is written to the file depends on the
\r
158 file number, so the length of each created file will be different. */
\r
159 for( xWriteNumber = 0; xWriteNumber < xFileNumber; xWriteNumber++ )
\r
161 lItemsWritten = f_write( cRAMBuffer, fsRAM_BUFFER_SIZE, 1, pxFile );
\r
162 configASSERT( lItemsWritten == 1 );
\r
165 /* Close the file so another file can be created. */
\r
169 /*-----------------------------------------------------------*/
\r
171 static void prvVerifyDemoFileUsing_f_read( void )
\r
173 BaseType_t xFileNumber, xReadNumber;
\r
174 char cFileName[ fsMAX_FILE_NAME_LEN ];
\r
175 const BaseType_t xMaxFiles = 5;
\r
176 long lItemsRead, lChar;
\r
179 /* Read back the files that were created by
\r
180 prvCreateDemoFilesUsing_f_write(). */
\r
181 for( xFileNumber = 1; xFileNumber <= xMaxFiles; xFileNumber++ )
\r
183 /* Generate the file name. */
\r
184 sprintf( cFileName, "root%03d.txt", ( int ) xFileNumber );
\r
186 /* Obtain the current working directory and print out the file name and
\r
187 the directory from which the file is being read. */
\r
188 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
190 /* Open the file for reading. */
\r
191 pxFile = f_open( cFileName, "r" );
\r
192 configASSERT( pxFile );
\r
194 /* Read the file into the RAM buffer, checking the file contents are as
\r
195 expected. The size of the file depends on the file number. */
\r
196 for( xReadNumber = 0; xReadNumber < xFileNumber; xReadNumber++ )
\r
198 /* Start with the RAM buffer clear. */
\r
199 memset( cRAMBuffer, 0x00, fsRAM_BUFFER_SIZE );
\r
201 lItemsRead = f_read( cRAMBuffer, fsRAM_BUFFER_SIZE, 1, pxFile );
\r
202 configASSERT( lItemsRead == 1 );
\r
204 /* Check the RAM buffer is filled with the expected data. Each
\r
205 file contains a different repeating ascii character that indicates
\r
206 the number of the file. */
\r
207 for( lChar = 0; lChar < fsRAM_BUFFER_SIZE; lChar++ )
\r
209 configASSERT( cRAMBuffer[ lChar ] == ( '0' + ( char ) xFileNumber ) );
\r
213 /* Close the file. */
\r
217 /*-----------------------------------------------------------*/
\r
219 static void prvCreateDemoFileUsing_f_putc( void )
\r
221 unsigned char ucReturn;
\r
222 int iByte, iReturned;
\r
224 char cFileName[ fsMAX_FILE_NAME_LEN ];
\r
226 /* Obtain and print out the working directory. */
\r
227 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
229 /* Create a sub directory. */
\r
230 ucReturn = f_mkdir( pcDirectory1 );
\r
231 configASSERT( ucReturn == F_NO_ERROR );
\r
233 /* Move into the created sub-directory. */
\r
234 ucReturn = f_chdir( pcDirectory1 );
\r
235 configASSERT( ucReturn == F_NO_ERROR );
\r
237 /* Obtain and print out the working directory. */
\r
238 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
240 /* Create a subdirectory in the new directory. */
\r
241 ucReturn = f_mkdir( pcDirectory2 );
\r
242 configASSERT( ucReturn == F_NO_ERROR );
\r
244 /* Move into the directory just created - now two directories down from
\r
246 ucReturn = f_chdir( pcDirectory2 );
\r
247 configASSERT( ucReturn == F_NO_ERROR );
\r
249 /* Obtain and print out the working directory. */
\r
250 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
251 configASSERT( strcmp( cRAMBuffer, pcFullPath ) == 0 );
\r
253 /* Generate the file name. */
\r
254 sprintf( cFileName, "%s.txt", pcDirectory2 );
\r
256 /* Print out the file name and the directory into which the file is being
\r
258 pxFile = f_open( cFileName, "w" );
\r
260 /* Create a file 1 byte at a time. The file is filled with incrementing
\r
261 ascii characters starting from '0'. */
\r
262 for( iByte = 0; iByte < fsPUTC_FILE_SIZE; iByte++ )
\r
264 iReturned = f_putc( ( ( int ) '0' + iByte ), pxFile );
\r
265 configASSERT( iReturned == ( ( int ) '0' + iByte ) );
\r
268 /* Finished so close the file. */
\r
271 /* Move back to the root directory. */
\r
272 ucReturn = f_chdir( "../.." );
\r
273 configASSERT( ucReturn == F_NO_ERROR );
\r
275 /* Obtain and print out the working directory. */
\r
276 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
277 configASSERT( strcmp( cRAMBuffer, pcRoot ) == 0 );
\r
279 /*-----------------------------------------------------------*/
\r
281 static void prvVerifyDemoFileUsing_f_getc( void )
\r
283 unsigned char ucReturn;
\r
284 int iByte, iReturned;
\r
286 char cFileName[ fsMAX_FILE_NAME_LEN ];
\r
288 /* Move into the directory in which the file was created. */
\r
289 ucReturn = f_chdir( pcFullPath );
\r
290 configASSERT( ucReturn == F_NO_ERROR );
\r
292 /* Obtain and print out the working directory. */
\r
293 f_getcwd( cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
294 configASSERT( strcmp( cRAMBuffer, pcFullPath ) == 0 );
\r
296 /* Generate the file name. */
\r
297 sprintf( cFileName, "%s.txt", pcDirectory2 );
\r
299 /* This time the file is opened for reading. */
\r
300 pxFile = f_open( cFileName, "r" );
\r
302 /* Read the file 1 byte at a time. */
\r
303 for( iByte = 0; iByte < fsPUTC_FILE_SIZE; iByte++ )
\r
305 iReturned = f_getc( 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