Bavo Van Achte
/
MLX90418_I2C_master
Fork
Diff: main.cpp
- Revision:
- 3:557d5725b1bb
- Parent:
- 2:9bdcd2c966de
- Child:
- 4:8d7322108eb7
--- a/main.cpp Tue Dec 08 13:53:10 2020 +0000 +++ b/main.cpp Fri Dec 18 10:25:46 2020 +0000 @@ -10,15 +10,15 @@ 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); +//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 serial_init(){ +// pc.format(8, SerialBase::None, 1); +// pc.baud(38400); +//} void i2c_init(){ i2c_master.frequency(35000); @@ -33,48 +33,141 @@ fpga_rstb = 0; i2c_init(); - serial_init(); - - i2c_master.start(); - - pc.printf("Mbed is ready!"); + //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}; - 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 + //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) | (0x03 << 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; - + 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_MOTOR_START_ADDR_H, \ + OTP_MOTOR_START_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(100); - fpga_rstb = 1; //enable FPGA - wait_ms(10); + //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_ms(10); - ack+=i2c_word_write(i2c_master,i2cStartMotor); - wait_ms(10); + //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; + //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; + //} + //if(c=='t'){ + // fpga_rstb = 0; //disable FPGA + // led1 = 0; - } + //} }