NM500 TEST PGM

Dependencies:   NM500Lib_Socket NM500_Test_Socket SDFileSystem_Socket mbed

Revision:
12:c8fbe49527e7
Parent:
11:6a81bde51be5
Child:
13:e756b6e50853
--- a/main.cpp	Mon May 29 04:32:29 2017 +0000
+++ b/main.cpp	Fri Jun 23 05:37:34 2017 +0000
@@ -2,41 +2,106 @@
 #include "NM500.h"
 
 int data_learn(void);
-int data_classify(void);
+void data_classify(void);
+void NSR_Test(void);
+
+int id_fail     = 0;
+int nsr_fail    = 0;
+
+int CMN_Maxif   = 0;
+int CMN_Nsr     = 0;
+int CMN_Ncount  = 0;
+int CMN_RC      = 0;
+int CMN         = 0;
+
+int Gcr1        = 0;
+int Gcr2        = 0;
+int Minif1      = 0;
+int Minif2      = 0;
+int Maxif1      = 0;
+int Maxif2      = 0;
+int Aif1        = 0;
+int Aif2        = 0;
+int Cat1        = 0;
+int Cat2        = 0;
+
+int Nid1        = 0;
+int Nid2        = 0;
+int Dist1       = 0;
+int Dist2       = 0;
+
+int Learn1      = 0;
+int Learn2      = 0;
+
+int Memory1     = 0;
+int Memory2     = 0;
+
+int NM1         = 0;
+int NM2         = 0;
+
+char array1[NEURONSIZE];
+char array2[NEURONSIZE];
 
 Timer t, st;
 //Serial pc(SERIAL_TX, SERIAL_RX);
 unsigned char vector[NEURONSIZE];
 
 int main() { 
+
     //NM500 & SD Card Init
-    if(begin() == 0)
-        printf("\n\nNM500 Init\n");
-    else
-        printf("\nInit error\n");          
+    if(begin() == 0){
+        printf("\nTest Board V2.0 \n");
+        printf("\n\nNM500 Init \n");
+    }
+    else{
+        printf("\nInit error\n");
+        //int forget = 1;
+        error("\nFORGET error\n");
+    } 
     
+    //memory array init
+    for(int i=0;i<NEURONSIZE;i=i+1){
+        array1[i]=0x0a;
+        array2[i]=0x05;         
+    }
+    NSR_Test();
+      
     //time measure start
     t.start();
+    //NCOUNT Test
+    int ncount_fail = read(NM_NCOUNT);
     
+    if(ncount_fail != 0){
+        CMN_Ncount = 1;
+        error("NCOUNT Fail\n");
+    }
+  
     //minif test
     write(NM_MINIF,3);
-    int minif = read(NM_MINIF);
+//    int minif = read(NM_MINIF);
+//    if(minif != 3)
+//        CMN_Minif = 1;
 
     //maxif test
     write(NM_MAXIF,10000);  
     int maxif = read(NM_MAXIF);
+    if(maxif != 10000)
+        CMN_Maxif = 1;
     
     //GCR test
     write(NM_GCR,5);
-    int gcr = read(NM_GCR);
-    //printf("gcr : %d",gcr);
+//    int gcr = read(NM_GCR);
+//    //printf("gcr : %d",gcr);
+//    if(gcr != 5)
+//        CMN_Gcr = 1;
      
     st.start();          
     //Neuron learn
     printf("Learn Start\n");
-    int learn = data_learn();
-    if(learn == 0x7fff)     //change
-        learn = MAXNEURONS;
+    
+
+    int daisy = data_learn();
+
     st.stop(); 
     printf("Learn Complete\n");
     printf("Learn Time : %d ms\n",st.read_ms());
@@ -45,7 +110,10 @@
     //data save
     st.start();
     printf("Save Start\n");
+    
     int save = NeuronToSD();
+    if(save == 0xFFFF || save == 0x7FFF)     //change
+        save = MAXNEURONS;
     st.stop();
     printf("Save Complete\n");
     printf("Save Time : %d ms\n",st.read_ms());
@@ -53,12 +121,16 @@
     
     //forget
     forget();
-    int forget = NCOUNT();
+
     
     //restore
     st.start();
     printf("Restore Start\n");
+    
     int restore = SDToNeurons();
+    
+    if(restore == 0xFFFF || restore == 0x7FFF)     //change
+        restore = MAXNEURONS;
     st.stop();
     printf("Restore Complete\n");
     printf("Restore Time : %d ms\n",st.read_ms());
@@ -68,146 +140,308 @@
     //classify
     st.start();
     printf("Classify Start\n");
-    int classify = data_classify();
+
+    data_classify();
     st.stop();
     printf("Classify Complete\n");
     printf("classify Time : %d ms\n",st.read_ms());
     
     
-    printf("\n****************Tset Result****************\n");
-    //minif result
-    if(minif == 3)
-        printf("MINIF # PASS\n");
-    else
-        printf("MINIF # FAIL\n");
-
-    //maxif result
-    if(maxif == 10000)
-        printf("MAXIF # PASS\n");
-    else
-        printf("MAXIF # FAIL\n");
+    printf("\n****************Test Result****************\n");
+    //nsr result
+     if(CMN_Nsr ==1){
+        printf("NSR Register Fail\n");
+        CMN++;
+    }
         
-    //GCR result
-    if(gcr == 5)
-        printf("GCR # PASS\n");
-    else
-        printf("GCR # FAIL\n");
-        
-        
-    //forget result
-    if(forget == 0)
-        printf("FORGET # PASS\n");
-    else
-        printf("FORGET # FAIL\n");
-        
-    //learn result
-    if(learn == MAXNEURONS){
-        printf("Daisy Chain # PASS\n");
-        printf("Learn # PASS\n");
+    //NCOUNT Result
+    if(CMN_Ncount ==1){
+        printf("NCOUNT Register Fail\n");
+        CMN++;
     }
         
-    else if(learn == 2){
-        printf("Daisy Chain # FAIL\n");
-        printf("Learn # PASS\n");
+    //MAXIF Result
+    if(CMN_Maxif ==1){
+        printf("MAXIF Register Fail\n");
+        CMN++;
+    }  
+        
+    //MINIF Result
+    if(Minif1 ==1){
+        printf("#1 NM500 MINIF Register Fail\n");
+        NM1++;
     }
-    else {
-        printf("Daisy Chain # unknown\n");
-        printf("# %d Neuron Learn FAIL\n",learn);        
+    if(Minif2 ==1){
+        printf("#2 NM500 MINIF Register Fail\n");
+        NM2++;
+    }
+    
+    //learn result
+    if(Learn1 == 1){
+        printf("#1 NM500 Learn Function Fail\n");
+        NM1++; 
+    }
+    if(Learn2 == 1){
+        printf("#2 NM500 Learn Function Fail\n");
+        NM2++; 
     }
     
-    //classify result
-    if(classify == 1){
-        printf("Classify # PASS\n");
-        printf("Distance # PASS\n");
+    //Daisy Chain result
+    if(daisy == 1){
+        printf("#1 NM500 Daisy Chain Fail\n");
+        NM1++; 
+    } 
+      
+    //GCR result
+    if(Gcr1 == 1){
+        printf("#1 NM500 GCR Register Fail\n");
+        NM1++;
+    }
+    if(Gcr2 == 1){
+        printf("#2 NM500 GCR Register Fail\n");
+        NM2++;
+    }
+    //AIF result
+    if(Aif1 == 1){
+        printf("#1 NM500 AIF Register Fail\n");
+        NM1++;
+    }
+    if(Aif2 == 1){
+        printf("#2 NM500 AIF Register Fail\n");
+        NM2++;
     }
-    else if(classify == 2){
-        printf("Classify # PASS\n");
-        printf("Distance # FAIL\n");
+    //Cat result
+    if(Cat1 == 1){
+        printf("#1 NM500 CAT Register Fail\n");
+        NM1++;
+    }
+    if(Cat2 == 1){
+        printf("#2 NM500 CAT Register Fail\n");
+        NM2++;
+    }
+    if(save != MAXNEURONS){
+        printf("NM500 Learn Function Fail\n");
+        CMN++;
     }
-    else {
-        printf("# %d Neuron Classify FAIL\n",classify);
-        printf("Distance # PASS\n");
+    if(restore != MAXNEURONS){
+        printf("NM500 Restore Function Fail\n");
+        CMN++;
+    }
+    //NID result
+    if(Nid1 == 1){
+        printf("#1 NM500 Nid Register Fail\n");
+        NM1++;
+    }
+    if(Nid2 == 1){
+        printf("#2 NM500 Nid Register Fail\n");
+        NM2++;
     }
     
-    printf("POWER SAVE ON\n");
-    write(NM_POWER_SAVE,0);
+    //Dist result
+    if(Dist1 == 1){
+        printf("#1 NM500 Dist Register Fail\n");
+        NM1++;
+    }
+    if(Dist2 == 1){
+        printf("#2 NM500 Dist Register Fail\n");
+        NM2++;
+    }
+    if(Memory1 == 1){
+        printf("#1 NM500 Memory Fail\n");
+        NM1++;
+    }
+    if(Memory2 == 1){
+        printf("#2 NM500 Memory Fail\n");
+        NM2++;
+    }
+    
+    //final result
+    if(NM1 >1)
+        printf("#1 NM500 Fail\n");
+    if(NM2 > 1)
+        printf("#2 NM500 Fail\n");   
+    if(CMN>1)
+        printf("#1, #2 Fail\n");
+    if(CMN == 0 && NM1 == 0 && NM2 == 0)
+        printf("#1, #2 NM500 PASS\n");
+        
+    
+            
+            
+//    //reset chain result
+//    if(CMN_RC == 1)
+//        printf("Reset Chain Register Fail\n");
+
+  //  printf("POWER SAVE ON\n");
+//    write(NM_POWER_SAVE,0);
     t.stop();
     printf("Total Time : %d ms\n",t.read_ms());
-             
-             
-        
-        
+       
+}
+void NSR_Test(void)
+{
+    //KNN Mode setting
+    setKNN();
+    //read KNN Register
+    int nsr = read(NM_NSR) & 0x20;
+    if(nsr != 32)
+        CMN_Nsr = 1;    //fail
+
+    //RBF Mode setting
+    setRBF();         
+    nsr = read(NM_NSR) & 0x20;
+    if(nsr != 0)
+        CMN_Nsr = 1;    //fail
+
+    int temp_nsr = read(NM_NSR);
+    
+    //SR Mode test
+    write(NM_NSR, 0x10);
+    nsr = read(NM_NSR) & 0x10;    
+    if(nsr != 16)
+        CMN_Nsr = 1;
+    
+    //Nomal Mode Test
+    write(NM_NSR, temp_nsr);
+    nsr = read(NM_NSR) & 0x10;
+    if(nsr != 0)
+        CMN_Nsr = 1; 
 }
 int data_learn(void)
 {
-        int ncnt=0,old_ncnt=0;  //ncount, old ncount
-        
-        //number of neuron 576*2
-        for(int i=1;i<=MAXNEURONS;i++){ 
-            // ex
-            //      1.      0 1 0 1.... 
-            //      2.      0 2 0 2.... 
-            //      3.      0 3 0 3....
-            //      .....
-            //      255.    0 255 0 255....
-            //      256.    1 0 1 0....
-            //      257.    1 1 1 1....
-            for(int j =0;j<NEURONSIZE;j=j+2){   
+    int ncnt=0,old_ncnt=0;  //ncount, old ncount
+    
+    //number of neuron 576*2
+    for(int i=1;i<=MAXNEURONS;i++){ 
+        // ex
+        //      1.      0 1 0 1.... 
+        //      2.      0 2 0 2.... 
+        //      3.      0 3 0 3....
+        //      .....
+        //      255.    0 255 0 255....
+        //      256.    1 0 1 0....
+        //      257.    1 1 1 1....
+        if(i%2 == 1){
+            for(int j =0;j<DATA_CNT;j=j+2){   
+                vector[j] = i>>8;          //upper bit save
+                vector[j+1] = i;           //low bit save
+            }
+            for(int j =DATA_CNT;j<NEURONSIZE; j++){   
+                vector[j] = array1[j];          //upper bit save
+            }
+        }
+        else if(i%2==0){
+            for(int j =0;j<DATA_CNT;j=j+2){   
                 vector[j] = i>>8;          //upper bit save
                 vector[j+1] = i;           //low bit save
             }
-            //data learn
-            ncnt = learn(vector, NEURONSIZE, i);    //input : data, neuronsize, cat,   return : ncount
-            if(ncnt == 0x7fff)     //change
-                ncnt = MAXNEURONS;
-            //printf("ncount # %d\n",ncnt);
-            
-            if(ncnt == old_ncnt)    //ncount가 변화가 없으면  
-                printf("\nNeuron Learn Fail # %d\n",ncnt);     
-            else                    //ncount가 증가하였을 경우
-                old_ncnt = ncnt;    //old ncount 갱신
+            for(int j =DATA_CNT;j<NEURONSIZE; j++){   
+                vector[j] = array2[j];          //upper bit save
+            }
+        }
+        
+        //data learn
+        ncnt = learn(vector, NEURONSIZE, i);    //input : data, neuronsize, cat,   return : ncount
+        if(ncnt == 0xffff || ncnt == 0x7FFF)     //change
+            ncnt = MAXNEURONS;
+        //printf("ncount # %d\n",ncnt);
+        
+        if(ncnt == old_ncnt){    //ncount가 변화가 없으면  
+            if(ncnt <=576){
+                Learn1 = 1;
+                error("\n Learn Fail NM500 #1\n");  
+            }
+            else{
+                Learn2 = 1;
+                error("\n Learn Fail NM500 #2\n");  
+            }
+        }   
+        //ncount가 증가하였을 경우    
+        else                 
+            old_ncnt = ncnt;    //old ncount 갱신
 
-            printf(".");
-        } 
-        printf("\n");
-        printf("NCOUNT : %d\n",ncnt);
-        //학습 완료 후 ncount가 MAXNEURONS보다 작을 경우
-        if(ncnt == MAXNEURONS/2)
-            return 2;       //데이지체인 불량
-        return ncnt;        //정상 출력
+        printf(".");
+    } 
+    printf("\n");
+    printf("NCOUNT : %d\n",ncnt);
+    //학습 완료 후 ncount가 MAXNEURONS보다 작을 경우
+    if(ncnt == MAXNEURONS/2)
+        return 1;       //데이지체인 불량
+    else if( ncnt != MAXNEURONS)
+        error("NCOUNT Fail\n");
+    return 0;        //정상 출력
 }
 
-int data_classify(void)
+void data_classify(void)
 {
     int distance, cat, nid;
     //number of neuron 1156
     for(int i=1;i<=MAXNEURONS;i++){          
         //test 벡터 생성
-        for(int j =0;j<NEURONSIZE;j=j+2){   //size of neuron 256
-            vector[j] = i>>8;               //upper bit save
-            vector[j+1] = i;                //low bit save
+        //for(int j =0;j<NEURONSIZE;j=j+2){   //size of neuron 256
+//            vector[j] = i>>8;               //upper bit save
+//            vector[j+1] = i;                //low bit save
+//        }
+        if(i%2 == 1){
+            for(int j =0;j<DATA_CNT;j=j+2){   
+                vector[j] = i>>8;          //upper bit save
+                vector[j+1] = i;           //low bit save
+            }
+            for(int j =DATA_CNT;j<NEURONSIZE; j++){   
+                vector[j] = array1[j];          //upper bit save
+            }
+        }
+        else if(i%2==0){
+            for(int j =0;j<DATA_CNT;j=j+2){   
+                vector[j] = i>>8;          //upper bit save
+                vector[j+1] = i;           //low bit save
+            }
+            for(int j =DATA_CNT;j<NEURONSIZE; j++){   
+                vector[j] = array2[j];          //upper bit save
+            }
         }
 
         //input : data, data length      output : distance, category, Neuron ID      return 인식된 데이터 갯수
         int nsr = classify(vector, NEURONSIZE, &distance, &cat, &nid);
+        id_fail = nsr & 0x70;
+        if(id_fail == 0)
+            nsr_fail = 1;
+        
         printf(".");
-        printf(" neuron# %d \t", nid);
-        printf(" distance = %d,\t ", distance);
-        printf(" cat = %d\n", cat);
+//        printf(" neuron# %d \t", nid);
+//        printf(" distance = %d,\t ", distance);
+//        printf(" cat = %d\n", cat);
 
-        if(nid!=cat){
-            printf("Classify Fail # %d \n",nid);
-            return cat;       //분류 실패
+        //nid test
+        //cat register pass AND (nid,cat diff)
+        if((nid!=cat) && (Cat1==0 && Cat2==0)){
+            if(nid < 576){
+                Nid1 = 1;
+                printf("\nNID Register Fail # %d\n",nid);
+            }
+            else{
+                Nid2 = 1;
+                printf("\nNID Register Fail # %d\n",nid);
+            }
         }
         if(distance != 0){
-            printf("Distance Fail \n");
-            return 2;       //distance error
+            if(nid < 576){
+                Dist1 = 1;
+                printf("\nDistance Register Fail # %d, Distance # %d\n",nid,distance);
+            }
+            else{
+                Dist2 = 1;
+                printf("\nDistance Register Fail # %d, Distance # %d\n",nid,distance);
+            }
         }
     
     }
     printf("\n");  
-    return 1;       //정상 분류
+
 } 
 
 
 
+
+
+