demo_filex_nand_flash.c 6.7 KB

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