levelx_nor_flash_test.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438
  1. /* Basic NOR flash tests... */
  2. #include <stdio.h>
  3. #include "lx_api.h"
  4. #define DEMO_STACK_SIZE 4096
  5. /* Define the ThreadX object control blocks... */
  6. #ifndef LX_STANDALONE_ENABLE
  7. TX_THREAD thread_0;
  8. #endif
  9. UCHAR thread_0_stack[DEMO_STACK_SIZE];
  10. /* Define LevelX structures. */
  11. LX_NOR_FLASH nor_sim_flash;
  12. ULONG buffer[128];
  13. ULONG readbuffer[128];
  14. /* Define LevelX NOR flash simulator prototoypes. */
  15. UINT _lx_nor_flash_simulator_erase_all(VOID);
  16. UINT _lx_nor_flash_simulator_initialize(LX_NOR_FLASH *nor_flash);
  17. /* Define thread prototypes. */
  18. void thread_0_entry(ULONG thread_input);
  19. /* Define main entry point. */
  20. int main()
  21. {
  22. /* Enter the ThreadX kernel. */
  23. #ifndef LX_STANDALONE_ENABLE
  24. tx_kernel_enter();
  25. #else
  26. thread_0_entry(0);
  27. #endif
  28. }
  29. /* Define what the initial system looks like. */
  30. #ifndef LX_STANDALONE_ENABLE
  31. void tx_application_define(void *first_unused_memory)
  32. {
  33. /* Create the main thread. */
  34. tx_thread_create(&thread_0, "thread 0", thread_0_entry, 0,
  35. thread_0_stack, DEMO_STACK_SIZE,
  36. 1, 1, TX_NO_TIME_SLICE, TX_AUTO_START);
  37. }
  38. #endif
  39. /* Define the test threads. */
  40. void thread_0_entry(ULONG thread_input)
  41. {
  42. ULONG i, j, sector;
  43. UINT status;
  44. ULONG *word_ptr;
  45. /* Erase the simulated NOR flash. */
  46. _lx_nor_flash_simulator_erase_all();
  47. /* Initialize LevelX. */
  48. _lx_nor_flash_initialize();
  49. /* Test 1: Simple write 100 sectors and read 100 sectors. */
  50. printf("Test 1: Simple write-read 100 sectors...........");
  51. lx_nor_flash_open(&nor_sim_flash, "sim nor flash", _lx_nor_flash_simulator_initialize);
  52. /* Write 100 sectors.... */
  53. for (i = 0; i < 100; i++)
  54. {
  55. for (j = 0; j < 128; j++)
  56. buffer[j] = i;
  57. status = lx_nor_flash_sector_write(&nor_sim_flash, i, buffer);
  58. if (status != LX_SUCCESS)
  59. {
  60. printf("FAILED!\n");
  61. #ifdef BATCH_TEST
  62. exit(1);
  63. #endif
  64. while(1)
  65. {
  66. }
  67. }
  68. }
  69. /* Read back 100 sectors... */
  70. for (i = 0; i < 100; i++)
  71. {
  72. status = lx_nor_flash_sector_read(&nor_sim_flash, i, buffer);
  73. if (status != LX_SUCCESS)
  74. {
  75. printf("FAILED!\n");
  76. #ifdef BATCH_TEST
  77. exit(1);
  78. #endif
  79. while(1)
  80. {
  81. }
  82. }
  83. for (j = 0; j < 128; j++)
  84. {
  85. if (buffer[j] != i)
  86. {
  87. printf("FAILED!\n");
  88. #ifdef BATCH_TEST
  89. exit(1);
  90. #endif
  91. while(1)
  92. {
  93. }
  94. }
  95. }
  96. }
  97. _lx_nor_flash_close(&nor_sim_flash);
  98. printf("SUCCESS!\n");
  99. /* Test 2: Write same sector 120 times. */
  100. printf("Test 2: Write same sector 120 times.............");
  101. /* Reinitialize... */
  102. _lx_nor_flash_simulator_erase_all();
  103. lx_nor_flash_initialize();
  104. lx_nor_flash_open(&nor_sim_flash, "sim nor flash", _lx_nor_flash_simulator_initialize);
  105. for (j = 0; j < 128; j++)
  106. buffer[j] = 0xFFFFFFFF;
  107. /* Write same sector 120 sectors.... */
  108. for (i = 0; i < 120; i++)
  109. {
  110. for (j = 0; j < 128; j++)
  111. buffer[j] = i;
  112. status = lx_nor_flash_sector_write(&nor_sim_flash, 7, buffer);
  113. if (status != LX_SUCCESS)
  114. {
  115. printf("FAILED!\n");
  116. #ifdef BATCH_TEST
  117. exit(1);
  118. #endif
  119. while(1)
  120. {
  121. }
  122. }
  123. status = lx_nor_flash_sector_read(&nor_sim_flash, 7, readbuffer);
  124. if (status != LX_SUCCESS)
  125. {
  126. printf("FAILED!\n");
  127. #ifdef BATCH_TEST
  128. exit(1);
  129. #endif
  130. while(1)
  131. {
  132. }
  133. }
  134. for (j = 0; j < 128; j++)
  135. {
  136. if (buffer[j] != readbuffer[j])
  137. {
  138. printf("FAILED!\n");
  139. #ifdef BATCH_TEST
  140. exit(1);
  141. #endif
  142. while(1)
  143. {
  144. }
  145. }
  146. }
  147. /* Write other sectors just to have additional sectors to manage. */
  148. if (i == 1)
  149. lx_nor_flash_sector_write(&nor_sim_flash, 1, buffer);
  150. if (i == 16)
  151. lx_nor_flash_sector_write(&nor_sim_flash, 16, buffer);
  152. if (i == 32)
  153. lx_nor_flash_sector_write(&nor_sim_flash, 32, buffer);
  154. if (i == 48)
  155. lx_nor_flash_sector_write(&nor_sim_flash, 48, buffer);
  156. if (i == 64)
  157. lx_nor_flash_sector_write(&nor_sim_flash, 64, buffer);
  158. if (i == 80)
  159. lx_nor_flash_sector_write(&nor_sim_flash, 80, buffer);
  160. if (i == 96)
  161. lx_nor_flash_sector_write(&nor_sim_flash, 96, buffer);
  162. if (i == 112)
  163. lx_nor_flash_sector_write(&nor_sim_flash, 112, buffer);
  164. }
  165. status = lx_nor_flash_defragment(&nor_sim_flash);
  166. status = lx_nor_flash_sector_read(&nor_sim_flash, 7, readbuffer);
  167. if (status != LX_SUCCESS)
  168. {
  169. printf("FAILED!\n");
  170. #ifdef BATCH_TEST
  171. exit(1);
  172. #endif
  173. while(1)
  174. {
  175. }
  176. }
  177. if (readbuffer[0] != 119)
  178. {
  179. printf("FAILED!\n");
  180. #ifdef BATCH_TEST
  181. exit(1);
  182. #endif
  183. while(1)
  184. {
  185. }
  186. }
  187. status = lx_nor_flash_sector_read(&nor_sim_flash, 1, readbuffer);
  188. if (status != LX_SUCCESS)
  189. {
  190. printf("FAILED!\n");
  191. #ifdef BATCH_TEST
  192. exit(1);
  193. #endif
  194. while(1)
  195. {
  196. }
  197. }
  198. if (readbuffer[0] != 1)
  199. {
  200. printf("FAILED!\n");
  201. #ifdef BATCH_TEST
  202. exit(1);
  203. #endif
  204. while(1)
  205. {
  206. }
  207. }
  208. status = lx_nor_flash_sector_read(&nor_sim_flash, 16, readbuffer);
  209. if (status != LX_SUCCESS)
  210. {
  211. printf("FAILED!\n");
  212. #ifdef BATCH_TEST
  213. exit(1);
  214. #endif
  215. while(1)
  216. {
  217. }
  218. }
  219. if (readbuffer[0] != 16)
  220. {
  221. printf("FAILED!\n");
  222. #ifdef BATCH_TEST
  223. exit(1);
  224. #endif
  225. while(1)
  226. {
  227. }
  228. }
  229. status = lx_nor_flash_sector_read(&nor_sim_flash, 32, readbuffer);
  230. if (status != LX_SUCCESS)
  231. {
  232. printf("FAILED!\n");
  233. #ifdef BATCH_TEST
  234. exit(1);
  235. #endif
  236. while(1)
  237. {
  238. }
  239. }
  240. if (readbuffer[0] != 32)
  241. {
  242. printf("FAILED!\n");
  243. #ifdef BATCH_TEST
  244. exit(1);
  245. #endif
  246. while(1)
  247. {
  248. }
  249. }
  250. status = lx_nor_flash_sector_read(&nor_sim_flash, 48, readbuffer);
  251. if (status != LX_SUCCESS)
  252. {
  253. printf("FAILED!\n");
  254. #ifdef BATCH_TEST
  255. exit(1);
  256. #endif
  257. while(1)
  258. {
  259. }
  260. }
  261. if (readbuffer[0] != 48)
  262. {
  263. printf("FAILED!\n");
  264. #ifdef BATCH_TEST
  265. exit(1);
  266. #endif
  267. while(1)
  268. {
  269. }
  270. }
  271. status = lx_nor_flash_sector_read(&nor_sim_flash, 64, readbuffer);
  272. if (status != LX_SUCCESS)
  273. {
  274. printf("FAILED!\n");
  275. #ifdef BATCH_TEST
  276. exit(1);
  277. #endif
  278. while(1)
  279. {
  280. }
  281. }
  282. if (readbuffer[0] != 64)
  283. {
  284. printf("FAILED!\n");
  285. #ifdef BATCH_TEST
  286. exit(1);
  287. #endif
  288. while(1)
  289. {
  290. }
  291. }
  292. status = lx_nor_flash_sector_read(&nor_sim_flash, 80, readbuffer);
  293. if (status != LX_SUCCESS)
  294. {
  295. printf("FAILED!\n");
  296. #ifdef BATCH_TEST
  297. exit(1);
  298. #endif
  299. while(1)
  300. {
  301. }
  302. }
  303. if (readbuffer[0] != 80)
  304. {
  305. printf("FAILED!\n");
  306. #ifdef BATCH_TEST
  307. exit(1);
  308. #endif
  309. while(1)
  310. {
  311. }
  312. }
  313. status = lx_nor_flash_sector_read(&nor_sim_flash, 96, readbuffer);
  314. if (status != LX_SUCCESS)
  315. {
  316. printf("FAILED!\n");
  317. #ifdef BATCH_TEST
  318. exit(1);
  319. #endif
  320. while(1)
  321. {
  322. }
  323. }
  324. if (readbuffer[0] != 96)
  325. {
  326. printf("FAILED!\n");
  327. #ifdef BATCH_TEST
  328. exit(1);
  329. #endif
  330. while(1)
  331. {
  332. }
  333. }
  334. status = lx_nor_flash_sector_read(&nor_sim_flash, 112, readbuffer);
  335. if (status != LX_SUCCESS)
  336. {
  337. printf("FAILED!\n");
  338. #ifdef BATCH_TEST
  339. exit(1);
  340. #endif
  341. while(1)
  342. {
  343. }
  344. }
  345. if (readbuffer[0] != 112)
  346. {
  347. printf("FAILED!\n");
  348. #ifdef BATCH_TEST
  349. exit(1);
  350. #endif
  351. while(1)
  352. {
  353. }
  354. }
  355. status = lx_nor_flash_defragment(&nor_sim_flash);
  356. if (status != LX_SUCCESS)
  357. {
  358. printf("FAILED!\n");
  359. #ifdef BATCH_TEST
  360. exit(1);
  361. #endif
  362. while(1)
  363. {
  364. }
  365. }
  366. /* Point at the simulated NOR flash memory. */
  367. word_ptr = nor_sim_flash.lx_nor_flash_base_address;
  368. status = lx_nor_flash_close(&nor_sim_flash);
  369. if (status != LX_SUCCESS)
  370. {
  371. printf("FAILED!\n");
  372. #ifdef BATCH_TEST
  373. exit(1);
  374. #endif
  375. while(1)
  376. {
  377. }
  378. }
  379. printf("SUCCESS!\n");
  380. /* Test 3: Corrupt block 0, simulate a power interruption during erase of block 0,
  381. after the erase, but before the free bit map and erase count is setup. */
  382. printf("Test 3: Block erase-initialize interrupted......");
  383. word_ptr[0] = 0xFFFFFFFF;
  384. word_ptr[3] = 0xFFFFFFFF;
  385. /* Open the flash and see if we recover properly. */
  386. status = lx_nor_flash_open(&nor_sim_flash, "sim nor flash", _lx_nor_flash_simulator_initialize);
  387. if ((status != LX_SUCCESS) ||
  388. (nor_sim_flash.lx_nor_flash_free_physical_sectors != 111) ||
  389. (nor_sim_flash.lx_nor_flash_mapped_physical_sectors != 9))
  390. {
  391. printf("FAILED!\n");
  392. #ifdef BATCH_TEST
  393. exit(1);
  394. #endif
  395. while(1)
  396. {
  397. }
  398. }
  399. status = lx_nor_flash_close(&nor_sim_flash);
  400. if (status != LX_SUCCESS)
  401. {
  402. printf("FAILED!\n");
  403. #ifdef BATCH_TEST
  404. exit(1);
  405. #endif
  406. while(1)
  407. {
  408. }
  409. }
  410. /* Corrupt block 0, simulate a power interruption during erase of block 0,
  411. after the erase, and after the free bit map setup, but before erase count is setup. */
  412. word_ptr[0] = 0xFFFFFFFF;
  413. /* Open the flash and see if we recover properly. */
  414. status = lx_nor_flash_open(&nor_sim_flash, "sim nor flash", _lx_nor_flash_simulator_initialize);
  415. if ((status != LX_SUCCESS) ||
  416. (nor_sim_flash.lx_nor_flash_free_physical_sectors != 111) ||
  417. (nor_sim_flash.lx_nor_flash_mapped_physical_sectors != 9))
  418. {
  419. printf("FAILED!\n");
  420. #ifdef BATCH_TEST
  421. exit(1);
  422. #endif
  423. while(1)
  424. {
  425. }
  426. }
  427. status = lx_nor_flash_close(&nor_sim_flash);
  428. if (status != LX_SUCCESS)
  429. {
  430. printf("FAILED!\n");
  431. #ifdef BATCH_TEST
  432. exit(1);
  433. #endif
  434. while(1)
  435. {
  436. }
  437. }
  438. printf("SUCCESS!\n");
  439. /* Test 4: simulate a power interruption after a new block is allocated but before
  440. anything else can be done. */
  441. printf("Test 4: Power interrupted new block allocation..");
  442. word_ptr[3] = word_ptr[3] & ~((ULONG) 1);
  443. /* Open the flash and see if we recover properly. */
  444. status = lx_nor_flash_open(&nor_sim_flash, "sim nor flash", _lx_nor_flash_simulator_initialize);
  445. if ((status != LX_SUCCESS) ||
  446. (nor_sim_flash.lx_nor_flash_free_physical_sectors != 110) ||
  447. (nor_sim_flash.lx_nor_flash_mapped_physical_sectors != 9))
  448. {
  449. printf("FAILED!\n");
  450. #ifdef BATCH_TEST
  451. exit(1);
  452. #endif
  453. while(1)
  454. {
  455. }
  456. }
  457. status = lx_nor_flash_close(&nor_sim_flash);
  458. if (status != LX_SUCCESS)
  459. {
  460. printf("FAILED!\n");
  461. #ifdef BATCH_TEST
  462. exit(1);
  463. #endif
  464. while(1)
  465. {
  466. }
  467. }
  468. /* Simulate a power interruption after a new block is allocated but before
  469. anything else can be done. */
  470. word_ptr[(16*128)+3] = 0x7C00;
  471. /* Open the flash and see if we recover properly. */
  472. status = lx_nor_flash_open(&nor_sim_flash, "sim nor flash", _lx_nor_flash_simulator_initialize);
  473. if ((status != LX_SUCCESS) ||
  474. (nor_sim_flash.lx_nor_flash_free_physical_sectors != 109) ||
  475. (nor_sim_flash.lx_nor_flash_mapped_physical_sectors != 9))
  476. {
  477. printf("FAILED!\n");
  478. #ifdef BATCH_TEST
  479. exit(1);
  480. #endif
  481. while(1)
  482. {
  483. }
  484. }
  485. status = lx_nor_flash_close(&nor_sim_flash);
  486. if (status != LX_SUCCESS)
  487. {
  488. printf("FAILED!\n");
  489. #ifdef BATCH_TEST
  490. exit(1);
  491. #endif
  492. while(1)
  493. {
  494. }
  495. }
  496. /* Simulate a power interruption after a new sector is allocated, after data
  497. had been copied, and the superceeded bit is clear, but before the new entry can be
  498. setup. */
  499. word_ptr[3] = 0x7FFC;
  500. word_ptr[(16*128)+6] = word_ptr[(16*128)+6] & ~((ULONG) 0x40000000);
  501. /* Open the flash and see if we recover properly. */
  502. status = lx_nor_flash_open(&nor_sim_flash, "sim nor flash", _lx_nor_flash_simulator_initialize);
  503. if ((status != LX_SUCCESS) ||
  504. (nor_sim_flash.lx_nor_flash_free_physical_sectors != 108) ||
  505. (nor_sim_flash.lx_nor_flash_mapped_physical_sectors != 9))
  506. {
  507. printf("FAILED!\n");
  508. #ifdef BATCH_TEST
  509. exit(1);
  510. #endif
  511. while(1)
  512. {
  513. }
  514. }
  515. lx_nor_flash_close(&nor_sim_flash);
  516. /* Simulate a power interruption after a new sector is allocated, after data
  517. had been copied, and the superceeded bit is clear, the new entry is setup, but the old entry
  518. has not been invalidated. */
  519. word_ptr[3] = 0x7FF8;
  520. word_ptr[6] = 0xC0000070;
  521. for (i = 0; i < 128; i++)
  522. {
  523. word_ptr[(3*128)+i] = 0x70;
  524. }
  525. /* Open the flash and see if we recover properly. */
  526. status = lx_nor_flash_open(&nor_sim_flash, "sim nor flash", _lx_nor_flash_simulator_initialize);
  527. if ((status != LX_SUCCESS) ||
  528. (nor_sim_flash.lx_nor_flash_free_physical_sectors != 107) ||
  529. (nor_sim_flash.lx_nor_flash_mapped_physical_sectors != 9))
  530. {
  531. printf("FAILED!\n");
  532. #ifdef BATCH_TEST
  533. exit(1);
  534. #endif
  535. while(1)
  536. {
  537. }
  538. }
  539. status = lx_nor_flash_sector_read(&nor_sim_flash, 7, readbuffer);
  540. if (status != LX_SUCCESS)
  541. {
  542. printf("FAILED!\n");
  543. #ifdef BATCH_TEST
  544. exit(1);
  545. #endif
  546. while(1)
  547. {
  548. }
  549. }
  550. if (readbuffer[0] != 119)
  551. {
  552. printf("FAILED!\n");
  553. #ifdef BATCH_TEST
  554. exit(1);
  555. #endif
  556. while(1)
  557. {
  558. }
  559. }
  560. status = lx_nor_flash_sector_read(&nor_sim_flash, 1, readbuffer);
  561. if (status != LX_SUCCESS)
  562. {
  563. printf("FAILED!\n");
  564. #ifdef BATCH_TEST
  565. exit(1);
  566. #endif
  567. while(1)
  568. {
  569. }
  570. }
  571. if (readbuffer[0] != 1)
  572. {
  573. printf("FAILED!\n");
  574. #ifdef BATCH_TEST
  575. exit(1);
  576. #endif
  577. while(1)
  578. {
  579. }
  580. }
  581. status = lx_nor_flash_sector_read(&nor_sim_flash, 16, readbuffer);
  582. if (status != LX_SUCCESS)
  583. {
  584. printf("FAILED!\n");
  585. #ifdef BATCH_TEST
  586. exit(1);
  587. #endif
  588. while(1)
  589. {
  590. }
  591. }
  592. if (readbuffer[0] != 16)
  593. {
  594. printf("FAILED!\n");
  595. #ifdef BATCH_TEST
  596. exit(1);
  597. #endif
  598. while(1)
  599. {
  600. }
  601. }
  602. status = lx_nor_flash_sector_read(&nor_sim_flash, 32, readbuffer);
  603. if (status != LX_SUCCESS)
  604. {
  605. printf("FAILED!\n");
  606. #ifdef BATCH_TEST
  607. exit(1);
  608. #endif
  609. while(1)
  610. {
  611. }
  612. }
  613. if (readbuffer[0] != 32)
  614. {
  615. printf("FAILED!\n");
  616. #ifdef BATCH_TEST
  617. exit(1);
  618. #endif
  619. while(1)
  620. {
  621. }
  622. }
  623. status = lx_nor_flash_sector_read(&nor_sim_flash, 48, readbuffer);
  624. if (status != LX_SUCCESS)
  625. {
  626. printf("FAILED!\n");
  627. #ifdef BATCH_TEST
  628. exit(1);
  629. #endif
  630. while(1)
  631. {
  632. }
  633. }
  634. if (readbuffer[0] != 48)
  635. {
  636. printf("FAILED!\n");
  637. #ifdef BATCH_TEST
  638. exit(1);
  639. #endif
  640. while(1)
  641. {
  642. }
  643. }
  644. status = lx_nor_flash_sector_read(&nor_sim_flash, 64, readbuffer);
  645. if (status != LX_SUCCESS)
  646. {
  647. printf("FAILED!\n");
  648. #ifdef BATCH_TEST
  649. exit(1);
  650. #endif
  651. while(1)
  652. {
  653. }
  654. }
  655. if (readbuffer[0] != 64)
  656. {
  657. printf("FAILED!\n");
  658. #ifdef BATCH_TEST
  659. exit(1);
  660. #endif
  661. while(1)
  662. {
  663. }
  664. }
  665. status = lx_nor_flash_sector_read(&nor_sim_flash, 80, readbuffer);
  666. if (status != LX_SUCCESS)
  667. {
  668. printf("FAILED!\n");
  669. #ifdef BATCH_TEST
  670. exit(1);
  671. #endif
  672. while(1)
  673. {
  674. }
  675. }
  676. if (readbuffer[0] != 80)
  677. {
  678. printf("FAILED!\n");
  679. #ifdef BATCH_TEST
  680. exit(1);
  681. #endif
  682. while(1)
  683. {
  684. }
  685. }
  686. status = lx_nor_flash_sector_read(&nor_sim_flash, 96, readbuffer);
  687. if (status != LX_SUCCESS)
  688. {
  689. printf("FAILED!\n");
  690. #ifdef BATCH_TEST
  691. exit(1);
  692. #endif
  693. while(1)
  694. {
  695. }
  696. }
  697. if (readbuffer[0] != 96)
  698. {
  699. printf("FAILED!\n");
  700. #ifdef BATCH_TEST
  701. exit(1);
  702. #endif
  703. while(1)
  704. {
  705. }
  706. }
  707. status = lx_nor_flash_sector_read(&nor_sim_flash, 112, readbuffer);
  708. if (status != LX_SUCCESS)
  709. {
  710. printf("FAILED!\n");
  711. #ifdef BATCH_TEST
  712. exit(1);
  713. #endif
  714. while(1)
  715. {
  716. }
  717. }
  718. if (readbuffer[0] != 112)
  719. {
  720. printf("FAILED!\n");
  721. #ifdef BATCH_TEST
  722. exit(1);
  723. #endif
  724. while(1)
  725. {
  726. }
  727. }
  728. status = lx_nor_flash_defragment(&nor_sim_flash);
  729. if (status != LX_SUCCESS)
  730. {
  731. printf("FAILED!\n");
  732. #ifdef BATCH_TEST
  733. exit(1);
  734. #endif
  735. while(1)
  736. {
  737. }
  738. }
  739. status = lx_nor_flash_defragment(&nor_sim_flash);
  740. if (status != LX_SUCCESS)
  741. {
  742. printf("FAILED!\n");
  743. #ifdef BATCH_TEST
  744. exit(1);
  745. #endif
  746. while(1)
  747. {
  748. }
  749. }
  750. status = lx_nor_flash_sector_read(&nor_sim_flash, 7, readbuffer);
  751. if (status != LX_SUCCESS)
  752. {
  753. printf("FAILED!\n");
  754. #ifdef BATCH_TEST
  755. exit(1);
  756. #endif
  757. while(1)
  758. {
  759. }
  760. }
  761. if (readbuffer[0] != 119)
  762. {
  763. printf("FAILED!\n");
  764. #ifdef BATCH_TEST
  765. exit(1);
  766. #endif
  767. while(1)
  768. {
  769. }
  770. }
  771. status = lx_nor_flash_sector_read(&nor_sim_flash, 1, readbuffer);
  772. if (status != LX_SUCCESS)
  773. {
  774. printf("FAILED!\n");
  775. #ifdef BATCH_TEST
  776. exit(1);
  777. #endif
  778. while(1)
  779. {
  780. }
  781. }
  782. if (readbuffer[0] != 1)
  783. {
  784. printf("FAILED!\n");
  785. #ifdef BATCH_TEST
  786. exit(1);
  787. #endif
  788. while(1)
  789. {
  790. }
  791. }
  792. status = lx_nor_flash_sector_read(&nor_sim_flash, 16, readbuffer);
  793. if (status != LX_SUCCESS)
  794. {
  795. printf("FAILED!\n");
  796. #ifdef BATCH_TEST
  797. exit(1);
  798. #endif
  799. while(1)
  800. {
  801. }
  802. }
  803. if (readbuffer[0] != 16)
  804. {
  805. printf("FAILED!\n");
  806. #ifdef BATCH_TEST
  807. exit(1);
  808. #endif
  809. while(1)
  810. {
  811. }
  812. }
  813. status = lx_nor_flash_sector_read(&nor_sim_flash, 32, readbuffer);
  814. if (status != LX_SUCCESS)
  815. {
  816. printf("FAILED!\n");
  817. #ifdef BATCH_TEST
  818. exit(1);
  819. #endif
  820. while(1)
  821. {
  822. }
  823. }
  824. if (readbuffer[0] != 32)
  825. {
  826. printf("FAILED!\n");
  827. #ifdef BATCH_TEST
  828. exit(1);
  829. #endif
  830. while(1)
  831. {
  832. }
  833. }
  834. status = lx_nor_flash_sector_read(&nor_sim_flash, 48, readbuffer);
  835. if (status != LX_SUCCESS)
  836. {
  837. printf("FAILED!\n");
  838. #ifdef BATCH_TEST
  839. exit(1);
  840. #endif
  841. while(1)
  842. {
  843. }
  844. }
  845. if (readbuffer[0] != 48)
  846. {
  847. printf("FAILED!\n");
  848. #ifdef BATCH_TEST
  849. exit(1);
  850. #endif
  851. while(1)
  852. {
  853. }
  854. }
  855. status = lx_nor_flash_sector_read(&nor_sim_flash, 64, readbuffer);
  856. if (status != LX_SUCCESS)
  857. {
  858. printf("FAILED!\n");
  859. #ifdef BATCH_TEST
  860. exit(1);
  861. #endif
  862. while(1)
  863. {
  864. }
  865. }
  866. if (readbuffer[0] != 64)
  867. {
  868. printf("FAILED!\n");
  869. #ifdef BATCH_TEST
  870. exit(1);
  871. #endif
  872. while(1)
  873. {
  874. }
  875. }
  876. status = lx_nor_flash_sector_read(&nor_sim_flash, 80, readbuffer);
  877. if (status != LX_SUCCESS)
  878. {
  879. printf("FAILED!\n");
  880. #ifdef BATCH_TEST
  881. exit(1);
  882. #endif
  883. while(1)
  884. {
  885. }
  886. }
  887. if (readbuffer[0] != 80)
  888. {
  889. printf("FAILED!\n");
  890. #ifdef BATCH_TEST
  891. exit(1);
  892. #endif
  893. while(1)
  894. {
  895. }
  896. }
  897. status = lx_nor_flash_sector_read(&nor_sim_flash, 96, readbuffer);
  898. if (status != LX_SUCCESS)
  899. {
  900. printf("FAILED!\n");
  901. #ifdef BATCH_TEST
  902. exit(1);
  903. #endif
  904. while(1)
  905. {
  906. }
  907. }
  908. if (readbuffer[0] != 96)
  909. {
  910. printf("FAILED!\n");
  911. #ifdef BATCH_TEST
  912. exit(1);
  913. #endif
  914. while(1)
  915. {
  916. }
  917. }
  918. status = lx_nor_flash_sector_read(&nor_sim_flash, 112, readbuffer);
  919. if (status != LX_SUCCESS)
  920. {
  921. printf("FAILED!\n");
  922. #ifdef BATCH_TEST
  923. exit(1);
  924. #endif
  925. while(1)
  926. {
  927. }
  928. }
  929. if (readbuffer[0] != 112)
  930. {
  931. printf("FAILED!\n");
  932. #ifdef BATCH_TEST
  933. exit(1);
  934. #endif
  935. while(1)
  936. {
  937. }
  938. }
  939. /* Read in reverse order to see if caching helps! */
  940. status = lx_nor_flash_sector_read(&nor_sim_flash, 112, readbuffer);
  941. if (status != LX_SUCCESS)
  942. {
  943. printf("FAILED!\n");
  944. #ifdef BATCH_TEST
  945. exit(1);
  946. #endif
  947. while(1)
  948. {
  949. }
  950. }
  951. if (readbuffer[0] != 112)
  952. {
  953. printf("FAILED!\n");
  954. #ifdef BATCH_TEST
  955. exit(1);
  956. #endif
  957. while(1)
  958. {
  959. }
  960. }
  961. status = lx_nor_flash_sector_read(&nor_sim_flash, 96, readbuffer);
  962. if (status != LX_SUCCESS)
  963. {
  964. printf("FAILED!\n");
  965. #ifdef BATCH_TEST
  966. exit(1);
  967. #endif
  968. while(1)
  969. {
  970. }
  971. }
  972. if (readbuffer[0] != 96)
  973. {
  974. printf("FAILED!\n");
  975. #ifdef BATCH_TEST
  976. exit(1);
  977. #endif
  978. while(1)
  979. {
  980. }
  981. }
  982. status = lx_nor_flash_sector_read(&nor_sim_flash, 80, readbuffer);
  983. if (status != LX_SUCCESS)
  984. {
  985. printf("FAILED!\n");
  986. #ifdef BATCH_TEST
  987. exit(1);
  988. #endif
  989. while(1)
  990. {
  991. }
  992. }
  993. if (readbuffer[0] != 80)
  994. {
  995. printf("FAILED!\n");
  996. #ifdef BATCH_TEST
  997. exit(1);
  998. #endif
  999. while(1)
  1000. {
  1001. }
  1002. }
  1003. status = lx_nor_flash_sector_read(&nor_sim_flash, 64, readbuffer);
  1004. if (status != LX_SUCCESS)
  1005. {
  1006. printf("FAILED!\n");
  1007. #ifdef BATCH_TEST
  1008. exit(1);
  1009. #endif
  1010. while(1)
  1011. {
  1012. }
  1013. }
  1014. if (readbuffer[0] != 64)
  1015. {
  1016. printf("FAILED!\n");
  1017. #ifdef BATCH_TEST
  1018. exit(1);
  1019. #endif
  1020. while(1)
  1021. {
  1022. }
  1023. }
  1024. status = lx_nor_flash_sector_read(&nor_sim_flash, 48, readbuffer);
  1025. if (status != LX_SUCCESS)
  1026. {
  1027. printf("FAILED!\n");
  1028. #ifdef BATCH_TEST
  1029. exit(1);
  1030. #endif
  1031. while(1)
  1032. {
  1033. }
  1034. }
  1035. if (readbuffer[0] != 48)
  1036. {
  1037. printf("FAILED!\n");
  1038. #ifdef BATCH_TEST
  1039. exit(1);
  1040. #endif
  1041. while(1)
  1042. {
  1043. }
  1044. }
  1045. status = lx_nor_flash_sector_read(&nor_sim_flash, 32, readbuffer);
  1046. if (status != LX_SUCCESS)
  1047. {
  1048. printf("FAILED!\n");
  1049. #ifdef BATCH_TEST
  1050. exit(1);
  1051. #endif
  1052. while(1)
  1053. {
  1054. }
  1055. }
  1056. if (readbuffer[0] != 32)
  1057. {
  1058. printf("FAILED!\n");
  1059. #ifdef BATCH_TEST
  1060. exit(1);
  1061. #endif
  1062. while(1)
  1063. {
  1064. }
  1065. }
  1066. status = lx_nor_flash_sector_read(&nor_sim_flash, 16, readbuffer);
  1067. if (status != LX_SUCCESS)
  1068. {
  1069. printf("FAILED!\n");
  1070. #ifdef BATCH_TEST
  1071. exit(1);
  1072. #endif
  1073. while(1)
  1074. {
  1075. }
  1076. }
  1077. if (readbuffer[0] != 16)
  1078. {
  1079. printf("FAILED!\n");
  1080. #ifdef BATCH_TEST
  1081. exit(1);
  1082. #endif
  1083. while(1)
  1084. {
  1085. }
  1086. }
  1087. status = lx_nor_flash_sector_read(&nor_sim_flash, 1, readbuffer);
  1088. if (status != LX_SUCCESS)
  1089. {
  1090. printf("FAILED!\n");
  1091. #ifdef BATCH_TEST
  1092. exit(1);
  1093. #endif
  1094. while(1)
  1095. {
  1096. }
  1097. }
  1098. if (readbuffer[0] != 1)
  1099. {
  1100. printf("FAILED!\n");
  1101. #ifdef BATCH_TEST
  1102. exit(1);
  1103. #endif
  1104. while(1)
  1105. {
  1106. }
  1107. }
  1108. status = lx_nor_flash_sector_read(&nor_sim_flash, 7, readbuffer);
  1109. if (status != LX_SUCCESS)
  1110. {
  1111. printf("FAILED!\n");
  1112. #ifdef BATCH_TEST
  1113. exit(1);
  1114. #endif
  1115. while(1)
  1116. {
  1117. }
  1118. }
  1119. if (readbuffer[0] != 119)
  1120. {
  1121. printf("FAILED!\n");
  1122. #ifdef BATCH_TEST
  1123. exit(1);
  1124. #endif
  1125. while(1)
  1126. {
  1127. }
  1128. }
  1129. status = lx_nor_flash_sector_release(&nor_sim_flash, 7);
  1130. if (status != LX_SUCCESS)
  1131. {
  1132. printf("FAILED!\n");
  1133. #ifdef BATCH_TEST
  1134. exit(1);
  1135. #endif
  1136. while(1)
  1137. {
  1138. }
  1139. }
  1140. status = lx_nor_flash_sector_release(&nor_sim_flash, 8);
  1141. if (status != LX_SECTOR_NOT_FOUND)
  1142. {
  1143. printf("FAILED!\n");
  1144. #ifdef BATCH_TEST
  1145. exit(1);
  1146. #endif
  1147. while(1)
  1148. {
  1149. }
  1150. }
  1151. status = lx_nor_flash_defragment(&nor_sim_flash);
  1152. if (status != LX_SUCCESS)
  1153. {
  1154. printf("FAILED!\n");
  1155. #ifdef BATCH_TEST
  1156. exit(1);
  1157. #endif
  1158. while(1)
  1159. {
  1160. }
  1161. }
  1162. status = lx_nor_flash_close(&nor_sim_flash);
  1163. if (status != LX_SUCCESS)
  1164. {
  1165. printf("FAILED!\n");
  1166. #ifdef BATCH_TEST
  1167. exit(1);
  1168. #endif
  1169. while(1)
  1170. {
  1171. }
  1172. }
  1173. printf("SUCCESS!\n");
  1174. printf("Test 5: Randow write/read sector................");
  1175. /* Erase the simulated NOR flash. */
  1176. _lx_nor_flash_simulator_erase_all();
  1177. /* Open the flash. */
  1178. status = lx_nor_flash_open(&nor_sim_flash, "sim nor flash", _lx_nor_flash_simulator_initialize);
  1179. if (status != LX_SUCCESS)
  1180. {
  1181. printf("FAILED!\n");
  1182. #ifdef BATCH_TEST
  1183. exit(1);
  1184. #endif
  1185. while(1)
  1186. {
  1187. }
  1188. }
  1189. /* Write 100 sectors.... */
  1190. for (i = 0; i < 100; i++)
  1191. {
  1192. for (j = 0; j < 128; j++)
  1193. buffer[j] = i;
  1194. status = lx_nor_flash_sector_write(&nor_sim_flash, i, buffer);
  1195. if (status != LX_SUCCESS)
  1196. {
  1197. printf("FAILED!\n");
  1198. #ifdef BATCH_TEST
  1199. exit(1);
  1200. #endif
  1201. while(1)
  1202. {
  1203. }
  1204. }
  1205. }
  1206. /* Read back 100 sectors... */
  1207. for (i = 0; i < 100; i++)
  1208. {
  1209. status = lx_nor_flash_sector_read(&nor_sim_flash, i, buffer);
  1210. if (status != LX_SUCCESS)
  1211. {
  1212. printf("FAILED!\n");
  1213. #ifdef BATCH_TEST
  1214. exit(1);
  1215. #endif
  1216. while(1)
  1217. {
  1218. }
  1219. }
  1220. for (j = 0; j < 128; j++)
  1221. {
  1222. if (buffer[j] != i)
  1223. {
  1224. printf("FAILED!\n");
  1225. #ifdef BATCH_TEST
  1226. exit(1);
  1227. #endif
  1228. while(1)
  1229. {
  1230. }
  1231. }
  1232. }
  1233. }
  1234. /* Now, perform 1000 sector writes to randomly selected sectors, each time
  1235. reading first to make sure the previous contents are valid. */
  1236. for (i = 0; i < 1000; i++)
  1237. {
  1238. /* Pickup random sector. */
  1239. sector = (rand() % 100);
  1240. /* Read that sector. */
  1241. status = lx_nor_flash_sector_read(&nor_sim_flash, sector, buffer);
  1242. if (status != LX_SUCCESS)
  1243. {
  1244. printf("FAILED!\n");
  1245. #ifdef BATCH_TEST
  1246. exit(1);
  1247. #endif
  1248. while(1)
  1249. {
  1250. }
  1251. }
  1252. for (j = 0; j < 128; j++)
  1253. {
  1254. if ((buffer[j] & 0x0000FFFF) != sector)
  1255. {
  1256. printf("FAILED!\n");
  1257. #ifdef BATCH_TEST
  1258. exit(1);
  1259. #endif
  1260. while(1)
  1261. {
  1262. }
  1263. }
  1264. }
  1265. /* Include the itteraction in the buffer to generate a new write. */
  1266. for (j = 0; j < 128; j++)
  1267. {
  1268. buffer[j] = (buffer[j] & 0x0000FFFF) | (i << 16);
  1269. }
  1270. status = lx_nor_flash_sector_write(&nor_sim_flash, sector, buffer);
  1271. if (status != LX_SUCCESS)
  1272. {
  1273. printf("FAILED!\n");
  1274. #ifdef BATCH_TEST
  1275. exit(1);
  1276. #endif
  1277. while(1)
  1278. {
  1279. }
  1280. }
  1281. }
  1282. status = lx_nor_flash_close(&nor_sim_flash);
  1283. if (status != LX_SUCCESS)
  1284. {
  1285. printf("FAILED!\n");
  1286. #ifdef BATCH_TEST
  1287. exit(1);
  1288. #endif
  1289. while(1)
  1290. {
  1291. }
  1292. }
  1293. printf("SUCCESS!\n");
  1294. #ifdef BATCH_TEST
  1295. exit(0);
  1296. #endif
  1297. /* All done! */
  1298. while(1)
  1299. {
  1300. }
  1301. }