1 /* This FileX test concentrates on the Fault-Tolerant Media Full test.  */
2 /*
3 For FAT 12, 16, 32, one cluster size is 1024 bytes;
4 Check media full operation:
5 Step1: Format and open the media;
6 Step2: Enable fault tolerant feature;
7 Step3: Create new file called "TEST.TXT";
8 Step4: Get the media available bytes;
9 Step5: Loop to write bytes out of the file to fill the media;
10 Step6: Check the media available bytes.
11 Step7: Loop to read and check the file data from the media;
12 */
13 #ifndef FX_STANDALONE_ENABLE
14 #include   "tx_api.h"
15 #endif
16 #include   "fx_api.h"
17 #include   "fx_fault_tolerant.h"
18 #include   <stdio.h>
19 #include   "fx_ram_driver_test.h"
20 extern void    test_control_return(UINT status);
21 void    filex_fault_tolerant_media_full_test_application_define(void *first_unused_memory);
22 
23 #if defined (FX_ENABLE_FAULT_TOLERANT) && defined (FX_FAULT_TOLERANT)
24 
25 #define DEMO_STACK_SIZE         4096
26 #define CACHE_SIZE                  2048
27 #define FAULT_TOLERANT_SIZE         FX_FAULT_TOLERANT_MINIMAL_BUFFER_SIZE
28 
29 
30 /* Define the ThreadX and FileX object control blocks...  */
31 
32 #ifndef FX_STANDALONE_ENABLE
33 static TX_THREAD                ftest_0;
34 #endif
35 static FX_MEDIA                 ram_disk;
36 static FX_FILE                  my_file;
37 static UCHAR                    *pointer;
38 
39 /* Define the counters used in the test application...  */
40 
41 #ifndef FX_STANDALONE_ENABLE
42 static UCHAR                    *cache_buffer;
43 static UCHAR                    *fault_tolerant_buffer;
44 #else
45 static UCHAR                    cache_buffer[CACHE_SIZE];
46 static UCHAR                    fault_tolerant_buffer[FAULT_TOLERANT_SIZE];
47 #endif
48 
49 #define TEST_COUNT              3
50 
51 /* Define thread prototypes.  */
52 
53 static void    ftest_0_entry(ULONG thread_input);
54 extern void    _fx_ram_driver(FX_MEDIA *media_ptr);
55 extern void    test_control_return(UINT status);
56 
57 
58 /* Define what the initial system looks like.  */
59 
60 #ifdef CTEST
test_application_define(void * first_unused_memory)61 void test_application_define(void *first_unused_memory)
62 #else
63 void    filex_fault_tolerant_media_full_test_application_define(void *first_unused_memory)
64 #endif
65 {
66 
67 
68 #ifndef FX_STANDALONE_ENABLE
69     /* Setup the working pointer.  */
70     pointer =  (UCHAR *) first_unused_memory;
71 
72     /* Create the main thread.  */
73 
74     tx_thread_create(&ftest_0, "thread 0", ftest_0_entry, 0,
75             pointer, DEMO_STACK_SIZE,
76             4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
77 
78     pointer =  pointer + DEMO_STACK_SIZE;
79 
80     /* Setup memory for the RAM disk and the sector cache.  */
81     cache_buffer =  pointer;
82     pointer += CACHE_SIZE;
83     fault_tolerant_buffer = pointer;
84     pointer += FAULT_TOLERANT_SIZE;
85 #endif
86 
87     /* Initialize the FileX system.  */
88     fx_system_initialize();
89 #ifdef FX_STANDALONE_ENABLE
90     ftest_0_entry(0);
91 #endif
92 }
93 
94 
95 /* Define the test threads.  */
96 
ftest_0_entry(ULONG thread_input)97 static void    ftest_0_entry(ULONG thread_input)
98 {
99 
100 UINT        status;
101 ULONG       actual;
102 ULONG       read_value;
103 ULONG       write_value;
104 ULONG       available_bytes;
105 ULONG       i, j;
106 
107     FX_PARAMETER_NOT_USED(thread_input);
108 
109     /* Print out some test information banners.  */
110     printf("FileX Test:   Fault Tolerant Media Full Test.........................");
111 
112     /* Loop to test FAT 12, 16, 32.   */
113     for (i = 0; i < TEST_COUNT; i ++)
114     {
115         if (i == 0)
116         {
117             /* Format the media with FAT12.  This needs to be done before opening it!  */
118             status =  fx_media_format(&ram_disk,
119                                      _fx_ram_driver,         // Driver entry
120                                      ram_disk_memory_large,  // RAM disk memory pointer
121                                      cache_buffer,           // Media buffer pointer
122                                      CACHE_SIZE,             // Media buffer size
123                                      "MY_RAM_DISK",          // Volume Name
124                                      1,                      // Number of FATs
125                                      32,                     // Directory Entries
126                                      0,                      // Hidden sectors
127                                      256,                    // Total sectors
128                                      256,                    // Sector size
129                                      8,                      // Sectors per cluster
130                                      1,                      // Heads
131                                      1);                     // Sectors per track
132         }
133         else if (i == 1)
134         {
135             /* Format the media with FAT16.  This needs to be done before opening it!  */
136             status =  fx_media_format(&ram_disk,
137                                      _fx_ram_driver,         // Driver entry
138                                      ram_disk_memory_large,  // RAM disk memory pointer
139                                      cache_buffer,           // Media buffer pointer
140                                      CACHE_SIZE,             // Media buffer size
141                                      "MY_RAM_DISK",          // Volume Name
142                                      1,                      // Number of FATs
143                                      32,                     // Directory Entries
144                                      0,                      // Hidden sectors
145                                      4200 * 8,               // Total sectors
146                                      256,                    // Sector size
147                                      8,                      // Sectors per cluster
148                                      1,                      // Heads
149                                      1);                     // Sectors per track
150         }
151         else if (i == 2)
152         {
153             /* Format the media with FAT32.  This needs to be done before opening it!  */
154             status =  fx_media_format(&ram_disk,
155                                      _fx_ram_driver,         // Driver entry
156                                      ram_disk_memory_large,  // RAM disk memory pointer
157                                      cache_buffer,           // Media buffer pointer
158                                      CACHE_SIZE,             // Media buffer size
159                                      "MY_RAM_DISK",          // Volume Name
160                                      1,                      // Number of FATs
161                                      32,                     // Directory Entries
162                                      0,                      // Hidden sectors
163                                      70000 * 8,              // Total sectors
164                                      256,                    // Sector size
165                                      8,                      // Sectors per cluster
166                                      1,                      // Heads
167                                      1);                     // Sectors per track
168         }
169 
170         return_if_fail( status == FX_SUCCESS);
171 
172         /* Open the ram_disk.  */
173         status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory_large, cache_buffer, CACHE_SIZE);
174         return_if_fail( status == FX_SUCCESS);
175 
176         /* Enable the Fault-tolerant feature.  */
177         status = fx_fault_tolerant_enable(&ram_disk, fault_tolerant_buffer, FAULT_TOLERANT_SIZE);
178         return_if_fail( status == FX_SUCCESS);
179 
180         /* Create a file called TEST.TXT in the root directory.  */
181         status =  fx_file_create(&ram_disk, "TEST.TXT");
182         return_if_fail( status == FX_SUCCESS);
183 
184         /* Open the test file.  */
185         status =  fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
186         return_if_fail( status == FX_SUCCESS);
187 
188         /* Pickup the available bytes in the media.  */
189         status =  fx_media_space_available(&ram_disk, &available_bytes);
190         return_if_fail( (status == FX_SUCCESS) && (available_bytes >= sizeof(ULONG)));
191 
192         /* Loop to write successive bytes out to the file.... to fill the media!  */
193         j =  0;
194         write_value =  0;
195         while (j < available_bytes)
196         {
197 
198             /* Write 4 bytes to the file.  */
199             status =  fx_file_write(&my_file, (void *) &write_value, sizeof(ULONG));
200             return_if_fail( status == FX_SUCCESS);
201 
202             /* Increment byte count.  */
203             j =  j + sizeof(ULONG);
204 
205             /* Increment write value.  */
206             write_value++;
207         }
208 
209         /* Pickup the available bytes in the media again.  */
210         status =  fx_media_space_available(&ram_disk, &j);
211         return_if_fail( (status == FX_SUCCESS) && (j == 0));
212 
213 #ifndef FX_DISABLE_CACHE
214         /* At this point, we should invalidate the (which also flushes the cache) media to ensure that all
215         dirty sectors are written.  */
216         status =  fx_media_cache_invalidate(&ram_disk);
217         return_if_fail((status == FX_SUCCESS) && (ram_disk.fx_media_sector_cache_dirty_count == 0));
218 
219         /* See if any sectors are still valid in the cache.  */
220         for (j = 0; j < ram_disk.fx_media_sector_cache_size; j++)
221         {
222 
223             /* Determine if this cache entry is still valid.  */
224             return_if_fail(ram_disk.fx_media_sector_cache[j].fx_cached_sector_valid == 0);
225         }
226 #endif /* FX_DISABLE_CACHE */
227 
228         /* Seek to the beginning of the test file.  */
229         status =  fx_file_seek(&my_file, 0);
230         return_if_fail( status == FX_SUCCESS);
231 
232         /* Now read in all the bytes again to make sure the file contents are really there.  */
233         j =  0;
234         read_value =  0;
235         write_value =  0;
236         while (j < available_bytes)
237         {
238 
239             /* Read 4 bytes from the file.  */
240             status =  fx_file_read(&my_file, (void *) &read_value, sizeof(ULONG), &actual);
241 
242             /* Check the file read status.  */
243             return_if_fail( (status == FX_SUCCESS) && (actual == 4) && (read_value == write_value));
244 
245             /* Increment byte count.  */
246             j =  j + sizeof(ULONG);
247 
248             /* Increment write value.  */
249             write_value++;
250         }
251 
252         /* Close the test file.  */
253         status =  fx_file_close(&my_file);
254         return_if_fail( status == FX_SUCCESS);
255 
256         /* Close the media.  */
257         status =  fx_media_close(&ram_disk);
258         return_if_fail( status == FX_SUCCESS);
259     }
260 
261     /* Output successful.  */
262     printf("SUCCESS!\n");
263     test_control_return(0);
264 }
265 
266 #else
267 
268 #ifdef CTEST
test_application_define(void * first_unused_memory)269 void test_application_define(void *first_unused_memory)
270 #else
271 void    filex_fault_tolerant_media_full_test_application_define(void *first_unused_memory)
272 #endif
273 {
274 
275     FX_PARAMETER_NOT_USED(first_unused_memory);
276 
277     /* Print out some test information banners.  */
278     printf("FileX Test:   Fault Tolerant Media Full Test.........................N/A\n");
279 
280     test_control_return(255);
281 }
282 #endif
283