1 /* This FileX test concentrates on the utility FAT flush operation.  */
2 
3 #ifndef FX_STANDALONE_ENABLE
4 #include   "tx_api.h"
5 #endif
6 #include   "fx_api.h"
7 #include   "fx_utility.h"
8 #include   <stdio.h>
9 #include   "fx_ram_driver_test.h"
10 
11 #define     DEMO_STACK_SIZE         4096
12 #define     CACHE_SIZE              16*128
13 
14 
15 /* Define the ThreadX and FileX object control blocks...  */
16 
17 #ifndef FX_STANDALONE_ENABLE
18 static TX_THREAD                ftest_0;
19 #endif
20 static FX_MEDIA                ram_disk;
21 static FX_FILE                 my_file;
22 
23 
24 /* Define the counters used in the test application...  */
25 
26 #ifndef FX_STANDALONE_ENABLE
27 static UCHAR                  *ram_disk_memory;
28 static UCHAR                  *cache_buffer;
29 #else
30 static UCHAR                   cache_buffer[CACHE_SIZE];
31 #endif
32 
33 
34 /* Define thread prototypes.  */
35 
36 void    filex_utility_fat_flush_application_define(void *first_unused_memory);
37 static void    ftest_0_entry(ULONG thread_input);
38 
39 VOID  _fx_ram_driver(FX_MEDIA *media_ptr);
40 void  test_control_return(UINT status);
41 
42 
43 
44 /* Define what the initial system looks like.  */
45 
46 #ifdef CTEST
test_application_define(void * first_unused_memory)47 void test_application_define(void *first_unused_memory)
48 #else
49 void    filex_utility_fat_flush_application_define(void *first_unused_memory)
50 #endif
51 {
52 
53 #ifndef FX_STANDALONE_ENABLE
54 UCHAR    *pointer;
55 
56 
57     /* Setup the working pointer.  */
58     pointer =  (UCHAR *) first_unused_memory;
59 
60     /* Create the main thread.  */
61     tx_thread_create(&ftest_0, "thread 0", ftest_0_entry, 0,
62             pointer, DEMO_STACK_SIZE,
63             4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
64     pointer =  pointer + DEMO_STACK_SIZE;
65 
66     /* Setup memory for the RAM disk and the sector cache.  */
67     cache_buffer =  pointer;
68     pointer =  pointer + CACHE_SIZE;
69     ram_disk_memory =  pointer;
70 #endif
71 
72     /* Initialize the FileX system.  */
73     fx_system_initialize();
74 
75 #ifdef FX_STANDALONE_ENABLE
76     ftest_0_entry(0);
77 #endif
78 }
79 
80 
81 
82 /* Define the test threads.  */
83 
ftest_0_entry(ULONG thread_input)84 static void    ftest_0_entry(ULONG thread_input)
85 {
86 
87 UINT        status;
88 ULONG       actual;
89 ULONG       read_value;
90 ULONG       write_value;
91 ULONG       available_bytes;
92 ULONG       i;
93 
94     FX_PARAMETER_NOT_USED(thread_input);
95 
96     /* Print out some test information banners.  */
97     printf("FileX Test:   Utility FAT flush test.......................................");
98 
99     /* Format the media.  This needs to be done before opening it!  */
100     status =  fx_media_format(&ram_disk,
101                             _fx_ram_driver,         // Driver entry
102                             ram_disk_memory,        // RAM disk memory pointer
103                             cache_buffer,           // Media buffer pointer
104                             CACHE_SIZE,             // Media buffer size
105                             "MY_RAM_DISK",          // Volume Name
106                             1,                      // Number of FATs
107                             32,                     // Directory Entries
108                             0,                      // Hidden sectors
109                             511,                    // Total sectors
110                             128,                    // Sector size
111                             1,                      // Sectors per cluster
112                             1,                      // Heads
113                             1);                     // Sectors per track
114 
115     /* Determine if the format had an error.  */
116     if (status)
117     {
118 
119         printf("ERROR!\n");
120         test_control_return(2);
121     }
122 
123     /* Open the ram_disk.  */
124     status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
125 
126     /* Check the status.  */
127     if (status != FX_SUCCESS)
128     {
129 
130         /* Error, return error code.  */
131         printf("ERROR!\n");
132         test_control_return(21);
133     }
134 
135     /* Create a file called TEST.TXT in the root directory.  */
136     status =  fx_file_create(&ram_disk, "TEST.TXT");
137 
138     /* Check the create status.  */
139     if (status != FX_SUCCESS)
140     {
141 
142         printf("ERROR!\n");
143         test_control_return(3);
144     }
145   /* Open the test file.  */
146     status =  fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
147 
148     /* Check the file open status.  */
149     if (status != FX_SUCCESS)
150     {
151 
152         printf("ERROR!\n");
153         test_control_return(4);
154     }
155 
156     /* Pickup the available bytes in the media.  */
157     status =  fx_media_space_available(&ram_disk, &available_bytes);
158 
159     /* Check for available bytes error.  */
160     if ((status != FX_SUCCESS) || (available_bytes < sizeof(ULONG)))
161     {
162 
163         printf("ERROR!\n");
164         test_control_return(5);
165     }
166 
167     /* Loop to write successive bytes out to the file.... to fill the media!  */
168     i =  0;
169     write_value =  0;
170     while (i < available_bytes)
171     {
172 
173         /* Write 4 bytes to the file.  */
174         status =  fx_file_write(&my_file, (void *) &write_value, sizeof(ULONG));
175 
176         /* Check the file write status.  */
177         if (status != FX_SUCCESS)
178         {
179 
180             printf("ERROR!\n");
181             test_control_return(6);
182         }
183 
184         /* Increment byte count.  */
185         i =  i + sizeof(ULONG);
186 
187         /* Increment write value.  */
188         write_value++;
189     }
190 
191     /* Pickup the available bytes in the media again.  */
192     status =  fx_media_space_available(&ram_disk, &i);
193 
194     /* Check for available bytes error.  */
195     if ((status != FX_SUCCESS) || (i != 0))
196     {
197 
198         printf("ERROR!\n");
199         test_control_return(7);
200     }
201 
202     for (i = 0; i < FX_MAX_FAT_CACHE; i++)
203     {
204         ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_cluster = 496;
205         ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_value =  1;
206         ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_dirty =  1;
207     }
208 
209     _fx_utility_FAT_flush(&ram_disk);
210 
211     /* Note: If definition of FX_FAT_MAP_SIZE is changed in future, the value checked in
212        return_value_if_fail will change for individual array element */
213     for(i=0;i<FX_FAT_MAP_SIZE;i++)
214     {
215     	return_value_if_fail((ram_disk.fx_media_fat_secondary_update_map[i]==63),8)
216     }
217 
218     fx_media_close(&ram_disk);
219 
220     /* Test with FAT 16 */
221     /* Format the media.  This needs to be done before opening it!  */
222     status =  fx_media_format(&ram_disk,
223                             _fx_ram_driver,         // Driver entry
224                             ram_disk_memory,        // RAM disk memory pointer
225                             cache_buffer,           // Media buffer pointer
226                             CACHE_SIZE,             // Media buffer size
227                             "MY_RAM_DISK",          // Volume Name
228                             1,                      // Number of FATs
229                             32,                     // Directory Entries
230                             0,                      // Hidden sectors
231                             14000,                   // Total sectors - FAT16
232                             64,                    // Sector size
233                             2,                      // Sectors per cluster
234                             1,                      // Heads
235                             1);                     // Sectors per track
236 
237     /* Determine if the format had an error.  */
238     if (status)
239     {
240 
241         printf("ERROR!\n");
242         test_control_return(2);
243     }
244 
245     /* Open the ram_disk.  */
246     status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
247 
248     /* Check the status.  */
249     if (status != FX_SUCCESS)
250     {
251 
252         /* Error, return error code.  */
253         printf("ERROR!\n");
254         test_control_return(21);
255     }
256     /* Create a file called TEST.TXT in the root directory.  */
257     status =  fx_file_create(&ram_disk, "TEST.TXT");
258 
259     /* Check the create status.  */
260     if (status != FX_SUCCESS)
261     {
262 
263         printf("ERROR!\n");
264         test_control_return(3);
265     }
266 
267     /* Open the test file.  */
268     status =  fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
269 
270     /* Check the file open status.  */
271     if (status != FX_SUCCESS)
272     {
273 
274         printf("ERROR!\n");
275         test_control_return(4);
276     }
277 
278     /* Pickup the available bytes in the media.  */
279     status =  fx_media_space_available(&ram_disk, &available_bytes);
280 
281     /* Check for available bytes error.  */
282     if ((status != FX_SUCCESS) || (available_bytes < sizeof(ULONG)))
283     {
284 
285         printf("ERROR!\n");
286         test_control_return(5);
287     }
288 
289     /* Loop to write successive bytes out to the file.... to fill the media!  */
290     i =  0;
291     write_value =  0;
292     while (i < available_bytes)
293     {
294 
295         /* Write 4 bytes to the file.  */
296         status =  fx_file_write(&my_file, (void *) &write_value, sizeof(ULONG));
297 
298         /* Check the file write status.  */
299         if (status != FX_SUCCESS)
300         {
301 
302             printf("ERROR!\n");
303             test_control_return(6);
304         }
305 
306         /* Increment byte count.  */
307         i =  i + sizeof(ULONG);
308 
309         /* Increment write value.  */
310         write_value++;
311     }
312 
313     /* Pickup the available bytes in the media again.  */
314     status =  fx_media_space_available(&ram_disk, &i);
315 
316     /* Check for available bytes error.  */
317     if ((status != FX_SUCCESS) || (i != 0))
318     {
319 
320         printf("ERROR!\n");
321         test_control_return(7);
322     }
323 
324     for (i = 0; i < FX_MAX_FAT_CACHE; i++)
325     {
326         ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_cluster = 0xA6;
327         ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_value =  1;
328         ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_dirty =  1;
329     }
330 
331     _fx_utility_FAT_flush(&ram_disk);
332 
333     /* Note: If definition of FX_FAT_MAP_SIZE is changed in future, the value checked in
334        return_value_if_fail will change for individual array element */
335     for(i=0;i<FX_FAT_MAP_SIZE;i++)
336     {
337     	return_value_if_fail((ram_disk.fx_media_fat_secondary_update_map[i]==255),8)
338     }
339 
340     fx_media_close(&ram_disk);
341 
342 
343     /* Test with FAT32. */
344     status = fx_media_format(&ram_disk,
345                             _fx_ram_driver,         // Driver entry
346                             ram_disk_memory,        // RAM disk memory pointer
347                             cache_buffer,           // Media buffer pointer
348                             CACHE_SIZE,             // Media buffer size
349                             "MY_RAM_DISK",          // Volume Name
350                             1,                      // Number of FATs
351                             32,                     // Directory Entries
352                             0,                      // Hidden sectors
353                             70000,                  // Total sectors - FAT32
354                             512,                    // Sector size
355                             1,                     // Sectors per cluster
356                             1,                      // Heads
357                             1);                     // Sectors per track
358     status += fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
359     status += fx_file_create(&ram_disk, "TEST.TXT");
360     return_if_fail(FX_SUCCESS == status);
361 
362     /* Open the test file.  */
363     status =  fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
364 
365     /* Check the file open status.  */
366     if (status != FX_SUCCESS)
367     {
368 
369         printf("ERROR!\n");
370         test_control_return(4);
371     }
372 
373     /* Pickup the available bytes in the media.  */
374     status =  fx_media_space_available(&ram_disk, &available_bytes);
375 
376     /* Check for available bytes error.  */
377     if ((status != FX_SUCCESS) || (available_bytes < sizeof(ULONG)))
378     {
379 
380         printf("ERROR!\n");
381         test_control_return(5);
382     }
383 
384     /* Loop to write successive bytes out to the file.... to fill the media!  */
385     i =  0;
386     write_value =  0;
387     while (i < available_bytes/128)
388     {
389 
390         /* Write 4 bytes to the file.  */
391         status =  fx_file_write(&my_file, (void *) &write_value, sizeof(ULONG));
392 
393         /* Check the file write status.  */
394         if (status != FX_SUCCESS)
395         {
396 
397             printf("ERROR!\n");
398             test_control_return(6);
399         }
400 
401         /* Increment byte count.  */
402         i =  i + sizeof(ULONG);
403 
404         /* Increment write value.  */
405         write_value++;
406     }
407 
408     for (i = 0; i < FX_MAX_FAT_CACHE; i++)
409     {
410         ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_cluster = 0xF160;
411         ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_value =  1;
412         ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_dirty =  1;
413     }
414 
415     _fx_utility_FAT_flush(&ram_disk);
416 
417     /* Note: If definition of FX_FAT_MAP_SIZE is changed in future, the value checked in
418        return_value_if_fail will change for individual array element */
419     for(i=0;i<FX_FAT_MAP_SIZE;i++)
420     {
421     	return_value_if_fail((ram_disk.fx_media_fat_secondary_update_map[i]==65),8)
422     }
423 
424     fx_media_close(&ram_disk);
425 
426     printf("SUCCESS!\n");
427 
428     test_control_return(0);
429 
430 }
431