demo_filex_nor_flash.c 6.6 KB

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