1 /* This is a small demo of the high-performance FileX FAT file system. It includes setup for
2 a small 34KB RAM disk and a loop that writes and reads a small file. */
3 #include "fx_api.h"
4 #ifdef FX_ENABLE_FAULT_TOLERANT
5 #include "fx_fault_tolerant.h"
6 #endif /* FX_ENABLE_FAULT_TOLERANT */
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 #ifdef FX_ENABLE_FAULT_TOLERANT
17 UCHAR fault_tolerant_memory[FX_FAULT_TOLERANT_MAXIMUM_LOG_FILE_SIZE];
18 #endif /* FX_ENABLE_FAULT_TOLERANT */
19
20
21 /* Define RAM device driver entry. */
22
23 VOID _fx_ram_driver(FX_MEDIA *media_ptr);
24
25
26 /* Define thread prototypes. */
27
28 void thread_0_entry(ULONG thread_input);
29
30
31 /* Define FileX global data structures. */
32
33 FX_MEDIA ram_disk;
34 FX_FILE my_file;
35
36 #ifndef FX_STANDALONE_ENABLE
37 CHAR *ram_disk_memory;
38 #else
39 unsigned char ram_disk_memory[256*512];
40 #endif
41
42 /* Define ThreadX global data structures. */
43
44 #ifndef FX_STANDALONE_ENABLE
45 TX_THREAD thread_0;
46 #endif
47 ULONG thread_0_counter;
48
49
main(void)50 void main(void)
51 {
52
53 #ifdef FX_STANDALONE_ENABLE
54
55 /* Initialize FileX. */
56 fx_system_initialize();
57
58 thread_0_entry(0);
59 #else
60 /* Enter the ThreadX kernel. */
61 tx_kernel_enter();
62 #endif
63
64 }
65
66
67 /* Define what the initial system looks like. */
68
69 #ifndef FX_STANDALONE_ENABLE
tx_application_define(void * first_unused_memory)70 void tx_application_define(void *first_unused_memory)
71 {
72
73 CHAR *pointer;
74
75 /* Put first available memory address into a character pointer. */
76 pointer = (CHAR *) first_unused_memory;
77
78 /* Put system definition stuff in here, e.g. thread creates and other assorted
79 create information. */
80
81 /* Create the main thread. */
82 tx_thread_create(&thread_0, "thread 0", thread_0_entry, 0,
83 pointer, DEMO_STACK_SIZE,
84 1, 1, TX_NO_TIME_SLICE, TX_AUTO_START);
85 pointer = pointer + DEMO_STACK_SIZE;
86
87 /* Save the memory pointer for the RAM disk. */
88 ram_disk_memory = pointer;
89
90 /* Initialize FileX. */
91 fx_system_initialize();
92 }
93
94 #endif
95
thread_0_entry(ULONG thread_input)96 void thread_0_entry(ULONG thread_input)
97 {
98
99 UINT status;
100 ULONG actual;
101 CHAR local_buffer[30];
102
103
104 /* Format the RAM disk - the memory for the RAM disk was setup in
105 tx_application_define above. */
106 fx_media_format(&ram_disk,
107 _fx_ram_driver, // Driver entry
108 ram_disk_memory, // RAM disk memory pointer
109 media_memory, // Media buffer pointer
110 sizeof(media_memory), // Media buffer size
111 "MY_RAM_DISK", // Volume Name
112 1, // Number of FATs
113 32, // Directory Entries
114 0, // Hidden sectors
115 256, // Total sectors
116 512, // Sector size
117 8, // Sectors per cluster
118 1, // Heads
119 1); // Sectors per track
120
121
122 /* Loop to repeat the demo over and over! */
123 do
124 {
125
126 /* Open the RAM disk. */
127 status = fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, 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 #ifdef FX_ENABLE_FAULT_TOLERANT
138 status = fx_fault_tolerant_enable(&ram_disk, fault_tolerant_memory, sizeof(fault_tolerant_memory));
139
140 if (status != FX_SUCCESS)
141 {
142
143 /* Fault tolerant enable error, break the loop. */
144 break;
145 }
146 #endif /* FX_ENABLE_FAULT_TOLERANT */
147
148 /* Create a file called TEST.TXT in the root directory. */
149 status = fx_file_create(&ram_disk, "TEST.TXT");
150
151 /* Check the create status. */
152 if (status != FX_SUCCESS)
153 {
154
155 /* Check for an already created status. This is expected on the
156 second pass of this loop! */
157 if (status != FX_ALREADY_CREATED)
158 {
159
160 /* Create error, break the loop. */
161 break;
162 }
163 }
164
165 /* Open the test file. */
166 status = fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
167
168 /* Check the file open status. */
169 if (status != FX_SUCCESS)
170 {
171
172 /* Error opening file, break the loop. */
173 break;
174 }
175
176 /* Seek to the beginning of the test file. */
177 status = fx_file_seek(&my_file, 0);
178
179 /* Check the file seek status. */
180 if (status != FX_SUCCESS)
181 {
182
183 /* Error performing file seek, break the loop. */
184 break;
185 }
186
187 /* Write a string to the test file. */
188 status = fx_file_write(&my_file, " ABCDEFGHIJKLMNOPQRSTUVWXYZ\n", 28);
189
190 /* Check the file write status. */
191 if (status != FX_SUCCESS)
192 {
193
194 /* Error writing to a file, break the loop. */
195 break;
196 }
197
198 /* Seek to the beginning of the test file. */
199 status = fx_file_seek(&my_file, 0);
200
201 /* Check the file seek status. */
202 if (status != FX_SUCCESS)
203 {
204
205 /* Error performing file seek, break the loop. */
206 break;
207 }
208
209 /* Read the first 28 bytes of the test file. */
210 status = fx_file_read(&my_file, local_buffer, 28, &actual);
211
212 /* Check the file read status. */
213 if ((status != FX_SUCCESS) || (actual != 28))
214 {
215
216 /* Error reading file, break the loop. */
217 break;
218 }
219
220 /* Close the test file. */
221 status = fx_file_close(&my_file);
222
223 /* Check the file close status. */
224 if (status != FX_SUCCESS)
225 {
226
227 /* Error closing the file, break the loop. */
228 break;
229 }
230
231 /* Close the media. */
232 status = fx_media_close(&ram_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
246 } while (1);
247
248 /* If we get here the FileX test failed! */
249 return;
250 }
251
252