Bavo Van Achte / Mbed 2 deprecated MLX90418_I2C_master

Dependencies:   mbed libscpi

Committer:
wuliqunyy
Date:
Fri Jun 04 08:21:10 2021 +0000
Revision:
20:26e934452728
Parent:
19:55189aea8f38
Child:
21:928fbeda074f
FPGA_V2 release

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 16:a0bfe33f8a4a 21 char i2cRamDumpFlag = 0;
wuliqunyy 6:019ab407ac3c 22
wuliqunyy 6:019ab407ac3c 23 /*init funcitons*/
wuliqunyy 5:daab0e0e67e2 24 i2c_mbed_fpga i2c;
wuliqunyy 6:019ab407ac3c 25 main_init(); /*reset the FPGA!*/
wuliqunyy 3:557d5725b1bb 26
wuliqunyy 6:019ab407ac3c 27 /*control codes*/
wuliqunyy 14:062850afdf38 28 wait_ms(2000); /* keep FPGA in reset for 5s, to let motor stop*/
wuliqunyy 14:062850afdf38 29 enbale_fpga(); /* enable FPGA */
wuliqunyy 20:26e934452728 30 wait_ms(38);
wuliqunyy 20:26e934452728 31 //*(i2cBuffer+0) = (char)(I2C_STATE)& 0xff;
wuliqunyy 20:26e934452728 32 //i2c.i2c_word_read(i2cBuffer);
wuliqunyy 20:26e934452728 33 //uart_print(i2cBuffer);
wuliqunyy 20:26e934452728 34 //wait_ms(1);
wuliqunyy 16:a0bfe33f8a4a 35 i2c.i2c_config_mode_entry();
wuliqunyy 14:062850afdf38 36 wait_ms(1);
wuliqunyy 14:062850afdf38 37 i2c.i2c_clear_spd_ctrl();
wuliqunyy 14:062850afdf38 38 wait_ms(1);
wuliqunyy 15:83bbc18cccbc 39 i2c.i2c_set_comm_i_thres(0x0fff,1); //note, 0fff is -1 signed
wuliqunyy 14:062850afdf38 40 wait_ms(1);
wuliqunyy 14:062850afdf38 41 i2c.i2c_set_comm_di_thres(0,1);
wuliqunyy 14:062850afdf38 42 wait_ms(1);
wuliqunyy 14:062850afdf38 43 i2c.i2c_set_fall_time_blank(4);
wuliqunyy 14:062850afdf38 44 wait_ms(1);
wuliqunyy 13:1faffc2dc651 45 i2c.i2c_set_50k_pwm(1);
wuliqunyy 14:062850afdf38 46 wait_ms(1);
wuliqunyy 19:55189aea8f38 47 i2c.i2c_set_position_pulse_width(3,5); /*unsigned int mantisaa_2b, unsigned int exponent_3b*/
wuliqunyy 14:062850afdf38 48 wait_ms(1);
wuliqunyy 20:26e934452728 49 i2c.i2c_set_position_flat(3,7);
wuliqunyy 20:26e934452728 50 wait_ms(1);
wuliqunyy 12:9f8c7f4da5f6 51 // i2c.i2c_set_position_duty(3);
wuliqunyy 12:9f8c7f4da5f6 52 // wait_ms(10);
wuliqunyy 12:9f8c7f4da5f6 53 // i2c.i2c_set_position_anti_cog(0);
wuliqunyy 12:9f8c7f4da5f6 54 // wait_ms(10);
wuliqunyy 12:9f8c7f4da5f6 55 // i2c.i2c_set_start_up_duty(3);
wuliqunyy 12:9f8c7f4da5f6 56 // wait_ms(10);
wuliqunyy 19:55189aea8f38 57 i2c.i2c_set_start_up_pulse_width(5,6); /*(unsigned int mantisaa_3b, unsigned int exponent_3b*/
wuliqunyy 14:062850afdf38 58 wait_ms(1);
wuliqunyy 20:26e934452728 59 i2c.i2c_set_start_up_flat(5,6); /*(unsigned int mantisaa_3b, unsigned int exponent_3b*/
wuliqunyy 20:26e934452728 60 wait_ms(1);
wuliqunyy 19:55189aea8f38 61 i2c.i2c_set_start_up_num_comm(1);
wuliqunyy 12:9f8c7f4da5f6 62 // wait_ms(10);
wuliqunyy 12:9f8c7f4da5f6 63 // //led4 = i2c.i2c_set_soft_start_up(1, 1, 0, 1, 1);
wuliqunyy 12:9f8c7f4da5f6 64 // wait_ms(10);
wuliqunyy 12:9f8c7f4da5f6 65 // i2c.i2c_clear_spd_ctrl();
wuliqunyy 14:062850afdf38 66 wait_ms(1);
wuliqunyy 14:062850afdf38 67 i2c.i2c_set_loop_mode(0);
wuliqunyy 14:062850afdf38 68 wait_ms(1);
wuliqunyy 14:062850afdf38 69 i2c.i2c_set_curve_type(3);
wuliqunyy 14:062850afdf38 70 wait_ms(1);
wuliqunyy 14:062850afdf38 71 i2c.i2c_set_dc_ini(2);
wuliqunyy 14:062850afdf38 72 wait_ms(1);
wuliqunyy 14:062850afdf38 73 i2c.i2c_set_dc_sr(0);
wuliqunyy 14:062850afdf38 74 wait_ms(1);
wuliqunyy 19:55189aea8f38 75 i2c.i2c_force_rough_reg(0);
wuliqunyy 19:55189aea8f38 76 wait_ms(1);
wuliqunyy 12:9f8c7f4da5f6 77 i2c.i2c_set_rough_gain(0);
wuliqunyy 14:062850afdf38 78 wait_ms(1);
wuliqunyy 14:062850afdf38 79 i2c.i2c_set_ehp_reg_gain(0);
wuliqunyy 14:062850afdf38 80 wait_ms(1);
wuliqunyy 16:a0bfe33f8a4a 81 i2c.i2c_set_clim_start_up(CLIM_LEVEL);
wuliqunyy 16:a0bfe33f8a4a 82 wait_ms(1);
wuliqunyy 16:a0bfe33f8a4a 83 i2c.i2c_set_clim_brake(CLIM_LEVEL);
wuliqunyy 16:a0bfe33f8a4a 84 wait_ms(1);
wuliqunyy 16:a0bfe33f8a4a 85 i2c.i2c_set_clim_run_time(CLIM_LEVEL);
wuliqunyy 16:a0bfe33f8a4a 86 wait_ms(1);
wuliqunyy 15:83bbc18cccbc 87 i2c.i2c_skip_app_copy();
wuliqunyy 15:83bbc18cccbc 88 wait_ms(1);
wuliqunyy 16:a0bfe33f8a4a 89
wuliqunyy 16:a0bfe33f8a4a 90 if(i2cRamDumpFlag == 1){
wuliqunyy 16:a0bfe33f8a4a 91 /*dump calibration data start*/
wuliqunyy 16:a0bfe33f8a4a 92 i2c.i2c_mlx_mode_entry();
wuliqunyy 16:a0bfe33f8a4a 93 wait_ms(1);
wuliqunyy 16:a0bfe33f8a4a 94 *(i2cBuffer+0) = (char)(I2C_STATUS)& 0xff;
wuliqunyy 16:a0bfe33f8a4a 95 i2c.i2c_word_read(i2cBuffer);
wuliqunyy 16:a0bfe33f8a4a 96 uart_print(i2cBuffer);
wuliqunyy 16:a0bfe33f8a4a 97
wuliqunyy 16:a0bfe33f8a4a 98 if( i2cBuffer[2] == 0xC0 ){
wuliqunyy 16:a0bfe33f8a4a 99 uart_I2C_test_pass();
wuliqunyy 16:a0bfe33f8a4a 100 i2cTestPassFlag = 1;
wuliqunyy 16:a0bfe33f8a4a 101 led4 = 0;
wuliqunyy 16:a0bfe33f8a4a 102 }
wuliqunyy 16:a0bfe33f8a4a 103 else {
wuliqunyy 16:a0bfe33f8a4a 104 uart_I2C_test_fail();
wuliqunyy 16:a0bfe33f8a4a 105 i2cTestPassFlag = 0;
wuliqunyy 16:a0bfe33f8a4a 106 led4 = 1;
wuliqunyy 16:a0bfe33f8a4a 107 }
wuliqunyy 16:a0bfe33f8a4a 108
wuliqunyy 16:a0bfe33f8a4a 109 if(i2cTestPassFlag == 1){
wuliqunyy 16:a0bfe33f8a4a 110 char i;
wuliqunyy 16:a0bfe33f8a4a 111 for(i=0;i<=RAM_END_ADDR;i++) {
wuliqunyy 16:a0bfe33f8a4a 112 *(i2cBuffer+0) = i;
wuliqunyy 16:a0bfe33f8a4a 113 i2c.i2c_word_read(i2cBuffer);
wuliqunyy 16:a0bfe33f8a4a 114 uart_print(i2cBuffer);
wuliqunyy 16:a0bfe33f8a4a 115 wait_ms(300);
wuliqunyy 16:a0bfe33f8a4a 116 }
wuliqunyy 16:a0bfe33f8a4a 117 uart_ram_dump_finish();
wuliqunyy 16:a0bfe33f8a4a 118 }
wuliqunyy 16:a0bfe33f8a4a 119 }
wuliqunyy 16:a0bfe33f8a4a 120
wuliqunyy 16:a0bfe33f8a4a 121
wuliqunyy 16:a0bfe33f8a4a 122 wait_ms(1);
wuliqunyy 16:a0bfe33f8a4a 123 i2c.i2c_soft_reset();
wuliqunyy 15:83bbc18cccbc 124 wait_ms(40);
wuliqunyy 20:26e934452728 125 i2c.i2c_set_open_loop_duty(0xffff); /*100%*/
wuliqunyy 20:26e934452728 126 //i2c.i2c_set_open_loop_duty(0xf850); /*97%*/
wuliqunyy 6:019ab407ac3c 127
wuliqunyy 6:019ab407ac3c 128 /*enter infinite loop, motor should be running*/
wuliqunyy 5:daab0e0e67e2 129 while (1) {}
wuliqunyy 0:fe3c7dde9771 130
wuliqunyy 0:fe3c7dde9771 131 }