filex_utility_fat_flush_test.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /* This FileX test concentrates on the utility FAT flush operation. */
  2. #ifndef FX_STANDALONE_ENABLE
  3. #include "tx_api.h"
  4. #endif
  5. #include "fx_api.h"
  6. #include "fx_utility.h"
  7. #include <stdio.h>
  8. #include "fx_ram_driver_test.h"
  9. #define DEMO_STACK_SIZE 4096
  10. #define CACHE_SIZE 16*128
  11. /* Define the ThreadX and FileX object control blocks... */
  12. #ifndef FX_STANDALONE_ENABLE
  13. static TX_THREAD ftest_0;
  14. #endif
  15. static FX_MEDIA ram_disk;
  16. static FX_FILE my_file;
  17. /* Define the counters used in the test application... */
  18. #ifndef FX_STANDALONE_ENABLE
  19. static UCHAR *ram_disk_memory;
  20. static UCHAR *cache_buffer;
  21. #else
  22. static UCHAR cache_buffer[CACHE_SIZE];
  23. #endif
  24. /* Define thread prototypes. */
  25. void filex_utility_fat_flush_application_define(void *first_unused_memory);
  26. static void ftest_0_entry(ULONG thread_input);
  27. VOID _fx_ram_driver(FX_MEDIA *media_ptr);
  28. void test_control_return(UINT status);
  29. /* Define what the initial system looks like. */
  30. #ifdef CTEST
  31. void test_application_define(void *first_unused_memory)
  32. #else
  33. void filex_utility_fat_flush_application_define(void *first_unused_memory)
  34. #endif
  35. {
  36. #ifndef FX_STANDALONE_ENABLE
  37. UCHAR *pointer;
  38. /* Setup the working pointer. */
  39. pointer = (UCHAR *) first_unused_memory;
  40. /* Create the main thread. */
  41. tx_thread_create(&ftest_0, "thread 0", ftest_0_entry, 0,
  42. pointer, DEMO_STACK_SIZE,
  43. 4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
  44. pointer = pointer + DEMO_STACK_SIZE;
  45. /* Setup memory for the RAM disk and the sector cache. */
  46. cache_buffer = pointer;
  47. pointer = pointer + CACHE_SIZE;
  48. ram_disk_memory = pointer;
  49. #endif
  50. /* Initialize the FileX system. */
  51. fx_system_initialize();
  52. #ifdef FX_STANDALONE_ENABLE
  53. ftest_0_entry(0);
  54. #endif
  55. }
  56. /* Define the test threads. */
  57. static void ftest_0_entry(ULONG thread_input)
  58. {
  59. UINT status;
  60. ULONG actual;
  61. ULONG read_value;
  62. ULONG write_value;
  63. ULONG available_bytes;
  64. ULONG i;
  65. FX_PARAMETER_NOT_USED(thread_input);
  66. /* Print out some test information banners. */
  67. printf("FileX Test: Utility FAT flush test.......................................");
  68. /* Format the media. This needs to be done before opening it! */
  69. status = fx_media_format(&ram_disk,
  70. _fx_ram_driver, // Driver entry
  71. ram_disk_memory, // RAM disk memory pointer
  72. cache_buffer, // Media buffer pointer
  73. CACHE_SIZE, // Media buffer size
  74. "MY_RAM_DISK", // Volume Name
  75. 1, // Number of FATs
  76. 32, // Directory Entries
  77. 0, // Hidden sectors
  78. 511, // Total sectors
  79. 128, // Sector size
  80. 1, // Sectors per cluster
  81. 1, // Heads
  82. 1); // Sectors per track
  83. /* Determine if the format had an error. */
  84. if (status)
  85. {
  86. printf("ERROR!\n");
  87. test_control_return(2);
  88. }
  89. /* Open the ram_disk. */
  90. status = fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
  91. /* Check the status. */
  92. if (status != FX_SUCCESS)
  93. {
  94. /* Error, return error code. */
  95. printf("ERROR!\n");
  96. test_control_return(21);
  97. }
  98. /* Create a file called TEST.TXT in the root directory. */
  99. status = fx_file_create(&ram_disk, "TEST.TXT");
  100. /* Check the create status. */
  101. if (status != FX_SUCCESS)
  102. {
  103. printf("ERROR!\n");
  104. test_control_return(3);
  105. }
  106. /* Open the test file. */
  107. status = fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
  108. /* Check the file open status. */
  109. if (status != FX_SUCCESS)
  110. {
  111. printf("ERROR!\n");
  112. test_control_return(4);
  113. }
  114. /* Pickup the available bytes in the media. */
  115. status = fx_media_space_available(&ram_disk, &available_bytes);
  116. /* Check for available bytes error. */
  117. if ((status != FX_SUCCESS) || (available_bytes < sizeof(ULONG)))
  118. {
  119. printf("ERROR!\n");
  120. test_control_return(5);
  121. }
  122. /* Loop to write successive bytes out to the file.... to fill the media! */
  123. i = 0;
  124. write_value = 0;
  125. while (i < available_bytes)
  126. {
  127. /* Write 4 bytes to the file. */
  128. status = fx_file_write(&my_file, (void *) &write_value, sizeof(ULONG));
  129. /* Check the file write status. */
  130. if (status != FX_SUCCESS)
  131. {
  132. printf("ERROR!\n");
  133. test_control_return(6);
  134. }
  135. /* Increment byte count. */
  136. i = i + sizeof(ULONG);
  137. /* Increment write value. */
  138. write_value++;
  139. }
  140. /* Pickup the available bytes in the media again. */
  141. status = fx_media_space_available(&ram_disk, &i);
  142. /* Check for available bytes error. */
  143. if ((status != FX_SUCCESS) || (i != 0))
  144. {
  145. printf("ERROR!\n");
  146. test_control_return(7);
  147. }
  148. for (i = 0; i < FX_MAX_FAT_CACHE; i++)
  149. {
  150. ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_cluster = 496;
  151. ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_value = 1;
  152. ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_dirty = 1;
  153. }
  154. _fx_utility_FAT_flush(&ram_disk);
  155. /* Note: If definition of FX_FAT_MAP_SIZE is changed in future, the value checked in
  156. return_value_if_fail will change for individual array element */
  157. for(i=0;i<FX_FAT_MAP_SIZE;i++)
  158. {
  159. return_value_if_fail((ram_disk.fx_media_fat_secondary_update_map[i]==63),8)
  160. }
  161. fx_media_close(&ram_disk);
  162. /* Test with FAT 16 */
  163. /* Format the media. This needs to be done before opening it! */
  164. status = fx_media_format(&ram_disk,
  165. _fx_ram_driver, // Driver entry
  166. ram_disk_memory, // RAM disk memory pointer
  167. cache_buffer, // Media buffer pointer
  168. CACHE_SIZE, // Media buffer size
  169. "MY_RAM_DISK", // Volume Name
  170. 1, // Number of FATs
  171. 32, // Directory Entries
  172. 0, // Hidden sectors
  173. 14000, // Total sectors - FAT16
  174. 64, // Sector size
  175. 2, // Sectors per cluster
  176. 1, // Heads
  177. 1); // Sectors per track
  178. /* Determine if the format had an error. */
  179. if (status)
  180. {
  181. printf("ERROR!\n");
  182. test_control_return(2);
  183. }
  184. /* Open the ram_disk. */
  185. status = fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
  186. /* Check the status. */
  187. if (status != FX_SUCCESS)
  188. {
  189. /* Error, return error code. */
  190. printf("ERROR!\n");
  191. test_control_return(21);
  192. }
  193. /* Create a file called TEST.TXT in the root directory. */
  194. status = fx_file_create(&ram_disk, "TEST.TXT");
  195. /* Check the create status. */
  196. if (status != FX_SUCCESS)
  197. {
  198. printf("ERROR!\n");
  199. test_control_return(3);
  200. }
  201. /* Open the test file. */
  202. status = fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
  203. /* Check the file open status. */
  204. if (status != FX_SUCCESS)
  205. {
  206. printf("ERROR!\n");
  207. test_control_return(4);
  208. }
  209. /* Pickup the available bytes in the media. */
  210. status = fx_media_space_available(&ram_disk, &available_bytes);
  211. /* Check for available bytes error. */
  212. if ((status != FX_SUCCESS) || (available_bytes < sizeof(ULONG)))
  213. {
  214. printf("ERROR!\n");
  215. test_control_return(5);
  216. }
  217. /* Loop to write successive bytes out to the file.... to fill the media! */
  218. i = 0;
  219. write_value = 0;
  220. while (i < available_bytes)
  221. {
  222. /* Write 4 bytes to the file. */
  223. status = fx_file_write(&my_file, (void *) &write_value, sizeof(ULONG));
  224. /* Check the file write status. */
  225. if (status != FX_SUCCESS)
  226. {
  227. printf("ERROR!\n");
  228. test_control_return(6);
  229. }
  230. /* Increment byte count. */
  231. i = i + sizeof(ULONG);
  232. /* Increment write value. */
  233. write_value++;
  234. }
  235. /* Pickup the available bytes in the media again. */
  236. status = fx_media_space_available(&ram_disk, &i);
  237. /* Check for available bytes error. */
  238. if ((status != FX_SUCCESS) || (i != 0))
  239. {
  240. printf("ERROR!\n");
  241. test_control_return(7);
  242. }
  243. for (i = 0; i < FX_MAX_FAT_CACHE; i++)
  244. {
  245. ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_cluster = 0xA6;
  246. ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_value = 1;
  247. ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_dirty = 1;
  248. }
  249. _fx_utility_FAT_flush(&ram_disk);
  250. /* Note: If definition of FX_FAT_MAP_SIZE is changed in future, the value checked in
  251. return_value_if_fail will change for individual array element */
  252. for(i=0;i<FX_FAT_MAP_SIZE;i++)
  253. {
  254. return_value_if_fail((ram_disk.fx_media_fat_secondary_update_map[i]==255),8)
  255. }
  256. fx_media_close(&ram_disk);
  257. /* Test with FAT32. */
  258. status = fx_media_format(&ram_disk,
  259. _fx_ram_driver, // Driver entry
  260. ram_disk_memory, // RAM disk memory pointer
  261. cache_buffer, // Media buffer pointer
  262. CACHE_SIZE, // Media buffer size
  263. "MY_RAM_DISK", // Volume Name
  264. 1, // Number of FATs
  265. 32, // Directory Entries
  266. 0, // Hidden sectors
  267. 70000, // Total sectors - FAT32
  268. 512, // Sector size
  269. 1, // Sectors per cluster
  270. 1, // Heads
  271. 1); // Sectors per track
  272. status += fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
  273. status += fx_file_create(&ram_disk, "TEST.TXT");
  274. return_if_fail(FX_SUCCESS == status);
  275. /* Open the test file. */
  276. status = fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
  277. /* Check the file open status. */
  278. if (status != FX_SUCCESS)
  279. {
  280. printf("ERROR!\n");
  281. test_control_return(4);
  282. }
  283. /* Pickup the available bytes in the media. */
  284. status = fx_media_space_available(&ram_disk, &available_bytes);
  285. /* Check for available bytes error. */
  286. if ((status != FX_SUCCESS) || (available_bytes < sizeof(ULONG)))
  287. {
  288. printf("ERROR!\n");
  289. test_control_return(5);
  290. }
  291. /* Loop to write successive bytes out to the file.... to fill the media! */
  292. i = 0;
  293. write_value = 0;
  294. while (i < available_bytes/128)
  295. {
  296. /* Write 4 bytes to the file. */
  297. status = fx_file_write(&my_file, (void *) &write_value, sizeof(ULONG));
  298. /* Check the file write status. */
  299. if (status != FX_SUCCESS)
  300. {
  301. printf("ERROR!\n");
  302. test_control_return(6);
  303. }
  304. /* Increment byte count. */
  305. i = i + sizeof(ULONG);
  306. /* Increment write value. */
  307. write_value++;
  308. }
  309. for (i = 0; i < FX_MAX_FAT_CACHE; i++)
  310. {
  311. ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_cluster = 0xF160;
  312. ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_value = 1;
  313. ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_dirty = 1;
  314. }
  315. _fx_utility_FAT_flush(&ram_disk);
  316. /* Note: If definition of FX_FAT_MAP_SIZE is changed in future, the value checked in
  317. return_value_if_fail will change for individual array element */
  318. for(i=0;i<FX_FAT_MAP_SIZE;i++)
  319. {
  320. return_value_if_fail((ram_disk.fx_media_fat_secondary_update_map[i]==65),8)
  321. }
  322. fx_media_close(&ram_disk);
  323. printf("SUCCESS!\n");
  324. test_control_return(0);
  325. }