AIチップをmbedから駆動するためのプログラムです.

Dependencies:   mbed

main.cpp

Committer:
toriten1024
Date:
2019-03-16
Revision:
1:18e8ead6f188
Parent:
0:80ae2acaa6a1

File content as of revision 1:18e8ead6f188:

#include "mbed.h"
#include "fixed18.hpp"
#include "weight.hpp"
#include "matrix.hpp"
#include "Activation.hpp"

DigitalOut myled(LED1);
DigitalOut myled2(LED2);


Serial pc(USBTX, USBRX); // tx, rx

DigitalOut CLK(p23);
DigitalOut RST(p24);

BusInOut High9(p5,p6,p7,p8,p9,p10,p11,p12,p13);
BusInOut Low9(p14,p15,p16,p17,p18,p19,p20,p21,p22);



int main() {
    


    mat::Matrix<Fix18> x(784, 1);
    printf("hello\n");
    while(1){


        
        /*
            const unsigned int data[] ={0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 68, 122, 14, 0, 0, 0, 0, 0, 0, 68, 122, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 300, 368, 68, 0, 0, 0, 0, 0, 0, 218, 368, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 190, 436, 300, 40, 0, 0, 0, 0, 0, 0, 244, 408, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 94, 394, 394, 94, 0, 0, 0, 0, 0, 0, 0, 244, 408, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 68, 312, 436, 190, 14, 0, 0, 0, 0, 0, 0, 14, 272, 326, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 218, 462, 300, 40, 0, 0, 0, 0, 0, 0, 0, 68, 380, 244, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 68, 340, 422, 108, 0, 0, 0, 0, 0, 0, 0, 0, 82, 408, 244, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 68, 312, 462, 300, 40, 0, 0, 0, 0, 0, 0, 0, 0, 108, 408, 218, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 244, 462, 312, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 218, 408, 108, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 150, 422, 340, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 326, 326, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 244, 490, 368, 244, 218, 108, 82, 82, 82, 82, 82, 82, 82, 150, 422, 286, 82, 82, 82, 82, 82, 68, 14, 0, 0, 0, 0, 0, 150, 380, 408, 408, 394, 382, 408, 408, 408, 408, 408, 408, 408, 422, 476, 450, 408, 408, 408, 408, 408, 380, 272, 244, 244, 0, 0, 0, 14, 68, 82, 82, 82, 162, 244, 244, 244, 244, 244, 244, 244, 286, 436, 340, 244, 244, 244, 244, 244, 244, 326, 408, 408, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 162, 382, 108, 0, 0, 0, 0, 0, 0, 40, 82, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 272, 380, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 68, 380, 272, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 82, 408, 244, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 82, 408, 244, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 82, 408, 244, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 82, 408, 244, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 82, 408, 244, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 82, 408, 244, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 82, 408, 244, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 82, 408, 244, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 82, 408, 244, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        
            for (int i = 0; i < x.width * x.height; i++)
            {
                x.data[i].value = data[i];
            }
        */
            
        
            int flag = 0;
            
            for (int i = 0; i < x.width * x.height; i++)
            {
                myled2 = 1;
                
                unsigned int b0_7 = (unsigned int)pc.getc();
                unsigned int b8_15 = (unsigned int)pc.getc();
                unsigned int b16_17 = (unsigned int)pc.getc();
                 
                unsigned int bit_18 = (b16_17 << 16) + (b8_15 << 8) + b0_7;
                
                printf("%d:%d\n",i,bit_18);
            
                flag = (flag == 0)? 1 : 0;
                myled = flag;     
                x.data[i].value = bit_18;
            }
            
            
            mat::ConstMatrix W1(1, 1);
            W1.width = 50;
            W1.height = 784;
            W1.data = W1_param;
            
            mat::ConstMatrix b1(1, 1);
            b1.width = 50;
            b1.height = 1;
            b1.data = b1_param;
        
            mat::ConstMatrix W2(10, 50);
            W2.width = 10;
            W2.height = 50;
            W2.data = W2_param;
            
        
            mat::ConstMatrix b2(10, 1);
            b2.data = b2_param;
         
        
            printf("start\n");
        
        //matrix
            mat::Matrix<Fix18> a1 = x.dot(W1) + b1;
            mat::Matrix<Fix18> z1 = a1.apply(relu);
            mat::Matrix<Fix18> a2 = z1.dot(W2) + b2;
            double result = a2.max_arg(0).data[0].to_double();
        
            printf("result is %lf\n",result);
            
            myled = 0;
   
    }
    return 0;
}