This is a library for the MAX17055 Li+ Battery Fuel Gauge.

Dependents:   Low_Power_Long_Distance_IR_Vision_Robot MAX17055_EZconfig MAX17055_EZconfig_Sample Low_Power_Long_Distance_IR_Vision_Robot

Fork of max17055 by Maxim Integrated

Revision:
12:519a18fc3b28
Parent:
11:bdbd3104995b
Child:
13:fc91b283e689
diff -r bdbd3104995b -r 519a18fc3b28 max17055.cpp
--- a/max17055.cpp	Tue Feb 27 15:53:04 2018 +0000
+++ b/max17055.cpp	Thu Apr 19 22:34:45 2018 +0000
@@ -3,14 +3,15 @@
 *
 * @author Felipe Neira - Maxim Integrated - TTS
 *
-* @version 1.4
+* @version 1.5
 *
-* Started: 6FEB18
+* Started: 13DEC17
 *
-* Updated: 
-* Modifications to reflect applicable functions and .h file association
-* 
-* 
+* Updated: 16APR18.
+* Remove unnecessary code.  
+* Check spelling
+* Error check
+*
 *
 /*******************************************************************************
 * Copyright (C) 2018 Maxim Integrated Products, Inc., All Rights Reserved.
@@ -48,47 +49,21 @@
 #include "mbed.h"
 #include "max17055.h"
 
-// #define DRV_NAME "max17055" ///not used
-
-// /* CONFIG register bits */
-// #define MAX17055_CONFIG_ALfRT_EN        (1 << 2) ///not used
-// #define MAX17055_CONFIG2_LDMDL          (1 << 5) ///not used
-
-// /* STATUS register bits */
-// #define MAX17055_STATUS_BST             (1 << 3) ///not used
-// #define MAX17055_STATUS_POR             (1 << 1)
-
 /* POR Mask */
 #define MAX17055_POR_MASK               (0xFFFD)
 
 /* MODELCFG register bits */
 #define MAX17055_MODELCFG_REFRESH       (1 << 15)
 
-// /* TALRTTH register bits */
-// #define MIN_TEMP_ALERT                  0 ///not used
-// #define MAX_TEMP_ALERT                  8 ///not used
 
 /* FSTAT register bits */
 #define MAX17055_FSTAT_DNR              (1)
 
-// /* STATUS interrupt status bits */
-// #define MAX17055_STATUS_ALRT_CLR_MASK   (0x88BB)
-// #define MAX17055_STATUS_SOC_MAX_ALRT    (1 << 14)
-// #define MAX17055_STATUS_TEMP_MAX_ALRT   (1 << 13)
-// #define MAX17055_STATUS_VOLT_MAX_ALRT   (1 << 12)
-// #define MAX17055_STATUS_SOC_MIN_ALRT    (1 << 10)
-// #define MAX17055_STATUS_TEMP_MIN_ALRT   (1 << 9)
-// #define MAX17055_STATUS_VOLT_MIN_ALRT   (1 << 8)
-// #define MAX17055_STATUS_CURR_MAX_ALRT   (1 << 6)
-// #define MAX17055_STATUS_CURR_MIN_ALRT   (1 << 2)
-
-// #define MAX17055_VMAX_TOLERANCE     50 /* 50 mV */
-
 /* LIBRARY FUNCTION SUCCESS*/
 #define F_SUCCESS_0  0
 
 /* LIBRARY FUNCTION ERROR CODES */  
-#define F_ERROR_1 -1    //-1 if read/write errors exist         
+#define F_ERROR_1 -1    //-1 if I2C read/write errors exist         
 #define F_ERROR_2 -2    //-2 if device is not present
 #define F_ERROR_3 -3    //-3 if function error
 #define F_ERROR_4 -4    //-4 if other error
@@ -172,14 +147,14 @@
 /**
  * @brief        Write and Verify a MAX17055 register
  * @par          Details
- *               This function wites and verifies if the writing process was successful
+ *               This function writes and verifies if the writing process was successful
  *               
  * @param[in]    reg_addr     - register address
  * @param[out]   reg_data     - the variable that contains the data to write
  *                               to the register address
  *
  * @retval       0 on success
- * @retval      non-0 for errors
+ * @retval       non-0 for errors
  */
 int MAX17055::write_and_verify_reg(Registers_e reg_addr, uint16_t reg_data)
 {
@@ -192,13 +167,13 @@
     do {
         statusWrite = writeReg(reg_addr, reg_data);
         if (statusWrite != F_SUCCESS_0)
-            ret = F_ERROR_1;
+            ret = -6;
         wait_ms(3);
         statusRead = readReg(reg_addr, read_data);
         if  (statusRead != F_SUCCESS_0)
-            ret = F_ERROR_1;
+            ret = -7;
         if (read_data != reg_data) {
-            ret = F_ERROR_1;
+            ret = -8;
             retries--;
         }
     } while (retries && read_data != reg_data);
@@ -212,8 +187,8 @@
 /**
  * @brief       Initialization Function for MAX17055.
  * @par         Details
- *              This function intitializes the MAX17055 for the implementation of the EZconfig model.\n
- *              The libraty needs to be customized for the implementation of customize model.\n  
+ *              This function initializes the MAX17055 for the implementation of the EZconfig model.\n
+ *              The library needs to be customized for the implementation of customize model.\n  
  *              
  * @retval      0 on success 
  * @retval      non-0 for errors
@@ -223,54 +198,46 @@
 
     int status, ret;
     int time_out = 10;
-    uint16_t read_data, hibcfg_value, reg;
+    uint16_t hibcfg_value,read_data;
 
 
     status = readReg(VERSION_REG, read_data);
     if (status != F_SUCCESS_0)
         return status;
 
-    /* Step 0: Check for POR */
-    /* Skip load model if POR bit is cleared */
+    ///STEP 0. Check for POR (Skip load model if POR bit is cleared)
 
     if (check_POR_func() == F_ERROR_5)
         return F_ERROR_5;  //POR not detected. Skip Initialization.
+    //This is not an error. 
 
-    /* Step 1: Check if FStat.DNR == 0 */
-    // Do not continue until FSTAT.DNR == 0
+    ///STEP 1. Check if FStat.DNR == 0 (Do not continue until FSTAT.DNR == 0)
     ret = poll_flag_clear(FSTAT_REG, MAX17055_FSTAT_DNR, time_out);
     if (ret < F_SUCCESS_0) {
-        //printf("Unsuccessful init: Data Not Ready!\n");
         return ret;
     }
 
-    /* Force exit from hibernate */
-    hibcfg_value = forcedExitHyberMode();
+    ///STEP 1.2. Force exit from hibernate
+    hibcfg_value = forcedExitHiberMode();
 
     //printf("step 1 check \r\n");
 
-    /* Step 2: Initialize configuration */
+    ///STEP 2. Initialize configuration
     switch (1) {
         case MODEL_LOADING_OPTION1:
-            /* Step 2.1: Option 1 EZ Config */
+            ///STEP 2.1. Load EZ Config
             EZconfig_init(des_data);
 
-            /* Poll ModelCFG.ModelRefresh bit for clear */
+            ///STEP 2.2. Poll ModelCFG.ModelRefresh bit for clear
             ret = poll_flag_clear(MODELCFG_REG, MAX17055_MODELCFG_REFRESH, time_out);
             if(ret < F_SUCCESS_0) {
-                //dev_err(priv->dev, "Option1 model refresh not completed!\n");
                 return ret;
             }
 
             break; 
     }
-    /* Restore original HibCfg */
+    ///STEP3. Restore original HibCfg
     writeReg(HIBCFG_REG, hibcfg_value);
-    //printf("Last section check \r\n");
-
-
-    /* Optional step - alert threshold initialization */
-    //set_alert_thresholds(priv);
 
     /* Clear Status.POR */
     ret = clear_POR_bit();
@@ -304,7 +271,7 @@
 /**
  * @brief        clear POR bit function
  * @par          Details
- *               This function clear the idicating bit for POR - MAX17055
+ *               This function clear the indicating bit for POR - MAX17055
  *
  * @retval       0 for Success
  * @retval      non-0 for errors
@@ -397,9 +364,9 @@
  * @par          Details
  *               This function executes a force exit from hibernate mode.
  *
- * @retval       HibCFG original value before forced Exit Hybernate mode *
+ * @retval       HibCFG original value before forced Exit Hibernate mode *
  */
-uint16_t MAX17055::forcedExitHyberMode()
+uint16_t MAX17055::forcedExitHiberMode()
 {
     uint16_t hibcfg;
 
@@ -407,46 +374,46 @@
     //STEP 0: Store original HibCFG value
     readReg(HIBCFG_REG, hibcfg);
 
-    //STEP 1: Write to Soft-Wakeup Commannd Register
-    writeReg(VFSOC0_QH0_LOCK_REG, 0x90); //Soft-Wakeup from hybernate
+    //STEP 1: Write to Soft-Wakeup Command Register
+    writeReg(VFSOC0_QH0_LOCK_REG, 0x90); //Soft-Wakeup from hibernate
 
     //STEP 2: Write to Hibernate Configuration register
     writeReg(HIBCFG_REG, 0x0); //disable hibernate mode
 
-    //STEP 3:Write to Soft-Wakeup Commannd Register
-    writeReg(VFSOC0_QH0_LOCK_REG, 0x0); //Clear All commnads
+    //STEP 3:Write to Soft-Wakeup Command Register
+    writeReg(VFSOC0_QH0_LOCK_REG, 0x0); //Clear All commands
 
     return hibcfg;
 }
 
 /**
- * @brief        EZ Confing Initialization function
+ * @brief        EZ Config Initialization function
  * @par          Details
- *               This function implements the steps for the EZ confing m5 FuelGauge
- * @param[in]    des_data - Plataform_data struct with information about the deisgn.
+ *               This function implements the steps for the EZ config m5 FuelGauge
+ * @param[in]    des_data - Plataform_data struct with information about the design.
  * @retval       0 on success
  * @retval       non-zero for errors
  */
 uint16_t MAX17055::EZconfig_init(platform_data des_data)
 {
-    ///EZ config values
+    ///STEP 2.1.1 EZ config values suggested by manufacturer.
     const int charger_th = 4275;
-    const int chg_V_high = 51200;
+    const int chg_V_high = 51200; // scaling factor high voltage charger
     const int chg_V_low = 44138;
-    const int param_EZ_FG1 = 0x8400;
+    const int param_EZ_FG1 = 0x8400; // Sets config bit for the charge voltage for the m5
     const int param_EZ_FG2 = 0x8000;
     uint16_t dpacc, ret;
 
-    /* Step 2.1: Option 1 EZ Config */
+    ///STEP 2.1.2 Store the EZ Config values into the appropriate registers. 
     ret = writeReg(DESIGNCAP_REG, des_data.designcap);
-    ret = writeReg(DQACC_REG, des_data.designcap >> 5);  ///DesignCap divide by 32
+    ret = writeReg(DQACC_REG, des_data.designcap >> 5);  //DesignCap divide by 32
     ret = writeReg(ICHGTERM_REG, des_data.ichgterm);
     ret = writeReg(VEMPTY_REG, des_data.vempty);
 
     if (des_data.vcharge > charger_th) {
         dpacc = (des_data.designcap >> 5) * chg_V_high / des_data.designcap;
         ret = writeReg(DPACC_REG, dpacc);
-        ret = writeReg(MODELCFG_REG, param_EZ_FG1); ///Why 0x8400??
+        ret = writeReg(MODELCFG_REG, param_EZ_FG1); // 
     } else {
         dpacc = (des_data.designcap >> 5) * chg_V_low / des_data.designcap;
         ret = writeReg(DPACC_REG, dpacc);
@@ -480,30 +447,29 @@
 }
 
 /**
- * @brief        Get Average State Of Charge(SOC) Function from MAX17055 Fuel Gauge.
+ * @brief       Get at rate Average State Of Charge(SOC) Function from MAX17055 Fuel Gauge.
  * @par          Details
- *               This function sends a request to access the avSOC register of the MAX17055.
+ *               This function sends a request to access the atAvSOC register of the MAX17055.
  *               The AvSOC registers hold the calculated available capacity and percentage of the
  *               battery based on all inputs from the ModelGauge m5 algorithm including empty 
  *               compensation. These registers provide unfiltered results. Jumps in the reported 
  *               values can be caused by abrupt changes in load current or temperature.
  *
- * @retval       avSOC_data - Average SOC data from the AVSOC register in % value. 
+ * @retval       atAvSOC_data - Average SOC data from the atAVSOC register in % value. 
  * @retval       non-0 negative values check for errors      
  */
-int MAX17055::get_avSOC()
+int MAX17055::get_atAvSOC()
 {
-
     int ret;
-    uint16_t avSOC_data;
+    uint16_t atAvSOC_data;
 
-    ret = readReg(AVSOC_REG, avSOC_data);
+    ret = readReg(AVSOC_REG, atAvSOC_data);
     if (ret < F_SUCCESS_0)
         return ret; //Check errors if data is not correct
 
-    avSOC_data = avSOC_data >> 8; /* avSOC LSB: 1/256 % */
+    atAvSOC_data = atAvSOC_data >> 8; /* avSOC LSB: 1/256 % */
 
-    return avSOC_data;
+    return atAvSOC_data;
 }
 
 /**
@@ -544,19 +510,20 @@
  * @retval      tte_data - Time to Empty data from the TTE register in seconds.
  * @retval      non-0 negative values check for errors
  */
-int MAX17055::get_TTE()
+float MAX17055::get_TTE()
 {
 
     int ret;
     uint16_t tte_data;
+    float f_tte_data;
 
     ret = readReg(TTE_REG, tte_data);
     if (ret < F_SUCCESS_0)
         return ret;
     else
-        tte_data = (tte_data * 45) >> 3; /* TTE LSB: 5.625 sec */
+        f_tte_data = ((float)tte_data * 5.625); /* TTE LSB: 5.625 sec */
 
-    return tte_data;
+    return f_tte_data;
 }
 
 /**
@@ -568,19 +535,20 @@
  * @retval      atTTE_data - Time to Empty data from the atTTE register in seconds. 
  * @retval      non-0 negative values check for errors
  */
-int MAX17055::get_atTTE()
+float MAX17055::get_atTTE()
 {
 
     int ret;
     uint16_t atTTE_data;
+    float f_atTTE_data;
 
     ret = readReg(ATTTE_REG, atTTE_data);
     if (ret < F_SUCCESS_0)
         return ret; //Check for errors
     else
-       atTTE_data = (atTTE_data * 45) >> 3; /* TTE LSB: 5.625 sec */
+        f_atTTE_data = ((float)atTTE_data * 5.625); /* TTE LSB: 5.625 sec */
 
-    return atTTE_data;
+    return  f_atTTE_data;
 }
 
 /**
@@ -597,19 +565,20 @@
  * @retval     ttf_data - Time to Full data from the TTF register in seconds. 
  * @retval     non-0 negative values check for errors
  */
-int MAX17055::get_TTF()
+float MAX17055::get_TTF()
 {
 
     int ret;
     uint16_t ttf_data;
+    float f_ttf_data;
 
     ret = readReg(TTF_REG, ttf_data);
     if (ret < F_SUCCESS_0)
         return ret;
     else
-        ttf_data = (ttf_data * 45) >> 3; /* TTF LSB: 5.625 sec */
+        f_ttf_data = ((float)ttf_data * 5.625); /* TTE LSB: 5.625 sec */
 
-    return ttf_data;
+    return  f_ttf_data;
 }
 
 /**
@@ -639,9 +608,9 @@
  * @brief       Get current Function for MAX17055 Fuel Gauge.
  * @par         Details
  *              This function sends a request to access the CURRENT register
- *              of the MAX17055 to read the current redings. 
+ *              of the MAX17055 to read the current readings. 
  *
- * @param[in]   des_data - Plataform_data struct with information about the deisgn.
+ * @param[in]   des_data - Plataform_data struct with information about the design.
  * 
  * @retval      curr_data  - current data from the CURRENT register in uAmps. 
  * @retval      non-0 negative values check for errors.
@@ -665,9 +634,9 @@
  * @brief       Get average current Function for MAX17055 Fuel Gauge.
  * @par         Details
  *              This function sends a request to access the aveCURRENT register
- *              of the MAX17055 to read the average current redings. 
+ *              of the MAX17055 to read the average current readings. 
  *
- * @param[in]   des_data - Plataform_data struct with information about the deisgn.
+ * @param[in]   des_data - Plataform_data struct with information about the design.
  * 
  * @retval      aveCurr_data - current data from the AVGCURRENT register in uAmps. 
  * @retval      non-0 negative values check for errors.
@@ -689,7 +658,7 @@
 }
 
 /**
- * @brief        lsb_to_uvolts Converssion Function 
+ * @brief        lsb_to_uvolts Conversion Function 
  * @par          Details
  *               This function takes the lsb value of the register and convert it
  *               to uvolts
@@ -705,7 +674,7 @@
 }
 
 /**
- * @brief        raw_current_to_uamp Converssion Function 
+ * @brief        raw_current_to_uamp Conversion Function 
  * @par          Details
  *               This function takes the raw current value of the register and 
  *               converts it to uamps
@@ -720,7 +689,7 @@
     if (res & 0x8000) {
         res |= 0xFFFF0000;
     } else {
-        res *= 1562500 /(rsense_value * 1000); //Change to interact with the rsense implementen in the design
+        res *= 1562500 /(rsense_value * 1000); //Change to interact with the rsense implemented in the design
     }
     return res;
 }
@@ -750,7 +719,7 @@
         value = data[3];
     }
 
-    ///STEP 2.Save the capacity parametes for the specific battery.
+    ///STEP 2. Save the capacity parameters for the specific battery.
     ret = readReg(RCOMP0_REG, data[0]);
     if (ret < F_SUCCESS_0)
         return ret;
@@ -780,7 +749,7 @@
 }
 
 /**
- * @brief        Resotore Parameters Function for battery Fuel Gauge model.
+ * @brief        Restore Parameters Function for battery Fuel Gauge model.
  * @par          Details
  *               If power is lost, then the capacity information 
  *               can be easily restored with this function. 
@@ -793,7 +762,7 @@
 {
     int ret;
     uint16_t temp_data, fullcapnom_data, mixCap_calc, dQacc_calc;
-    uint16_t dPacc_value = 0x0C80;//Why this vcalue?
+    uint16_t dPacc_value = 0x0C80;//Set it to 200%
 
     ///STEP 1. Restoring capacity parameters 
     write_and_verify_reg(RCOMP0_REG, FG_params.rcomp0);
@@ -807,7 +776,7 @@
     if (ret < F_SUCCESS_0)
         return ret;
 
-    ret = readReg(MIXSOC_REG, temp_data); //check if Error in sofware guide register incorrect
+    ret = readReg(MIXSOC_REG, temp_data); //check if Error in software guide register incorrect
     if (ret < F_SUCCESS_0)
         return ret;
     
@@ -834,7 +803,7 @@
 /**
  * @brief        Function to Save Average Current to At Rate register.
  * @par          Details
- *               For User frendliness display of TTE, avSOC, avCAP
+ *               For User friendliness display of atTTE, atAvSOC, atAvCAP
  *               write the average current to At Rate registers every 10sec 
  *               when the battery is in use.
  *               NOTE: do not use this function when the Battery is charging. 
@@ -848,12 +817,14 @@
     uint16_t avCurr_data;
 
     ret = readReg(AVGCURRENT_REG, avCurr_data);
-    if (ret < F_SUCCESS_0)
-        return ret;
+    if (ret < F_SUCCESS_0){
+        return ret = -3;
+    }      
 
     //Write avCurrent to atRate Register
-    ret = write_and_verify_reg(ATRATE_REG, avCurr_data);
-    if (ret < F_SUCCESS_0)
+    ret = writeReg(ATRATE_REG, avCurr_data);
+    if (ret < F_SUCCESS_0){
         return ret;
-    return ret;
+    }
+    return F_SUCCESS_0;
 }
\ No newline at end of file