NM500 Lib
Diff: NM500.cpp
- Revision:
- 5:dacd883e994d
- Parent:
- 4:b23c96320c3f
- Child:
- 6:4bc46c554aed
--- a/NM500.cpp Fri May 19 07:26:04 2017 +0000 +++ b/NM500.cpp Mon May 29 04:32:19 2017 +0000 @@ -6,39 +6,29 @@ SPI NM500(SPI_MOSI, SPI_MISO, SPI_SCK); +//Serial test(SERIAL_TX, SERIAL_RX); + FILE *fp=NULL; - DigitalOut BC_CS(D9); - -DigitalOut Flash(D8); DigitalOut SDCard_CS(D10); - - - +DigitalOut CLK_EN(PA_15); // NM500 init int begin() { //test CS - BC_CS = HIGH; - Flash = HIGH; + BC_CS = HIGH; //disable + SDCard_CS = HIGH; //disable + CLK_EN = HIGH; //enable NM500.format(8,0); - NM500.frequency(1000000); - + NM500.frequency(3000000); - wait(0.2); - - BC_CS = LOW; - Flash = LOW; - wait(0.2); - - BC_CS = HIGH; - Flash = HIGH; wait(0.2); + mkdir("/sd", 0777); fp = fopen("/sd/data.txt", "w"); @@ -47,18 +37,11 @@ error("Could not open file for write\n"); } fclose(fp); + // test.baud(115200); return clearNeurons(); //neruon init() } - -//neruon data -void getNeuronsInfo(int* neuronSize, int* neuronsAvailable, int* neuronsCommitted) -{ - *neuronSize=NEURONSIZE; - *neuronsAvailable=MAXNEURONS; - *neuronsCommitted= read(NM_NCOUNT); -} //forget void forget() { @@ -89,26 +72,10 @@ *distance = read(NM_DIST); *category = read(NM_CAT) & 0x7FFF; //Degenerated bit15 is masked *nid = read(NM_NID); + //printf("nid :%d",read(NM_NID)); return(read(NM_NSR)); } -void setContext(int context, int minif, int maxif) -{ - // context[15-8]= unused - // context[7]= Norm (0 for L1; 1 for LSup) - // context[6-0]= Active context value - write(NM_GCR, context); - write(NM_MINIF, minif); - write(NM_MAXIF, maxif); -} -void getContext(int* context, int* minif, int* maxif) -{ - // context[15-8]= unused - // context[7]= Norm (0 for L1; 1 for LSup) - // context[6-0]= Active context value - *context = read(NM_GCR); - *minif = read(NM_MINIF); - *maxif = read(NM_MAXIF); -} + void setRBF() { int tempNSR=read(NM_NSR); @@ -181,6 +148,7 @@ { char neurons[NEURONSIZE]; + int aif=0; fp = fopen("/sd/data.txt", "w"); if(fp == NULL) @@ -192,6 +160,10 @@ //read NCOUNT int nm_cnt = read(NM_NCOUNT); + if(nm_cnt == 0x7fff) //change + nm_cnt = MAXNEURONS; + //printf("NCOUNT : %d\n", nm_cnt); + //write NSR-SR Mode write(NM_NSR, 0x10); @@ -200,25 +172,25 @@ write(NM_RSTCHAIN, 0); //loop until max neruon - for(int cnt = 0; 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); - //printf("Neruon Data : "); + //printf("Neruon Data : "); //read Neuron data for (int j=0; j<NEURONSIZE; j++){ //read COMP neurons[j]=read(NM_COMP); fprintf(fp,"%d,", neurons[j]); - // printf("%d,", neurons[j]); + //printf("%d,", neurons[j]); } //read AIF - int aif=read(NM_AIF); + aif=read(NM_AIF); //printf("AIF : %d,\t", aif); fprintf(fp,"\t%d\t", aif); @@ -234,9 +206,11 @@ fprintf(fp,"%d\t", cat); fprintf(fp,"\n"); - //printf("\n"); + printf("."); + } + printf("\n"); fclose(fp); @@ -254,9 +228,7 @@ if(fp == NULL) error("Read SD Fail"); - else - printf("SD Read OK\n"); - + //save NSR int TempNSR = read(NM_NSR); @@ -267,10 +239,10 @@ write(NM_RSTCHAIN, 0); //SD data read -> Neurons - for(int i=0;i<MAXNEURONS;i++){ + 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); @@ -278,43 +250,46 @@ 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); + // printf("MINIF # %d", minif); write(NM_MINIF,minif); fscanf(fp,"%d\t", &cat); - // printf("CAT # %d \n", cat); - write(NM_CAT,cat); + // printf("CAT # %d \n", cat); + write(NM_CAT,cat); + printf("."); } + printf("\n"); fclose(fp); write(NM_NSR, TempNSR); // set the NN back to its calling status return NCOUNT(); } -void readNeuron(int nid, unsigned char model[], int* context, int* aif, int* category) -{ - int TempNSR=read(NM_NSR); - write(NM_NSR, 0x10); - write(NM_RSTCHAIN, 0); - if (nid>0) - { - // move to index in the chain of neurons - for (int i=0; i<nid; i++) read(NM_CAT); - } - *context=read(NM_NCR); - for (int j=0; j<NEURONSIZE; j++) model[j]=read(NM_COMP); - *aif=read(NM_AIF); - *category=read(NM_CAT); - *category=*category&0x7fff; - write(NM_NSR, TempNSR); // set the NN back to its calling status -} +//void readNeuron(int nid, unsigned char model[], int* context, int* aif, int* category) +//{ +// int TempNSR=read(NM_NSR); +// write(NM_NSR, 0x10); +// write(NM_RSTCHAIN, 0); +// if (nid>0) +// { +// // move to index in the chain of neurons +// for (int i=0; i<nid; i++) read(NM_CAT); +// } +// *context=read(NM_NCR); +// for (int j=0; j<NEURONSIZE; j++) model[j]=read(NM_COMP); +// *aif=read(NM_AIF); +// *category=read(NM_CAT); +// *category=*category&0x7fff; +// write(NM_NSR, TempNSR); // set the NN back to its calling status +//} +// +// +// +// - - -