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