| 1 | /***************************************************************************
|
|---|
| 2 | * Copyright (c) 2024 Microsoft Corporation
|
|---|
| 3 | * Copyright (c) 2025 Eclipse ThreadX contributors
|
|---|
| 4 | *
|
|---|
| 5 | * This program and the accompanying materials are made available under the
|
|---|
| 6 | * terms of the MIT License which is available at
|
|---|
| 7 | * https://opensource.org/licenses/MIT.
|
|---|
| 8 | *
|
|---|
| 9 | * SPDX-License-Identifier: MIT
|
|---|
| 10 | **************************************************************************/
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 | /* This is a small demo of the high-performance FileX FAT file system. It includes setup for
|
|---|
| 14 | a small 34KB RAM disk and a loop that writes and reads a small file. */
|
|---|
| 15 | #include "fx_api.h"
|
|---|
| 16 | #ifdef FX_ENABLE_FAULT_TOLERANT
|
|---|
| 17 | #include "fx_fault_tolerant.h"
|
|---|
| 18 | #endif /* FX_ENABLE_FAULT_TOLERANT */
|
|---|
| 19 |
|
|---|
| 20 | #define DEMO_STACK_SIZE 2048
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 | /* Buffer for FileX FX_MEDIA sector cache. This must be large enough for at least one
|
|---|
| 24 | sector, which are typically 512 bytes in size. */
|
|---|
| 25 |
|
|---|
| 26 | unsigned char media_memory[512];
|
|---|
| 27 |
|
|---|
| 28 | #ifdef FX_ENABLE_FAULT_TOLERANT
|
|---|
| 29 | UCHAR fault_tolerant_memory[FX_FAULT_TOLERANT_MAXIMUM_LOG_FILE_SIZE];
|
|---|
| 30 | #endif /* FX_ENABLE_FAULT_TOLERANT */
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 | /* Define RAM device driver entry. */
|
|---|
| 34 |
|
|---|
| 35 | VOID _fx_ram_driver(FX_MEDIA *media_ptr);
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 | /* Define thread prototypes. */
|
|---|
| 39 |
|
|---|
| 40 | void thread_0_entry(ULONG thread_input);
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 | /* Define FileX global data structures. */
|
|---|
| 44 |
|
|---|
| 45 | #define TOTAL_SECTORS 256
|
|---|
| 46 | #define SECTOR_SIZE 512
|
|---|
| 47 |
|
|---|
| 48 | FX_MEDIA ram_disk;
|
|---|
| 49 | FX_FILE my_file;
|
|---|
| 50 |
|
|---|
| 51 | #ifndef FX_STANDALONE_ENABLE
|
|---|
| 52 | CHAR *ram_disk_memory;
|
|---|
| 53 | #else
|
|---|
| 54 | unsigned char ram_disk_memory[TOTAL_SECTORS*SECTOR_SIZE];
|
|---|
| 55 | #endif
|
|---|
| 56 |
|
|---|
| 57 | /* Define ThreadX global data structures. */
|
|---|
| 58 |
|
|---|
| 59 | #ifndef FX_STANDALONE_ENABLE
|
|---|
| 60 | TX_THREAD thread_0;
|
|---|
| 61 | #endif
|
|---|
| 62 | ULONG thread_0_counter;
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 | void main(void)
|
|---|
| 66 | {
|
|---|
| 67 |
|
|---|
| 68 | #ifdef FX_STANDALONE_ENABLE
|
|---|
| 69 |
|
|---|
| 70 | /* Initialize FileX. */
|
|---|
| 71 | fx_system_initialize();
|
|---|
| 72 |
|
|---|
| 73 | thread_0_entry(0);
|
|---|
| 74 | #else
|
|---|
| 75 | /* Enter the ThreadX kernel. */
|
|---|
| 76 | tx_kernel_enter();
|
|---|
| 77 | #endif
|
|---|
| 78 |
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 | /* Define what the initial system looks like. */
|
|---|
| 83 |
|
|---|
| 84 | #ifndef FX_STANDALONE_ENABLE
|
|---|
| 85 | void tx_application_define(void *first_unused_memory)
|
|---|
| 86 | {
|
|---|
| 87 |
|
|---|
| 88 | CHAR *pointer;
|
|---|
| 89 |
|
|---|
| 90 | /* Put first available memory address into a character pointer. */
|
|---|
| 91 | pointer = (CHAR *) first_unused_memory;
|
|---|
| 92 |
|
|---|
| 93 | /* Put system definition stuff in here, e.g. thread creates and other assorted
|
|---|
| 94 | create information. */
|
|---|
| 95 |
|
|---|
| 96 | /* Create the main thread. */
|
|---|
| 97 | tx_thread_create(&thread_0, "thread 0", thread_0_entry, 0,
|
|---|
| 98 | pointer, DEMO_STACK_SIZE,
|
|---|
| 99 | 1, 1, TX_NO_TIME_SLICE, TX_AUTO_START);
|
|---|
| 100 | pointer = pointer + DEMO_STACK_SIZE;
|
|---|
| 101 |
|
|---|
| 102 | /* Save the memory pointer for the RAM disk. */
|
|---|
| 103 | ram_disk_memory = pointer;
|
|---|
| 104 |
|
|---|
| 105 | /* Initialize FileX. */
|
|---|
| 106 | fx_system_initialize();
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | #endif
|
|---|
| 110 |
|
|---|
| 111 | void thread_0_entry(ULONG thread_input)
|
|---|
| 112 | {
|
|---|
| 113 |
|
|---|
| 114 | UINT status;
|
|---|
| 115 | ULONG actual;
|
|---|
| 116 | CHAR local_buffer[30];
|
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 | /* Format the RAM disk - the memory for the RAM disk was setup in
|
|---|
| 120 | tx_application_define above.
|
|---|
| 121 |
|
|---|
| 122 | Important Note: The user must ensure there is enough RAM for the format
|
|---|
| 123 | specified. Otherwise, memory corruption can occur.
|
|---|
| 124 | */
|
|---|
| 125 | status = fx_media_format(&ram_disk
|
|---|
| 126 | _fx_ram_driver, // Driver entry
|
|---|
| 127 | ram_disk_memory, // RAM disk memory pointer
|
|---|
| 128 | media_memory, // Media buffer pointer
|
|---|
| 129 | sizeof(media_memory), // Media buffer size
|
|---|
| 130 | "MY_RAM_DISK", // Volume Name
|
|---|
| 131 | 1, // Number of FATs
|
|---|
| 132 | 32, // Directory Entries
|
|---|
| 133 | 0, // Hidden sectors
|
|---|
| 134 | TOTAL_SECTORS, // Total sectors
|
|---|
| 135 | SECTOR_SIZE, // Sector size
|
|---|
| 136 | 8, // Sectors per cluster
|
|---|
| 137 | 1, // Heads
|
|---|
| 138 | 1); // Sectors per track
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 | /* Determine if the RAM disk format was successful. */
|
|---|
| 142 | if (status != FX_SUCCESS)
|
|---|
| 143 | {
|
|---|
| 144 | /* Error formatting the RAM disk. */
|
|---|
| 145 | printf("HTTPS RAM disk format failed, error: %x\n", status);
|
|---|
| 146 | return;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | /* Loop to repeat the demo over and over! */
|
|---|
| 150 | do
|
|---|
| 151 | {
|
|---|
| 152 | /* Open the RAM disk. */
|
|---|
| 153 | status = fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, media_memory, sizeof(media_memory));
|
|---|
| 154 |
|
|---|
| 155 | /* Determine if the RAM disk open was successful. */
|
|---|
| 156 | if (status != FX_SUCCESS)
|
|---|
| 157 | {
|
|---|
| 158 | /* Error opening the RAM disk. */
|
|---|
| 159 | printf("RAM disk open failed, error: %x\n", status);
|
|---|
| 160 | return;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 | #ifdef FX_ENABLE_FAULT_TOLERANT
|
|---|
| 165 | status = fx_fault_tolerant_enable(&ram_disk, fault_tolerant_memory, sizeof(fault_tolerant_memory));
|
|---|
| 166 |
|
|---|
| 167 | if (status != FX_SUCCESS)
|
|---|
| 168 | {
|
|---|
| 169 |
|
|---|
| 170 | /* Fault tolerant enable error, break the loop. */
|
|---|
| 171 | break;
|
|---|
| 172 | }
|
|---|
| 173 | #endif /* FX_ENABLE_FAULT_TOLERANT */
|
|---|
| 174 |
|
|---|
| 175 | /* Create a file called TEST.TXT in the root directory. */
|
|---|
| 176 | status = fx_file_create(&ram_disk, "TEST.TXT");
|
|---|
| 177 |
|
|---|
| 178 | /* Check the create status. */
|
|---|
| 179 | if (status != FX_SUCCESS)
|
|---|
| 180 | {
|
|---|
| 181 |
|
|---|
| 182 | /* Check for an already created status. This is expected on the
|
|---|
| 183 | second pass of this loop! */
|
|---|
| 184 | if (status != FX_ALREADY_CREATED)
|
|---|
| 185 | {
|
|---|
| 186 |
|
|---|
| 187 | /* Create error, break the loop. */
|
|---|
| 188 | break;
|
|---|
| 189 | }
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | /* Open the test file. */
|
|---|
| 193 | status = fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
|
|---|
| 194 |
|
|---|
| 195 | /* Check the file open status. */
|
|---|
| 196 | if (status != FX_SUCCESS)
|
|---|
| 197 | {
|
|---|
| 198 |
|
|---|
| 199 | /* Error opening file, break the loop. */
|
|---|
| 200 | break;
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | /* Seek to the beginning of the test file. */
|
|---|
| 204 | status = fx_file_seek(&my_file, 0);
|
|---|
| 205 |
|
|---|
| 206 | /* Check the file seek status. */
|
|---|
| 207 | if (status != FX_SUCCESS)
|
|---|
| 208 | {
|
|---|
| 209 |
|
|---|
| 210 | /* Error performing file seek, break the loop. */
|
|---|
| 211 | break;
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | /* Write a string to the test file. */
|
|---|
| 215 | status = fx_file_write(&my_file, " ABCDEFGHIJKLMNOPQRSTUVWXYZ\n", 28);
|
|---|
| 216 |
|
|---|
| 217 | /* Check the file write status. */
|
|---|
| 218 | if (status != FX_SUCCESS)
|
|---|
| 219 | {
|
|---|
| 220 |
|
|---|
| 221 | /* Error writing to a file, break the loop. */
|
|---|
| 222 | break;
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | /* Seek to the beginning of the test file. */
|
|---|
| 226 | status = fx_file_seek(&my_file, 0);
|
|---|
| 227 |
|
|---|
| 228 | /* Check the file seek status. */
|
|---|
| 229 | if (status != FX_SUCCESS)
|
|---|
| 230 | {
|
|---|
| 231 |
|
|---|
| 232 | /* Error performing file seek, break the loop. */
|
|---|
| 233 | break;
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | /* Read the first 28 bytes of the test file. */
|
|---|
| 237 | status = fx_file_read(&my_file, local_buffer, 28, &actual);
|
|---|
| 238 |
|
|---|
| 239 | /* Check the file read status. */
|
|---|
| 240 | if ((status != FX_SUCCESS) || (actual != 28))
|
|---|
| 241 | {
|
|---|
| 242 |
|
|---|
| 243 | /* Error reading file, break the loop. */
|
|---|
| 244 | break;
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | /* Close the test file. */
|
|---|
| 248 | status = fx_file_close(&my_file);
|
|---|
| 249 |
|
|---|
| 250 | /* Check the file close status. */
|
|---|
| 251 | if (status != FX_SUCCESS)
|
|---|
| 252 | {
|
|---|
| 253 |
|
|---|
| 254 | /* Error closing the file, break the loop. */
|
|---|
| 255 | break;
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | /* Close the media. */
|
|---|
| 259 | status = fx_media_close(&ram_disk);
|
|---|
| 260 |
|
|---|
| 261 | /* Check the media close status. */
|
|---|
| 262 | if (status != FX_SUCCESS)
|
|---|
| 263 | {
|
|---|
| 264 |
|
|---|
| 265 | /* Error closing the media, break the loop. */
|
|---|
| 266 | break;
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | /* Increment the thread counter, which represents the number
|
|---|
| 270 | of successful passes through this loop. */
|
|---|
| 271 | thread_0_counter++;
|
|---|
| 272 |
|
|---|
| 273 | } while (1);
|
|---|
| 274 |
|
|---|
| 275 | /* If we get here the FileX test failed! */
|
|---|
| 276 | return;
|
|---|
| 277 | }
|
|---|
| 278 |
|
|---|