Bavo Van Achte / Mbed 2 deprecated MLX90418_I2C_master

Dependencies:   mbed libscpi

Committer:
bavovanachte
Date:
Tue Jun 08 07:58:57 2021 +0000
Revision:
24:831545f3d455
Parent:
23:0237430bf3ec
Child:
25:8367b3fe2891
Test pulse flag = 0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wuliqunyy 0:fe3c7dde9771 1 #include "mbed.h"
wuliqunyy 5:daab0e0e67e2 2 #include "main_init.h"
wuliqunyy 6:019ab407ac3c 3 #include "i2c_mbed_fpga.h"
wuliqunyy 16:a0bfe33f8a4a 4 #include "uart_mbed.h"
wuliqunyy 0:fe3c7dde9771 5
wuliqunyy 16:a0bfe33f8a4a 6 #define RAM_END_ADDR 0x4B
wuliqunyy 16:a0bfe33f8a4a 7
wuliqunyy 19:55189aea8f38 8 //#define CLIM_LEVEL 47 //1.5A
wuliqunyy 19:55189aea8f38 9 //#define CLIM_LEVEL 57 //1.8A
wuliqunyy 19:55189aea8f38 10 //#define CLIM_LEVEL 67 //2.1A
wuliqunyy 19:55189aea8f38 11 //#define CLIM_LEVEL 76 //2.4A
wuliqunyy 19:55189aea8f38 12 #define CLIM_LEVEL 86 //2.7A
wuliqunyy 19:55189aea8f38 13 //#define CLIM_LEVEL 95 //3.0A Runda fan setting
wuliqunyy 19:55189aea8f38 14 //#define CLIM_LEVEL 105 //3.3A
wuliqunyy 16:a0bfe33f8a4a 15 // I_LIM (A) = 4A * CLIM_LEVEL/127
wuliqunyy 0:fe3c7dde9771 16
wuliqunyy 0:fe3c7dde9771 17 /* Main function */
wuliqunyy 0:fe3c7dde9771 18 int main() {
wuliqunyy 16:a0bfe33f8a4a 19 char i2cBuffer[3];
wuliqunyy 16:a0bfe33f8a4a 20 char i2cTestPassFlag = 0;
wuliqunyy 22:fd929620e873 21 char i2cRamDumpFlag = 0;
bavovanachte 24:831545f3d455 22 char i2cTestPulseDebugFlag = 0;
wuliqunyy 6:019ab407ac3c 23
wuliqunyy 6:019ab407ac3c 24 /*init funcitons*/
wuliqunyy 5:daab0e0e67e2 25 i2c_mbed_fpga i2c;
wuliqunyy 6:019ab407ac3c 26 main_init(); /*reset the FPGA!*/
wuliqunyy 3:557d5725b1bb 27
wuliqunyy 6:019ab407ac3c 28 /*control codes*/
wuliqunyy 14:062850afdf38 29 wait_ms(2000); /* keep FPGA in reset for 5s, to let motor stop*/
wuliqunyy 14:062850afdf38 30 enbale_fpga(); /* enable FPGA */
wuliqunyy 23:0237430bf3ec 31 wait_ms(40);
wuliqunyy 20:26e934452728 32 //*(i2cBuffer+0) = (char)(I2C_STATE)& 0xff;
wuliqunyy 20:26e934452728 33 //i2c.i2c_word_read(i2cBuffer);
wuliqunyy 20:26e934452728 34 //uart_print(i2cBuffer);
wuliqunyy 20:26e934452728 35 //wait_ms(1);
wuliqunyy 16:a0bfe33f8a4a 36 i2c.i2c_config_mode_entry();
wuliqunyy 14:062850afdf38 37 wait_ms(1);
wuliqunyy 23:0237430bf3ec 38 i2c.i2c_mlx_mode_entry();
wuliqunyy 23:0237430bf3ec 39 wait_ms(1);
wuliqunyy 14:062850afdf38 40 i2c.i2c_clear_spd_ctrl();
wuliqunyy 14:062850afdf38 41 wait_ms(1);
wuliqunyy 15:83bbc18cccbc 42 i2c.i2c_set_comm_i_thres(0x0fff,1); //note, 0fff is -1 signed
wuliqunyy 14:062850afdf38 43 wait_ms(1);
wuliqunyy 14:062850afdf38 44 i2c.i2c_set_comm_di_thres(0,1);
wuliqunyy 14:062850afdf38 45 wait_ms(1);
wuliqunyy 14:062850afdf38 46 i2c.i2c_set_fall_time_blank(4);
wuliqunyy 14:062850afdf38 47 wait_ms(1);
wuliqunyy 13:1faffc2dc651 48 i2c.i2c_set_50k_pwm(1);
wuliqunyy 14:062850afdf38 49 wait_ms(1);
wuliqunyy 22:fd929620e873 50 //i2c.i2c_set_position_pulse_width(3,5); /* Runda unsigned int mantisaa_2b, unsigned int exponent_3b*/
wuliqunyy 22:fd929620e873 51 i2c.i2c_set_position_pulse_width(2,4); /* VGA unsigned int mantisaa_2b, unsigned int exponent_3b */
wuliqunyy 14:062850afdf38 52 wait_ms(1);
wuliqunyy 20:26e934452728 53 i2c.i2c_set_position_flat(3,7);
wuliqunyy 20:26e934452728 54 wait_ms(1);
wuliqunyy 12:9f8c7f4da5f6 55 // i2c.i2c_set_position_duty(3);
wuliqunyy 12:9f8c7f4da5f6 56 // wait_ms(10);
wuliqunyy 12:9f8c7f4da5f6 57 // i2c.i2c_set_position_anti_cog(0);
wuliqunyy 12:9f8c7f4da5f6 58 // wait_ms(10);
wuliqunyy 12:9f8c7f4da5f6 59 // i2c.i2c_set_start_up_duty(3);
wuliqunyy 12:9f8c7f4da5f6 60 // wait_ms(10);
wuliqunyy 22:fd929620e873 61 //i2c.i2c_set_start_up_pulse_width(5,6); /*Runda(unsigned int mantisaa_3b, unsigned int exponent_3b*/
wuliqunyy 22:fd929620e873 62 i2c.i2c_set_start_up_pulse_width(3,5); /* VGA (unsigned int mantisaa_3b, unsigned int exponent_3b*/
wuliqunyy 14:062850afdf38 63 wait_ms(1);
wuliqunyy 22:fd929620e873 64 //i2c.i2c_set_start_up_flat(5,6); /*Runda(unsigned int mantisaa_3b, unsigned int exponent_3b*/
wuliqunyy 22:fd929620e873 65 i2c.i2c_set_start_up_flat(3,6); /*VGA (unsigned int mantisaa_3b, unsigned int exponent_3b*/
wuliqunyy 20:26e934452728 66 wait_ms(1);
wuliqunyy 19:55189aea8f38 67 i2c.i2c_set_start_up_num_comm(1);
wuliqunyy 12:9f8c7f4da5f6 68 // wait_ms(10);
wuliqunyy 12:9f8c7f4da5f6 69 // //led4 = i2c.i2c_set_soft_start_up(1, 1, 0, 1, 1);
wuliqunyy 12:9f8c7f4da5f6 70 // wait_ms(10);
wuliqunyy 12:9f8c7f4da5f6 71 // i2c.i2c_clear_spd_ctrl();
wuliqunyy 14:062850afdf38 72 wait_ms(1);
wuliqunyy 14:062850afdf38 73 i2c.i2c_set_loop_mode(0);
wuliqunyy 14:062850afdf38 74 wait_ms(1);
wuliqunyy 14:062850afdf38 75 i2c.i2c_set_curve_type(3);
wuliqunyy 14:062850afdf38 76 wait_ms(1);
wuliqunyy 14:062850afdf38 77 i2c.i2c_set_dc_ini(2);
wuliqunyy 14:062850afdf38 78 wait_ms(1);
wuliqunyy 14:062850afdf38 79 i2c.i2c_set_dc_sr(0);
wuliqunyy 14:062850afdf38 80 wait_ms(1);
wuliqunyy 19:55189aea8f38 81 i2c.i2c_force_rough_reg(0);
wuliqunyy 19:55189aea8f38 82 wait_ms(1);
wuliqunyy 12:9f8c7f4da5f6 83 i2c.i2c_set_rough_gain(0);
wuliqunyy 14:062850afdf38 84 wait_ms(1);
wuliqunyy 14:062850afdf38 85 i2c.i2c_set_ehp_reg_gain(0);
wuliqunyy 14:062850afdf38 86 wait_ms(1);
wuliqunyy 16:a0bfe33f8a4a 87 i2c.i2c_set_clim_start_up(CLIM_LEVEL);
wuliqunyy 16:a0bfe33f8a4a 88 wait_ms(1);
wuliqunyy 16:a0bfe33f8a4a 89 i2c.i2c_set_clim_brake(CLIM_LEVEL);
wuliqunyy 16:a0bfe33f8a4a 90 wait_ms(1);
wuliqunyy 16:a0bfe33f8a4a 91 i2c.i2c_set_clim_run_time(CLIM_LEVEL);
wuliqunyy 16:a0bfe33f8a4a 92 wait_ms(1);
wuliqunyy 23:0237430bf3ec 93
wuliqunyy 16:a0bfe33f8a4a 94
wuliqunyy 16:a0bfe33f8a4a 95 if(i2cRamDumpFlag == 1){
wuliqunyy 16:a0bfe33f8a4a 96 /*dump calibration data start*/
wuliqunyy 16:a0bfe33f8a4a 97 i2c.i2c_mlx_mode_entry();
wuliqunyy 16:a0bfe33f8a4a 98 wait_ms(1);
wuliqunyy 16:a0bfe33f8a4a 99 *(i2cBuffer+0) = (char)(I2C_STATUS)& 0xff;
wuliqunyy 16:a0bfe33f8a4a 100 i2c.i2c_word_read(i2cBuffer);
wuliqunyy 16:a0bfe33f8a4a 101 uart_print(i2cBuffer);
wuliqunyy 16:a0bfe33f8a4a 102
wuliqunyy 21:928fbeda074f 103 if( i2cBuffer[2] == 0x0C ){
wuliqunyy 16:a0bfe33f8a4a 104 uart_I2C_test_pass();
wuliqunyy 16:a0bfe33f8a4a 105 i2cTestPassFlag = 1;
wuliqunyy 16:a0bfe33f8a4a 106 led4 = 0;
wuliqunyy 16:a0bfe33f8a4a 107 }
wuliqunyy 16:a0bfe33f8a4a 108 else {
wuliqunyy 16:a0bfe33f8a4a 109 uart_I2C_test_fail();
wuliqunyy 16:a0bfe33f8a4a 110 i2cTestPassFlag = 0;
wuliqunyy 16:a0bfe33f8a4a 111 led4 = 1;
wuliqunyy 16:a0bfe33f8a4a 112 }
wuliqunyy 16:a0bfe33f8a4a 113
wuliqunyy 16:a0bfe33f8a4a 114 if(i2cTestPassFlag == 1){
wuliqunyy 16:a0bfe33f8a4a 115 char i;
wuliqunyy 16:a0bfe33f8a4a 116 for(i=0;i<=RAM_END_ADDR;i++) {
wuliqunyy 16:a0bfe33f8a4a 117 *(i2cBuffer+0) = i;
wuliqunyy 16:a0bfe33f8a4a 118 i2c.i2c_word_read(i2cBuffer);
wuliqunyy 16:a0bfe33f8a4a 119 uart_print(i2cBuffer);
wuliqunyy 16:a0bfe33f8a4a 120 wait_ms(300);
wuliqunyy 16:a0bfe33f8a4a 121 }
wuliqunyy 16:a0bfe33f8a4a 122 uart_ram_dump_finish();
wuliqunyy 16:a0bfe33f8a4a 123 }
wuliqunyy 16:a0bfe33f8a4a 124 }
wuliqunyy 16:a0bfe33f8a4a 125
wuliqunyy 16:a0bfe33f8a4a 126 wait_ms(1);
wuliqunyy 22:fd929620e873 127 if(i2cTestPulseDebugFlag == 1) {
wuliqunyy 22:fd929620e873 128 i2c.i2c_mlx_mode_entry();
wuliqunyy 22:fd929620e873 129 wait_ms(1);
wuliqunyy 23:0237430bf3ec 130 *(i2cBuffer+0) = (char)(I2C_STATUS)& 0xff;
wuliqunyy 23:0237430bf3ec 131 i2c.i2c_word_read(i2cBuffer);
wuliqunyy 23:0237430bf3ec 132 uart_print(i2cBuffer);
wuliqunyy 23:0237430bf3ec 133
wuliqunyy 23:0237430bf3ec 134 if( i2cBuffer[2] == 0x0C ){
wuliqunyy 23:0237430bf3ec 135 uart_I2C_test_pass();
wuliqunyy 23:0237430bf3ec 136 i2cTestPassFlag = 1;
wuliqunyy 23:0237430bf3ec 137 led4 = 0;
wuliqunyy 23:0237430bf3ec 138 }
wuliqunyy 23:0237430bf3ec 139 else {
wuliqunyy 23:0237430bf3ec 140 uart_I2C_test_fail();
wuliqunyy 23:0237430bf3ec 141 i2cTestPassFlag = 0;
wuliqunyy 23:0237430bf3ec 142 led4 = 1;
wuliqunyy 23:0237430bf3ec 143 }
wuliqunyy 23:0237430bf3ec 144
wuliqunyy 23:0237430bf3ec 145 if(i2cTestPassFlag == 1){
wuliqunyy 23:0237430bf3ec 146 i2c.i2c_enable_pules_debug_mode(1);
wuliqunyy 23:0237430bf3ec 147 wait_ms(1);
wuliqunyy 23:0237430bf3ec 148 }
wuliqunyy 22:fd929620e873 149 }
wuliqunyy 22:fd929620e873 150
wuliqunyy 23:0237430bf3ec 151 i2c.i2c_skip_app_copy();
wuliqunyy 22:fd929620e873 152 wait_ms(1);
wuliqunyy 16:a0bfe33f8a4a 153 i2c.i2c_soft_reset();
wuliqunyy 15:83bbc18cccbc 154 wait_ms(40);
wuliqunyy 23:0237430bf3ec 155
wuliqunyy 20:26e934452728 156 i2c.i2c_set_open_loop_duty(0xffff); /*100%*/
wuliqunyy 23:0237430bf3ec 157 //i2c.i2c_set_open_loop_duty(0xf850); /*97%*/
wuliqunyy 23:0237430bf3ec 158 if(i2cTestPulseDebugFlag == 1) {
wuliqunyy 23:0237430bf3ec 159 wait_ms(100);
wuliqunyy 23:0237430bf3ec 160 char i;
wuliqunyy 23:0237430bf3ec 161 for(i=0xEA;i<=0xEF;i++) {
wuliqunyy 23:0237430bf3ec 162 *(i2cBuffer+0) = i;
wuliqunyy 23:0237430bf3ec 163 i2c.i2c_word_read(i2cBuffer);
wuliqunyy 23:0237430bf3ec 164 uart_print(i2cBuffer);
wuliqunyy 23:0237430bf3ec 165 wait_ms(300);
wuliqunyy 23:0237430bf3ec 166 }
wuliqunyy 23:0237430bf3ec 167 uart_ram_dump_finish();
wuliqunyy 23:0237430bf3ec 168 }
wuliqunyy 6:019ab407ac3c 169 /*enter infinite loop, motor should be running*/
wuliqunyy 5:daab0e0e67e2 170 while (1) {}
wuliqunyy 0:fe3c7dde9771 171
wuliqunyy 0:fe3c7dde9771 172 }