NM500 Lib

NM500.cpp

Committer:
Nasungil
Date:
2017-05-18
Revision:
1:5cdfdb2e5691
Parent:
0:fadd3703a42c
Child:
2:52da04ab1c78

File content as of revision 1:5cdfdb2e5691:

#include "NM500.h"
#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

SPI NM500(SPI_MOSI, SPI_MISO, SPI_SCK);

FILE *fp=NULL;


DigitalOut BC_CS(D10);
DigitalOut Flash(D8);
DigitalOut SDCard_CS(D6);


Serial test(SERIAL_TX, SERIAL_RX);


// NM500 init
int begin()
{
    BC_CS = HIGH;
    Flash = HIGH;
    
    NM500.format(8,0);
    NM500.frequency(1000000);


    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"); 
    
    if(fp == NULL)
    {
        error("Could not open file for write\n");
    }
    fclose(fp);

    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()
{
    write(NM_FORGET, 0);
}
int clearNeurons()
{
    int TempNSR=read(NM_NSR);
    write(NM_FORGET,0);
    write(NM_NSR, 16);
    for (int i=0; i< NEURONSIZE; i++) write(NM_TESTCOMP,0);
    write(NM_NSR, TempNSR);
    if((read(NM_MINIF)==2))
        return(0);      //clear ok
    else
        return(1);      //clear error
}
int learn(unsigned char vector[], int length, int category)
{
    broadcast(vector, length);
    write(NM_CAT,category);
    return(read(NM_NCOUNT));
}
/*
int classify(unsigned char vector[], int length, int K, int distance[], int category[], int nid[])
{
    int recoNbr=0;
    broadcast(vector, length);
    for (int i=0; i<K; i++) {
        distance[i] = read(NM_DIST);
        if (distance[i]==0xFFFF) {
            category[i]=0xFFFF;
            nid[i]=0xFFFF;
        } else {
            recoNbr++;
            category[i]= read(NM_CAT) & 0x7FFF; //Degenerated bit15 is masked
            nid[i] = read(NM_NID);
        }
    }
    return(recoNbr);
}
*/
int classify(unsigned char vector[], int length, int* distance, int* category, int* nid)
{
    broadcast(vector, length);
    *distance = read(NM_DIST);
    *category = read(NM_CAT) & 0x7FFF; //Degenerated bit15 is masked
    *nid = 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);
    write(NM_NSR, tempNSR & 0xDF);
}
void setKNN()
{
    int tempNSR = read(NM_NSR);
    write(NM_NSR, tempNSR | 0x20);
}

//Read the selected neuron data.
int NCOUNT()
{
    return(read(NM_NCOUNT));
}


//write data
void write(char reg, int data)
{
    NM500.format(8,0);
    BC_CS = LOW;
    NM500.write(1);      //dummy
    
    NM500.write(CM1K+0x80);
    
    NM500.write(0);
    
    NM500.write(0);
    
    NM500.write(reg);
    
    NM500.write(0);
    
    NM500.write(0);
    
    NM500.write(1);
    
    NM500.write((unsigned char)(data >> 8)); // upper data
    
    NM500.write((unsigned char)(data & 0x00FF)); // lower data
    
    //for(int i=0;i<10;i++);
    BC_CS = HIGH;


}

//read data
int read(char reg)
{
    NM500.format(8,0);
    BC_CS = LOW;        //BC enable

    NM500.write(1);      //dummy
    NM500.write(CM1K);
    NM500.write(0);
    NM500.write(0);
    NM500.write(reg);
    NM500.write(0);
    NM500.write(0);
    NM500.write(1);
    int data = NM500.write(0); // Send 0 to push upper data out
    data = (data << 8) + NM500.write(0); // Send 0 to push lower data out
    BC_CS = HIGH;
    return(data);
}
int broadcast(unsigned char vector[], int length)
{
    for(int i=0; i<length-1; i++)
        write(NM_COMP,vector[i]);
    write(NM_LCOMP, vector[length-1]);

    return read(NM_NSR);
}
 
void NeuronToSD()
{
    char str[MAX_SIZE];
    char neurons[NEURONSIZE];
    printf("Insert data:");
    scanf("%s",str);
    printf("\r\n");
    fp = fopen("/sd/data.txt", "w");
    fflush(stdin);
    
    //save NSR
    int TempNSR = read(NM_NSR);
 
    //read NCOUNT
    int nm_cnt = read(NM_NCOUNT);
    
    //write NSR-SR Mode
    write(NM_NSR, 0x10);   
    printf("%d",nm_cnt);
    
    //reset chain
    write(NM_RSTCHAIN, 0);
    
    
    
    
    //fprintf(fp,"%s\t", str);

    
    //loop until max neruon
    //for(int cnt = 0; cnt<nm_cnt; cnt++){
        //read context
//        int context = read(NM_NCR);
//       // printf("%d\t,", context);
//        fprintf(fp,"%d\t", context);
//        
//        //read Neuron data
//        for (int j=0; j<NEURONSIZE; j++){
//            //read COMP 
//            neurons[j]=read(NM_COMP);
//            fprintf(fp,"%d\t", neurons[j]);
//            printf("%d\t,", neurons[j]);
//        }
        
 
    fclose(fp);
}



void SDToNeurons()
{
     char str[MAX_SIZE];
     
         printf("Reading from SD card...\r\n");
    
    fp = fopen("/sd/data.txt", "r");
    
    if (fp != NULL)
    {
        fgets(str,MAX_SIZE,fp);
        printf("%s", str);
        printf("\r\n");
        fclose(fp);
    } 
    else 
    {
        printf("failed!\n");
    } 
    
    
    
    //
//     char neurons[NEURONSIZE];
//     //save NSR
//    int TempNSR = read(NM_NSR);
//    
//    //read NCOUNT
//    //int nm_cnt = read(NM_NCOUNT);
//   
//
//    //write NSR-SR Mode
//    write(NM_NSR, 0x10);
//    
//    //reset chain
//    write(NM_RSTCHAIN, 0);
//    
//     //loop until max neruon
//    for(int cnt = 0; cnt<MAXNEURONS;cnt++){
//        //read context
//        int context = read(NM_NCR);    
//
//        test.printf("NCR : %d \t", context);
//        test.printf(" DATA : ");
//        //read neuron data<256
//        for (int j=0; j<NEURONSIZE; j++){
//            //read COMP 
//            neurons[j]=read(NM_COMP);
//            test.printf("%d,", neurons[j]);
//        }
//
//        test.printf("  \t");
//        
//        //read AIF
//        int aif=read(NM_AIF);
//      
//        test.printf("AIF : %d,\t", aif);
//        
//        //raed minif
//        int minif=read(NM_MINIF);
//       
//        test.printf("MINIF : %d,\t", minif);
//        
//        //read cat
//        int cat=read(NM_CAT) & 0x7fff;
//    
//        test.printf("CAT : %d, \t\n", cat);
//
//    }
}
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
}