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 UCHAR   nor_cache_memory[2048+16+8];
22 UCHAR   nor_cache_memory2[8192];
23 
24 
25 /* Define LevelX NOR flash simulator prototoypes.  */
26 
27 UINT  _lx_nor_flash_simulator_erase_all(VOID);
28 UINT  _lx_nor_flash_simulator_initialize(LX_NOR_FLASH *nor_flash);
29 
30 
31 
32 /* Define thread prototypes.  */
33 
34 void    thread_0_entry(ULONG thread_input);
35 
36 
37 
38 /* Define main entry point.  */
39 
main()40 int main()
41 {
42 
43     /* Enter the ThreadX kernel.  */
44 #ifndef LX_STANDALONE_ENABLE
45     tx_kernel_enter();
46 #else
47     thread_0_entry(0);
48 #endif
49 
50 }
51 
52 
53 /* Define what the initial system looks like.  */
54 #ifndef LX_STANDALONE_ENABLE
tx_application_define(void * first_unused_memory)55 void    tx_application_define(void *first_unused_memory)
56 {
57 
58 
59     /* Create the main thread.  */
60     tx_thread_create(&thread_0, "thread 0", thread_0_entry, 0,
61             thread_0_stack, DEMO_STACK_SIZE,
62             1, 1, TX_NO_TIME_SLICE, TX_AUTO_START);
63 }
64 #endif
65 
66 /* Define the test threads.  */
67 
thread_0_entry(ULONG thread_input)68 void    thread_0_entry(ULONG thread_input)
69 {
70 
71 ULONG   i, j, sector;
72 UINT    status;
73 
74 ULONG   *word_ptr;
75 
76 
77     /* Erase the simulated NOR flash.  */
78     _lx_nor_flash_simulator_erase_all();
79 
80     /* Initialize LevelX.  */
81     _lx_nor_flash_initialize();
82 
83     /* Test 1: Simple write 100 sectors and read 100 sectors.  */
84     printf("Test 1: Simple write-read 100 sectors...........");
85 
86     lx_nor_flash_open(&nor_sim_flash, "sim nor flash", _lx_nor_flash_simulator_initialize);
87 #ifndef LX_NOR_DISABLE_EXTENDED_CACHE
88 lx_nor_flash_extended_cache_enable(&nor_sim_flash, nor_cache_memory, sizeof(nor_cache_memory));
89 #endif
90 #ifdef LX_NOR_ENABLE_MAPPING_BITMAP
91     if (nor_sim_flash.lx_nor_flash_extended_cache_mapping_bitmap_max_logical_sector != 128)
92     {
93           printf("FAILED!\n");
94 #ifdef BATCH_TEST
95     exit(1);
96 #endif
97           while(1)
98           {
99           }
100     }
101 #endif
102 #ifdef LX_NOR_ENABLE_OBSOLETE_COUNT_CACHE
103 
104     if (nor_sim_flash.lx_nor_flash_extended_cache_obsolete_count_max_block != 8)
105     {
106           printf("FAILED!\n");
107 #ifdef BATCH_TEST
108     exit(1);
109 #endif
110           while(1)
111           {
112           }
113     }
114 #endif
115 
116     /* Write 100 sectors....  */
117     for (i = 0; i < 100; i++)
118     {
119         for (j = 0; j < 128; j++)
120           buffer[j] =  i;
121 
122         status =  lx_nor_flash_sector_write(&nor_sim_flash, i, buffer);
123 
124         if (status != LX_SUCCESS)
125         {
126           printf("FAILED!\n");
127 #ifdef BATCH_TEST
128     exit(1);
129 #endif
130           while(1)
131           {
132           }
133         }
134     }
135 
136     /* Read back 100 sectors...  */
137     for (i = 0; i < 100; i++)
138     {
139 
140         status =  lx_nor_flash_sector_read(&nor_sim_flash, i, buffer);
141 
142         if (status != LX_SUCCESS)
143         {
144           printf("FAILED!\n");
145 #ifdef BATCH_TEST
146     exit(1);
147 #endif
148           while(1)
149           {
150           }
151         }
152 
153         for (j = 0; j < 128; j++)
154         {
155 
156           if (buffer[j] !=  i)
157           {
158             printf("FAILED!\n");
159 #ifdef BATCH_TEST
160     exit(1);
161 #endif
162             while(1)
163             {
164             }
165           }
166         }
167     }
168 #ifdef LX_NOR_ENABLE_MAPPING_BITMAP
169     for (i = 0; i < 100; i++)
170     {
171         if (nor_sim_flash.lx_nor_flash_extended_cache_mapping_bitmap[i>>5] & (ULONG)(1<<(i & 31)) == 0)
172         {
173             printf("FAILED!\n");
174 #ifdef BATCH_TEST
175     exit(1);
176 #endif
177             while(1)
178             {
179             }
180         }
181     }
182 #endif
183 
184 #ifdef LX_NOR_ENABLE_OBSOLETE_COUNT_CACHE
185     for (i = 0; i < 8; i++)
186     {
187         if(nor_sim_flash.lx_nor_flash_extended_cache_obsolete_count[i] != 0)
188         {
189             printf("FAILED!\n");
190 #ifdef BATCH_TEST
191     exit(1);
192 #endif
193             while(1)
194             {
195             }
196         }
197 
198     }
199 #endif
200 
201     /* Release 100 sectors...  */
202     for (i = 0; i < 100; i++)
203     {
204 
205         status =  lx_nor_flash_sector_release(&nor_sim_flash, i);
206 
207         if (status != LX_SUCCESS)
208         {
209           printf("FAILED!\n");
210 #ifdef BATCH_TEST
211     exit(1);
212 #endif
213           while(1)
214           {
215           }
216         }
217     }
218 
219 #ifdef LX_NOR_ENABLE_MAPPING_BITMAP
220     for (i = 0; i < 100; i++)
221     {
222         if (nor_sim_flash.lx_nor_flash_extended_cache_mapping_bitmap[i>>5] & (ULONG)(1<<(i & 31)) != 0)
223         {
224             printf("FAILED!\n");
225 #ifdef BATCH_TEST
226     exit(1);
227 #endif
228             while(1)
229             {
230             }
231         }
232     }
233 #endif
234 
235 #ifdef LX_NOR_ENABLE_OBSOLETE_COUNT_CACHE
236     for (i = 0; i < 6; i++)
237     {
238         if(nor_sim_flash.lx_nor_flash_extended_cache_obsolete_count[i] != 15)
239         {
240             printf("FAILED!\n");
241 #ifdef BATCH_TEST
242     exit(1);
243 #endif
244             while(1)
245             {
246             }
247         }
248 
249     }
250     if (nor_sim_flash.lx_nor_flash_extended_cache_obsolete_count[6] != 10 || nor_sim_flash.lx_nor_flash_extended_cache_obsolete_count[7] != 0)
251     {
252         printf("FAILED!\n");
253 #ifdef BATCH_TEST
254     exit(1);
255 #endif
256         while(1)
257         {
258         }
259     }
260 #endif
261 
262 
263     _lx_nor_flash_close(&nor_sim_flash);
264     printf("SUCCESS!\n");
265 
266     /* Test 2: Write same sector 120 times.  */
267     printf("Test 2: Write same sector 120 times.............");
268 
269     /* Reinitialize...  */
270     _lx_nor_flash_simulator_erase_all();
271 
272 
273     lx_nor_flash_initialize();
274     lx_nor_flash_open(&nor_sim_flash, "sim nor flash", _lx_nor_flash_simulator_initialize);
275 #ifndef LX_NOR_DISABLE_EXTENDED_CACHE
276 lx_nor_flash_extended_cache_enable(&nor_sim_flash, nor_cache_memory, sizeof(nor_cache_memory));
277 #endif
278 
279     for (j = 0; j < 128; j++)
280          buffer[j] =  0xFFFFFFFF;
281 
282     /* Write same sector 120 sectors....  */
283     for (i = 0; i < 120; i++)
284     {
285         for (j = 0; j < 128; j++)
286           buffer[j] =  i;
287 
288         status =  lx_nor_flash_sector_write(&nor_sim_flash, 7, buffer);
289 
290         if (status != LX_SUCCESS)
291         {
292           printf("FAILED!\n");
293 #ifdef BATCH_TEST
294     exit(1);
295 #endif
296           while(1)
297           {
298           }
299         }
300 
301         status =  lx_nor_flash_sector_read(&nor_sim_flash, 7, readbuffer);
302 
303         if (status != LX_SUCCESS)
304         {
305           printf("FAILED!\n");
306 #ifdef BATCH_TEST
307     exit(1);
308 #endif
309           while(1)
310           {
311           }
312         }
313 
314         for (j = 0; j < 128; j++)
315         {
316 
317           if (buffer[j] !=  readbuffer[j])
318           {
319             printf("FAILED!\n");
320 #ifdef BATCH_TEST
321     exit(1);
322 #endif
323             while(1)
324             {
325             }
326           }
327         }
328 
329         /* Write other sectors just to have additional sectors to manage.  */
330         if (i == 1)
331           lx_nor_flash_sector_write(&nor_sim_flash, 1, buffer);
332         if (i == 16)
333           lx_nor_flash_sector_write(&nor_sim_flash, 16, buffer);
334         if (i == 32)
335           lx_nor_flash_sector_write(&nor_sim_flash, 32, buffer);
336         if (i == 48)
337           lx_nor_flash_sector_write(&nor_sim_flash, 48, buffer);
338         if (i == 64)
339           lx_nor_flash_sector_write(&nor_sim_flash, 64, buffer);
340         if (i == 80)
341           lx_nor_flash_sector_write(&nor_sim_flash, 80, buffer);
342         if (i == 96)
343           lx_nor_flash_sector_write(&nor_sim_flash, 96, buffer);
344         if (i == 112)
345           lx_nor_flash_sector_write(&nor_sim_flash, 112, buffer);
346     }
347 
348     status =  lx_nor_flash_defragment(&nor_sim_flash);
349 
350 
351     status =  lx_nor_flash_sector_read(&nor_sim_flash, 7, readbuffer);
352         if (status != LX_SUCCESS)
353         {
354           printf("FAILED!\n");
355 #ifdef BATCH_TEST
356     exit(1);
357 #endif
358           while(1)
359           {
360           }
361         }
362         if (readbuffer[0] != 119)
363         {
364           printf("FAILED!\n");
365 #ifdef BATCH_TEST
366     exit(1);
367 #endif
368           while(1)
369           {
370           }
371         }
372     status =  lx_nor_flash_sector_read(&nor_sim_flash, 1, readbuffer);
373         if (status != LX_SUCCESS)
374         {
375           printf("FAILED!\n");
376 #ifdef BATCH_TEST
377     exit(1);
378 #endif
379           while(1)
380           {
381           }
382         }
383         if (readbuffer[0] != 1)
384         {
385           printf("FAILED!\n");
386 #ifdef BATCH_TEST
387     exit(1);
388 #endif
389           while(1)
390           {
391           }
392         }
393      status =  lx_nor_flash_sector_read(&nor_sim_flash, 16, readbuffer);
394         if (status != LX_SUCCESS)
395         {
396           printf("FAILED!\n");
397 #ifdef BATCH_TEST
398     exit(1);
399 #endif
400           while(1)
401           {
402           }
403         }
404         if (readbuffer[0] != 16)
405         {
406           printf("FAILED!\n");
407 #ifdef BATCH_TEST
408     exit(1);
409 #endif
410           while(1)
411           {
412           }
413         }
414     status =  lx_nor_flash_sector_read(&nor_sim_flash, 32, readbuffer);
415         if (status != LX_SUCCESS)
416         {
417           printf("FAILED!\n");
418 #ifdef BATCH_TEST
419     exit(1);
420 #endif
421           while(1)
422           {
423           }
424         }
425         if (readbuffer[0] != 32)
426         {
427           printf("FAILED!\n");
428 #ifdef BATCH_TEST
429     exit(1);
430 #endif
431           while(1)
432           {
433           }
434         }
435     status =  lx_nor_flash_sector_read(&nor_sim_flash, 48, readbuffer);
436         if (status != LX_SUCCESS)
437         {
438           printf("FAILED!\n");
439 #ifdef BATCH_TEST
440     exit(1);
441 #endif
442           while(1)
443           {
444           }
445         }
446         if (readbuffer[0] != 48)
447         {
448           printf("FAILED!\n");
449 #ifdef BATCH_TEST
450     exit(1);
451 #endif
452           while(1)
453           {
454           }
455         }
456     status =  lx_nor_flash_sector_read(&nor_sim_flash, 64, readbuffer);
457         if (status != LX_SUCCESS)
458         {
459           printf("FAILED!\n");
460 #ifdef BATCH_TEST
461     exit(1);
462 #endif
463           while(1)
464           {
465           }
466         }
467         if (readbuffer[0] != 64)
468         {
469           printf("FAILED!\n");
470 #ifdef BATCH_TEST
471     exit(1);
472 #endif
473           while(1)
474           {
475           }
476         }
477     status =  lx_nor_flash_sector_read(&nor_sim_flash, 80, readbuffer);
478         if (status != LX_SUCCESS)
479         {
480           printf("FAILED!\n");
481 #ifdef BATCH_TEST
482     exit(1);
483 #endif
484           while(1)
485           {
486           }
487         }
488         if (readbuffer[0] != 80)
489         {
490           printf("FAILED!\n");
491 #ifdef BATCH_TEST
492     exit(1);
493 #endif
494           while(1)
495           {
496           }
497         }
498     status =  lx_nor_flash_sector_read(&nor_sim_flash, 96, readbuffer);
499         if (status != LX_SUCCESS)
500         {
501           printf("FAILED!\n");
502 #ifdef BATCH_TEST
503     exit(1);
504 #endif
505           while(1)
506           {
507           }
508         }
509         if (readbuffer[0] != 96)
510         {
511           printf("FAILED!\n");
512 #ifdef BATCH_TEST
513     exit(1);
514 #endif
515           while(1)
516           {
517           }
518         }
519     status =  lx_nor_flash_sector_read(&nor_sim_flash, 112, readbuffer);
520         if (status != LX_SUCCESS)
521         {
522           printf("FAILED!\n");
523 #ifdef BATCH_TEST
524     exit(1);
525 #endif
526           while(1)
527           {
528           }
529         }
530         if (readbuffer[0] != 112)
531         {
532           printf("FAILED!\n");
533 #ifdef BATCH_TEST
534     exit(1);
535 #endif
536           while(1)
537           {
538           }
539         }
540 
541     status =  lx_nor_flash_defragment(&nor_sim_flash);
542 
543     if (status != LX_SUCCESS)
544     {
545           printf("FAILED!\n");
546 #ifdef BATCH_TEST
547     exit(1);
548 #endif
549           while(1)
550           {
551           }
552     }
553 
554 
555     /* Point at the simulated NOR flash memory.  */
556     word_ptr =  nor_sim_flash.lx_nor_flash_base_address;
557 
558     status =  lx_nor_flash_close(&nor_sim_flash);
559 
560     if (status != LX_SUCCESS)
561     {
562           printf("FAILED!\n");
563 #ifdef BATCH_TEST
564     exit(1);
565 #endif
566           while(1)
567           {
568           }
569     }
570     printf("SUCCESS!\n");
571 
572     /* Test 3: Corrupt block 0, simulate a power interruption during erase of block 0,
573        after the erase, but before the free bit map and erase count is setup.  */
574     printf("Test 3: Block erase-initialize interrupted......");
575     word_ptr[0] =  0xFFFFFFFF;
576     word_ptr[3] =  0xFFFFFFFF;
577 
578     /* Open the flash and see if we recover properly.  */
579     status =  lx_nor_flash_open(&nor_sim_flash, "sim nor flash", _lx_nor_flash_simulator_initialize);
580 #ifndef LX_NOR_DISABLE_EXTENDED_CACHE
581 status += lx_nor_flash_extended_cache_enable(&nor_sim_flash, nor_cache_memory, sizeof(nor_cache_memory));
582 #endif
583 
584     if ((status != LX_SUCCESS) ||
585         (nor_sim_flash.lx_nor_flash_free_physical_sectors != 111) ||
586         (nor_sim_flash.lx_nor_flash_mapped_physical_sectors != 9))
587     {
588 
589           printf("FAILED!\n");
590 #ifdef BATCH_TEST
591     exit(1);
592 #endif
593           while(1)
594           {
595           }
596     }
597 
598     status =  lx_nor_flash_close(&nor_sim_flash);
599 
600     if (status != LX_SUCCESS)
601     {
602           printf("FAILED!\n");
603 #ifdef BATCH_TEST
604     exit(1);
605 #endif
606           while(1)
607           {
608           }
609     }
610 
611     /* Corrupt block 0, simulate a power interruption during erase of block 0,
612        after the erase, and after the free bit map setup, but before erase count is setup.  */
613     word_ptr[0] =  0xFFFFFFFF;
614 
615     /* Open the flash and see if we recover properly.  */
616     status =  lx_nor_flash_open(&nor_sim_flash, "sim nor flash", _lx_nor_flash_simulator_initialize);
617 #ifndef LX_NOR_DISABLE_EXTENDED_CACHE
618 status += lx_nor_flash_extended_cache_enable(&nor_sim_flash, nor_cache_memory, sizeof(nor_cache_memory));
619 #endif
620 
621     if ((status != LX_SUCCESS) ||
622         (nor_sim_flash.lx_nor_flash_free_physical_sectors != 111) ||
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_close(&nor_sim_flash);
636 
637     if (status != LX_SUCCESS)
638     {
639           printf("FAILED!\n");
640 #ifdef BATCH_TEST
641     exit(1);
642 #endif
643           while(1)
644           {
645           }
646     }
647     printf("SUCCESS!\n");
648 
649     /* Test 4: simulate a power interruption after a new block is allocated but before
650        anything else can be done.  */
651     printf("Test 4: Power interrupted new block allocation..");
652     word_ptr[3] =  word_ptr[3] & ~((ULONG) 1);
653 
654     /* Open the flash and see if we recover properly.  */
655     status =  lx_nor_flash_open(&nor_sim_flash, "sim nor flash", _lx_nor_flash_simulator_initialize);
656 #ifndef LX_NOR_DISABLE_EXTENDED_CACHE
657 status += lx_nor_flash_extended_cache_enable(&nor_sim_flash, nor_cache_memory, sizeof(nor_cache_memory));
658 #endif
659 
660     if ((status != LX_SUCCESS) ||
661         (nor_sim_flash.lx_nor_flash_free_physical_sectors != 110) ||
662         (nor_sim_flash.lx_nor_flash_mapped_physical_sectors != 9))
663     {
664 
665           printf("FAILED!\n");
666 #ifdef BATCH_TEST
667     exit(1);
668 #endif
669           while(1)
670           {
671           }
672     }
673 
674     status =  lx_nor_flash_close(&nor_sim_flash);
675 
676     if (status != LX_SUCCESS)
677     {
678           printf("FAILED!\n");
679 #ifdef BATCH_TEST
680     exit(1);
681 #endif
682           while(1)
683           {
684           }
685     }
686 
687     /* Simulate a power interruption after a new block is allocated but before
688        anything else can be done.  */
689     word_ptr[(16*128)+3] =  0x7C00;
690 
691     /* Open the flash and see if we recover properly.  */
692     status =  lx_nor_flash_open(&nor_sim_flash, "sim nor flash", _lx_nor_flash_simulator_initialize);
693 #ifndef LX_NOR_DISABLE_EXTENDED_CACHE
694 status += lx_nor_flash_extended_cache_enable(&nor_sim_flash, nor_cache_memory, sizeof(nor_cache_memory));
695 #endif
696 
697     if ((status != LX_SUCCESS) ||
698         (nor_sim_flash.lx_nor_flash_free_physical_sectors != 109) ||
699         (nor_sim_flash.lx_nor_flash_mapped_physical_sectors != 9))
700     {
701 
702           printf("FAILED!\n");
703 #ifdef BATCH_TEST
704     exit(1);
705 #endif
706           while(1)
707           {
708           }
709     }
710 
711     status =  lx_nor_flash_close(&nor_sim_flash);
712 
713     if (status != LX_SUCCESS)
714     {
715           printf("FAILED!\n");
716 #ifdef BATCH_TEST
717     exit(1);
718 #endif
719           while(1)
720           {
721           }
722     }
723 
724     /* Simulate a power interruption after a new sector is allocated, after data
725        had been copied, and the superceeded bit is clear, but before the new entry can be
726        setup.  */
727     word_ptr[3] =  0x7FFC;
728     word_ptr[(16*128)+6] =  word_ptr[(16*128)+6] & ~((ULONG) 0x40000000);
729 
730     /* Open the flash and see if we recover properly.  */
731     status =  lx_nor_flash_open(&nor_sim_flash, "sim nor flash", _lx_nor_flash_simulator_initialize);
732 #ifndef LX_NOR_DISABLE_EXTENDED_CACHE
733 status += lx_nor_flash_extended_cache_enable(&nor_sim_flash, nor_cache_memory, sizeof(nor_cache_memory));
734 #endif
735 
736     if ((status != LX_SUCCESS) ||
737         (nor_sim_flash.lx_nor_flash_free_physical_sectors != 108) ||
738         (nor_sim_flash.lx_nor_flash_mapped_physical_sectors != 9))
739     {
740 
741           printf("FAILED!\n");
742 #ifdef BATCH_TEST
743     exit(1);
744 #endif
745           while(1)
746           {
747           }
748     }
749 
750     lx_nor_flash_close(&nor_sim_flash);
751 
752     /* Simulate a power interruption after a new sector is allocated, after data
753        had been copied, and the superceeded bit is clear, the new entry is setup, but the old entry
754        has not been invalidated.  */
755     word_ptr[3] =  0x7FF8;
756     word_ptr[6] =  0xC0000070;
757     for (i = 0; i < 128; i++)
758     {
759         word_ptr[(3*128)+i] =  0x70;
760     }
761 
762     /* Open the flash and see if we recover properly.  */
763     status =  lx_nor_flash_open(&nor_sim_flash, "sim nor flash", _lx_nor_flash_simulator_initialize);
764 #ifndef LX_NOR_DISABLE_EXTENDED_CACHE
765 status += lx_nor_flash_extended_cache_enable(&nor_sim_flash, nor_cache_memory, sizeof(nor_cache_memory));
766 #endif
767 
768     if ((status != LX_SUCCESS) ||
769         (nor_sim_flash.lx_nor_flash_free_physical_sectors != 107) ||
770         (nor_sim_flash.lx_nor_flash_mapped_physical_sectors != 9))
771     {
772 
773           printf("FAILED!\n");
774 #ifdef BATCH_TEST
775     exit(1);
776 #endif
777           while(1)
778           {
779           }
780     }
781 
782     status =  lx_nor_flash_sector_read(&nor_sim_flash, 7, 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] != 119)
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, 1, 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] != 1)
815         {
816           printf("FAILED!\n");
817 #ifdef BATCH_TEST
818     exit(1);
819 #endif
820           while(1)
821           {
822           }
823         }
824      status =  lx_nor_flash_sector_read(&nor_sim_flash, 16, readbuffer);
825         if (status != LX_SUCCESS)
826         {
827           printf("FAILED!\n");
828 #ifdef BATCH_TEST
829     exit(1);
830 #endif
831           while(1)
832           {
833           }
834         }
835         if (readbuffer[0] != 16)
836         {
837           printf("FAILED!\n");
838 #ifdef BATCH_TEST
839     exit(1);
840 #endif
841           while(1)
842           {
843           }
844         }
845     status =  lx_nor_flash_sector_read(&nor_sim_flash, 32, readbuffer);
846         if (status != LX_SUCCESS)
847         {
848           printf("FAILED!\n");
849 #ifdef BATCH_TEST
850     exit(1);
851 #endif
852           while(1)
853           {
854           }
855         }
856         if (readbuffer[0] != 32)
857         {
858           printf("FAILED!\n");
859 #ifdef BATCH_TEST
860     exit(1);
861 #endif
862           while(1)
863           {
864           }
865         }
866     status =  lx_nor_flash_sector_read(&nor_sim_flash, 48, readbuffer);
867         if (status != LX_SUCCESS)
868         {
869           printf("FAILED!\n");
870 #ifdef BATCH_TEST
871     exit(1);
872 #endif
873           while(1)
874           {
875           }
876         }
877         if (readbuffer[0] != 48)
878         {
879           printf("FAILED!\n");
880 #ifdef BATCH_TEST
881     exit(1);
882 #endif
883           while(1)
884           {
885           }
886         }
887     status =  lx_nor_flash_sector_read(&nor_sim_flash, 64, readbuffer);
888         if (status != LX_SUCCESS)
889         {
890           printf("FAILED!\n");
891 #ifdef BATCH_TEST
892     exit(1);
893 #endif
894           while(1)
895           {
896           }
897         }
898         if (readbuffer[0] != 64)
899         {
900           printf("FAILED!\n");
901 #ifdef BATCH_TEST
902     exit(1);
903 #endif
904           while(1)
905           {
906           }
907         }
908     status =  lx_nor_flash_sector_read(&nor_sim_flash, 80, readbuffer);
909         if (status != LX_SUCCESS)
910         {
911           printf("FAILED!\n");
912 #ifdef BATCH_TEST
913     exit(1);
914 #endif
915           while(1)
916           {
917           }
918         }
919         if (readbuffer[0] != 80)
920         {
921           printf("FAILED!\n");
922 #ifdef BATCH_TEST
923     exit(1);
924 #endif
925           while(1)
926           {
927           }
928         }
929     status =  lx_nor_flash_sector_read(&nor_sim_flash, 96, readbuffer);
930         if (status != LX_SUCCESS)
931         {
932           printf("FAILED!\n");
933 #ifdef BATCH_TEST
934     exit(1);
935 #endif
936           while(1)
937           {
938           }
939         }
940         if (readbuffer[0] != 96)
941         {
942           printf("FAILED!\n");
943 #ifdef BATCH_TEST
944     exit(1);
945 #endif
946           while(1)
947           {
948           }
949         }
950     status =  lx_nor_flash_sector_read(&nor_sim_flash, 112, readbuffer);
951         if (status != LX_SUCCESS)
952         {
953           printf("FAILED!\n");
954 #ifdef BATCH_TEST
955     exit(1);
956 #endif
957           while(1)
958           {
959           }
960         }
961         if (readbuffer[0] != 112)
962         {
963           printf("FAILED!\n");
964 #ifdef BATCH_TEST
965     exit(1);
966 #endif
967           while(1)
968           {
969           }
970         }
971 
972     status =  lx_nor_flash_defragment(&nor_sim_flash);
973 
974     if (status != LX_SUCCESS)
975     {
976           printf("FAILED!\n");
977 #ifdef BATCH_TEST
978     exit(1);
979 #endif
980           while(1)
981           {
982           }
983     }
984 
985 
986     status =  lx_nor_flash_defragment(&nor_sim_flash);
987 
988     if (status != LX_SUCCESS)
989     {
990           printf("FAILED!\n");
991 #ifdef BATCH_TEST
992     exit(1);
993 #endif
994           while(1)
995           {
996           }
997     }
998 
999     status =  lx_nor_flash_sector_read(&nor_sim_flash, 7, 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] != 119)
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, 1, 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] != 1)
1032         {
1033           printf("FAILED!\n");
1034 #ifdef BATCH_TEST
1035     exit(1);
1036 #endif
1037           while(1)
1038           {
1039           }
1040         }
1041      status =  lx_nor_flash_sector_read(&nor_sim_flash, 16, readbuffer);
1042         if (status != LX_SUCCESS)
1043         {
1044           printf("FAILED!\n");
1045 #ifdef BATCH_TEST
1046     exit(1);
1047 #endif
1048           while(1)
1049           {
1050           }
1051         }
1052         if (readbuffer[0] != 16)
1053         {
1054           printf("FAILED!\n");
1055 #ifdef BATCH_TEST
1056     exit(1);
1057 #endif
1058           while(1)
1059           {
1060           }
1061         }
1062     status =  lx_nor_flash_sector_read(&nor_sim_flash, 32, readbuffer);
1063         if (status != LX_SUCCESS)
1064         {
1065           printf("FAILED!\n");
1066 #ifdef BATCH_TEST
1067     exit(1);
1068 #endif
1069           while(1)
1070           {
1071           }
1072         }
1073         if (readbuffer[0] != 32)
1074         {
1075           printf("FAILED!\n");
1076 #ifdef BATCH_TEST
1077     exit(1);
1078 #endif
1079           while(1)
1080           {
1081           }
1082         }
1083     status =  lx_nor_flash_sector_read(&nor_sim_flash, 48, readbuffer);
1084         if (status != LX_SUCCESS)
1085         {
1086           printf("FAILED!\n");
1087 #ifdef BATCH_TEST
1088     exit(1);
1089 #endif
1090           while(1)
1091           {
1092           }
1093         }
1094         if (readbuffer[0] != 48)
1095         {
1096           printf("FAILED!\n");
1097 #ifdef BATCH_TEST
1098     exit(1);
1099 #endif
1100           while(1)
1101           {
1102           }
1103         }
1104     status =  lx_nor_flash_sector_read(&nor_sim_flash, 64, readbuffer);
1105         if (status != LX_SUCCESS)
1106         {
1107           printf("FAILED!\n");
1108 #ifdef BATCH_TEST
1109     exit(1);
1110 #endif
1111           while(1)
1112           {
1113           }
1114         }
1115         if (readbuffer[0] != 64)
1116         {
1117           printf("FAILED!\n");
1118 #ifdef BATCH_TEST
1119     exit(1);
1120 #endif
1121           while(1)
1122           {
1123           }
1124         }
1125     status =  lx_nor_flash_sector_read(&nor_sim_flash, 80, readbuffer);
1126         if (status != LX_SUCCESS)
1127         {
1128           printf("FAILED!\n");
1129 #ifdef BATCH_TEST
1130     exit(1);
1131 #endif
1132           while(1)
1133           {
1134           }
1135         }
1136         if (readbuffer[0] != 80)
1137         {
1138           printf("FAILED!\n");
1139 #ifdef BATCH_TEST
1140     exit(1);
1141 #endif
1142           while(1)
1143           {
1144           }
1145         }
1146     status =  lx_nor_flash_sector_read(&nor_sim_flash, 96, readbuffer);
1147         if (status != LX_SUCCESS)
1148         {
1149           printf("FAILED!\n");
1150 #ifdef BATCH_TEST
1151     exit(1);
1152 #endif
1153           while(1)
1154           {
1155           }
1156         }
1157         if (readbuffer[0] != 96)
1158         {
1159           printf("FAILED!\n");
1160 #ifdef BATCH_TEST
1161     exit(1);
1162 #endif
1163           while(1)
1164           {
1165           }
1166         }
1167     status =  lx_nor_flash_sector_read(&nor_sim_flash, 112, readbuffer);
1168         if (status != LX_SUCCESS)
1169         {
1170           printf("FAILED!\n");
1171 #ifdef BATCH_TEST
1172     exit(1);
1173 #endif
1174           while(1)
1175           {
1176           }
1177         }
1178         if (readbuffer[0] != 112)
1179         {
1180           printf("FAILED!\n");
1181 #ifdef BATCH_TEST
1182     exit(1);
1183 #endif
1184           while(1)
1185           {
1186           }
1187         }
1188 
1189     /* Read in reverse order to see if caching helps!  */
1190     status =  lx_nor_flash_sector_read(&nor_sim_flash, 112, 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] != 112)
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, 96, 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] != 96)
1223         {
1224           printf("FAILED!\n");
1225 #ifdef BATCH_TEST
1226     exit(1);
1227 #endif
1228           while(1)
1229           {
1230           }
1231         }
1232      status =  lx_nor_flash_sector_read(&nor_sim_flash, 80, readbuffer);
1233         if (status != LX_SUCCESS)
1234         {
1235           printf("FAILED!\n");
1236 #ifdef BATCH_TEST
1237     exit(1);
1238 #endif
1239           while(1)
1240           {
1241           }
1242         }
1243         if (readbuffer[0] != 80)
1244         {
1245           printf("FAILED!\n");
1246 #ifdef BATCH_TEST
1247     exit(1);
1248 #endif
1249           while(1)
1250           {
1251           }
1252         }
1253     status =  lx_nor_flash_sector_read(&nor_sim_flash, 64, readbuffer);
1254         if (status != LX_SUCCESS)
1255         {
1256           printf("FAILED!\n");
1257 #ifdef BATCH_TEST
1258     exit(1);
1259 #endif
1260           while(1)
1261           {
1262           }
1263         }
1264         if (readbuffer[0] != 64)
1265         {
1266           printf("FAILED!\n");
1267 #ifdef BATCH_TEST
1268     exit(1);
1269 #endif
1270           while(1)
1271           {
1272           }
1273         }
1274     status =  lx_nor_flash_sector_read(&nor_sim_flash, 48, readbuffer);
1275         if (status != LX_SUCCESS)
1276         {
1277           printf("FAILED!\n");
1278 #ifdef BATCH_TEST
1279     exit(1);
1280 #endif
1281           while(1)
1282           {
1283           }
1284         }
1285         if (readbuffer[0] != 48)
1286         {
1287           printf("FAILED!\n");
1288 #ifdef BATCH_TEST
1289     exit(1);
1290 #endif
1291           while(1)
1292           {
1293           }
1294         }
1295     status =  lx_nor_flash_sector_read(&nor_sim_flash, 32, readbuffer);
1296         if (status != LX_SUCCESS)
1297         {
1298           printf("FAILED!\n");
1299 #ifdef BATCH_TEST
1300     exit(1);
1301 #endif
1302           while(1)
1303           {
1304           }
1305         }
1306         if (readbuffer[0] != 32)
1307         {
1308           printf("FAILED!\n");
1309 #ifdef BATCH_TEST
1310     exit(1);
1311 #endif
1312           while(1)
1313           {
1314           }
1315         }
1316     status =  lx_nor_flash_sector_read(&nor_sim_flash, 16, readbuffer);
1317         if (status != LX_SUCCESS)
1318         {
1319           printf("FAILED!\n");
1320 #ifdef BATCH_TEST
1321     exit(1);
1322 #endif
1323           while(1)
1324           {
1325           }
1326         }
1327         if (readbuffer[0] != 16)
1328         {
1329           printf("FAILED!\n");
1330 #ifdef BATCH_TEST
1331     exit(1);
1332 #endif
1333           while(1)
1334           {
1335           }
1336         }
1337     status =  lx_nor_flash_sector_read(&nor_sim_flash, 1, readbuffer);
1338         if (status != LX_SUCCESS)
1339         {
1340           printf("FAILED!\n");
1341 #ifdef BATCH_TEST
1342     exit(1);
1343 #endif
1344           while(1)
1345           {
1346           }
1347         }
1348         if (readbuffer[0] != 1)
1349         {
1350           printf("FAILED!\n");
1351 #ifdef BATCH_TEST
1352     exit(1);
1353 #endif
1354           while(1)
1355           {
1356           }
1357         }
1358     status =  lx_nor_flash_sector_read(&nor_sim_flash, 7, readbuffer);
1359         if (status != LX_SUCCESS)
1360         {
1361           printf("FAILED!\n");
1362 #ifdef BATCH_TEST
1363     exit(1);
1364 #endif
1365           while(1)
1366           {
1367           }
1368         }
1369         if (readbuffer[0] != 119)
1370         {
1371           printf("FAILED!\n");
1372 #ifdef BATCH_TEST
1373     exit(1);
1374 #endif
1375           while(1)
1376           {
1377           }
1378         }
1379 
1380     status =  lx_nor_flash_sector_release(&nor_sim_flash, 7);
1381     if (status != LX_SUCCESS)
1382         {
1383           printf("FAILED!\n");
1384 #ifdef BATCH_TEST
1385     exit(1);
1386 #endif
1387           while(1)
1388           {
1389           }
1390         }
1391     status =  lx_nor_flash_sector_release(&nor_sim_flash, 8);
1392     if (status != LX_SECTOR_NOT_FOUND)
1393         {
1394           printf("FAILED!\n");
1395 #ifdef BATCH_TEST
1396     exit(1);
1397 #endif
1398           while(1)
1399           {
1400           }
1401         }
1402 
1403     status =  lx_nor_flash_defragment(&nor_sim_flash);
1404 
1405     if (status != LX_SUCCESS)
1406     {
1407           printf("FAILED!\n");
1408 #ifdef BATCH_TEST
1409     exit(1);
1410 #endif
1411           while(1)
1412           {
1413           }
1414     }
1415 
1416     status =  lx_nor_flash_close(&nor_sim_flash);
1417 
1418     if (status != LX_SUCCESS)
1419     {
1420           printf("FAILED!\n");
1421 #ifdef BATCH_TEST
1422     exit(1);
1423 #endif
1424           while(1)
1425           {
1426           }
1427     }
1428 
1429     printf("SUCCESS!\n");
1430 
1431     printf("Test 5: Randow write/read sector................");
1432 
1433     /* Erase the simulated NOR flash.  */
1434     _lx_nor_flash_simulator_erase_all();
1435 
1436     /* Open the flash.  */
1437     status =  lx_nor_flash_open(&nor_sim_flash, "sim nor flash", _lx_nor_flash_simulator_initialize);
1438 #ifndef LX_NOR_DISABLE_EXTENDED_CACHE
1439 status += lx_nor_flash_extended_cache_enable(&nor_sim_flash, nor_cache_memory, sizeof(nor_cache_memory));
1440 #endif
1441 
1442     if (status != LX_SUCCESS)
1443     {
1444 
1445           printf("FAILED!\n");
1446 #ifdef BATCH_TEST
1447     exit(1);
1448 #endif
1449           while(1)
1450           {
1451           }
1452     }
1453 
1454     /* Write 100 sectors....  */
1455     for (i = 0; i < 100; i++)
1456     {
1457         for (j = 0; j < 128; j++)
1458           buffer[j] =  i;
1459 
1460         status =  lx_nor_flash_sector_write(&nor_sim_flash, i, buffer);
1461 
1462         if (status != LX_SUCCESS)
1463         {
1464           printf("FAILED!\n");
1465 #ifdef BATCH_TEST
1466     exit(1);
1467 #endif
1468           while(1)
1469           {
1470           }
1471         }
1472     }
1473 
1474     /* Read back 100 sectors...  */
1475     for (i = 0; i < 100; i++)
1476     {
1477 
1478         status =  lx_nor_flash_sector_read(&nor_sim_flash, i, buffer);
1479 
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 
1491         for (j = 0; j < 128; j++)
1492         {
1493 
1494           if (buffer[j] !=  i)
1495           {
1496             printf("FAILED!\n");
1497 #ifdef BATCH_TEST
1498     exit(1);
1499 #endif
1500             while(1)
1501             {
1502             }
1503           }
1504         }
1505     }
1506 
1507     /* Now, perform 1000 sector writes to randomly selected sectors, each time
1508        reading first to make sure the previous contents are valid.  */
1509     for (i = 0; i < 1000; i++)
1510     {
1511 
1512         /* Pickup random sector.  */
1513         sector =  (rand() % 100);
1514 
1515         /* Read that sector.  */
1516         status =  lx_nor_flash_sector_read(&nor_sim_flash, sector, buffer);
1517 
1518         if (status != LX_SUCCESS)
1519         {
1520           printf("FAILED!\n");
1521 #ifdef BATCH_TEST
1522     exit(1);
1523 #endif
1524           while(1)
1525           {
1526           }
1527         }
1528 
1529         for (j = 0; j < 128; j++)
1530         {
1531 
1532           if ((buffer[j] & 0x0000FFFF) !=  sector)
1533           {
1534             printf("FAILED!\n");
1535 #ifdef BATCH_TEST
1536     exit(1);
1537 #endif
1538             while(1)
1539             {
1540             }
1541           }
1542         }
1543 
1544         /* Include the itteraction in the buffer to generate a new write.  */
1545         for (j = 0; j < 128; j++)
1546         {
1547 
1548             buffer[j] =  (buffer[j] & 0x0000FFFF) | (i << 16);
1549         }
1550 
1551         status =  lx_nor_flash_sector_write(&nor_sim_flash, sector, buffer);
1552 
1553         if (status != LX_SUCCESS)
1554         {
1555           printf("FAILED!\n");
1556 #ifdef BATCH_TEST
1557     exit(1);
1558 #endif
1559           while(1)
1560           {
1561           }
1562         }
1563     }
1564 
1565     status =  lx_nor_flash_close(&nor_sim_flash);
1566 
1567     if (status != LX_SUCCESS)
1568     {
1569           printf("FAILED!\n");
1570 #ifdef BATCH_TEST
1571     exit(1);
1572 #endif
1573           while(1)
1574           {
1575           }
1576     }
1577     printf("SUCCESS!\n");
1578 
1579     printf("Test 6: Check lx_nor_flash_extended_cache_entries size................");
1580 
1581     /* Erase the simulated NOR flash.  */
1582     _lx_nor_flash_simulator_erase_all();
1583 
1584     /* Open the flash.  */
1585     status =  lx_nor_flash_open(&nor_sim_flash, "sim nor flash", _lx_nor_flash_simulator_initialize);
1586 #ifndef LX_NOR_DISABLE_EXTENDED_CACHE
1587 status += lx_nor_flash_extended_cache_enable(&nor_sim_flash, nor_cache_memory2, sizeof(nor_cache_memory2));
1588 #endif
1589 
1590     if (status != LX_SUCCESS)
1591     {
1592 
1593           printf("FAILED!\n");
1594 #ifdef BATCH_TEST
1595     exit(1);
1596 #endif
1597           while(1)
1598           {
1599           }
1600     }
1601 
1602     /* Write 100 sectors....  */
1603     for (i = 0; i < 100; i++)
1604     {
1605         for (j = 0; j < 128; j++)
1606           buffer[j] =  i;
1607 
1608         status =  lx_nor_flash_sector_write(&nor_sim_flash, i, buffer);
1609 
1610         if (status != LX_SUCCESS)
1611         {
1612           printf("FAILED!\n");
1613 #ifdef BATCH_TEST
1614     exit(1);
1615 #endif
1616           while(1)
1617           {
1618           }
1619         }
1620     }
1621 
1622     /* Read back 100 sectors...  */
1623     for (i = 0; i < 100; i++)
1624     {
1625 
1626         status =  lx_nor_flash_sector_read(&nor_sim_flash, i, buffer);
1627 
1628         if (status != LX_SUCCESS)
1629         {
1630           printf("FAILED!\n");
1631 #ifdef BATCH_TEST
1632     exit(1);
1633 #endif
1634           while(1)
1635           {
1636           }
1637         }
1638 
1639         for (j = 0; j < 128; j++)
1640         {
1641 
1642           if (buffer[j] !=  i)
1643           {
1644             printf("FAILED!\n");
1645 #ifdef BATCH_TEST
1646     exit(1);
1647 #endif
1648             while(1)
1649             {
1650             }
1651           }
1652         }
1653     }
1654 
1655 #ifdef BATCH_TEST
1656     exit(0);
1657 #endif
1658      /* All done!  */
1659      while(1)
1660      {
1661      }
1662 }
1663 
1664 
1665