Ben Willems / Mbed 2 deprecated MLX90418_I2C_master_bwi

Dependencies:   mbed

Revision:
12:9f8c7f4da5f6
Parent:
11:b86aea372744
Child:
14:062850afdf38
--- a/i2c_mbed_fpga.cpp	Wed Mar 03 14:55:01 2021 +0000
+++ b/i2c_mbed_fpga.cpp	Wed Mar 24 14:17:25 2021 +0000
@@ -16,6 +16,21 @@
     return (ack == 0) ? 0 : 1; 
 }
 
+//I2C read even NAK happens
+int i2c_mbed_fpga::i2c_word_read_by_byte(char *word){
+    int ack = 0;
+    i2c_master.start();
+    ack = i2c_master.write(I2C_SLAVE_ADDR);
+    ack = i2c_master.write(*word);
+    ack = i2c_master.write(*(word+1));
+    i2c_master.start();
+    *(word+2) = i2c_master.read(1);
+    *(word+3) = i2c_master.read(1);
+    i2c_master.stop();
+    return ack; 
+}
+
+
 /** i2c write to slave DUT
 *   ==> one time write, not read back check
 *
@@ -27,6 +42,18 @@
     ack = i2c_master.write(I2C_SLAVE_ADDR, word, 4, false);
     return ack; 
 }
+//I2C write even NAK happens
+int i2c_mbed_fpga::i2c_word_write_by_byte(char *word){
+    int ack = 0;
+    i2c_master.start();
+    ack = i2c_master.write(I2C_SLAVE_ADDR);
+    ack = i2c_master.write(*word);
+    ack = i2c_master.write(*(word+1));
+    ack = i2c_master.write(*(word+2));
+    ack = i2c_master.write(*(word+3));
+    i2c_master.stop();
+    return ack; 
+}
 
 
 /** i2c write to slave DUT
@@ -58,24 +85,39 @@
 */
 int i2c_mbed_fpga::i2c_window_open(){
     char i2cMessage[4];
-    *(i2cMessage+0) = (char)(NVADDR_NV_TRACEABILITY_0 >> 8)& 0xff;
-    *(i2cMessage+1) = (char)(NVADDR_NV_TRACEABILITY_0 >> 0)& 0xff;
+    *(i2cMessage+0) = (char)(NVADDR_CUST_ID3 >> 8)& 0xff;
+    *(i2cMessage+1) = (char)(NVADDR_CUST_ID3 >> 0)& 0xff;
     *(i2cMessage+2) = (char)(0xD0)& 0xff;
     *(i2cMessage+3) = (char)(0xD0)& 0xff;  
-    return i2c_word_safe_write(i2cMessage);
+    return i2c_word_write_by_byte(i2cMessage);
 }
 
 /** i2c enter key to Start the motor
 */
 int i2c_mbed_fpga::i2c_motor_start(){
     char i2cMessage[4];
-    *(i2cMessage+0) = (char)(NVADDR_NV_TRACEABILITY_0 >> 8)& 0xff;
-    *(i2cMessage+1) = (char)(NVADDR_NV_TRACEABILITY_0 >> 0)& 0xff;
+    *(i2cMessage+0) = (char)(NVADDR_CUST_ID3 >> 8)& 0xff;
+    *(i2cMessage+1) = (char)(NVADDR_CUST_ID3 >> 0)& 0xff;
     *(i2cMessage+2) = (char)(0xCA)& 0xff;
     *(i2cMessage+3) = (char)(0xFE)& 0xff;  
+    return i2c_word_write_by_byte(i2cMessage);
+}
+
+/** i2c to set the 50k PWM
+*/
+int i2c_mbed_fpga::i2c_set_50k_pwm(unsigned int pwm50k){
+    nv_gen_ctrl_val &= ~NV_PWM_36K_MASK;
+    nv_gen_ctrl_val |= pwm50k << NV_PWM_36K_OFFSET;
+    char i2cMessage[4];
+    *(i2cMessage+0) = (char)(NVADDR_NV_GEN_CTRL >> 8)& 0xff;
+    *(i2cMessage+1) = (char)(NVADDR_NV_GEN_CTRL >> 0)& 0xff;
+    *(i2cMessage+2) = (char)(nv_gen_ctrl_val >> 8)& 0xff;
+    *(i2cMessage+3) = (char)(nv_gen_ctrl_val >> 0)& 0xff;          
     return i2c_word_safe_write(i2cMessage);
 }
 
+
+
 /** i2c to set the Postion Pulse width
 */
 int i2c_mbed_fpga::i2c_set_position_pulse_width(unsigned int mantisaa_2b, unsigned int exponent_3b){
@@ -250,6 +292,63 @@
     return ack; 
 }
 
+/** i2c to set the open loop mode
+*/
+int i2c_mbed_fpga::i2c_set_rough_gain(unsigned int rough_gain){
+    int ack = 0;
+    nv_gen_ctrl_val &= ~NV_ROUGH_GAIN_MASK;
+    nv_gen_ctrl_val |= rough_gain << NV_ROUGH_GAIN_OFFSET;
+    char i2cMessage[4];
+    *(i2cMessage+0) = (char)(NVADDR_NV_GEN_CTRL >> 8)& 0xff;
+    *(i2cMessage+1) = (char)(NVADDR_NV_GEN_CTRL >> 0)& 0xff;
+    *(i2cMessage+2) = (char)(nv_gen_ctrl_val >> 8)& 0xff;
+    *(i2cMessage+3) = (char)(nv_gen_ctrl_val >> 0)& 0xff;  
+    ack += i2c_word_safe_write(i2cMessage);
+    
+    return ack; 
+}
+
+
+
+/** i2c to set the current threshold for I_didt
+*/
+int i2c_mbed_fpga::i2c_set_comm_i_thres(unsigned int i_thr_low, unsigned int i_thr_high){
+    int ack = 0;
+    nv_comm_ctrl_val &= ~NV_I_ZC_TH_LOW_MASK;
+    nv_comm_ctrl_val |= i_thr_low << NV_I_ZC_TH_LOW_OFFSET;
+    nv_comm_ctrl_val &= ~NV_I_ZC_TH_HIGH_MASK;
+    nv_comm_ctrl_val |= i_thr_high << NV_I_ZC_TH_HIGH_OFFSET;
+    char i2cMessage[4];
+    *(i2cMessage+0) = (char)(NVADDR_NV_COMM >> 8)& 0xff;
+    *(i2cMessage+1) = (char)(NVADDR_NV_COMM >> 0)& 0xff;
+    *(i2cMessage+2) = (char)(nv_comm_ctrl_val >> 8)& 0xff;
+    *(i2cMessage+3) = (char)(nv_comm_ctrl_val >> 0)& 0xff;  
+    ack += i2c_word_safe_write(i2cMessage);
+    
+    return ack; 
+}
+
+/** i2c to set the di current threshold for didt
+*/
+int i2c_mbed_fpga::i2c_set_comm_di_thres(unsigned int di_1st, unsigned int di_2nd){
+    int ack = 0;
+    nv_comm_ctrl_val &= ~NV_DI_TH_1ST_MASK;
+    nv_comm_ctrl_val |= di_1st << NV_DI_TH_1ST_OFFSET;
+    nv_comm_ctrl_val &= ~NV_DI_TH_2ND_MASK;
+    nv_comm_ctrl_val |= di_2nd << NV_DI_TH_2ND_OFFSET;
+    char i2cMessage[4];
+    *(i2cMessage+0) = (char)(NVADDR_NV_COMM >> 8)& 0xff;
+    *(i2cMessage+1) = (char)(NVADDR_NV_COMM >> 0)& 0xff;
+    *(i2cMessage+2) = (char)(nv_comm_ctrl_val >> 8)& 0xff;
+    *(i2cMessage+3) = (char)(nv_comm_ctrl_val >> 0)& 0xff;  
+    ack += i2c_word_safe_write(i2cMessage);
+    
+    return ack; 
+}
+
+
+
+
 /** i2c to clean the I2C controller settins
 */
 int i2c_mbed_fpga::i2c_clear_spd_ctrl(){
@@ -289,6 +388,21 @@
     return ack; 
 }
 
+/** i2c to set the Single Pulse Start Up (pulse train)
+*/
+int i2c_mbed_fpga::i2c_set_open_loop_duty(unsigned int duty){
+    int ack = 0;
+    ram_open_duty_val = duty;
+    char i2cMessage[4];
+    *(i2cMessage+0) = (char)(0x10)& 0xff;
+    *(i2cMessage+1) = (char)(0x00)& 0xff;
+    *(i2cMessage+2) = (char)(ram_open_duty_val >> 8)& 0xff;
+    *(i2cMessage+3) = (char)(ram_open_duty_val >> 0)& 0xff;  
+    ack += i2c_word_safe_write(i2cMessage);
+        
+    return ack; 
+}
+
 /** i2c to set the speed curve type
 */
 int i2c_mbed_fpga::i2c_set_curve_type(unsigned int curvetype){
@@ -337,17 +451,4 @@
     return ack; 
 }
 
-/** i2c to set the Single Pulse Start Up (pulse train)
-*/
-int i2c_mbed_fpga::i2c_set_open_loop_duty(unsigned int duty){
-    int ack = 0;
-    ram_open_duty_val = duty;
-    char i2cMessage[4];
-    *(i2cMessage+0) = (char)(0x10)& 0xff;
-    *(i2cMessage+1) = (char)(0x3e)& 0xff;
-    *(i2cMessage+2) = (char)(ram_open_duty_val >> 8)& 0xff;
-    *(i2cMessage+3) = (char)(ram_open_duty_val >> 0)& 0xff;  
-    ack += i2c_word_safe_write(i2cMessage);
-        
-    return ack; 
-}
+