1 /* This is a small demo of the high-performance FileX FAT file system with LevelX
2    and the NAND simulated driver.  */
3 
4 #include <stdio.h>
5 #include "fx_api.h"
6 #include "lx_api.h"
7 
8 
9 #define DEMO_STACK_SIZE 4096
10 
11 
12 /* Buffer for FileX FX_MEDIA sector cache. This must be large enough for at least one
13    sector, which are typically 512 bytes in size.  */
14 
15 unsigned char media_memory[4096];
16 
17 
18 /* Define NAND simulated device driver entry.  */
19 
20 VOID  _fx_nand_flash_simulator_driver(FX_MEDIA *media_ptr);
21 
22 
23 /* Define LevelX NAND simulated flash erase.  */
24 
25 UINT  _lx_nand_flash_simulator_erase_all(VOID);
26 
27 
28 /* Define thread prototypes.  */
29 
30 void    thread_0_entry(ULONG thread_input);
31 UCHAR   thread_0_stack[DEMO_STACK_SIZE];
32 
33 
34 /* Define FileX global data structures.  */
35 
36 FX_MEDIA                nand_disk;
37 FX_FILE                 my_file;
38 FX_FILE                 my_file1;
39 
40 
41 /* Define ThreadX global data structures.  */
42 
43 #ifndef LX_STANDALONE_ENABLE
44 TX_THREAD       thread_0;
45 #endif
46 ULONG           thread_0_counter;
47 
48 
main(void)49 int  main(void)
50 {
51     /* Enter the ThreadX kernel.  */
52 #ifndef LX_STANDALONE_ENABLE
53     tx_kernel_enter();
54 #else
55 
56     /* Initialize NAND flash.  */
57     lx_nand_flash_initialize();
58 
59     /* Initialize FileX.  */
60     fx_system_initialize();
61 
62     thread_0_entry(0);
63 #endif
64 
65 }
66 
67 
68 /* Define what the initial system looks like.  */
69 
70 #ifndef LX_STANDALONE_ENABLE
tx_application_define(void * first_unused_memory)71 void    tx_application_define(void *first_unused_memory)
72 {
73 
74 
75     /* Put system definition stuff in here, e.g. thread creates and other assorted
76        create information.  */
77 
78     /* Create the main thread.  */
79     tx_thread_create(&thread_0, "thread 0", thread_0_entry, 0,
80             thread_0_stack, DEMO_STACK_SIZE,
81             1, 1, TX_NO_TIME_SLICE, TX_AUTO_START);
82 
83     /* Initialize NAND flash.  */
84     lx_nand_flash_initialize();
85 
86     /* Initialize FileX.  */
87     fx_system_initialize();
88 }
89 #endif
90 
91 
thread_0_entry(ULONG thread_input)92 void    thread_0_entry(ULONG thread_input)
93 {
94 
95 UINT        status;
96 ULONG       actual;
97 CHAR        local_buffer[30];
98 
99     LX_PARAMETER_NOT_USED(thread_input);
100 
101     /* Erase the simulated NAND flash.  */
102     _lx_nand_flash_simulator_erase_all();
103 
104     /* Format the NAND disk - the memory for the NAND flash disk is setup in
105        the NAND simulator. Note that for best performance, the format of the
106        NAND flash should be less than one full NAND flash block of sectors.  */
107     fx_media_format(&nand_disk,
108                             _fx_nand_flash_simulator_driver,  // Driver entry
109                             FX_NULL,                          // Unused
110                             media_memory,                     // Media buffer pointer
111                             sizeof(media_memory),             // Media buffer size
112                             "MY_NAND_DISK",                   // Volume Name
113                             1,                                // Number of FATs
114                             32,                               // Directory Entries
115                             0,                                // Hidden sectors
116                             120,                              // Total sectors
117                             2048,                             // Sector size
118                             1,                                // Sectors per cluster
119                             1,                                // Heads
120                             1);                               // Sectors per track
121 
122     /* Loop to repeat the demo over and over!  */
123     do
124     {
125 
126         /* Open the NAND disk.  */
127         status =  fx_media_open(&nand_disk, "NAND DISK", _fx_nand_flash_simulator_driver, FX_NULL, media_memory, sizeof(media_memory));
128 
129         /* Check the media open status.  */
130         if (status != FX_SUCCESS)
131         {
132 
133             /* Error, break the loop!  */
134             break;
135         }
136 
137         /* Create a file called TEST.TXT in the root directory.  */
138         status =  fx_file_create(&nand_disk, "TEST.TXT");
139 
140         /* Check the create status.  */
141         if (status != FX_SUCCESS)
142         {
143 
144             /* Check for an already created status. This is expected on the
145                second pass of this loop!  */
146             if (status != FX_ALREADY_CREATED)
147             {
148 
149                 /* Create error, break the loop.  */
150                 break;
151             }
152         }
153 
154         /* Open the test file.  */
155         status =  fx_file_open(&nand_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
156 
157         /* Check the file open status.  */
158         if (status != FX_SUCCESS)
159         {
160 
161             /* Error opening file, break the loop.  */
162             break;
163         }
164 
165         /* Seek to the beginning of the test file.  */
166         status =  fx_file_seek(&my_file, 0);
167 
168         /* Check the file seek status.  */
169         if (status != FX_SUCCESS)
170         {
171 
172             /* Error performing file seek, break the loop.  */
173             break;
174         }
175 
176         /* Write a string to the test file.  */
177         status =  fx_file_write(&my_file, " ABCDEFGHIJKLMNOPQRSTUVWXYZ\n", 28);
178 
179         /* Check the file write status.  */
180         if (status != FX_SUCCESS)
181         {
182 
183             /* Error writing to a file, break the loop.  */
184             break;
185         }
186 
187         /* Seek to the beginning of the test file.  */
188         status =  fx_file_seek(&my_file, 0);
189 
190         /* Check the file seek status.  */
191         if (status != FX_SUCCESS)
192         {
193 
194             /* Error performing file seek, break the loop.  */
195             break;
196         }
197 
198         /* Read the first 28 bytes of the test file.  */
199         status =  fx_file_read(&my_file, local_buffer, 28, &actual);
200 
201         /* Check the file read status.  */
202         if ((status != FX_SUCCESS) || (actual != 28))
203         {
204 
205             /* Error reading file, break the loop.  */
206             break;
207         }
208 
209         /* Close the test file.  */
210         status =  fx_file_close(&my_file);
211 
212         /* Check the file close status.  */
213         if (status != FX_SUCCESS)
214         {
215 
216             /* Error closing the file, break the loop.  */
217             break;
218         }
219 
220         /* Delete the file.  */
221         status =  fx_file_delete(&nand_disk, "TEST.TXT");
222 
223         /* Check the file delete status.  */
224         if (status != FX_SUCCESS)
225         {
226 
227             /* Error deleting the file, break the loop.  */
228             break;
229         }
230 
231         /* Close the media.  */
232         status =  fx_media_close(&nand_disk);
233 
234         /* Check the media close status.  */
235         if (status != FX_SUCCESS)
236         {
237 
238             /* Error closing the media, break the loop.  */
239             break;
240         }
241 
242         /* Increment the thread counter, which represents the number
243            of successful passes through this loop.  */
244         thread_0_counter++;
245     } while (1);
246 
247     /* If we get here the FileX test failed!  */
248     return;
249 }
250 
251