demo_filex.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. #define DEMO_STACK_SIZE 2048
  8. /* Buffer for FileX FX_MEDIA sector cache. This must be large enough for at least one
  9. sector, which are typically 512 bytes in size. */
  10. unsigned char media_memory[512];
  11. #ifdef FX_ENABLE_FAULT_TOLERANT
  12. UCHAR fault_tolerant_memory[FX_FAULT_TOLERANT_MAXIMUM_LOG_FILE_SIZE];
  13. #endif /* FX_ENABLE_FAULT_TOLERANT */
  14. /* Define RAM device driver entry. */
  15. VOID _fx_ram_driver(FX_MEDIA *media_ptr);
  16. /* Define thread prototypes. */
  17. void thread_0_entry(ULONG thread_input);
  18. /* Define FileX global data structures. */
  19. FX_MEDIA ram_disk;
  20. FX_FILE my_file;
  21. #ifndef FX_STANDALONE_ENABLE
  22. CHAR *ram_disk_memory;
  23. #else
  24. unsigned char ram_disk_memory[256*512];
  25. #endif
  26. /* Define ThreadX global data structures. */
  27. #ifndef FX_STANDALONE_ENABLE
  28. TX_THREAD thread_0;
  29. #endif
  30. ULONG thread_0_counter;
  31. void main(void)
  32. {
  33. #ifdef FX_STANDALONE_ENABLE
  34. /* Initialize FileX. */
  35. fx_system_initialize();
  36. thread_0_entry(0);
  37. #else
  38. /* Enter the ThreadX kernel. */
  39. tx_kernel_enter();
  40. #endif
  41. }
  42. /* Define what the initial system looks like. */
  43. #ifndef FX_STANDALONE_ENABLE
  44. void tx_application_define(void *first_unused_memory)
  45. {
  46. CHAR *pointer;
  47. /* Put first available memory address into a character pointer. */
  48. pointer = (CHAR *) first_unused_memory;
  49. /* Put system definition stuff in here, e.g. thread creates and other assorted
  50. create information. */
  51. /* Create the main thread. */
  52. tx_thread_create(&thread_0, "thread 0", thread_0_entry, 0,
  53. pointer, DEMO_STACK_SIZE,
  54. 1, 1, TX_NO_TIME_SLICE, TX_AUTO_START);
  55. pointer = pointer + DEMO_STACK_SIZE;
  56. /* Save the memory pointer for the RAM disk. */
  57. ram_disk_memory = pointer;
  58. /* Initialize FileX. */
  59. fx_system_initialize();
  60. }
  61. #endif
  62. void thread_0_entry(ULONG thread_input)
  63. {
  64. UINT status;
  65. ULONG actual;
  66. CHAR local_buffer[30];
  67. /* Format the RAM disk - the memory for the RAM disk was setup in
  68. tx_application_define above. */
  69. fx_media_format(&ram_disk,
  70. _fx_ram_driver, // Driver entry
  71. ram_disk_memory, // RAM disk memory pointer
  72. media_memory, // Media buffer pointer
  73. sizeof(media_memory), // Media buffer size
  74. "MY_RAM_DISK", // Volume Name
  75. 1, // Number of FATs
  76. 32, // Directory Entries
  77. 0, // Hidden sectors
  78. 256, // Total sectors
  79. 512, // Sector size
  80. 8, // Sectors per cluster
  81. 1, // Heads
  82. 1); // Sectors per track
  83. /* Loop to repeat the demo over and over! */
  84. do
  85. {
  86. /* Open the RAM disk. */
  87. status = fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, media_memory, sizeof(media_memory));
  88. /* Check the media open status. */
  89. if (status != FX_SUCCESS)
  90. {
  91. /* Error, break the loop! */
  92. break;
  93. }
  94. #ifdef FX_ENABLE_FAULT_TOLERANT
  95. status = fx_fault_tolerant_enable(&ram_disk, fault_tolerant_memory, sizeof(fault_tolerant_memory));
  96. if (status != FX_SUCCESS)
  97. {
  98. /* Fault tolerant enable error, break the loop. */
  99. break;
  100. }
  101. #endif /* FX_ENABLE_FAULT_TOLERANT */
  102. /* Create a file called TEST.TXT in the root directory. */
  103. status = fx_file_create(&ram_disk, "TEST.TXT");
  104. /* Check the create status. */
  105. if (status != FX_SUCCESS)
  106. {
  107. /* Check for an already created status. This is expected on the
  108. second pass of this loop! */
  109. if (status != FX_ALREADY_CREATED)
  110. {
  111. /* Create error, break the loop. */
  112. break;
  113. }
  114. }
  115. /* Open the test file. */
  116. status = fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
  117. /* Check the file open status. */
  118. if (status != FX_SUCCESS)
  119. {
  120. /* Error opening file, break the loop. */
  121. break;
  122. }
  123. /* Seek to the beginning of the test file. */
  124. status = fx_file_seek(&my_file, 0);
  125. /* Check the file seek status. */
  126. if (status != FX_SUCCESS)
  127. {
  128. /* Error performing file seek, break the loop. */
  129. break;
  130. }
  131. /* Write a string to the test file. */
  132. status = fx_file_write(&my_file, " ABCDEFGHIJKLMNOPQRSTUVWXYZ\n", 28);
  133. /* Check the file write status. */
  134. if (status != FX_SUCCESS)
  135. {
  136. /* Error writing to a file, break the loop. */
  137. break;
  138. }
  139. /* Seek to the beginning of the test file. */
  140. status = fx_file_seek(&my_file, 0);
  141. /* Check the file seek status. */
  142. if (status != FX_SUCCESS)
  143. {
  144. /* Error performing file seek, break the loop. */
  145. break;
  146. }
  147. /* Read the first 28 bytes of the test file. */
  148. status = fx_file_read(&my_file, local_buffer, 28, &actual);
  149. /* Check the file read status. */
  150. if ((status != FX_SUCCESS) || (actual != 28))
  151. {
  152. /* Error reading file, break the loop. */
  153. break;
  154. }
  155. /* Close the test file. */
  156. status = fx_file_close(&my_file);
  157. /* Check the file close status. */
  158. if (status != FX_SUCCESS)
  159. {
  160. /* Error closing the file, break the loop. */
  161. break;
  162. }
  163. /* Close the media. */
  164. status = fx_media_close(&ram_disk);
  165. /* Check the media close status. */
  166. if (status != FX_SUCCESS)
  167. {
  168. /* Error closing the media, break the loop. */
  169. break;
  170. }
  171. /* Increment the thread counter, which represents the number
  172. of successful passes through this loop. */
  173. thread_0_counter++;
  174. } while (1);
  175. /* If we get here the FileX test failed! */
  176. return;
  177. }