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