Fork

Dependencies:   mbed libscpi

main.cpp

Committer:
wuliqunyy
Date:
2021-01-12
Revision:
4:8d7322108eb7
Parent:
3:557d5725b1bb
Child:
5:daab0e0e67e2

File content as of revision 4:8d7322108eb7:

#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();
      
    //pc.printf("Mbed is ready!");
}

/* Main function */
int main() {
    
    //char i2cWriteTest[4] = {0x13, 0x8a, 0xac, 0x35};
    //char i2cReadTest[4] = {0x13, 0x8a, 0x00, 0x00};
    
    //NV_POSITION
    #define NV_POSITION_DUTY (14 - 8)
    #define NV_POSITION_PULSE_TIME  (9 - 8)
    #define NV_POSI_MAJO_VOTE  (8 - 8)
    #define NV_ANTI_COG  7
    #define NV_FIRST_NON_FLAT_TIME  0

    #define OTP_I2C_POSITIONPULSE_DATA_H (0x03 << NV_POSITION_DUTY) | (0x09 << NV_POSITION_PULSE_TIME) | (0x00 << NV_POSI_MAJO_VOTE)  
    #define OTP_I2C_POSITIONPULSE_DATA_L (0x00 << NV_ANTI_COG)  |  (0x2c << NV_FIRST_NON_FLAT_TIME)
                                         
    #define OTP_I2C_POSITIONPULSE_ADDR_H 0x13
    #define OTP_I2C_POSITIONPULSE_ADDR_L 0x82
    char i2cPositionPulseSetup[4] = {OTP_I2C_POSITIONPULSE_ADDR_H, OTP_I2C_POSITIONPULSE_ADDR_L, OTP_I2C_POSITIONPULSE_DATA_H,OTP_I2C_POSITIONPULSE_DATA_L};
    
    //NV_START_UP
    #define NV_LONG_START (14 - 8)
    #define NV_SOFT_START (13 - 8)
    #define NV_COMM_START_NUM (11 - 8)
    #define NV_START_DUTY (9 - 8)
    #define NV_SOFT_STEP_SIZE 6
    #define NV_START_UP_TIME 0

    #define OTP_I2C_STARTUP_DATA_H (0x00 << NV_SOFT_START) | (0x00 << NV_LONG_START) | (0x02 << NV_COMM_START_NUM) | (0x03 << NV_START_DUTY)
    #define OTP_I2C_STARTUP_DATA_L (0x03 << NV_SOFT_STEP_SIZE) | (0x24 << NV_START_UP_TIME)
    
    #define OTP_I2C_STARTUP_ADDR_H 0x13
    #define OTP_I2C_STARTUP_ADDR_L 0x84
    
    char i2cStartUpSetup[4] =   {OTP_I2C_STARTUP_ADDR_H, OTP_I2C_STARTUP_ADDR_L, OTP_I2C_STARTUP_DATA_H, OTP_I2C_STARTUP_DATA_L};
    
    //Start Motor
    #define OTP_MOTOR_START_ADDR_H 0x13
    #define OTP_MOTOR_START_ADDR_L 0xa4
    #define OTP_MOTOR_START_DATA_H 0xca
    #define OTP_MOTOR_START_DATA_L 0xfe
    
    char i2cMotorStartSetup[4] =   {OTP_MOTOR_START_ADDR_H, \
                                    OTP_MOTOR_START_ADDR_L, \
                                    OTP_MOTOR_START_DATA_H, \
                                    OTP_MOTOR_START_DATA_L};
    
    int ack;    
    main_init();
    wait_ms(2000);   //reset the FPGA for 2s
    fpga_rstb = 1;   //enable FPGA
    wait_ms(1);
    
    //Postion Test Pulse setting
    char i2cPositionPulseRead[4] =  {OTP_I2C_POSITIONPULSE_ADDR_H, \
                                    OTP_I2C_POSITIONPULSE_ADDR_L, \
                                    0x00, \
                                    0x00};
    int i = 3;
    while (i > 0) {
        wait_ms(1);
        ack+=i2c_word_write(i2c_master,i2cPositionPulseSetup);
        wait_ms(1);
        ack+=i2c_word_read(i2c_master,i2cPositionPulseRead);
        if( (i2cPositionPulseRead[1] ==  OTP_I2C_POSITIONPULSE_DATA_H) && (i2cPositionPulseRead[0] == OTP_I2C_POSITIONPULSE_DATA_L) ){
            i=0;
            led1 = 1;
            led2 = 0; 
            }
        else{
            i--;
            led1 = 0;
            led2 = 1;
            fpga_rstb = 0;   //disable FPGA  
        };
    }



    
    //Start Up setting
    char i2cStartUpPulseRead[4] =  {OTP_I2C_STARTUP_ADDR_H, \
                                    OTP_I2C_STARTUP_ADDR_L, \
                                    0x00, \
                                    0x00};
    i = 3;
    while (i > 0) {
        wait_ms(1);
        ack+=i2c_word_write(i2c_master,i2cStartUpSetup);
        wait_ms(1);
        ack+=i2c_word_read(i2c_master,i2cStartUpPulseRead);
        if( (i2cStartUpPulseRead[1] ==  OTP_I2C_STARTUP_DATA_H) && (i2cStartUpPulseRead[0] ==  OTP_I2C_STARTUP_DATA_L) ){
            i=0;
            led3 = 1;
            led4 = 0; 
            }
        else{
            i--;
            led3 = 0;
            led4 = 1;
            fpga_rstb = 0;   //disable FPGA  
        };
    }
 
    
    //ack+=i2c_word_write(i2c_master,i2cMotorStartSetup);
    
    while (1) {
        //char c = pc.getc();
        //if(c=='s'){
            //fpga_rstb = 0;   //reset FPGA
            //wait_ms(3000);
            //fpga_rstb = 1;   //enable FPGA
            //wait_ms(10);
            //i2c_keyEntry(i2c_master);
            //ack+=i2c_word_write(i2c_master,i2cPositionPulseSetup);
            //wait_us(100);
            //ack+=i2c_word_read(i2c_master,i2cPositionPulseSetup);
            //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);
//    }
}