Na sungil / NM500_Test_Socket
Revision:
3:1a838bfb4101
Parent:
2:4a75bdce2735
Child:
4:b906344e8ddb
--- a/Test.cpp	Wed Nov 29 04:59:44 2017 +0000
+++ b/Test.cpp	Mon Dec 17 04:15:24 2018 +0000
@@ -3,6 +3,8 @@
 #include "mbed.h"
 #include "SDFileSystem.h"
 
+
+
 SDFileSystem sd(SD_MOSI, SD_MISO, SD_SCK, SD_CS, "sd"); // the pinout on the mbed Cool Components workshop board
 
 FILE *fp=NULL;
@@ -58,24 +60,23 @@
 
 //SPI init
 void SD_Init(void)
-{    
+{
 //test CS
-    
+
     SDCard_CS = HIGH;   //disable
-    wait(0.1);  
+    wait(0.1);
     mkdir("/sd", 0777);
-    fp = fopen("/sd/data.txt", "w"); 
-    
-    if(fp == NULL)
-    {
+    fp = fopen("/sd/data.txt", "w");
+
+    if(fp == NULL) {
         error("Could not open file for write\n");
     }
-    fclose(fp); 
+    fclose(fp);
     printf("SD Init OK\n");
 }
 void RESET_Test(void)
 {
-    
+
     printf("\nRESET Test\n");
 
     //nomal mode
@@ -91,14 +92,14 @@
     int dist = read(NM_DIST);
     //printf("dist : 0x%x \n",dist);
     int cat = read(NM_CAT);
-    //printf("cat : 0x%x \n",cat);   
+    //printf("cat : 0x%x \n",cat);
     int ncount = read(NM_NCOUNT);
     //printf("ncount : 0x%x \n",ncount);
 
 
 
     //sr mode
-   // printf("\nSR MODE \n");
+    // printf("\nSR MODE \n");
     write(NM_NSR,0x10);
 //    int comp = read(NM_COMP);
 //    //printf("COMP : 0x%x \n",comp);
@@ -111,14 +112,14 @@
 //    dist = read(NM_DIST);
 //    //printf("dist : 0x%x \n",dist);
 //    cat = read(NM_CAT);
-//    //printf("cat : 0x%x \n",cat); 
+//    //printf("cat : 0x%x \n",cat);
     int aif = read(NM_AIF);
 //    //printf("aif : 0x%x \n",aif);
     int nid = read(NM_NID);
 //    //printf("nid : 0x%x \n",nid);
-    
+
     write(NM_NSR,0x00);
-    
+
     if(nsr != 0x00)
         Reset_fail = 1;
     if(gcr != 0x01)
@@ -137,10 +138,10 @@
         Reset_fail = 1;
     if(nid != 0x0000)
         Reset_fail = 1;
-    
+
     if(Reset_fail == 0)
         printf("Reset Test Pass\n");
-    else{
+    else {
         printf("Reset Test Fail\n");
         printf("Reset the NM500 and proceed\n");
     }
@@ -162,34 +163,34 @@
 //        NSR_Conn_fail=1;
 //        Connection = 1;
 //    }
-//    
+//
 //    //NSR의 데이터를 0x55로 변경하여 write
 //    write(NM_NSR, 0x055);
 //    temp_NSR = read(NM_NSR)&0x3f;
 //    //printf("NSR : 0x%x\n",temp_NSR);
 //    if(temp_NSR != 0x19){
-//        printf("NSR Connection error\n"); 
+//        printf("NSR Connection error\n");
 //        NSR_Conn_fail=1;
 //        Connection = 1;
-//    }  
+//    }
 ////    if(NSR_Conn_fail == 0)
 ////        printf("NSR_Connetion PASS \n");
-//        
+//
 //    //NSR을 초기 저장값으로 복원
 //    write(NM_NSR, 0);
 
     ////////////NSR 종료////////////
-    
-    
-    ///////////GCR 시작//////////////    
+
+
+    ///////////GCR 시작//////////////
     //write GCR 0xaa
     write(NM_GCR, 0xaa);
     //GCR Read
     unsigned int temp_GCR = read(NM_GCR);
-   // printf("GCR : 0x%x\n", temp_GCR);
-    
+    // printf("GCR : 0x%x\n", temp_GCR);
+
     //GCR data가 깨졌을 경우
-    if(temp_GCR != 0xaa){
+    if(temp_GCR != 0xaa) {
         GCR_Conn_fail = 1;
         Connection = 1;
         printf("GCR Connection error\n");
@@ -199,23 +200,23 @@
     temp_GCR = read(NM_GCR);
     //printf("GCR : 0x%x\n", temp_GCR);
     //GCR data가 깨졌을 경우
-    if(temp_GCR != 0x55){
+    if(temp_GCR != 0x55) {
         GCR_Conn_fail = 1;
         Connection = 1;
         printf("GCR Connection error\n");
     }
 
     ///////////GCR 종료//////////////
-    
-    
-    
+
+
+
     ///////////MINIF 시작//////////////
-        
+
     write(NM_MINIF, 0xaaaa);
     int temp_MINIF = read(NM_MINIF);
     //printf("MINIF : 0x%x\n", temp_MINIF);
     //minif data 확인
-    if(temp_MINIF != 0xaaaa){
+    if(temp_MINIF != 0xaaaa) {
         MINIF_Conn_fail = 1;
         Connection = 1;
         printf("MINIF Connection error\n");
@@ -225,24 +226,24 @@
     temp_MINIF = read(NM_MINIF);
     //printf("MINIF : 0x%x\n", temp_MINIF);
     //MINIF test
-    if(temp_MINIF != 0x5555){
+    if(temp_MINIF != 0x5555) {
         MINIF_Conn_fail = 1;
         Connection = 1;
         printf("MINIF Connection error\n");
     }
-       
-       //////////END MINIF/////////////////
-       
-      ///////////MAXIF 시작//////////////
-    
+
+    //////////END MINIF/////////////////
+
+    ///////////MAXIF 시작//////////////
+
 
     //write maxif 0xaaaa
-    
+
     write(NM_MAXIF, 0xaaaa);
     int temp_MAXIF = read(NM_MAXIF);
     //printf("MAXIF : 0x%x\n", temp_MAXIF);
     //maxif data 확인
-    if(temp_MAXIF != 0xaaaa){
+    if(temp_MAXIF != 0xaaaa) {
         MAXIF_Conn_fail = 1;
         Connection = 1;
         printf("MAXIF Connection error\n");
@@ -252,11 +253,11 @@
     temp_MAXIF = read(NM_MAXIF);
     //printf("MAXIF : 0x%x\n", temp_MAXIF);
     //MAXIF test
-    if(temp_MAXIF != 0x5555){
+    if(temp_MAXIF != 0x5555) {
         MAXIF_Conn_fail = 1;
         Connection = 1;
         printf("MAXIF Connection error\n");
-    }      
+    }
     //////////END MAXIF/////////////////
 
 
@@ -271,7 +272,7 @@
     int temp_NCR = read(NM_NCR);
     //printf("NCR : 0x%x\n", temp_NCR);
     //NCR test
-    if(temp_NCR != 0xaa){
+    if(temp_NCR != 0xaa) {
         NCR_Conn_fail = 1;
         Connection = 1;
         printf("NCR Connection error\n");
@@ -281,13 +282,13 @@
     temp_NCR = read(NM_NCR);
     //printf("NCR : 0x%x\n", temp_NCR);
     //NCR test
-    if(temp_NCR != 0x55){
+    if(temp_NCR != 0x55) {
         NCR_Conn_fail = 1;
         Connection = 1;
         printf("NCR Connection error\n");
     }
 
-///////////NCR 종료//////////////    
+///////////NCR 종료//////////////
 
 /////////////////AIF
 
@@ -295,7 +296,7 @@
     int temp_AIF = read(NM_AIF);
     //printf("AIF : 0x%x\n", temp_AIF);
     //AIF test
-    if(temp_AIF != 0xaaaa){
+    if(temp_AIF != 0xaaaa) {
         AIF_Conn_fail = 1;
         Connection = 1;
         printf("AIF Connection error\n");
@@ -303,16 +304,16 @@
     //write AIF 0x55
     write(NM_AIF, 0x5555);
     temp_AIF = read(NM_AIF);
-   /// printf("AIF : 0x%x\n", temp_AIF);
+    /// printf("AIF : 0x%x\n", temp_AIF);
     //AIF test
-    if(temp_AIF != 0x5555){
+    if(temp_AIF != 0x5555) {
         AIF_Conn_fail = 1;
         Connection = 1;
         printf("AIF Connection error\n");
     }
 
     write(NM_NSR, 0x00);
-    
+
     if(Connection == 1)
         printf("Connection Test Fail\n");
     else
@@ -324,81 +325,80 @@
     int ncount, old_ncnt;
 
     printf("Memory Test Start \n");
-        printf("Data Save \n");
+    printf("Data Save \n");
     //1번째 학습 홀수 : 0x0a, 짝수 : 0x05
-    for(int i = 1; i <=MAXNEURONS; i=i+2){
+    for(int i = 1; i <=MAXNEURONS; i=i+2) {
         //학습할 데이터
-        for(int j=0;j<NEURONSIZE;j=j+2){
-            array1[j]   =0xa;
-            array1[j+1] =0x5;
-            array2[j]   =0x5;
-            array2[j+1] =0xa;         
+        for(int j=0; j<NEURONSIZE; j=j+2) {
+            array1[j]   =0xaa;
+            array1[j+1] =0x55;
+            array2[j]   =0x55;
+            array2[j+1] =0xaa;
         }
         //A 5 A 5....학습
         ncount = learn(array1, NEURONSIZE, i);    //input : data, neuronsize, cat,   return : ncount
         //5 A 5 A....학습
         ncount = learn(array2, NEURONSIZE, i+1);    //input : data, neuronsize, cat,   return : ncount
-        
+
         //학습여부 판단
-        if(ncount == old_ncnt){    //ncount가 변화가 없으면  
-            if(ncount <=576){
+        if(ncount == old_ncnt) {   //ncount가 변화가 없으면
+            if(ncount <=576) {
                 Learn1 = 1;
                 printf("NCOUNT : %d\n ", ncount);
-                error("\n Learn Fail NM500 #1\n");  
-            }
-            else{
+                error("\n Learn Fail NM500 #1\n");
+            } else {
                 Learn2 = 1;
                 printf("NCOUNT : %d\n ", ncount);
-                error("\n Learn Fail NM500 #2\n");  
+                error("\n Learn Fail NM500 #2\n");
             }
-        }   
-        //ncount가 증가하였을 경우    
-        else{                 
+        }
+        //ncount가 증가하였을 경우
+        else {
             old_ncnt = ncount;    //old ncount 갱신
             printf(".");
-        }       
-        
+        }
+
     }
     //학습 뉴런 갯수가 MAXNEURONS일 경우
     if(ncount == 0xFFFF || ncount == 0x7FFF)     //change
         ncount = MAXNEURONS;
-        
+
     printf("\n");
     //학습된 뉴런 갯수 출력
 //    printf("NCOUNT : %d ",ncount);
     //end memory write
-    
+
     //NSR 백업
     printf("Data Compare\n");
     int temp_NSR = read(NM_NSR);
     //write NSR-SR Mode
     write(NM_NSR, 0x10);
     write(NM_RSTCHAIN, 0);
-    
+
     int temp_data[NEURONSIZE];
-    for(int i = 1; i<=MAXNEURONS; i++){
+    for(int i = 1; i<=MAXNEURONS; i++) {
         printf(".");
         //전체 뉴런 데이터 리드
-        for(int j = 0; j<NEURONSIZE; j++){
+        for(int j = 0; j<NEURONSIZE; j++) {
             temp_data[j]=read(NM_COMP);
             //fprintf(fp,"%d,", temp_data[j]);
             //A 5 A 5 일 경우
-            if(i%2 == 1){
+            if(i%2 == 1) {
                 //학습한 데이터와 다를 경우
-                if(temp_data[j]!= array1[j]){
+                if(temp_data[j]!= array1[j]) {
                     printf("\nNID : %d , %d byte Memory Error\n",i,j);
                     Memory_fail = 1;
                 }
-                
+
             }
             //5 A 5 A 일 경우
-            else if( i%2 == 0){
+            else if( i%2 == 0) {
                 //학습한 데이터와 다를 경우
-                if(temp_data[j]!= array2[j]){
+                if(temp_data[j]!= array2[j]) {
                     printf("\nNID : %d , %d byte Memory Error\n",i,j);
                     Memory_fail = 1;
                 }
-            }            
+            }
         }
         //다음 뉴런으로 변경
         int cat = read(NM_CAT)& 0x7FFF;
@@ -409,74 +409,72 @@
     //초기화
     forget();
     ////////////데이터 변경//////////////////
- 
+
     ////////5 a  5 a learn///////////////////
     printf("\nData Change\n");
-    for(int i = 1; i <=MAXNEURONS; i=i+2){
+    for(int i = 1; i <=MAXNEURONS; i=i+2) {
         //memory array init
-        for(int j=0;j<NEURONSIZE;j=j+2){
-            array1[j]   =0xa;
-            array1[j+1] =0x5;
-            array2[j]   =0x5;
-            array2[j+1] =0xa;         
+        for(int j=0; j<NEURONSIZE; j=j+2) {
+            array1[j]   =0xaa;
+            array1[j+1] =0x55;
+            array2[j]   =0x55;
+            array2[j+1] =0xaa;
         }
 
         ncount = learn(array2, NEURONSIZE, i);    //input : data, neuronsize, cat,   return : ncount
         ncount = learn(array1, NEURONSIZE, i+1);    //input : data, neuronsize, cat,   return : ncount
 
-        if(ncount == old_ncnt){    //ncount가 변화가 없으면  
-            if(ncount <=576){
+        if(ncount == old_ncnt) {   //ncount가 변화가 없으면
+            if(ncount <=576) {
                 Learn1 = 1;
                 printf("NCOUNT : %d\n",ncount);
-                error("\n Learn Fail NM500 #1\n");  
-            }
-            else{
+                error("\n Learn Fail NM500 #1\n");
+            } else {
                 Learn2 = 1;
                 printf("NCOUNT : %d\n",ncount);
-                error("\n Learn Fail NM500 #2\n");  
+                error("\n Learn Fail NM500 #2\n");
             }
-        }   
-        //ncount가 증가하였을 경우    
-        else{                 
-            old_ncnt = ncount;    //old ncount 갱신   
+        }
+        //ncount가 증가하였을 경우
+        else {
+            old_ncnt = ncount;    //old ncount 갱신
             printf(".");
-        }    
+        }
     }
     //NCOUNT cheak
     if(ncount == 0xFFFF || ncount == 0x7FFF)     //change
         ncount = MAXNEURONS;
     //printf("NCOUNT : %d ",ncount);
     //end memory write
- //////////////////////////////////////////////////////////////  
+//////////////////////////////////////////////////////////////
     //fprintf(fp,"5 A\n");
     //backup NSR
     temp_NSR = read(NM_NSR);
     //write NSR-SR Mode
     write(NM_NSR, 0x10);
     write(NM_RSTCHAIN, 0);
-    
-    
+
+
 
     //compare memory(cheak)
     printf("\nData Compare \n");
-    for(int i = 1; i<=MAXNEURONS; i++){
+    for(int i = 1; i<=MAXNEURONS; i++) {
         printf(".");
-        for(int j = 0; j<NEURONSIZE; j++){
+        for(int j = 0; j<NEURONSIZE; j++) {
             temp_data[j]=read(NM_COMP);
             //fprintf(fp,"%d,", temp_data[j]);
-            if(i%2 == 1){
-                if(temp_data[j]!= array2[j]){
+            if(i%2 == 1) {
+                if(temp_data[j]!= array2[j]) {
                     printf("\nNID : %d , %d byte Memory Error\n",i,j);
                     Memory_fail = 1;
                 }
-                
-            }
-            else if( i%2 == 0){
-                if(temp_data[j]!= array1[j]){
+
+            } else if( i%2 == 0) {
+                if(temp_data[j]!= array1[j]) {
                     printf("\nNID : %d , %d byte Memory Error\n",i,j);
                     Memory_fail = 1;
                 }
-            }            
+            }
         }
         int cat = read(NM_CAT)& 0x7FFF;
 //        fprintf(fp,"\t%d\n", cat);
@@ -487,64 +485,62 @@
     //fclose(fp);
     //error("Momory test ok");
     if(Memory_fail == 0)
-        printf("\nMemort Test Pass\n"); 
+        printf("\nMemort Test Pass\n");
 }
 
 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++){
+    for(int i=1; i<=MAXNEURONS; i++) {
         // ex
-        //      1.      0 1 0 1.... 
-        //      2.      0 2 0 2.... 
+        //      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){   
+        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++){   
+            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){   
+        } 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++){   
+            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;
         //pc.printf("ncount # %d\n",ncnt);
-        
-        if(ncnt == old_ncnt){    //ncount가 변화가 없으면  
-            if(ncnt <=576){
+
+        if(ncnt == old_ncnt) {   //ncount가 변화가 없으면
+            if(ncnt <=576) {
                 Learn1 = 1;
-                error("\n Learn Fail NM500 #1\n");  
+                error("\n Learn Fail NM500 #1\n");
+            } else {
+                Learn2 = 1;
+                error("\n Learn Fail NM500 #2\n");
             }
-            else{
-                Learn2 = 1;
-                error("\n Learn Fail NM500 #2\n");  
-            }
-        }   
-        //ncount가 증가하였을 경우    
-        else                 
+        }
+        //ncount가 증가하였을 경우
+        else
             old_ncnt = ncnt;    //old ncount 갱신
 
         printf(".");
-    } 
+    }
     printf("\n");
 //    printf("NCOUNT : %d\n",ncnt);
     //학습 완료 후 ncount가 MAXNEURONS보다 작을 경우
@@ -564,142 +560,136 @@
     if(fp == NULL)
         error("Write SD Fail");
     fflush(stdin);
-    
+
     //save NSR
     int TempNSR = read(NM_NSR);
- 
+
     //read NCOUNT
     int nm_cnt = read(NM_NCOUNT);
     if(nm_cnt == 0xFFFF || nm_cnt == 0x7FFF)     //change
         nm_cnt = MAXNEURONS;
     //printf("NCOUNT : %d\n", nm_cnt);
 
-    
+
     //write NSR-SR Mode
-    write(NM_NSR, 0x10);   
-    
+    write(NM_NSR, 0x10);
+
     //reset chain
     write(NM_RSTCHAIN, 0);
-    
+
     //loop until max neruon
-    for(int cnt = 1; cnt<=nm_cnt; cnt++){  
-        
+    for(int cnt = 1; cnt<=nm_cnt; cnt++) {
+
         //read context
         int context = read(NM_NCR);
-       //printf("context #%d\t,", context);
+        //printf("context #%d\t,", context);
         fprintf(fp,"%d\t", context);
-        
+
         //GCR register fail
-        if(context != 5){   
-            if(cnt < 577){ //neuron1 fail
+        if(context != 5) {
+            if(cnt < 577) { //neuron1 fail
                 Gcr1=1;
                 printf("\nNID # %d, GCR # %d \n",cnt,context);
-            }
-            else{                   //neruon2 fail
+            } else {                //neruon2 fail
                 Gcr2=1;
                 printf("\nNID # %d, GCR # %d \n",cnt,context);
             }
         }
         char temp_neuron[NEURONSIZE];
-//        for(int j =0;j<NEURONSIZE;j=j+2){   
+//        for(int j =0;j<NEURONSIZE;j=j+2){
 //            temp_neuron[j] = cnt>>8;          //upper bit save
 //            temp_neuron[j+1] = cnt;           //low bit save
 //        }
-        if(cnt%2 == 1){
-            for(int j =0;j<DATA_CNT;j=j+2){   
+        if(cnt%2 == 1) {
+            for(int j =0; j<DATA_CNT; j=j+2) {
                 temp_neuron[j] = cnt>>8;          //upper bit save
                 temp_neuron[j+1] = cnt;           //low bit save
             }
-            for(int j =DATA_CNT;j<NEURONSIZE; j++){   
+            for(int j =DATA_CNT; j<NEURONSIZE; j++) {
                 temp_neuron[j] = array1[j];          //upper bit save
             }
-        }
-        else if(cnt%2 == 0){
-            for(int j =0;j<DATA_CNT;j=j+2){   
+        } else if(cnt%2 == 0) {
+            for(int j =0; j<DATA_CNT; j=j+2) {
                 temp_neuron[j] = cnt>>8;          //upper bit save
                 temp_neuron[j+1] = cnt;           //low bit save
             }
-            for(int j =DATA_CNT;j<NEURONSIZE; j++){   
+            for(int j =DATA_CNT; j<NEURONSIZE; j++) {
                 temp_neuron[j] = array2[j];          //upper bit save
             }
-        }    
-       //printf("Neruon Data : ");       
+        }
+        //printf("Neruon Data : ");
         //read Neuron data
-        for (int j=0; j<NEURONSIZE; j++){
-            //read COMP 
+        for (int j=0; j<NEURONSIZE; j++) {
+            //read COMP
             neurons[j]=read(NM_COMP);
             fprintf(fp,"%d,", neurons[j]);
             //printf("%d,", neurons[j]);
-            if(temp_neuron[j]!= neurons[j]){
-                
-                if(j<MAXNEURONS/2){
+            if(temp_neuron[j]!= neurons[j]) {
+
+                if(j<MAXNEURONS/2) {
                     printf("NID # %d Memory Fail\n", cnt);
                     Memory1 = 1;
-                }
-                else{
+                } else {
                     printf("NID # %d Memory Fail\n", cnt);
                     Memory2 = 1;
                 }
                 printf("# %d : temp %d, neruon %d\n",j, temp_neuron[j],neurons[j]);
-                    
+
             }
-                
+
         }
 
 
-        
+
         //read AIF
         aif=read(NM_AIF);
         fprintf(fp,"\t%d\t", aif);
         //printf("AIF : %d,\t", aif);
-        
+
         //aif register fail
-        if(aif != 5){
-            if(cnt <= 576){ //neuron1 fail
+        if(aif != 5) {
+            if(cnt <= 576) { //neuron1 fail
                 Aif1=1;
                 printf("\nNID # %d, AIF # %d \n",cnt,aif);
-            }
-            else{                   //neruon2 fail
+            } else {                //neruon2 fail
                 Aif2=1;
                 printf("\nNID # %d, AIF # %d \n",cnt,aif);
             }
-        }      
-        
+        }
+
         //raed minif
         int minif=read(NM_MINIF);
         //printf("MINIF : %d,\t", minif);
         fprintf(fp,"%d\t", minif);
-        
+
         //minif register fail
-        if(minif != 3){
-            if(cnt <= 576){ //neuron1 fail
+        if(minif != 3) {
+            if(cnt <= 576) { //neuron1 fail
                 Minif1 = 1;
                 printf("\nNID # %d, MINIF # %d \n",cnt,minif);
-            }
-            else{                   //neruon2 fail
+            } else {                //neruon2 fail
                 Minif2 = 1;
                 printf("\nNID # %d, MINIF # %d \n",cnt,minif);
             }
         }
-        
+
         //read cat
         int cat = read(NM_CAT) & 0x7FFF;
-        fprintf(fp,"%d\t", cat);      
+        fprintf(fp,"%d\t", cat);
         fprintf(fp,"\n");
         //printf("****CAT : %d\n", cat);
-        
+
         //reset chain test
 //        if(cnt > 2 && cat == 1)
 //            CMN_RC = 1;
-            
+
         //cat test
-        if(cnt != cat){
+        if(cnt != cat) {
             printf("cnt : %d , cat : %d \n", cnt, cat);
-            if(cnt <= 576){
+            if(cnt <= 576) {
                 Cat1 = 1;
                 printf("\n NM500 #1 Cat Fail # %d\n", cnt);
-            }
-            else {
+            } else {
                 Cat2 = 1;
                 printf("\n NM500 #2 Cat Fail # %d\n", cnt);
             }
@@ -707,8 +697,8 @@
         printf(".");
     }
     printf("\n");
-        
- 
+
+
     fclose(fp);
     write(NM_NSR, TempNSR); // set the NN back to its calling status
     return read(NM_NCOUNT);
@@ -720,48 +710,48 @@
     int context,aif,minif,cat;
     char neurons[NEURONSIZE];
     fp = fopen("/sd/data.txt", "r");
-    
+
 
     if(fp == NULL)
         error("Read SD Fail");
 
     //save NSR
     int TempNSR = read(NM_NSR);
-    
+
     //write NSR-SR Mode
-    write(NM_NSR, 0x10);   
-    
+    write(NM_NSR, 0x10);
+
     //reset chain
     write(NM_RSTCHAIN, 0);
-           
-    //SD data read -> Neurons   
-    for(int i=1;i<=MAXNEURONS;i++){
+
+    //SD data read -> Neurons
+    for(int i=1; i<=MAXNEURONS; i++) {
         //read context
         fscanf(fp,"%d", &context);
-      // printf("context # %d ", context);
+        // printf("context # %d ", context);
         write(NM_GCR,context);
-        
-        
+
+
         //read comp
-        for (int j=0; j<NEURONSIZE; j++){
-            //read COMP 
+        for (int j=0; j<NEURONSIZE; j++) {
+            //read COMP
             fscanf(fp,"%d,", &neurons[j]);
-          // printf("%d\t,", neurons[j]);
+            // printf("%d\t,", neurons[j]);
             write(NM_COMP,neurons[j]);
         }
         fscanf(fp,"\t%d\t", &aif);
-      // printf("AIF # %d \t", aif);
+        // printf("AIF # %d \t", aif);
         write(NM_AIF,aif);
 
         fscanf(fp,"%d\t", &minif);
-      // printf("MINIF # %d", minif);
-        write(NM_MINIF,minif);    
-        
+        // printf("MINIF # %d", minif);
+        write(NM_MINIF,minif);
+
         fscanf(fp,"%d\t", &cat);
-     //  printf("CAT # %d \n", cat);
+        //  printf("CAT # %d \n", cat);
         write(NM_CAT,cat);
         printf(".");
-        
+
     }
     printf("\n");
     fclose(fp);
@@ -779,24 +769,24 @@
         CMN_Nsr = 1;    //fail
 
     //RBF Mode setting
-    setRBF();         
+    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;    
+    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; 
+        CMN_Nsr = 1;
 }
 
 
@@ -804,22 +794,21 @@
 {
     int distance, cat, nid;
     //number of neuron 1156
-        for(int i=1;i<=MAXNEURONS;i++){          
-        if(i%2 == 1){
-            for(int j =0;j<DATA_CNT;j=j+2){   
+    for(int i=1; i<=MAXNEURONS; i++) {
+        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++){   
+            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){   
+        } 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++){   
+            for(int j =DATA_CNT; j<NEURONSIZE; j++) {
                 vector[j] = array2[j];          //upper bit save
             }
         }
@@ -830,7 +819,7 @@
         id_fail = nsr & 0x70;
         if(id_fail == 0)
             nsr_fail = 1;
-        
+
         printf(".");
 //        printf(" neuron# %d \t", nid);
 //        printf(" distance = %d,\t ", distance);
@@ -838,31 +827,265 @@
 
         //nid test
         //cat register pass AND (nid,cat diff)
-        if((nid!=cat) && (Cat1==0 && Cat2==0)){
-            if(nid < 576){
+        if((nid!=cat) && (Cat1==0 && Cat2==0)) {
+            if(nid < 576) {
                 Nid1 = 1;
                 printf("\nNID Register Fail # %d\n",nid);
-            }
-            else{
+            } else {
                 Nid2 = 1;
                 printf("\nNID Register Fail # %d\n",nid);
             }
         }
-        if(distance != 0){
-            if(nid < 576){
+        if(distance != 0) {
+            if(nid < 576) {
                 Dist1 = 1;
                 printf("\nDistance Register Fail # %d, Distance # %d\n",nid,distance);
-            }
-            else{
+            } else {
                 Dist2 = 1;
                 printf("\nDistance Register Fail # %d, Distance # %d\n",nid,distance);
             }
         }
-    
+
+    }
+    printf("\n");
+
+}
+
+void Cell_Test(void)
+{
+    unsigned char cell_vector[NEURONSIZE];
+    unsigned char read_vector[NEURONSIZE];
+    printf("\n\nAging Test\n\n");
+    int aging_cnt = 0;
+    int mode = 0;
+    int category, ncount,context,aif, cat;
+    int aging_result=0;
+
+    mode = 1001;
+    if(mode == 1001) {
+//        printf("Mode : %d",mode);
+        printf("\n\nPatern Write Test (0xAA, 0x55)");
+        forget();
+        write(NM_NSR,0x0000);
+        for(int i = 1; i<= MAXNEURONS; i++) {
+            category = i;
+            //pattern make
+            for (int j = 0; j < NEURONSIZE; j++) {
+                if (j % 2) cell_vector[j] = 0xAA;
+                else       cell_vector[j] = 0x55;
+            }
+            //learn
+            ncount = learn(cell_vector, NEURONSIZE, category);
+            //learned neuron count
+            if ((ncount != 0xFFFF) && (ncount != i)) {
+                printf("\n\n Patern Write Test Fail");
+                while (1);
+            }
+        }
+        printf("\n\nPatern Write Test Pass");
+        mode = 1002;
+    }
+
+    if (mode == 1002) {
+//        printf("Mode : %d",mode);
+        printf("\n\nPatern Read Test (0xAA, 0x55)");
+//        hnn.setNsr(0x0010);
+        write(NM_NSR,0x0010);
+//        hnn.resetChain();
+        write(NM_RSTCHAIN,0);
+//        for (int i = 1; i <= repeat_cnt; i++) {
+        for (int i = 1; i <= MAXNEURONS; i++) {
+//            context = hnn.getNcr();
+            context = read(NM_NCR);
+//            hnn.readCompVector(vector16, AGING_LENGTH);
+            for (int j = 0; j < NEURONSIZE; j++) {
+                read_vector[j] = read(NM_COMP);
+//                printf("%x, ",read_vector[j]);
+            }
+//            printf("\n");
+
+            aif = read(NM_AIF);
+            cat = read(NM_CAT);
+//            aif = hnn.getAif();
+//            cat = hnn.getCat();
+            for (int j = 0; j < NEURONSIZE; j++) {
+                if ((((j % 2) == 1) && (read_vector[j] != 0xAA)) || (((j % 2) == 0) && (read_vector[j] != 0x55))) {
+                    //Serial.print("\n Error#2!!!");
+                    printf("\n\nPatern Read Test Fail");
+                    printf("\n i=");
+                    printf("%d",i);
+                    printf(", j=");
+                    printf("%d",j);
+                    printf(", vector[j]=");
+                    printf("%x",read_vector[j]);
+                    aging_result = 0;
+                    break;
+                }
+
+
+            }
+            if ((context != 1) || (aif != 2) || ((cat & 0x7FFF) != i)) {
+                //Serial.print("\n Error#3!!!");
+                printf("\n\nRead Register Test Fail");
+                printf("\n i=");
+                printf("%d",i);
+                printf(", context=");
+                printf("%d",context);
+                printf(", aif=");
+                printf("%d",aif);
+                printf(", cat=");
+                printf("%d",cat);
+                aging_result = 0;
+            }
+
+        }
+        printf("\n\nPatern Read Test Pass");
+        mode = 1003;
     }
-    printf("\n");  
+
+    //0x55, 0xaa
+    if(mode == 1003) {
+//        printf("Mode : %d",mode);
+        printf("\n\nPatern Write Test (0x55, 0xAA)");
+        forget();
+        write(NM_NSR,0x0000);
+        for(int i = 1; i<= MAXNEURONS; i++) {
+            category = i;
+            for (int j = 0; j < NEURONSIZE; j++) {
+                if (j % 2) cell_vector[j] = 0x55;
+                else       cell_vector[j] = 0xaa;
+            }
+
+            ncount = learn(cell_vector, NEURONSIZE, category);
+            if ((ncount != 0xFFFF) && (ncount != i)) {
+                printf("\n\n Patern Write Test Fail");
+                while (1);
+            }
+        }
+        printf("\n\nnPatern Write Test Pass");
+        mode = 1004;
+
+
+    }
+    if (mode == 1004) {
+//        printf("Mode : %d",mode);
+        printf("\n\nPatern Read Test (0x55, 0xAA)");
+//        hnn.setNsr(0x0010);
+        write(NM_NSR,0x0010);
+//        hnn.resetChain();
+        write(NM_RSTCHAIN,0);
+//        for (int i = 1; i <= repeat_cnt; i++) {
+        for (int i = 1; i <= MAXNEURONS; i++) {
+//            context = hnn.getNcr();
+            context = read(NM_NCR);
+//            hnn.readCompVector(vector16, AGING_LENGTH);
+            for (int j = 0; j < NEURONSIZE; j++) {
+                read_vector[j] = read(NM_COMP);
+//                printf("0x%x  ", read_vector[j]);
+            }
+//            printf("\n");
+
+            aif = read(NM_AIF);
+            cat = read(NM_CAT);
+//            aif = hnn.getAif();
+//            cat = hnn.getCat();
+            for (int j = 0; j < NEURONSIZE; j++) {
+                if ((((j % 2) == 1) && (read_vector[j] != 0x55)) || (((j % 2) == 0) && (read_vector[j] != 0xAA))) {
+                    //Serial.print("\n Error#2!!!");
+                    printf("\n\nPatern Read Test Fail");
+                    printf("\n i=");
+                    printf("%d",i);
+                    printf(", j=");
+                    printf("%d",j);
+                    printf(", vector[j]=");
+                    printf("%x",read_vector[j]);
+                    aging_result = 0;
+                    break;
+                }
+
 
-} 
+            }
+            if ((context != 1) || (aif != 2) || ((cat & 0x7FFF) != i)) {
+                //Serial.print("\n Error#3!!!");
+                printf("\n\nRead Register Test Fail");
+                printf("\n i=");
+                printf("%d",i);
+                printf(", context=");
+                printf("%d",context);
+                printf(", aif=");
+                printf("%d",aif);
+                printf(", cat=");
+                printf("%d",cat);
+                aging_result = 0;
+            }
+
+        }
+        printf("\n\nPatern Read Test Pass");
+        mode = 1005;
+    }
+
+
+
+    if (mode == 1005) {
+//        printf("Mode : %d",mode);
+        printf("\n\nIncrease pattern Test");
+        for (int i = 0; i < NEURONSIZE; i++) {
+            cell_vector[i] = i;
+//            printf("%d, ",cell_vector[i]);
+        }
+        write(NM_NSR,0x0000);
+        forget();
+        write(NM_NSR,0x0010);
+//        hnn.setNsr(0x0000);
+//        hnn.forget();
+//        hnn.setNsr(0x0010);
+        for (int i = 1; i <= MAXNEURONS; i++) {
+            for (int j = 0; j < NEURONSIZE; j++) {
+                write(NM_COMP, cell_vector[j]);
+            }
+            write(NM_CAT,1);
+
+            //hnn.writeCompVector(vector16, AGING_LENGTH);
+            //hnn.setCat(0x0001);
+        }
+        write(NM_NSR,0x0000);
+        write(NM_NSR,0x0010);
+        write(NM_RSTCHAIN,0);
+//        hnn.setNsr(0x0000);
+//        hnn.setNsr(0x0010);
+//        hnn.resetChain();
+        for (int i = 1; i <= MAXNEURONS; i++) {
+//            hnn.readCompVector(vector16, AGING_LENGTH);
+            for (int j = 0; j < NEURONSIZE; j++) {
+                read_vector[j]=read(NM_COMP);
+  //              if(i == 629) {
+//                    printf("%d, ", read_vector[j]);
+//                }
+                if (read_vector[j] != cell_vector[j]) {
+                    //Serial.print("\n Error#7!!!");
+                    printf("\n i=");
+                    printf("%d",i);
+                    printf(", j=");
+                    printf("%d",j);
+                    printf(", read_vector[j]=");
+                    printf("%x",read_vector[j]);
+                    printf(", cell_vector[j]=");
+                    printf("%x",cell_vector[j]);
+                    printf("\nIncrease pattern Test Fail\n");
+                    while(1);
+                    break;
+                }
+            }
+            read(NM_CAT);
+//            hnn.getCat();
+        }
+        write(NM_NSR,0x0000);
+//        hnn.setNsr(0x0000);
+        printf("\nIncrease pass\n");
+        mode = 1006;
+        while(1);
+    }
+}