Fork

Dependencies:   mbed libscpi

main.cpp

Committer:
wuliqunyy
Date:
2020-12-08
Revision:
2:9bdcd2c966de
Parent:
1:47cc6952e346
Child:
3:557d5725b1bb

File content as of revision 2:9bdcd2c966de:

#include "mbed.h"
#include "i2c_mbed_fpga.h"
//#include "serial_fsm.h"

/*Digital outputs*/
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);
DigitalOut fpga_rstb(p21);   //p21 of MBED is connectted to p4 of the FPGA CMOD for 90415FPGA_EVB2

/*Creat serial port to coummunicate with pc*/
Serial pc(USBTX, USBRX);

///*Creat I2C master*/
I2C i2c_master(p9, p10);

void serial_init(){
    pc.format(8, SerialBase::None, 1);
    pc.baud(38400);
}

void i2c_init(){
    i2c_master.frequency(35000);
}

void main_init(){
    led1 = 1;
    led2 = 0;
    led3 = 0;
    led4 = 0;

    fpga_rstb = 0;
    
    i2c_init();
    serial_init();
    
    i2c_master.start();
    
    pc.printf("Mbed is ready!");
}

/* Main function */
int main() {
    
    char i2cWriteTest[4] = {0x13, 0x8a, 0xac, 0x35};
    char i2cReadTest[4] = {0x13, 0x8a, 0x00, 0x00};
    char i2cStartMotor[4] = {0x13, 0xa4, 0xca, 0xfe};  //Start motor to skip the 1s
    char i2cPositionPulseSetup[4] = {0x13, 0x82, 0xc6, 0x00};  //Start motor to skip the 1s
    char i2cStartUpRead[4] = {0x13, 0x84, 0x00, 0x00};  //Start motor to skip the 1s
    
    int ack;
        
    main_init();
    
    while (1) {
        char c = pc.getc();
        if(c=='s'){
            fpga_rstb = 0;   //reset FPGA
            wait_ms(100);
            fpga_rstb = 1;   //enable FPGA
            wait_ms(10);
            //i2c_keyEntry(i2c_master);
            ack+=i2c_word_write(i2c_master,i2cPositionPulseSetup);
            wait_ms(10);
            ack+=i2c_word_write(i2c_master,i2cStartMotor);
            wait_ms(10);
            //ack+=i2c_word_read(i2c_master,i2cStartUpRead);
            led1 = ack;
            //pc.printf("d[0]=%2d\n",i2cStartUpRead[3]);
            //pc.printf("d[1]=%2d\n",i2cStartUpRead[2]);
        }
        if(c=='t'){
            fpga_rstb = 0;   //disable FPGA
            led1 = 0;
            
        }       
    }

    
    
//    fpga_rstb.write(1);
//    
//    wait_us(10000);  //10ms
//    
////    i2c_master.start();
////    wait_ms(200);
////    i2c_master.stop();
//    
//    ack+=i2c_keyEntry(i2c_master);
//        
//    //ack+=ctrPort_ReadActive(i2c_master);
//    buff[0] = 0x00;
//    buff[1] = 0x30;
//    buff[2] = 0x00;
//    buff[3] = 0x01;
//    
//    ack+=i2c_word_write(i2c_master, buff);
//    buff[0] = 0x00;
//    buff[1] = 0x0A;
//    buff[2] = 0x00;
//    buff[3] = 0x00;
//    
//    ack+=i2c_word_read(i2c_master, buff);
//    
//    wait_us(100);
//    
//    pc.printf("d[0]=%2d\n", buff[3]);
//    pc.printf("d[1]=%2d\n", buff[2]);
//    pc.printf("ack=%2d\n", ack);
//    pc.printf("endl\n");
//   
//    
//    while (1) {
//        led1 = !led1;
//        wait(1);
//    }
}