Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
NM500.cpp
00001 #include "NM500.h" 00002 #include "mbed.h" 00003 //#include "SDFileSystem.h" 00004 00005 //SDFileSystem sd(SD_MOSI, SD_MISO, SD_SCK, SD_CS, "sd"); // the pinout on the mbed Cool Components workshop board 00006 00007 SPI NM500(SPI_MOSI, SPI_MISO, SPI_SCK); 00008 00009 //Serial test(SERIAL_TX, SERIAL_RX); 00010 //extern int Reset_fail; 00011 //extern int CMN_RC; 00012 //extern int aif_fail; 00013 //extern int ncount_fail; 00014 //extern int Gcr1; 00015 //extern int Gcr2; 00016 //extern int Aif1; 00017 //extern int Aif2; 00018 //extern int Minif1; 00019 //extern int Minif2; 00020 //extern int Maxif1; 00021 //extern int Maxif2; 00022 //extern int Cat1; 00023 //extern int Cat2; 00024 // 00025 //extern int Memory1; 00026 //extern int Memory2; 00027 // 00028 //extern char array1[NEURONSIZE]; 00029 //extern char array2[NEURONSIZE]; 00030 00031 00032 //FILE *fp=NULL; 00033 00034 DigitalOut BC_CS(D9); 00035 //DigitalOut SDCard_CS(D10); 00036 //DigitalOut CLK_EN(PA_15); 00037 00038 //SPI init 00039 void SPI_Init(void) 00040 { 00041 00042 BC_CS = HIGH; //disable 00043 00044 NM500.format(8,0); 00045 NM500.frequency(1000000); 00046 printf("SPI Init OK\n"); 00047 00048 00049 00050 } 00051 int begin() 00052 { 00053 return clearNeurons(); //neruon init() 00054 } 00055 //void RESET_Test(void) 00056 //{ 00057 // int nsr = read(NM_NSR); 00058 // int gcr = read(NM_GCR); 00059 // int minif = read(NM_MINIF); 00060 // int maxif = read(NM_MAXIF); 00061 // 00062 // printf("RESET Test\n"); 00063 // 00064 // if(nsr != 0x00) 00065 // Reset_fail = 1; 00066 // if(gcr != 0x01) 00067 // Reset_fail = 1; 00068 // if(minif != 0x02) 00069 // Reset_fail = 1; 00070 // if(maxif != 0x4000) 00071 // Reset_fail = 1; 00072 // 00073 // if(Reset_fail == 0) 00074 // printf("Reset Test Pass\n"); 00075 // else 00076 // error("Reset the NM500 and proceed"); 00077 //// printf("NSR : 0x%x\n", nsr); 00078 //// printf("GCR : 0x%x\n", gcr); 00079 //// printf("MINIF : 0x%x\n", minif); 00080 //// printf("MAXIF : 0x%x\n", maxif); 00081 // 00082 // 00083 //} 00084 //forget 00085 void forget() 00086 { 00087 write(NM_FORGET, 0); 00088 } 00089 int clearNeurons() 00090 { 00091 write(NM_FORGET,0); 00092 write(NM_NSR, 0x10); 00093 for (int i=0; i< NEURONSIZE; i++) write(NM_TESTCOMP,0); 00094 write(NM_NSR, 0); 00095 00096 if(read(NM_MINIF)==2) 00097 return(0); //clear ok 00098 else 00099 return(1); //clear error 00100 00101 } 00102 int learn(unsigned char vector[], int length, int category) 00103 { 00104 broadcast(vector, length); 00105 write(NM_CAT,category); 00106 return(read(NM_NCOUNT)); 00107 } 00108 00109 int classify(unsigned char vector[], int length, int* distance, int* category, int* nid) 00110 { 00111 broadcast(vector, length); 00112 *distance = read(NM_DIST); 00113 *category = read(NM_CAT) & 0x7FFF; //Degenerated bit15 is masked 00114 *nid = read(NM_NID); 00115 00116 return(read(NM_NSR)); 00117 } 00118 00119 void setRBF() 00120 { 00121 int tempNSR=read(NM_NSR); 00122 write(NM_NSR, tempNSR & 0xDF); 00123 } 00124 void setKNN() 00125 { 00126 int tempNSR = read(NM_NSR); 00127 write(NM_NSR, tempNSR | 0x20); 00128 } 00129 00130 //Read the selected neuron data. 00131 int NCOUNT() 00132 { 00133 return(read(NM_NCOUNT)); 00134 } 00135 00136 00137 //write data 00138 void write(char reg, int data) 00139 { 00140 NM500.format(8,0); 00141 BC_CS = LOW; 00142 NM500.write(1); //dummy 00143 NM500.write(CM1K+0x80); 00144 NM500.write(0); 00145 NM500.write(0); 00146 NM500.write(reg); 00147 NM500.write(0); 00148 NM500.write(0); 00149 NM500.write(1); 00150 NM500.write((unsigned char)(data >> 8)); // upper data 00151 NM500.write((unsigned char)(data & 0x00FF)); // lower data 00152 //for(int i=0;i<10;i++); 00153 BC_CS = HIGH; 00154 00155 00156 } 00157 00158 //read data 00159 int read(char reg) 00160 { 00161 NM500.format(8,0); 00162 BC_CS = LOW; //BC enable 00163 00164 NM500.write(1); //dummy 00165 NM500.write(CM1K); 00166 NM500.write(0); 00167 NM500.write(0); 00168 NM500.write(reg); 00169 NM500.write(0); 00170 NM500.write(0); 00171 NM500.write(1); 00172 int data = NM500.write(0); // Send 0 to push upper data out 00173 data = (data << 8) + NM500.write(0); // Send 0 to push lower data out 00174 BC_CS = HIGH; 00175 return(data); 00176 } 00177 int broadcast(unsigned char vector[], int length) 00178 { 00179 for(int i=0; i<length-1; i++) 00180 write(NM_COMP,vector[i]); 00181 write(NM_LCOMP, vector[length-1]); 00182 00183 return read(NM_NSR); 00184 } 00185 00186 //int NeuronToSD() 00187 //{ 00188 // 00189 // char neurons[NEURONSIZE]; 00190 // int aif=0; 00191 // fp = fopen("/sd/data.txt", "w"); 00192 // if(fp == NULL) 00193 // error("Write SD Fail"); 00194 // fflush(stdin); 00195 // 00196 // //save NSR 00197 // int TempNSR = read(NM_NSR); 00198 // 00199 // //read NCOUNT 00200 // int nm_cnt = read(NM_NCOUNT); 00201 // if(nm_cnt == 0xFFFF || nm_cnt == 0x7FFF) //change 00202 // nm_cnt = MAXNEURONS; 00203 // //printf("NCOUNT : %d\n", nm_cnt); 00204 // 00205 // 00206 // //write NSR-SR Mode 00207 // write(NM_NSR, 0x10); 00208 // 00209 // //reset chain 00210 // write(NM_RSTCHAIN, 0); 00211 // 00212 // //loop until max neruon 00213 // for(int cnt = 1; cnt<=nm_cnt; cnt++){ 00214 // 00215 // //read context 00216 // int context = read(NM_NCR); 00217 // //printf("context #%d\t,", context); 00218 // fprintf(fp,"%d\t", context); 00219 // 00220 // //GCR register fail 00221 // if(context != 5){ 00222 // if(cnt < MAXNEURONS/2){ //neuron1 fail 00223 // Gcr1=1; 00224 // printf("\nNID # %d, GCR # %d \n",cnt,context); 00225 // } 00226 // else{ //neruon2 fail 00227 // Gcr2=1; 00228 // printf("\nNID # %d, GCR # %d \n",cnt,context); 00229 // } 00230 // } 00231 // char temp_neuron[NEURONSIZE]; 00232 //// for(int j =0;j<NEURONSIZE;j=j+2){ 00233 //// temp_neuron[j] = cnt>>8; //upper bit save 00234 //// temp_neuron[j+1] = cnt; //low bit save 00235 //// } 00236 // if(cnt%2 == 1){ 00237 // for(int j =0;j<DATA_CNT;j=j+2){ 00238 // temp_neuron[j] = cnt>>8; //upper bit save 00239 // temp_neuron[j+1] = cnt; //low bit save 00240 // } 00241 // for(int j =DATA_CNT;j<NEURONSIZE; j++){ 00242 // temp_neuron[j] = array1[j]; //upper bit save 00243 // } 00244 // } 00245 // else if(cnt%2 == 0){ 00246 // for(int j =0;j<DATA_CNT;j=j+2){ 00247 // temp_neuron[j] = cnt>>8; //upper bit save 00248 // temp_neuron[j+1] = cnt; //low bit save 00249 // } 00250 // for(int j =DATA_CNT;j<NEURONSIZE; j++){ 00251 // temp_neuron[j] = array2[j]; //upper bit save 00252 // } 00253 // } 00254 // //printf("Neruon Data : "); 00255 // //read Neuron data 00256 // for (int j=0; j<NEURONSIZE; j++){ 00257 // //read COMP 00258 // neurons[j]=read(NM_COMP); 00259 // fprintf(fp,"%d,", neurons[j]); 00260 // //printf("%d,", neurons[j]); 00261 // if(temp_neuron[j]!= neurons[j]){ 00262 // 00263 // if(j<MAXNEURONS/2){ 00264 // printf("NID # %d Memory Fail\n", cnt); 00265 // Memory1 = 1; 00266 // } 00267 // else{ 00268 // printf("NID # %d Memory Fail\n", cnt); 00269 // Memory2 = 1; 00270 // } 00271 // printf("# %d : temp %d, neruon %d\n",j, temp_neuron[j],neurons[j]); 00272 // 00273 // } 00274 // 00275 // } 00276 // 00277 // 00278 // 00279 // //read AIF 00280 // aif=read(NM_AIF); 00281 // fprintf(fp,"\t%d\t", aif); 00282 // //printf("AIF : %d,\t", aif); 00283 // 00284 // //aif register fail 00285 // if(aif != 5){ 00286 // if(cnt <= 576){ //neuron1 fail 00287 // Aif1=1; 00288 // printf("\nNID # %d, AIF # %d \n",cnt,aif); 00289 // } 00290 // else{ //neruon2 fail 00291 // Aif2=1; 00292 // printf("\nNID # %d, AIF # %d \n",cnt,aif); 00293 // } 00294 // } 00295 // 00296 // //raed minif 00297 // int minif=read(NM_MINIF); 00298 // //printf("MINIF : %d,\t", minif); 00299 // fprintf(fp,"%d\t", minif); 00300 // 00301 // //minif register fail 00302 // if(minif != 3){ 00303 // if(cnt <= 576){ //neuron1 fail 00304 // Minif1 = 1; 00305 // printf("\nNID # %d, MINIF # %d \n",cnt,minif); 00306 // } 00307 // else{ //neruon2 fail 00308 // Minif2 = 1; 00309 // printf("\nNID # %d, MINIF # %d \n",cnt,minif); 00310 // } 00311 // } 00312 // 00313 // //read cat 00314 // int cat = read(NM_CAT) & 0x7FFF; 00315 // fprintf(fp,"%d\t", cat); 00316 // fprintf(fp,"\n"); 00317 // //printf("****CAT : %d\n", cat); 00318 // 00319 // //reset chain test 00320 //// if(cnt > 2 && cat == 1) 00321 //// CMN_RC = 1; 00322 // 00323 // //cat test 00324 // if(cnt != cat){ 00325 // printf("cnt : %d , cat : %d \n", cnt, cat); 00326 // if(cnt <= 576){ 00327 // Cat1 = 1; 00328 // printf("\n NM500 #1 Cat Fail # %d\n", cnt); 00329 // } 00330 // else { 00331 // Cat2 = 1; 00332 // printf("\n NM500 #2 Cat Fail # %d\n", cnt); 00333 // } 00334 // } 00335 // printf("."); 00336 // } 00337 // printf("\n"); 00338 // 00339 // 00340 // fclose(fp); 00341 // write(NM_NSR, TempNSR); // set the NN back to its calling status 00342 // return read(NM_NCOUNT); 00343 //} 00344 // 00345 // 00346 //int SDToNeurons() 00347 //{ 00348 // int context,aif,minif,cat; 00349 // char neurons[NEURONSIZE]; 00350 // fp = fopen("/sd/data.txt", "r"); 00351 // 00352 // 00353 // if(fp == NULL) 00354 // error("Read SD Fail"); 00355 // 00356 // //save NSR 00357 // int TempNSR = read(NM_NSR); 00358 // 00359 // //write NSR-SR Mode 00360 // write(NM_NSR, 0x10); 00361 // 00362 // //reset chain 00363 // write(NM_RSTCHAIN, 0); 00364 // 00365 // //SD data read -> Neurons 00366 // for(int i=1;i<=MAXNEURONS;i++){ 00367 // //read context 00368 // fscanf(fp,"%d", &context); 00369 // // printf("context # %d ", context); 00370 // write(NM_GCR,context); 00371 // 00372 // 00373 // //read comp 00374 // for (int j=0; j<NEURONSIZE; j++){ 00375 // //read COMP 00376 // fscanf(fp,"%d,", &neurons[j]); 00377 // // printf("%d\t,", neurons[j]); 00378 // write(NM_COMP,neurons[j]); 00379 // } 00380 // fscanf(fp,"\t%d\t", &aif); 00381 // // printf("AIF # %d \t", aif); 00382 // write(NM_AIF,aif); 00383 // 00384 // fscanf(fp,"%d\t", &minif); 00385 // // printf("MINIF # %d", minif); 00386 // write(NM_MINIF,minif); 00387 // 00388 // fscanf(fp,"%d\t", &cat); 00389 // // printf("CAT # %d \n", cat); 00390 // write(NM_CAT,cat); 00391 // printf("."); 00392 // 00393 // } 00394 // printf("\n"); 00395 // fclose(fp); 00396 // write(NM_NSR, TempNSR); // set the NN back to its calling status 00397 // return NCOUNT(); 00398 //} 00399 00400 00401
Generated on Mon Oct 31 2022 00:25:55 by
