levelx_nand_flash_test.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384
  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 EXTENDED_CACHE
  11. #ifdef EXTENDED_CACHE
  12. UCHAR cache_memory[50000];
  13. #endif
  14. /* Define LevelX structures. */
  15. LX_NAND_FLASH nand_sim_flash;
  16. ULONG buffer[2048];
  17. ULONG readbuffer[2048];
  18. extern ULONG *nand_flash_memory;
  19. /* Define LevelX NOR flash simulator prototoypes. */
  20. UINT _lx_nand_flash_simulator_erase_all(VOID);
  21. UINT _lx_nand_flash_simulator_initialize(LX_NAND_FLASH *nand_flash);
  22. VOID _fx_nand_flash_read_sectors(ULONG logical_sector, ULONG sectors, UCHAR *destination_buffer);
  23. VOID _fx_nand_flash_write_sectors(ULONG logical_sector, ULONG sectors, UCHAR *source_buffer);
  24. #if 0
  25. typedef struct SECTOR_STRUCT
  26. {
  27. ULONG words[2048/4];
  28. } SECTOR;
  29. SECTOR test_write[8];
  30. SECTOR test_read[8];
  31. #endif
  32. #if 1
  33. UCHAR byte_buffer[2048];
  34. UCHAR ecc_bytes[24];
  35. UCHAR lx_ecc_buffer[24];
  36. #endif
  37. /* Define thread prototypes. */
  38. void thread_0_entry(ULONG thread_input);
  39. VOID ComputePageECC(UCHAR *data, INT size, UCHAR *code);
  40. INT CorrectPageECC(UCHAR *data, INT size, UCHAR *code);
  41. /* Define main entry point. */
  42. int main()
  43. {
  44. /* Enter the ThreadX kernel. */
  45. #ifndef LX_STANDALONE_ENABLE
  46. tx_kernel_enter();
  47. #else
  48. thread_0_entry(0);
  49. #endif
  50. }
  51. /* Define what the initial system looks like. */
  52. #ifndef LX_STANDALONE_ENABLE
  53. void tx_application_define(void *first_unused_memory)
  54. {
  55. /* Create the main thread. */
  56. tx_thread_create(&thread_0, "thread 0", thread_0_entry, 0,
  57. thread_0_stack, DEMO_STACK_SIZE,
  58. 1, 1, TX_NO_TIME_SLICE, TX_AUTO_START);
  59. }
  60. #endif
  61. #define NAND_MEMORY_SIZE 2056
  62. ULONG nand_memory_space[NAND_MEMORY_SIZE];
  63. /* For random read/write test */
  64. #define MAX_SECTOR_ADDRESS 64000*4
  65. #define SECTOR_SIZE 512
  66. UCHAR local_data_buffer[MAX_SECTOR_ADDRESS * SECTOR_SIZE];
  67. /* Define the test threads. */
  68. void thread_0_entry(ULONG thread_input)
  69. {
  70. ULONG i, j, sector;
  71. UINT status;
  72. ULONG *word_ptr;
  73. UCHAR *byte_ptr;
  74. /* Erase the simulated NOR flash. */
  75. _lx_nand_flash_simulator_erase_all();
  76. /* Initialize LevelX. */
  77. _lx_nand_flash_initialize();
  78. /* Test 1: Simple write 520 sectors and read 520 sectors. */
  79. printf("Test 1: Simple write-read 520 sectors...........");
  80. status = lx_nand_flash_format(&nand_sim_flash, "sim nand flash", _lx_nand_flash_simulator_initialize, nand_memory_space, sizeof(nand_memory_space));
  81. if (status != LX_SUCCESS)
  82. {
  83. printf("FAILED!\n");
  84. #ifdef BATCH_TEST
  85. exit(1);
  86. #endif
  87. while (1)
  88. {
  89. }
  90. }
  91. status= lx_nand_flash_open(&nand_sim_flash, "sim nand flash", _lx_nand_flash_simulator_initialize, nand_memory_space, sizeof(nand_memory_space));
  92. if (status != LX_SUCCESS)
  93. {
  94. printf("FAILED!\n");
  95. #ifdef BATCH_TEST
  96. exit(1);
  97. #endif
  98. while(1)
  99. {
  100. }
  101. }
  102. #ifdef EXTENDED_CACHE
  103. lx_nand_flash_extended_cache_enable(&nand_sim_flash, cache_memory, sizeof(cache_memory));
  104. #endif
  105. /* Write 520 sectors.... */
  106. for (i = 0; i < 520; i++)
  107. {
  108. for (j = 0; j < 512; j++)
  109. buffer[j] = i;
  110. status = lx_nand_flash_sector_write(&nand_sim_flash, i, buffer);
  111. if (status != LX_SUCCESS)
  112. {
  113. printf("FAILED!\n");
  114. #ifdef BATCH_TEST
  115. exit(1);
  116. #endif
  117. while(1)
  118. {
  119. }
  120. }
  121. }
  122. /* Close and reopen the flash. */
  123. lx_nand_flash_close(&nand_sim_flash);
  124. LX_MEMSET(nand_memory_space, 0, sizeof(nand_memory_space));
  125. LX_MEMSET(&nand_sim_flash, 0, sizeof(nand_sim_flash));
  126. status = lx_nand_flash_open(&nand_sim_flash, "sim nand flash", _lx_nand_flash_simulator_initialize, nand_memory_space, sizeof(nand_memory_space));
  127. if (status != LX_SUCCESS)
  128. {
  129. printf("FAILED!\n");
  130. #ifdef BATCH_TEST
  131. exit(1);
  132. #endif
  133. while (1)
  134. {
  135. }
  136. }
  137. /* Read back 520 sectors... */
  138. for (i = 0; i < 520; i++)
  139. {
  140. status = lx_nand_flash_sector_read(&nand_sim_flash, i, buffer);
  141. if (status != LX_SUCCESS)
  142. {
  143. #ifdef BATCH_TEST
  144. exit(1);
  145. #endif
  146. printf("FAILED!\n");
  147. while(1)
  148. {
  149. }
  150. }
  151. for (j = 0; j < 128; j++)
  152. {
  153. if (buffer[j] != i)
  154. {
  155. printf("FAILED!\n");
  156. #ifdef BATCH_TEST
  157. exit(1);
  158. #endif
  159. while(1)
  160. {
  161. }
  162. }
  163. }
  164. }
  165. _lx_nand_flash_close(&nand_sim_flash);
  166. printf("SUCCESS!\n");
  167. /* Test 2: Write same sector 320 times. */
  168. printf("Test 2: Write same sector 320 times.............");
  169. /* Reinitialize... */
  170. _lx_nand_flash_simulator_erase_all();
  171. lx_nand_flash_initialize();
  172. status = lx_nand_flash_format(&nand_sim_flash, "sim nand flash", _lx_nand_flash_simulator_initialize, nand_memory_space, sizeof(nand_memory_space));
  173. lx_nand_flash_open(&nand_sim_flash, "sim nand flash", _lx_nand_flash_simulator_initialize, nand_memory_space, sizeof(nand_memory_space));
  174. #ifdef EXTENDED_CACHE
  175. lx_nand_flash_extended_cache_enable(&nand_sim_flash, cache_memory, sizeof(cache_memory));
  176. #endif
  177. for (j = 0; j < 512; j++)
  178. buffer[j] = 0xFFFFFFFF;
  179. /* Write same sector 320 sectors.... */
  180. for (i = 0; i < 320; i++)
  181. {
  182. for (j = 0; j < 512; j++)
  183. buffer[j] = i;
  184. if (i == 319)
  185. buffer[j-1]--;
  186. status = lx_nand_flash_sector_write(&nand_sim_flash, 7, buffer);
  187. if (status != LX_SUCCESS)
  188. {
  189. printf("FAILED!\n");
  190. #ifdef BATCH_TEST
  191. exit(1);
  192. #endif
  193. while(1)
  194. {
  195. }
  196. }
  197. status = lx_nand_flash_sector_read(&nand_sim_flash, 7, readbuffer);
  198. if (status != LX_SUCCESS)
  199. {
  200. printf("FAILED!\n");
  201. #ifdef BATCH_TEST
  202. exit(1);
  203. #endif
  204. while(1)
  205. {
  206. }
  207. }
  208. for (j = 0; j < 128; j++)
  209. {
  210. if (buffer[j] != readbuffer[j])
  211. {
  212. printf("FAILED!\n");
  213. #ifdef BATCH_TEST
  214. exit(1);
  215. #endif
  216. while(1)
  217. {
  218. }
  219. }
  220. }
  221. /* Write other sectors just to have additional sectors to manage. */
  222. if (i == 1)
  223. lx_nand_flash_sector_write(&nand_sim_flash, 1, buffer);
  224. if (i == 16)
  225. lx_nand_flash_sector_write(&nand_sim_flash, 16, buffer);
  226. if (i == 32)
  227. lx_nand_flash_sector_write(&nand_sim_flash, 32, buffer);
  228. if (i == 48)
  229. lx_nand_flash_sector_write(&nand_sim_flash, 48, buffer);
  230. if (i == 64)
  231. lx_nand_flash_sector_write(&nand_sim_flash, 64, buffer);
  232. if (i == 80)
  233. lx_nand_flash_sector_write(&nand_sim_flash, 80, buffer);
  234. if (i == 96)
  235. lx_nand_flash_sector_write(&nand_sim_flash, 96, buffer);
  236. if (i == 112)
  237. lx_nand_flash_sector_write(&nand_sim_flash, 112, buffer);
  238. }
  239. status = lx_nand_flash_defragment(&nand_sim_flash);
  240. status = lx_nand_flash_sector_read(&nand_sim_flash, 7, readbuffer);
  241. if (status != LX_SUCCESS)
  242. {
  243. printf("FAILED!\n");
  244. #ifdef BATCH_TEST
  245. exit(1);
  246. #endif
  247. while(1)
  248. {
  249. }
  250. }
  251. if (readbuffer[0] != 319)
  252. {
  253. printf("FAILED!\n");
  254. #ifdef BATCH_TEST
  255. exit(1);
  256. #endif
  257. while(1)
  258. {
  259. }
  260. }
  261. status = lx_nand_flash_sector_read(&nand_sim_flash, 1, readbuffer);
  262. if (status != LX_SUCCESS)
  263. {
  264. printf("FAILED!\n");
  265. #ifdef BATCH_TEST
  266. exit(1);
  267. #endif
  268. while(1)
  269. {
  270. }
  271. }
  272. if (readbuffer[0] != 1)
  273. {
  274. printf("FAILED!\n");
  275. #ifdef BATCH_TEST
  276. exit(1);
  277. #endif
  278. while(1)
  279. {
  280. }
  281. }
  282. status = lx_nand_flash_sector_read(&nand_sim_flash, 16, readbuffer);
  283. if (status != LX_SUCCESS)
  284. {
  285. printf("FAILED!\n");
  286. #ifdef BATCH_TEST
  287. exit(1);
  288. #endif
  289. while(1)
  290. {
  291. }
  292. }
  293. if (readbuffer[0] != 16)
  294. {
  295. printf("FAILED!\n");
  296. #ifdef BATCH_TEST
  297. exit(1);
  298. #endif
  299. while(1)
  300. {
  301. }
  302. }
  303. status = lx_nand_flash_sector_read(&nand_sim_flash, 32, readbuffer);
  304. if (status != LX_SUCCESS)
  305. {
  306. printf("FAILED!\n");
  307. #ifdef BATCH_TEST
  308. exit(1);
  309. #endif
  310. while(1)
  311. {
  312. }
  313. }
  314. if (readbuffer[0] != 32)
  315. {
  316. printf("FAILED!\n");
  317. #ifdef BATCH_TEST
  318. exit(1);
  319. #endif
  320. while(1)
  321. {
  322. }
  323. }
  324. status = lx_nand_flash_sector_read(&nand_sim_flash, 48, readbuffer);
  325. if (status != LX_SUCCESS)
  326. {
  327. printf("FAILED!\n");
  328. #ifdef BATCH_TEST
  329. exit(1);
  330. #endif
  331. while(1)
  332. {
  333. }
  334. }
  335. if (readbuffer[0] != 48)
  336. {
  337. printf("FAILED!\n");
  338. #ifdef BATCH_TEST
  339. exit(1);
  340. #endif
  341. while(1)
  342. {
  343. }
  344. }
  345. status = lx_nand_flash_sector_read(&nand_sim_flash, 64, readbuffer);
  346. if (status != LX_SUCCESS)
  347. {
  348. printf("FAILED!\n");
  349. #ifdef BATCH_TEST
  350. exit(1);
  351. #endif
  352. while(1)
  353. {
  354. }
  355. }
  356. if (readbuffer[0] != 64)
  357. {
  358. printf("FAILED!\n");
  359. #ifdef BATCH_TEST
  360. exit(1);
  361. #endif
  362. while(1)
  363. {
  364. }
  365. }
  366. status = lx_nand_flash_sector_read(&nand_sim_flash, 80, readbuffer);
  367. if (status != LX_SUCCESS)
  368. {
  369. printf("FAILED!\n");
  370. #ifdef BATCH_TEST
  371. exit(1);
  372. #endif
  373. while(1)
  374. {
  375. }
  376. }
  377. if (readbuffer[0] != 80)
  378. {
  379. printf("FAILED!\n");
  380. #ifdef BATCH_TEST
  381. exit(1);
  382. #endif
  383. while(1)
  384. {
  385. }
  386. }
  387. status = lx_nand_flash_sector_read(&nand_sim_flash, 96, readbuffer);
  388. if (status != LX_SUCCESS)
  389. {
  390. printf("FAILED!\n");
  391. #ifdef BATCH_TEST
  392. exit(1);
  393. #endif
  394. while(1)
  395. {
  396. }
  397. }
  398. if (readbuffer[0] != 96)
  399. {
  400. printf("FAILED!\n");
  401. #ifdef BATCH_TEST
  402. exit(1);
  403. #endif
  404. while(1)
  405. {
  406. }
  407. }
  408. status = lx_nand_flash_sector_read(&nand_sim_flash, 112, readbuffer);
  409. if (status != LX_SUCCESS)
  410. {
  411. printf("FAILED!\n");
  412. #ifdef BATCH_TEST
  413. exit(1);
  414. #endif
  415. while(1)
  416. {
  417. }
  418. }
  419. if (readbuffer[0] != 112)
  420. {
  421. printf("FAILED!\n");
  422. #ifdef BATCH_TEST
  423. exit(1);
  424. #endif
  425. while(1)
  426. {
  427. }
  428. }
  429. status = lx_nand_flash_defragment(&nand_sim_flash);
  430. if (status != LX_NOT_SUPPORTED)
  431. {
  432. printf("FAILED!\n");
  433. #ifdef BATCH_TEST
  434. exit(1);
  435. #endif
  436. while(1)
  437. {
  438. }
  439. }
  440. status = lx_nand_flash_close(&nand_sim_flash);
  441. if (status != LX_SUCCESS)
  442. {
  443. printf("FAILED!\n");
  444. #ifdef BATCH_TEST
  445. exit(1);
  446. #endif
  447. while(1)
  448. {
  449. }
  450. }
  451. printf("SUCCESS!\n");
  452. printf("Test 3: Page copy test..........................");
  453. /* Reinitialize... */
  454. _lx_nand_flash_simulator_erase_all();
  455. lx_nand_flash_initialize();
  456. status = lx_nand_flash_format(&nand_sim_flash, "sim nand flash", _lx_nand_flash_simulator_initialize, nand_memory_space, sizeof(nand_memory_space));
  457. lx_nand_flash_open(&nand_sim_flash, "sim nand flash", _lx_nand_flash_simulator_initialize, nand_memory_space, sizeof(nand_memory_space));
  458. for (j = 0; j < 512; j++)
  459. buffer[j] = 0xFFFFFFFF;
  460. status = lx_nand_flash_sector_write(&nand_sim_flash, 0, buffer);
  461. /* Write same sector 320 sectors.... */
  462. for (i = 256; i < 512; i++)
  463. {
  464. for (j = 0; j < 512; j++)
  465. buffer[j] = i;
  466. if (i == 300) continue;
  467. status = lx_nand_flash_sector_write(&nand_sim_flash, i, buffer);
  468. if (status != LX_SUCCESS)
  469. {
  470. printf("FAILED!\n");
  471. #ifdef BATCH_TEST
  472. exit(1);
  473. #endif
  474. while (1)
  475. {
  476. }
  477. }
  478. }
  479. status = lx_nand_flash_sector_write(&nand_sim_flash, 300, buffer);
  480. if (status != LX_SUCCESS)
  481. {
  482. printf("FAILED!\n");
  483. #ifdef BATCH_TEST
  484. exit(1);
  485. #endif
  486. while (1)
  487. {
  488. }
  489. }
  490. status = lx_nand_flash_sector_write(&nand_sim_flash, 400, buffer);
  491. if (status != LX_SUCCESS)
  492. {
  493. printf("FAILED!\n");
  494. #ifdef BATCH_TEST
  495. exit(1);
  496. #endif
  497. while (1)
  498. {
  499. }
  500. }
  501. if (status != LX_SUCCESS)
  502. {
  503. printf("FAILED!\n");
  504. #ifdef BATCH_TEST
  505. exit(1);
  506. #endif
  507. while (1)
  508. {
  509. }
  510. }
  511. if (nand_sim_flash.lx_nand_flash_block_status_table[nand_sim_flash.lx_nand_flash_block_mapping_table[1]] & LX_NAND_BLOCK_STATUS_NON_SEQUENTIAL)
  512. {
  513. printf("FAILED!\n");
  514. #ifdef BATCH_TEST
  515. exit(1);
  516. #endif
  517. while (1)
  518. {
  519. }
  520. }
  521. status = lx_nand_flash_close(&nand_sim_flash);
  522. if (status != LX_SUCCESS)
  523. {
  524. printf("FAILED!\n");
  525. #ifdef BATCH_TEST
  526. exit(1);
  527. #endif
  528. while(1)
  529. {
  530. }
  531. }
  532. printf("SUCCESS!\n");
  533. printf("Test 4: Random read/write test..................");
  534. LX_MEMSET(local_data_buffer, 0xff, sizeof(local_data_buffer));
  535. /* Reinitialize... */
  536. _lx_nand_flash_simulator_erase_all();
  537. lx_nand_flash_initialize();
  538. status = lx_nand_flash_format(&nand_sim_flash, "sim nand flash", _lx_nand_flash_simulator_initialize, nand_memory_space, sizeof(nand_memory_space));
  539. if (status != LX_SUCCESS)
  540. {
  541. printf("FAILED!\n");
  542. #ifdef BATCH_TEST
  543. exit(1);
  544. #endif
  545. while (1)
  546. {
  547. }
  548. }
  549. status = lx_nand_flash_open(&nand_sim_flash, "sim nand flash", _lx_nand_flash_simulator_initialize, nand_memory_space, sizeof(nand_memory_space));
  550. if (status != LX_SUCCESS)
  551. {
  552. printf("FAILED!\n");
  553. #ifdef BATCH_TEST
  554. exit(1);
  555. #endif
  556. while (1)
  557. {
  558. }
  559. }
  560. static ULONG loop_count = 200;
  561. ULONG operation;
  562. ULONG sector_number;
  563. ULONG data_byte;
  564. ULONG sector_count;
  565. static ULONG read_count = 0;
  566. static ULONG write_count = 0;
  567. static ULONG release_count = 0;
  568. static ULONG reopen_count = 0;
  569. while (loop_count--)
  570. {
  571. operation = (ULONG)rand() % 32768;
  572. if (operation < 16384)
  573. {
  574. sector_number = ((ULONG)rand() * (ULONG)rand()) % MAX_SECTOR_ADDRESS;
  575. sector_count = ((ULONG)rand() % 768) + 1;
  576. if (sector_number + sector_count > MAX_SECTOR_ADDRESS)
  577. {
  578. sector_count = MAX_SECTOR_ADDRESS - sector_number;
  579. }
  580. while (sector_count--)
  581. {
  582. status = lx_nand_flash_sector_read(&nand_sim_flash, sector_number, readbuffer);
  583. if (status != LX_SUCCESS)
  584. {
  585. printf("FAILED!\n");
  586. #ifdef BATCH_TEST
  587. exit(1);
  588. #endif
  589. while (1)
  590. {
  591. }
  592. }
  593. #ifdef FULL_SECTOR_DATA_VERIFY
  594. status = memcmp((local_data_buffer + sector_number * SECTOR_SIZE), readbuffer, SECTOR_SIZE);
  595. #else
  596. status = *(ULONG*)(local_data_buffer + sector_number * SECTOR_SIZE) - readbuffer[0];
  597. #endif
  598. if (status != 0)
  599. {
  600. printf("FAILED!\n");
  601. #ifdef BATCH_TEST
  602. exit(1);
  603. #endif
  604. while (1)
  605. {
  606. }
  607. }
  608. read_count++;
  609. sector_number++;
  610. }
  611. }
  612. else if (operation < 29491)
  613. {
  614. sector_number = ((ULONG)rand() * (ULONG)rand()) % MAX_SECTOR_ADDRESS;
  615. sector_count = ((ULONG)rand() % 768) + 1;
  616. if (sector_number + sector_count > MAX_SECTOR_ADDRESS)
  617. {
  618. sector_count = MAX_SECTOR_ADDRESS - sector_number;
  619. }
  620. while (sector_count--)
  621. {
  622. data_byte = (ULONG)rand();
  623. #ifdef FULL_SECTOR_DATA_VERIFY
  624. LX_MEMSET((local_data_buffer + sector_number * SECTOR_SIZE), data_byte, SECTOR_SIZE);
  625. #else
  626. *(ULONG*)(local_data_buffer + sector_number * SECTOR_SIZE) = data_byte;
  627. #endif
  628. status = lx_nand_flash_sector_write(&nand_sim_flash, sector_number, local_data_buffer + sector_number * SECTOR_SIZE);
  629. if (status != LX_SUCCESS)
  630. {
  631. printf("FAILED!\n");
  632. #ifdef BATCH_TEST
  633. exit(1);
  634. #endif
  635. while (1)
  636. {
  637. }
  638. }
  639. write_count++;
  640. sector_number++;
  641. }
  642. }
  643. else if(operation < 32760)
  644. {
  645. sector_number = ((ULONG)rand() * (ULONG)rand()) % MAX_SECTOR_ADDRESS;
  646. data_byte = 0xffffffff;
  647. sector_count = ((ULONG)rand() % 768) + 1;
  648. if (sector_number + sector_count > MAX_SECTOR_ADDRESS)
  649. {
  650. sector_count = MAX_SECTOR_ADDRESS - sector_number;
  651. }
  652. while (sector_count--)
  653. {
  654. #ifdef FULL_SECTOR_DATA_VERIFY
  655. LX_MEMSET((local_data_buffer + sector_number * SECTOR_SIZE), data_byte, SECTOR_SIZE);
  656. #else
  657. * (ULONG*)(local_data_buffer + sector_number * SECTOR_SIZE) = data_byte;
  658. #endif
  659. status = lx_nand_flash_sector_release(&nand_sim_flash, sector_number);
  660. if (status != LX_SUCCESS)
  661. {
  662. printf("FAILED!\n");
  663. #ifdef BATCH_TEST
  664. exit(1);
  665. #endif
  666. while (1)
  667. {
  668. }
  669. }
  670. release_count++;
  671. sector_number++;
  672. }
  673. }
  674. else
  675. {
  676. /* Close and reopen the flash. */
  677. status = lx_nand_flash_close(&nand_sim_flash);
  678. if (status != LX_SUCCESS)
  679. {
  680. printf("FAILED!\n");
  681. #ifdef BATCH_TEST
  682. exit(1);
  683. #endif
  684. while (1)
  685. {
  686. }
  687. }
  688. status = lx_nand_flash_open(&nand_sim_flash, "sim nor flash", _lx_nand_flash_simulator_initialize, nand_memory_space, sizeof(nand_memory_space));
  689. if (status != LX_SUCCESS)
  690. {
  691. printf("FAILED!\n");
  692. #ifdef BATCH_TEST
  693. exit(1);
  694. #endif
  695. while (1)
  696. {
  697. }
  698. }
  699. reopen_count++;
  700. }
  701. }
  702. printf("SUCCESS!\n");
  703. #if 0
  704. /* Point at the simulated NOR flash memory. */
  705. word_ptr = nand_flash_memory;
  706. /* Test 3: Corrupt block 0, simulate a power interruption during erase of block 0,
  707. after the erase, but before the erase count is setup. */
  708. printf("Test 3: Block erase-initialize interrupted......");
  709. word_ptr[0] = 0xFFFFFFFF;
  710. word_ptr[3] = 0x00000000; /* This simulates a non-erased block. */
  711. /* Open the flash and see if we recover properly. */
  712. status = lx_nand_flash_open(&nand_sim_flash, "sim nor flash", _lx_nand_flash_simulator_initialize, nand_memory_space, sizeof(nand_memory_space));
  713. #ifdef EXTENDED_CACHE
  714. status += lx_nand_flash_extended_cache_enable(&nand_sim_flash, cache_memory, sizeof(cache_memory));
  715. #endif
  716. if ((status != LX_SUCCESS) ||
  717. (nand_sim_flash.lx_nand_flash_free_pages != 111) ||
  718. (nand_sim_flash.lx_nand_flash_mapped_pages != 9))
  719. {
  720. printf("FAILED!\n");
  721. #ifdef BATCH_TEST
  722. exit(1);
  723. #endif
  724. while(1)
  725. {
  726. }
  727. }
  728. status = lx_nand_flash_close(&nand_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. /* Corrupt block 0, simulate a power interruption after erase of block 0,
  740. but before erase count is setup. */
  741. word_ptr[0] = 0xFFFFFFFF;
  742. /* Open the flash and see if we recover properly. */
  743. status = lx_nand_flash_open(&nand_sim_flash, "sim nor flash", _lx_nand_flash_simulator_initialize, nand_memory_space, sizeof(nand_memory_space));
  744. #ifdef EXTENDED_CACHE
  745. status += lx_nand_flash_extended_cache_enable(&nand_sim_flash, cache_memory, sizeof(cache_memory));
  746. #endif
  747. if ((status != LX_SUCCESS) ||
  748. (nand_sim_flash.lx_nand_flash_free_pages != 111) ||
  749. (nand_sim_flash.lx_nand_flash_mapped_pages != 9))
  750. {
  751. printf("FAILED!\n");
  752. #ifdef BATCH_TEST
  753. exit(1);
  754. #endif
  755. while(1)
  756. {
  757. }
  758. }
  759. status = lx_nand_flash_close(&nand_sim_flash);
  760. if (status != LX_SUCCESS)
  761. {
  762. printf("FAILED!\n");
  763. #ifdef BATCH_TEST
  764. exit(1);
  765. #endif
  766. while(1)
  767. {
  768. }
  769. }
  770. printf("SUCCESS!\n");
  771. /* Test 4: simulate a power interruption while a new page is being setup. */
  772. printf("Test 4: Power interrupted new page setup........");
  773. /* Partially setup new page. */
  774. byte_ptr = (UCHAR *) word_ptr;
  775. byte_ptr[2053] = 0xBF; /* Set block 0/page 0 extra bytes to indicate block not empty. */
  776. word_ptr[528] = 0x60;
  777. /* Open the flash and see if we recover properly. We should mark this page as obsolete. */
  778. status = lx_nand_flash_open(&nand_sim_flash, "sim nor flash", _lx_nand_flash_simulator_initialize, nand_memory_space, sizeof(nand_memory_space));
  779. #ifdef EXTENDED_CACHE
  780. status += lx_nand_flash_extended_cache_enable(&nand_sim_flash, cache_memory, sizeof(cache_memory));
  781. #endif
  782. if ((status != LX_SUCCESS) ||
  783. (nand_sim_flash.lx_nand_flash_free_pages != 110) ||
  784. (nand_sim_flash.lx_nand_flash_mapped_pages != 9))
  785. {
  786. printf("FAILED!\n");
  787. #ifdef BATCH_TEST
  788. exit(1);
  789. #endif
  790. while(1)
  791. {
  792. }
  793. }
  794. status = lx_nand_flash_close(&nand_sim_flash);
  795. if (status != LX_SUCCESS)
  796. {
  797. printf("FAILED!\n");
  798. #ifdef BATCH_TEST
  799. exit(1);
  800. #endif
  801. while(1)
  802. {
  803. }
  804. }
  805. /* Simulate a power interruption after a new sector is allocated, after data
  806. had been copied, and the superceeded bit is clear, but before the new entry can be
  807. setup. */
  808. /* Copy data block and reset spare. */
  809. byte_ptr[2053] = 0xBF; /* Set block 0/page 0 extra bytes to indicate block not empty. */
  810. for (i = 1; i < 512; i++) /* Fill block 0, page 1 data */
  811. word_ptr[i+528] = 0x60;
  812. word_ptr[i+528] = 0xFFFFFFFF; /* Reset extra bytes for block 0, page 1 */
  813. word_ptr[i+529] = 0xFFFFFFFF; /* Reset extra bytes for block 0, page 1 */
  814. /* Set the superceded bit in block 1/page 1. */
  815. byte_ptr[37957] = 0x80;
  816. /* Open the flash and see if we recover properly. */
  817. status = lx_nand_flash_open(&nand_sim_flash, "sim nor flash", _lx_nand_flash_simulator_initialize, nand_memory_space, sizeof(nand_memory_space));
  818. #ifdef EXTENDED_CACHE
  819. lx_nand_flash_extended_cache_enable(&nand_sim_flash, cache_memory, sizeof(cache_memory));
  820. #endif
  821. if ((status != LX_SUCCESS) ||
  822. (nand_sim_flash.lx_nand_flash_free_pages != 110) ||
  823. (nand_sim_flash.lx_nand_flash_mapped_pages != 9))
  824. {
  825. printf("FAILED!\n");
  826. #ifdef BATCH_TEST
  827. exit(1);
  828. #endif
  829. while(1)
  830. {
  831. }
  832. }
  833. lx_nand_flash_close(&nand_sim_flash);
  834. /* Simulate a power interruption after a new sector is allocated, after data
  835. had been copied, and the superceeded bit is clear, the new entry is setup, but the old entry
  836. has not been invalidated. */
  837. byte_ptr[2053] = 0xBF; /* Set block 0/page 0 extra bytes to indicate block not empty. */
  838. for (i = 1; i < 512; i++) /* Fill block 0, page 1 data */
  839. word_ptr[i+528] = 0x60;
  840. word_ptr[i+528] = 0xFFFFFFFF; /* Reset extra bytes for block 0, page 1 */
  841. word_ptr[i+529] = 0xFFFFFFFF; /* Reset extra bytes for block 0, page 1 */
  842. byte_ptr = (UCHAR *) word_ptr;
  843. /* set the spare info in the new page. */
  844. byte_ptr[4162] = 0x60; /* Setup extra bytes for block 0, page 1 */
  845. byte_ptr[4163] = 0; /* Setup extra bytes for block 0, page 1 */
  846. byte_ptr[4164] = 0; /* Setup extra bytes for block 0, page 1 */
  847. byte_ptr[4165] = 0xC0; /* Setup extra bytes for block 0, page 1 */
  848. /* Set the superceded bit in block 1/page 1. */
  849. byte_ptr[37957] = 0x80;
  850. /* Open the flash and see if we recover properly. */
  851. status = lx_nand_flash_open(&nand_sim_flash, "sim nor flash", _lx_nand_flash_simulator_initialize, nand_memory_space, sizeof(nand_memory_space));
  852. #ifdef EXTENDED_CACHE
  853. status += lx_nand_flash_extended_cache_enable(&nand_sim_flash, cache_memory, sizeof(cache_memory));
  854. #endif
  855. if ((status != LX_SUCCESS) ||
  856. (nand_sim_flash.lx_nand_flash_free_pages != 110) ||
  857. (nand_sim_flash.lx_nand_flash_mapped_pages != 9))
  858. {
  859. printf("FAILED!\n");
  860. #ifdef BATCH_TEST
  861. exit(1);
  862. #endif
  863. while(1)
  864. {
  865. }
  866. }
  867. status = lx_nand_flash_sector_read(&nand_sim_flash, 7, readbuffer);
  868. if (status != LX_SUCCESS)
  869. {
  870. printf("FAILED!\n");
  871. #ifdef BATCH_TEST
  872. exit(1);
  873. #endif
  874. while(1)
  875. {
  876. }
  877. }
  878. if (readbuffer[0] != 119)
  879. {
  880. printf("FAILED!\n");
  881. #ifdef BATCH_TEST
  882. exit(1);
  883. #endif
  884. while(1)
  885. {
  886. }
  887. }
  888. status = lx_nand_flash_sector_read(&nand_sim_flash, 1, readbuffer);
  889. if (status != LX_SUCCESS)
  890. {
  891. printf("FAILED!\n");
  892. #ifdef BATCH_TEST
  893. exit(1);
  894. #endif
  895. while(1)
  896. {
  897. }
  898. }
  899. if (readbuffer[0] != 1)
  900. {
  901. printf("FAILED!\n");
  902. #ifdef BATCH_TEST
  903. exit(1);
  904. #endif
  905. while(1)
  906. {
  907. }
  908. }
  909. status = lx_nand_flash_sector_read(&nand_sim_flash, 16, readbuffer);
  910. if (status != LX_SUCCESS)
  911. {
  912. printf("FAILED!\n");
  913. #ifdef BATCH_TEST
  914. exit(1);
  915. #endif
  916. while(1)
  917. {
  918. }
  919. }
  920. if (readbuffer[0] != 16)
  921. {
  922. printf("FAILED!\n");
  923. #ifdef BATCH_TEST
  924. exit(1);
  925. #endif
  926. while(1)
  927. {
  928. }
  929. }
  930. status = lx_nand_flash_sector_read(&nand_sim_flash, 32, readbuffer);
  931. if (status != LX_SUCCESS)
  932. {
  933. printf("FAILED!\n");
  934. #ifdef BATCH_TEST
  935. exit(1);
  936. #endif
  937. while(1)
  938. {
  939. }
  940. }
  941. if (readbuffer[0] != 32)
  942. {
  943. printf("FAILED!\n");
  944. #ifdef BATCH_TEST
  945. exit(1);
  946. #endif
  947. while(1)
  948. {
  949. }
  950. }
  951. status = lx_nand_flash_sector_read(&nand_sim_flash, 48, readbuffer);
  952. if (status != LX_SUCCESS)
  953. {
  954. printf("FAILED!\n");
  955. #ifdef BATCH_TEST
  956. exit(1);
  957. #endif
  958. while(1)
  959. {
  960. }
  961. }
  962. if (readbuffer[0] != 48)
  963. {
  964. printf("FAILED!\n");
  965. #ifdef BATCH_TEST
  966. exit(1);
  967. #endif
  968. while(1)
  969. {
  970. }
  971. }
  972. status = lx_nand_flash_sector_read(&nand_sim_flash, 64, readbuffer);
  973. if (status != LX_SUCCESS)
  974. {
  975. printf("FAILED!\n");
  976. #ifdef BATCH_TEST
  977. exit(1);
  978. #endif
  979. while(1)
  980. {
  981. }
  982. }
  983. if (readbuffer[0] != 64)
  984. {
  985. printf("FAILED!\n");
  986. #ifdef BATCH_TEST
  987. exit(1);
  988. #endif
  989. while(1)
  990. {
  991. }
  992. }
  993. status = lx_nand_flash_sector_read(&nand_sim_flash, 80, readbuffer);
  994. if (status != LX_SUCCESS)
  995. {
  996. printf("FAILED!\n");
  997. #ifdef BATCH_TEST
  998. exit(1);
  999. #endif
  1000. while(1)
  1001. {
  1002. }
  1003. }
  1004. if (readbuffer[0] != 80)
  1005. {
  1006. printf("FAILED!\n");
  1007. #ifdef BATCH_TEST
  1008. exit(1);
  1009. #endif
  1010. while(1)
  1011. {
  1012. }
  1013. }
  1014. status = lx_nand_flash_sector_read(&nand_sim_flash, 96, readbuffer);
  1015. if (status != LX_SUCCESS)
  1016. {
  1017. printf("FAILED!\n");
  1018. #ifdef BATCH_TEST
  1019. exit(1);
  1020. #endif
  1021. while(1)
  1022. {
  1023. }
  1024. }
  1025. if (readbuffer[0] != 96)
  1026. {
  1027. printf("FAILED!\n");
  1028. #ifdef BATCH_TEST
  1029. exit(1);
  1030. #endif
  1031. while(1)
  1032. {
  1033. }
  1034. }
  1035. status = lx_nand_flash_sector_read(&nand_sim_flash, 112, readbuffer);
  1036. if (status != LX_SUCCESS)
  1037. {
  1038. printf("FAILED!\n");
  1039. #ifdef BATCH_TEST
  1040. exit(1);
  1041. #endif
  1042. while(1)
  1043. {
  1044. }
  1045. }
  1046. if (readbuffer[0] != 112)
  1047. {
  1048. printf("FAILED!\n");
  1049. #ifdef BATCH_TEST
  1050. exit(1);
  1051. #endif
  1052. while(1)
  1053. {
  1054. }
  1055. }
  1056. status = lx_nand_flash_defragment(&nand_sim_flash);
  1057. if (status != LX_NOT_SUPPORTED)
  1058. {
  1059. printf("FAILED!\n");
  1060. #ifdef BATCH_TEST
  1061. exit(1);
  1062. #endif
  1063. while(1)
  1064. {
  1065. }
  1066. }
  1067. status = lx_nand_flash_defragment(&nand_sim_flash);
  1068. if (status != LX_NOT_SUPPORTED)
  1069. {
  1070. printf("FAILED!\n");
  1071. #ifdef BATCH_TEST
  1072. exit(1);
  1073. #endif
  1074. while(1)
  1075. {
  1076. }
  1077. }
  1078. status = lx_nand_flash_sector_read(&nand_sim_flash, 7, readbuffer);
  1079. if (status != LX_SUCCESS)
  1080. {
  1081. printf("FAILED!\n");
  1082. #ifdef BATCH_TEST
  1083. exit(1);
  1084. #endif
  1085. while(1)
  1086. {
  1087. }
  1088. }
  1089. if (readbuffer[0] != 119)
  1090. {
  1091. printf("FAILED!\n");
  1092. #ifdef BATCH_TEST
  1093. exit(1);
  1094. #endif
  1095. while(1)
  1096. {
  1097. }
  1098. }
  1099. status = lx_nand_flash_sector_read(&nand_sim_flash, 1, readbuffer);
  1100. if (status != LX_SUCCESS)
  1101. {
  1102. printf("FAILED!\n");
  1103. #ifdef BATCH_TEST
  1104. exit(1);
  1105. #endif
  1106. while(1)
  1107. {
  1108. }
  1109. }
  1110. if (readbuffer[0] != 1)
  1111. {
  1112. printf("FAILED!\n");
  1113. #ifdef BATCH_TEST
  1114. exit(1);
  1115. #endif
  1116. while(1)
  1117. {
  1118. }
  1119. }
  1120. status = lx_nand_flash_sector_read(&nand_sim_flash, 16, readbuffer);
  1121. if (status != LX_SUCCESS)
  1122. {
  1123. printf("FAILED!\n");
  1124. #ifdef BATCH_TEST
  1125. exit(1);
  1126. #endif
  1127. while(1)
  1128. {
  1129. }
  1130. }
  1131. if (readbuffer[0] != 16)
  1132. {
  1133. printf("FAILED!\n");
  1134. #ifdef BATCH_TEST
  1135. exit(1);
  1136. #endif
  1137. while(1)
  1138. {
  1139. }
  1140. }
  1141. status = lx_nand_flash_sector_read(&nand_sim_flash, 32, readbuffer);
  1142. if (status != LX_SUCCESS)
  1143. {
  1144. printf("FAILED!\n");
  1145. #ifdef BATCH_TEST
  1146. exit(1);
  1147. #endif
  1148. while(1)
  1149. {
  1150. }
  1151. }
  1152. if (readbuffer[0] != 32)
  1153. {
  1154. printf("FAILED!\n");
  1155. #ifdef BATCH_TEST
  1156. exit(1);
  1157. #endif
  1158. while(1)
  1159. {
  1160. }
  1161. }
  1162. status = lx_nand_flash_sector_read(&nand_sim_flash, 48, readbuffer);
  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. if (readbuffer[0] != 48)
  1174. {
  1175. printf("FAILED!\n");
  1176. #ifdef BATCH_TEST
  1177. exit(1);
  1178. #endif
  1179. while(1)
  1180. {
  1181. }
  1182. }
  1183. status = lx_nand_flash_sector_read(&nand_sim_flash, 64, readbuffer);
  1184. if (status != LX_SUCCESS)
  1185. {
  1186. printf("FAILED!\n");
  1187. #ifdef BATCH_TEST
  1188. exit(1);
  1189. #endif
  1190. while(1)
  1191. {
  1192. }
  1193. }
  1194. if (readbuffer[0] != 64)
  1195. {
  1196. printf("FAILED!\n");
  1197. #ifdef BATCH_TEST
  1198. exit(1);
  1199. #endif
  1200. while(1)
  1201. {
  1202. }
  1203. }
  1204. status = lx_nand_flash_sector_read(&nand_sim_flash, 80, readbuffer);
  1205. if (status != LX_SUCCESS)
  1206. {
  1207. printf("FAILED!\n");
  1208. #ifdef BATCH_TEST
  1209. exit(1);
  1210. #endif
  1211. while(1)
  1212. {
  1213. }
  1214. }
  1215. if (readbuffer[0] != 80)
  1216. {
  1217. printf("FAILED!\n");
  1218. #ifdef BATCH_TEST
  1219. exit(1);
  1220. #endif
  1221. while(1)
  1222. {
  1223. }
  1224. }
  1225. status = lx_nand_flash_sector_read(&nand_sim_flash, 96, readbuffer);
  1226. if (status != LX_SUCCESS)
  1227. {
  1228. printf("FAILED!\n");
  1229. #ifdef BATCH_TEST
  1230. exit(1);
  1231. #endif
  1232. while(1)
  1233. {
  1234. }
  1235. }
  1236. if (readbuffer[0] != 96)
  1237. {
  1238. printf("FAILED!\n");
  1239. #ifdef BATCH_TEST
  1240. exit(1);
  1241. #endif
  1242. while(1)
  1243. {
  1244. }
  1245. }
  1246. status = lx_nand_flash_sector_read(&nand_sim_flash, 112, readbuffer);
  1247. if (status != LX_SUCCESS)
  1248. {
  1249. printf("FAILED!\n");
  1250. #ifdef BATCH_TEST
  1251. exit(1);
  1252. #endif
  1253. while(1)
  1254. {
  1255. }
  1256. }
  1257. if (readbuffer[0] != 112)
  1258. {
  1259. printf("FAILED!\n");
  1260. #ifdef BATCH_TEST
  1261. exit(1);
  1262. #endif
  1263. while(1)
  1264. {
  1265. }
  1266. }
  1267. /* Read in reverse order to see if caching helps! */
  1268. status = lx_nand_flash_sector_read(&nand_sim_flash, 112, readbuffer);
  1269. if (status != LX_SUCCESS)
  1270. {
  1271. printf("FAILED!\n");
  1272. #ifdef BATCH_TEST
  1273. exit(1);
  1274. #endif
  1275. while(1)
  1276. {
  1277. }
  1278. }
  1279. if (readbuffer[0] != 112)
  1280. {
  1281. printf("FAILED!\n");
  1282. #ifdef BATCH_TEST
  1283. exit(1);
  1284. #endif
  1285. while(1)
  1286. {
  1287. }
  1288. }
  1289. status = lx_nand_flash_sector_read(&nand_sim_flash, 96, readbuffer);
  1290. if (status != LX_SUCCESS)
  1291. {
  1292. printf("FAILED!\n");
  1293. #ifdef BATCH_TEST
  1294. exit(1);
  1295. #endif
  1296. while(1)
  1297. {
  1298. }
  1299. }
  1300. if (readbuffer[0] != 96)
  1301. {
  1302. printf("FAILED!\n");
  1303. #ifdef BATCH_TEST
  1304. exit(1);
  1305. #endif
  1306. while(1)
  1307. {
  1308. }
  1309. }
  1310. status = lx_nand_flash_sector_read(&nand_sim_flash, 80, readbuffer);
  1311. if (status != LX_SUCCESS)
  1312. {
  1313. printf("FAILED!\n");
  1314. #ifdef BATCH_TEST
  1315. exit(1);
  1316. #endif
  1317. while(1)
  1318. {
  1319. }
  1320. }
  1321. if (readbuffer[0] != 80)
  1322. {
  1323. printf("FAILED!\n");
  1324. #ifdef BATCH_TEST
  1325. exit(1);
  1326. #endif
  1327. while(1)
  1328. {
  1329. }
  1330. }
  1331. status = lx_nand_flash_sector_read(&nand_sim_flash, 64, readbuffer);
  1332. if (status != LX_SUCCESS)
  1333. {
  1334. printf("FAILED!\n");
  1335. #ifdef BATCH_TEST
  1336. exit(1);
  1337. #endif
  1338. while(1)
  1339. {
  1340. }
  1341. }
  1342. if (readbuffer[0] != 64)
  1343. {
  1344. printf("FAILED!\n");
  1345. #ifdef BATCH_TEST
  1346. exit(1);
  1347. #endif
  1348. while(1)
  1349. {
  1350. }
  1351. }
  1352. status = lx_nand_flash_sector_read(&nand_sim_flash, 48, readbuffer);
  1353. if (status != LX_SUCCESS)
  1354. {
  1355. printf("FAILED!\n");
  1356. #ifdef BATCH_TEST
  1357. exit(1);
  1358. #endif
  1359. while(1)
  1360. {
  1361. }
  1362. }
  1363. if (readbuffer[0] != 48)
  1364. {
  1365. printf("FAILED!\n");
  1366. #ifdef BATCH_TEST
  1367. exit(1);
  1368. #endif
  1369. while(1)
  1370. {
  1371. }
  1372. }
  1373. status = lx_nand_flash_sector_read(&nand_sim_flash, 32, readbuffer);
  1374. if (status != LX_SUCCESS)
  1375. {
  1376. printf("FAILED!\n");
  1377. #ifdef BATCH_TEST
  1378. exit(1);
  1379. #endif
  1380. while(1)
  1381. {
  1382. }
  1383. }
  1384. if (readbuffer[0] != 32)
  1385. {
  1386. printf("FAILED!\n");
  1387. #ifdef BATCH_TEST
  1388. exit(1);
  1389. #endif
  1390. while(1)
  1391. {
  1392. }
  1393. }
  1394. status = lx_nand_flash_sector_read(&nand_sim_flash, 16, readbuffer);
  1395. if (status != LX_SUCCESS)
  1396. {
  1397. printf("FAILED!\n");
  1398. #ifdef BATCH_TEST
  1399. exit(1);
  1400. #endif
  1401. while(1)
  1402. {
  1403. }
  1404. }
  1405. if (readbuffer[0] != 16)
  1406. {
  1407. printf("FAILED!\n");
  1408. #ifdef BATCH_TEST
  1409. exit(1);
  1410. #endif
  1411. while(1)
  1412. {
  1413. }
  1414. }
  1415. status = lx_nand_flash_sector_read(&nand_sim_flash, 1, readbuffer);
  1416. if (status != LX_SUCCESS)
  1417. {
  1418. printf("FAILED!\n");
  1419. #ifdef BATCH_TEST
  1420. exit(1);
  1421. #endif
  1422. while(1)
  1423. {
  1424. }
  1425. }
  1426. if (readbuffer[0] != 1)
  1427. {
  1428. printf("FAILED!\n");
  1429. #ifdef BATCH_TEST
  1430. exit(1);
  1431. #endif
  1432. while(1)
  1433. {
  1434. }
  1435. }
  1436. status = lx_nand_flash_sector_read(&nand_sim_flash, 7, readbuffer);
  1437. if (status != LX_SUCCESS)
  1438. {
  1439. printf("FAILED!\n");
  1440. #ifdef BATCH_TEST
  1441. exit(1);
  1442. #endif
  1443. while(1)
  1444. {
  1445. }
  1446. }
  1447. if (readbuffer[0] != 119)
  1448. {
  1449. printf("FAILED!\n");
  1450. #ifdef BATCH_TEST
  1451. exit(1);
  1452. #endif
  1453. while(1)
  1454. {
  1455. }
  1456. }
  1457. status = lx_nand_flash_sector_release(&nand_sim_flash, 7);
  1458. if (status != LX_SUCCESS)
  1459. {
  1460. printf("FAILED!\n");
  1461. #ifdef BATCH_TEST
  1462. exit(1);
  1463. #endif
  1464. while(1)
  1465. {
  1466. }
  1467. }
  1468. status = lx_nand_flash_sector_release(&nand_sim_flash, 8);
  1469. if (status != LX_SECTOR_NOT_FOUND)
  1470. {
  1471. printf("FAILED!\n");
  1472. #ifdef BATCH_TEST
  1473. exit(1);
  1474. #endif
  1475. while(1)
  1476. {
  1477. }
  1478. }
  1479. status = lx_nand_flash_defragment(&nand_sim_flash);
  1480. if (status != LX_SUCCESS)
  1481. {
  1482. printf("FAILED!\n");
  1483. #ifdef BATCH_TEST
  1484. exit(1);
  1485. #endif
  1486. while(1)
  1487. {
  1488. }
  1489. }
  1490. status = lx_nand_flash_close(&nand_sim_flash);
  1491. if (status != LX_SUCCESS)
  1492. {
  1493. printf("FAILED!\n");
  1494. #ifdef BATCH_TEST
  1495. exit(1);
  1496. #endif
  1497. while(1)
  1498. {
  1499. }
  1500. }
  1501. printf("SUCCESS!\n");
  1502. #endif
  1503. #if 0
  1504. printf("Test 5: Random write/read sector................");
  1505. /* Erase the simulated NOR flash. */
  1506. _lx_nand_flash_simulator_erase_all();
  1507. /* Open the flash. */
  1508. status = lx_nand_flash_open(&nand_sim_flash, "sim nor flash", _lx_nand_flash_simulator_initialize, nand_memory_space, sizeof(nand_memory_space));
  1509. #ifdef EXTENDED_CACHE
  1510. status += lx_nand_flash_extended_cache_enable(&nand_sim_flash, cache_memory, sizeof(cache_memory));
  1511. #endif
  1512. if (status != LX_SUCCESS)
  1513. {
  1514. printf("FAILED!\n");
  1515. #ifdef BATCH_TEST
  1516. exit(1);
  1517. #endif
  1518. while(1)
  1519. {
  1520. }
  1521. }
  1522. /* Write 100 sectors.... */
  1523. for (i = 0; i < 100; i++)
  1524. {
  1525. for (j = 0; j < 512; j++)
  1526. buffer[j] = i;
  1527. status = lx_nand_flash_sector_write(&nand_sim_flash, i, buffer);
  1528. if (status != LX_SUCCESS)
  1529. {
  1530. printf("FAILED!\n");
  1531. #ifdef BATCH_TEST
  1532. exit(1);
  1533. #endif
  1534. while(1)
  1535. {
  1536. }
  1537. }
  1538. }
  1539. /* Read back 100 sectors... */
  1540. for (i = 0; i < 100; i++)
  1541. {
  1542. status = lx_nand_flash_sector_read(&nand_sim_flash, i, buffer);
  1543. if (status != LX_SUCCESS)
  1544. {
  1545. printf("FAILED!\n");
  1546. #ifdef BATCH_TEST
  1547. exit(1);
  1548. #endif
  1549. while(1)
  1550. {
  1551. }
  1552. }
  1553. for (j = 0; j < 512; j++)
  1554. {
  1555. if (buffer[j] != i)
  1556. {
  1557. printf("FAILED!\n");
  1558. #ifdef BATCH_TEST
  1559. exit(1);
  1560. #endif
  1561. while(1)
  1562. {
  1563. }
  1564. }
  1565. }
  1566. }
  1567. /* Now, perform 1000 sector writes to randomly selected sectors, each time
  1568. reading first to make sure the previous contents are valid. */
  1569. for (i = 0; i < 1000; i++)
  1570. {
  1571. /* Pickup random sector. */
  1572. sector = (rand() % 100);
  1573. /* Read that sector. */
  1574. status = lx_nand_flash_sector_read(&nand_sim_flash, sector, buffer);
  1575. if (status != LX_SUCCESS)
  1576. {
  1577. printf("FAILED!\n");
  1578. #ifdef BATCH_TEST
  1579. exit(1);
  1580. #endif
  1581. while(1)
  1582. {
  1583. }
  1584. }
  1585. for (j = 0; j < 512; j++)
  1586. {
  1587. if ((buffer[j] & 0x0000FFFF) != sector)
  1588. {
  1589. printf("FAILED!\n");
  1590. #ifdef BATCH_TEST
  1591. exit(1);
  1592. #endif
  1593. while(1)
  1594. {
  1595. }
  1596. }
  1597. }
  1598. /* Include the itteraction in the buffer to generate a new write. */
  1599. for (j = 0; j < 512; j++)
  1600. {
  1601. buffer[j] = (buffer[j] & 0x0000FFFF) | (i << 16);
  1602. }
  1603. status = lx_nand_flash_sector_write(&nand_sim_flash, sector, buffer);
  1604. if (status != LX_SUCCESS)
  1605. {
  1606. printf("FAILED!\n");
  1607. #ifdef BATCH_TEST
  1608. exit(1);
  1609. #endif
  1610. while(1)
  1611. {
  1612. }
  1613. }
  1614. }
  1615. status = lx_nand_flash_close(&nand_sim_flash);
  1616. if (status != LX_SUCCESS)
  1617. {
  1618. printf("FAILED!\n");
  1619. #ifdef BATCH_TEST
  1620. exit(1);
  1621. #endif
  1622. while(1)
  1623. {
  1624. }
  1625. }
  1626. printf("SUCCESS!\n");
  1627. #endif
  1628. #if 0
  1629. for (i = 0; i < 256; i++)
  1630. {
  1631. /* Setup buffer. */
  1632. for (j = 0; j < 2048; j++)
  1633. byte_buffer[j] = (UCHAR) (rand() % 256);
  1634. /* Call the ECC calculate routine. */
  1635. ComputePageECC(byte_buffer, 2048, ecc_bytes);
  1636. status = (UINT) CorrectPageECC(byte_buffer, 2048, ecc_bytes);
  1637. if (status != 0)
  1638. {
  1639. /* Check for corrected return value. */
  1640. if (status != 1)
  1641. {
  1642. while(1)
  1643. {
  1644. }
  1645. }
  1646. }
  1647. /* Call LevelX ECC routines to do the same thing. */
  1648. status = lx_nand_flash_page_ecc_compute(&nand_sim_flash, byte_buffer, lx_ecc_buffer);
  1649. if (status != LX_SUCCESS)
  1650. {
  1651. printf("FAILED!\n");
  1652. while(1)
  1653. {
  1654. }
  1655. }
  1656. // ComputePageECC(byte_buffer, 2048, ecc_bytes);
  1657. /* Determine if there is any difference between the original and the new ECC routine. */
  1658. if ((ecc_bytes[0] != lx_ecc_buffer[0]) ||
  1659. (ecc_bytes[1] != lx_ecc_buffer[1]) ||
  1660. (ecc_bytes[2] != lx_ecc_buffer[2]) ||
  1661. (ecc_bytes[3] != lx_ecc_buffer[3]) ||
  1662. (ecc_bytes[4] != lx_ecc_buffer[4]) ||
  1663. (ecc_bytes[5] != lx_ecc_buffer[5]) ||
  1664. (ecc_bytes[6] != lx_ecc_buffer[6]) ||
  1665. (ecc_bytes[7] != lx_ecc_buffer[7]) ||
  1666. (ecc_bytes[8] != lx_ecc_buffer[8]) ||
  1667. (ecc_bytes[9] != lx_ecc_buffer[9]) ||
  1668. (ecc_bytes[10] != lx_ecc_buffer[10]) ||
  1669. (ecc_bytes[11] != lx_ecc_buffer[11]))
  1670. {
  1671. while(1)
  1672. {
  1673. }
  1674. }
  1675. status = lx_nand_flash_page_ecc_check(&nand_sim_flash, byte_buffer, lx_ecc_buffer);
  1676. if (status != LX_SUCCESS)
  1677. {
  1678. /* Is it the corrected status? */
  1679. if (status != LX_NAND_ERROR_CORRECTED)
  1680. {
  1681. printf("FAILED!\n");
  1682. while(1)
  1683. {
  1684. }
  1685. }
  1686. }
  1687. }
  1688. #endif
  1689. #if 0
  1690. for (i = 0; i < 256; i++)
  1691. {
  1692. /* Setup buffer. */
  1693. for (j = 0; j < 2048; j++)
  1694. byte_buffer[j] = (UCHAR) (rand() % 256);
  1695. /* Call the ECC calculate routine. */
  1696. ComputePageECC(byte_buffer, 2048, ecc_bytes);
  1697. /* Corrupt a bit in each 256 byte page. */
  1698. if (byte_buffer[7] & 1)
  1699. byte_buffer[7] = byte_buffer[7] & 0xFE;
  1700. else
  1701. byte_buffer[7] = byte_buffer[7] | 1;
  1702. if (byte_buffer[277] & 1)
  1703. byte_buffer[277] = byte_buffer[277] & 0xFE;
  1704. else
  1705. byte_buffer[277] = byte_buffer[277] | 1;
  1706. if (byte_buffer[577] & 1)
  1707. byte_buffer[577] = byte_buffer[577] & 0xFE;
  1708. else
  1709. byte_buffer[577] = byte_buffer[577] | 1;
  1710. if (byte_buffer[777] & 1)
  1711. byte_buffer[777] = byte_buffer[777] & 0xFE;
  1712. else
  1713. byte_buffer[777] = byte_buffer[777] | 1;
  1714. if (byte_buffer[1077] & 1)
  1715. byte_buffer[1077] = byte_buffer[1077] & 0xFE;
  1716. else
  1717. byte_buffer[1077] = byte_buffer[1077] | 1;
  1718. if (byte_buffer[1297] & 1)
  1719. byte_buffer[1297] = byte_buffer[1297] & 0xFE;
  1720. else
  1721. byte_buffer[1297] = byte_buffer[1297] | 1;
  1722. if (byte_buffer[1636] & 1)
  1723. byte_buffer[1636] = byte_buffer[1636] & 0xFE;
  1724. else
  1725. byte_buffer[1636] = byte_buffer[1636] | 1;
  1726. if (byte_buffer[1892] & 1)
  1727. byte_buffer[1892] = byte_buffer[1892] & 0xFE;
  1728. else
  1729. byte_buffer[1892] = byte_buffer[1892] | 1;
  1730. status = (UINT) CorrectPageECC(byte_buffer, 2048, ecc_bytes);
  1731. if (status != 0)
  1732. {
  1733. /* Check for corrected return value. */
  1734. if (status != 1)
  1735. {
  1736. while(1)
  1737. {
  1738. }
  1739. }
  1740. }
  1741. /* Call LevelX ECC routines to do the same thing. */
  1742. status = lx_nand_flash_page_ecc_compute(&nand_sim_flash, byte_buffer, lx_ecc_buffer);
  1743. if (status != LX_SUCCESS)
  1744. {
  1745. printf("FAILED!\n");
  1746. while(1)
  1747. {
  1748. }
  1749. }
  1750. /* Determine if there is any difference between the original and the new ECC routine. */
  1751. if ((ecc_bytes[0] != lx_ecc_buffer[0]) ||
  1752. (ecc_bytes[1] != lx_ecc_buffer[1]) ||
  1753. (ecc_bytes[2] != lx_ecc_buffer[2]) ||
  1754. (ecc_bytes[3] != lx_ecc_buffer[3]) ||
  1755. (ecc_bytes[4] != lx_ecc_buffer[4]) ||
  1756. (ecc_bytes[5] != lx_ecc_buffer[5]) ||
  1757. (ecc_bytes[6] != lx_ecc_buffer[6]) ||
  1758. (ecc_bytes[7] != lx_ecc_buffer[7]) ||
  1759. (ecc_bytes[8] != lx_ecc_buffer[8]) ||
  1760. (ecc_bytes[9] != lx_ecc_buffer[9]) ||
  1761. (ecc_bytes[10] != lx_ecc_buffer[10]) ||
  1762. (ecc_bytes[11] != lx_ecc_buffer[11]))
  1763. {
  1764. while(1)
  1765. {
  1766. }
  1767. }
  1768. /* Corrupt a bit in each 256 byte page. */
  1769. if (byte_buffer[7] & 1)
  1770. byte_buffer[7] = byte_buffer[7] & 0xFE;
  1771. else
  1772. byte_buffer[7] = byte_buffer[7] | 1;
  1773. if (byte_buffer[277] & 1)
  1774. byte_buffer[277] = byte_buffer[277] & 0xFE;
  1775. else
  1776. byte_buffer[277] = byte_buffer[277] | 1;
  1777. if (byte_buffer[577] & 1)
  1778. byte_buffer[577] = byte_buffer[577] & 0xFE;
  1779. else
  1780. byte_buffer[577] = byte_buffer[577] | 1;
  1781. if (byte_buffer[777] & 1)
  1782. byte_buffer[777] = byte_buffer[777] & 0xFE;
  1783. else
  1784. byte_buffer[777] = byte_buffer[777] | 1;
  1785. if (byte_buffer[1077] & 1)
  1786. byte_buffer[1077] = byte_buffer[1077] & 0xFE;
  1787. else
  1788. byte_buffer[1077] = byte_buffer[1077] | 1;
  1789. if (byte_buffer[1297] & 1)
  1790. byte_buffer[1297] = byte_buffer[1297] & 0xFE;
  1791. else
  1792. byte_buffer[1297] = byte_buffer[1297] | 1;
  1793. if (byte_buffer[1636] & 1)
  1794. byte_buffer[1636] = byte_buffer[1636] & 0xFE;
  1795. else
  1796. byte_buffer[1636] = byte_buffer[1636] | 1;
  1797. if (byte_buffer[1892] & 1)
  1798. byte_buffer[1892] = byte_buffer[1892] & 0xFE;
  1799. else
  1800. byte_buffer[1892] = byte_buffer[1892] | 1;
  1801. status = lx_nand_flash_page_ecc_check(&nand_sim_flash, byte_buffer, lx_ecc_buffer);
  1802. if (status != LX_SUCCESS)
  1803. {
  1804. /* Is it the corrected status? */
  1805. if (status != LX_NAND_ERROR_CORRECTED)
  1806. {
  1807. printf("FAILED!\n");
  1808. #ifdef BATCH_TEST
  1809. exit(1);
  1810. #endif
  1811. while(1)
  1812. {
  1813. }
  1814. }
  1815. }
  1816. /* Call LevelX ECC routines to create the ECC over the corrected buffer. */
  1817. status = lx_nand_flash_page_ecc_compute(&nand_sim_flash, byte_buffer, lx_ecc_buffer);
  1818. if (status != LX_SUCCESS)
  1819. {
  1820. printf("FAILED!\n");
  1821. #ifdef BATCH_TEST
  1822. exit(1);
  1823. #endif
  1824. while(1)
  1825. {
  1826. }
  1827. }
  1828. }
  1829. #endif
  1830. #if 0
  1831. /* Corrupting ECC code is detected and returns a failure... this test is not valid! */
  1832. for (i = 0; i < 256; i++)
  1833. {
  1834. /* Setup buffer. */
  1835. for (j = 0; j < 2048; j++)
  1836. byte_buffer[j] = (UCHAR) (rand() % 256);
  1837. /* Call the ECC calculate routine. */
  1838. ComputePageECC(byte_buffer, 2048, ecc_bytes);
  1839. /* Call LevelX ECC routines to do the same thing. */
  1840. status = lx_nand_flash_page_ecc_compute(&nand_sim_flash, byte_buffer, lx_ecc_buffer);
  1841. if (status != LX_SUCCESS)
  1842. {
  1843. printf("FAILED!\n");
  1844. while(1)
  1845. {
  1846. }
  1847. }
  1848. /* Determine if there is any difference between the original and the new ECC routine. */
  1849. if ((ecc_bytes[0] != lx_ecc_buffer[0]) ||
  1850. (ecc_bytes[1] != lx_ecc_buffer[1]) ||
  1851. (ecc_bytes[2] != lx_ecc_buffer[2]) ||
  1852. (ecc_bytes[3] != lx_ecc_buffer[3]) ||
  1853. (ecc_bytes[4] != lx_ecc_buffer[4]) ||
  1854. (ecc_bytes[5] != lx_ecc_buffer[5]) ||
  1855. (ecc_bytes[6] != lx_ecc_buffer[6]) ||
  1856. (ecc_bytes[7] != lx_ecc_buffer[7]) ||
  1857. (ecc_bytes[8] != lx_ecc_buffer[8]) ||
  1858. (ecc_bytes[9] != lx_ecc_buffer[9]) ||
  1859. (ecc_bytes[10] != lx_ecc_buffer[10]) ||
  1860. (ecc_bytes[11] != lx_ecc_buffer[11]))
  1861. {
  1862. while(1)
  1863. {
  1864. }
  1865. }
  1866. /* Corrupt a bit in each ECC for 256 byte page. */
  1867. if (ecc_bytes[1] & 1)
  1868. ecc_bytes[1] = ecc_bytes[1] & 0xFE;
  1869. else
  1870. ecc_bytes[1] = ecc_bytes[1] | 1;
  1871. if (ecc_bytes[4] & 1)
  1872. ecc_bytes[4] = ecc_bytes[4] & 0xFE;
  1873. else
  1874. ecc_bytes[4] = ecc_bytes[4] | 1;
  1875. if (ecc_bytes[7] & 1)
  1876. ecc_bytes[7] = ecc_bytes[7] & 0xFE;
  1877. else
  1878. ecc_bytes[7] = ecc_bytes[7] | 1;
  1879. if (ecc_bytes[10] & 1)
  1880. ecc_bytes[10] = ecc_bytes[10] & 0xFE;
  1881. else
  1882. ecc_bytes[10] = ecc_bytes[10] | 1;
  1883. if (ecc_bytes[13] & 1)
  1884. ecc_bytes[13] = ecc_bytes[13] & 0xFE;
  1885. else
  1886. ecc_bytes[13] = ecc_bytes[13] | 1;
  1887. if (ecc_bytes[16] & 1)
  1888. ecc_bytes[16] = ecc_bytes[16] & 0xFE;
  1889. else
  1890. ecc_bytes[16] = ecc_bytes[16] | 1;
  1891. if (ecc_bytes[19] & 1)
  1892. ecc_bytes[19] = ecc_bytes[19] & 0xFE;
  1893. else
  1894. ecc_bytes[19] = ecc_bytes[19] | 1;
  1895. if (ecc_bytes[22] & 1)
  1896. ecc_bytes[22] = ecc_bytes[22] & 0xFE;
  1897. else
  1898. ecc_bytes[22] = ecc_bytes[22] | 1;
  1899. status = (UINT) CorrectPageECC(byte_buffer, 2048, ecc_bytes);
  1900. if (status != 0)
  1901. {
  1902. /* Check for corrected return value. */
  1903. if (status != 1)
  1904. {
  1905. while(1)
  1906. {
  1907. }
  1908. }
  1909. }
  1910. /* Corrupt a bit in each ECC for 256 byte page. */
  1911. if (lx_ecc_buffer[1] & 1)
  1912. lx_ecc_buffer[1] = lx_ecc_buffer[1] & 0xFE;
  1913. else
  1914. lx_ecc_buffer[1] = lx_ecc_buffer[1] | 1;
  1915. if (lx_ecc_buffer[4] & 1)
  1916. lx_ecc_buffer[4] = lx_ecc_buffer[4] & 0xFE;
  1917. else
  1918. lx_ecc_buffer[4] = lx_ecc_buffer[4] | 1;
  1919. if (lx_ecc_buffer[7] & 1)
  1920. lx_ecc_buffer[7] = lx_ecc_buffer[7] & 0xFE;
  1921. else
  1922. lx_ecc_buffer[7] = lx_ecc_buffer[7] | 1;
  1923. if (lx_ecc_buffer[10] & 1)
  1924. lx_ecc_buffer[10] = lx_ecc_buffer[10] & 0xFE;
  1925. else
  1926. lx_ecc_buffer[10] = lx_ecc_buffer[10] | 1;
  1927. if (lx_ecc_buffer[13] & 1)
  1928. lx_ecc_buffer[13] = lx_ecc_buffer[13] & 0xFE;
  1929. else
  1930. lx_ecc_buffer[13] = lx_ecc_buffer[13] | 1;
  1931. if (lx_ecc_buffer[16] & 1)
  1932. lx_ecc_buffer[16] = lx_ecc_buffer[16] & 0xFE;
  1933. else
  1934. lx_ecc_buffer[16] = lx_ecc_buffer[16] | 1;
  1935. if (lx_ecc_buffer[19] & 1)
  1936. lx_ecc_buffer[19] = lx_ecc_buffer[19] & 0xFE;
  1937. else
  1938. lx_ecc_buffer[19] = lx_ecc_buffer[19] | 1;
  1939. if (lx_ecc_buffer[22] & 1)
  1940. lx_ecc_buffer[22] = lx_ecc_buffer[22] & 0xFE;
  1941. else
  1942. lx_ecc_buffer[22] = lx_ecc_buffer[22] | 1;
  1943. status = lx_nand_flash_page_ecc_check(&nand_sim_flash, byte_buffer, lx_ecc_buffer);
  1944. if (status != LX_SUCCESS)
  1945. {
  1946. /* Is it the corrected status? */
  1947. if (status != LX_NAND_ERROR_CORRECTED)
  1948. {
  1949. printf("FAILED!\n");
  1950. #ifdef BATCH_TEST
  1951. exit(1);
  1952. #endif
  1953. while(1)
  1954. {
  1955. }
  1956. }
  1957. }
  1958. /* Call LevelX ECC routines to create the ECC over the corrected buffer. */
  1959. status = lx_nand_flash_page_ecc_compute(&nand_sim_flash, byte_buffer, lx_ecc_buffer);
  1960. if (status != LX_SUCCESS)
  1961. {
  1962. printf("FAILED!\n");
  1963. #ifdef BATCH_TEST
  1964. exit(1);
  1965. #endif
  1966. while(1)
  1967. {
  1968. }
  1969. }
  1970. }
  1971. #endif
  1972. #if 0
  1973. /* TEST FileX packing routines. */
  1974. /* Erase the simulated NAND flash. */
  1975. _lx_nand_flash_simulator_erase_all();
  1976. /* Open the flash. */
  1977. status = lx_nand_flash_open(&nand_sim_flash, "sim nor flash", _lx_nand_flash_simulator_initialize);
  1978. #ifdef EXTENDED_CACHE
  1979. status += lx_nand_flash_extended_cache_enable(&nand_sim_flash, cache_memory, sizeof(cache_memory));
  1980. #endif
  1981. if (status != LX_SUCCESS)
  1982. {
  1983. printf("FAILED!\n");
  1984. while(1)
  1985. {
  1986. }
  1987. }
  1988. /* Setup the write data. */
  1989. for (i = 0; i < 8; i++)
  1990. {
  1991. for (j = 0; j < 512/4; j++)
  1992. test_write[i].words[j] = i;
  1993. }
  1994. /* Now write the data one logical sector at a time. */
  1995. for (i = 0; i < 8; i++)
  1996. {
  1997. _fx_nand_flash_write_sectors(i, 1, (UCHAR *) &test_write[i]);
  1998. }
  1999. /* Setup the read buffer. */
  2000. for (i = 0; i < 8; i++)
  2001. {
  2002. for (j = 0; j < 512/4; j++)
  2003. test_read[i].words[j] = 0xEFEFEFEF;
  2004. }
  2005. /* Read back in one sector at a time. */
  2006. for (i = 0; i < 8; i++)
  2007. {
  2008. _fx_nand_flash_read_sectors(i, 1, (UCHAR *) &test_read[i].words[0]);
  2009. }
  2010. /* Setup the read buffer. */
  2011. for (i = 0; i < 8; i++)
  2012. {
  2013. for (j = 0; j < 512/4; j++)
  2014. test_read[i].words[j] = 0xEFEFEFEF;
  2015. }
  2016. _fx_nand_flash_read_sectors(3, 5, (UCHAR *) &test_read[3].words[0]);
  2017. _fx_nand_flash_read_sectors(1, 2, (UCHAR *) &test_read[1].words[0]);
  2018. _fx_nand_flash_read_sectors(0, 1, (UCHAR *) &test_read[0].words[0]);
  2019. /* Setup the read buffer. */
  2020. for (i = 0; i < 8; i++)
  2021. {
  2022. for (j = 0; j < 512/4; j++)
  2023. test_read[i].words[j] = 0xEFEFEFEF;
  2024. }
  2025. _fx_nand_flash_read_sectors(0, 8, (UCHAR *) &test_read[0].words[0]);
  2026. status = lx_nand_flash_close(&nand_sim_flash);
  2027. if (status != LX_SUCCESS)
  2028. {
  2029. printf("FAILED!\n");
  2030. while(1)
  2031. {
  2032. }
  2033. }
  2034. /* Erase the simulated NAND flash. */
  2035. _lx_nand_flash_simulator_erase_all();
  2036. /* Open the flash. */
  2037. status = lx_nand_flash_open(&nand_sim_flash, "sim nor flash", _lx_nand_flash_simulator_initialize);
  2038. #ifdef EXTENDED_CACHE
  2039. status += lx_nand_flash_extended_cache_enable(&nand_sim_flash, cache_memory, sizeof(cache_memory));
  2040. #endif
  2041. if (status != LX_SUCCESS)
  2042. {
  2043. printf("FAILED!\n");
  2044. while(1)
  2045. {
  2046. }
  2047. }
  2048. _fx_nand_flash_write_sectors(3, 5, (UCHAR *) &test_read[3].words[0]);
  2049. _fx_nand_flash_write_sectors(1, 2, (UCHAR *) &test_read[1].words[0]);
  2050. _fx_nand_flash_write_sectors(0, 1, (UCHAR *) &test_read[0].words[0]);
  2051. /* Setup the read buffer. */
  2052. for (i = 0; i < 8; i++)
  2053. {
  2054. for (j = 0; j < 512/4; j++)
  2055. test_read[i].words[j] = 0xEFEFEFEF;
  2056. }
  2057. _fx_nand_flash_read_sectors(0, 1, (UCHAR *) &test_read[0].words[0]);
  2058. _fx_nand_flash_read_sectors(1, 2, (UCHAR *) &test_read[1].words[0]);
  2059. _fx_nand_flash_read_sectors(3, 5, (UCHAR *) &test_read[3].words[0]);
  2060. status = lx_nand_flash_close(&nand_sim_flash);
  2061. if (status != LX_SUCCESS)
  2062. {
  2063. printf("FAILED!\n");
  2064. while(1)
  2065. {
  2066. }
  2067. }
  2068. /* End */
  2069. #endif
  2070. #ifdef BATCH_TEST
  2071. exit(0);
  2072. #endif
  2073. /* All done! */
  2074. while(1)
  2075. {
  2076. }
  2077. }