Maxim Integrated / MAX17055_EZconfig

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

Files at this revision

API Documentation at this revision

Comitter:
fneirab
Date:
Tue Oct 09 20:28:41 2018 +0000
Parent:
16:839186d0693a
Child:
18:37bca022d144
Commit message:
New sample code Updated, new functions for REP cap and specific register reads and clean up some of the old functions.

Changed in this revision

max17055.cpp Show annotated file Show diff for this revision Revisions of this file
max17055.h Show annotated file Show diff for this revision Revisions of this file
--- a/max17055.cpp	Mon Jun 18 21:47:59 2018 +0000
+++ b/max17055.cpp	Tue Oct 09 20:28:41 2018 +0000
@@ -36,6 +36,8 @@
 
 /* POR Mask */
 #define MAX17055_POR_MASK               (0xFFFD)
+#define MAX17055_CYCLE_MASK             (0x0002)
+
 
 /* MODELCFG register bits */
 #define MAX17055_MODELCFG_REFRESH       (1 << 15)
@@ -47,7 +49,7 @@
 /* LIBRARY FUNCTION SUCCESS*/
 #define F_SUCCESS_0  0
 
-/* LIBRARY FUNCTION ERROR CODES */  
+/* LIBRARY FUNCTION ERROR CODES */
 #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
@@ -130,10 +132,33 @@
 }
 
 /**
+ * @brief      Reads an specified register from the MAX17055 register.
+ *
+ * @param[in]  reg_addr  The register address
+ * @param      value     The value
+ *
+ * @retval     reg_data register data
+ * @retval     statusRead non-0 for errors
+ */
+
+int16_t MAX17055::get_regInfo(Registers_e reg_addr)
+{
+    uint16_t read_data;
+    int statusRead;
+
+    statusRead = readReg(reg_addr, read_data);
+    if (statusRead != F_SUCCESS_0)
+        return statusRead;
+    else
+        return read_data;
+
+}
+
+/**
  * @brief        Write and Verify a MAX17055 register
  * @par          Details
  *               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
@@ -152,13 +177,13 @@
     do {
         statusWrite = writeReg(reg_addr, reg_data);
         if (statusWrite != F_SUCCESS_0)
-            ret = -6;
+            ret = F_ERROR_1;
         wait_ms(3);
         statusRead = readReg(reg_addr, read_data);
         if  (statusRead != F_SUCCESS_0)
-            ret = -7;
+            ret = F_ERROR_1;
         if (read_data != reg_data) {
-            ret = -8;
+            ret = F_ERROR_3;
             retries--;
         }
     } while (retries && read_data != reg_data);
@@ -173,28 +198,27 @@
  * @brief       Initialization Function for MAX17055.
  * @par         Details
  *              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 
+ *              The library needs to be customized for the implementation of customize model.\n
+ *
+ * @retval      0 on success
  * @retval      non-0 for errors
  */
 int MAX17055::init(platform_data des_data)
 {
 
-    int status, ret;
+    int ret;
     int time_out = 10;
-    uint16_t hibcfg_value,read_data;
+    uint16_t hibcfg_value;
 
 
-    status = readReg(VERSION_REG, read_data);
-    if (status != F_SUCCESS_0)
-        return status;
+//    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)
 
     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)
     ret = poll_flag_clear(FSTAT_REG, MAX17055_FSTAT_DNR, time_out);
@@ -205,21 +229,15 @@
     ///STEP 1.2. Force exit from hibernate
     hibcfg_value = forcedExitHiberMode();
 
-    //printf("step 1 check \r\n");
 
     ///STEP 2. Initialize configuration
-    switch (1) {
-        case MODEL_LOADING_OPTION1:
-            ///STEP 2.1. Load EZ Config
-            EZconfig(des_data);
+    ///STEP 2.1. Load EZ Config
+    EZconfig(des_data);
 
-            ///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) {
-                return ret;
-            }
-
-            break; 
+    ///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) {
+        return ret;
     }
     ///STEP3. Restore original HibCfg
     writeReg(HIBCFG_REG, hibcfg_value);
@@ -227,7 +245,7 @@
     /* Clear Status.POR */
     ret = clear_POR_bit();
     if (ret < F_SUCCESS_0)
-        return ret; //See errors   
+        return ret; //See errors
     return F_SUCCESS_0;
 }
 
@@ -239,17 +257,17 @@
  *
  * @retval    0 on success (POR detected)
  * @retval    non-0 for errors (POR not detected)
- * 
+ *
  */
 int MAX17055::check_POR_func()
 {
     uint16_t read_data;
-    
+
     readReg(STATUS_REG, read_data);
-
-    if (!(read_data & MAX17055_STATUS_POR ) )
+    printf("STATUS REF = %X \r\n", read_data);
+    if (!(read_data & MAX17055_STATUS_POR ) ) {
         return F_ERROR_5;  //POR not detected.
-    else 
+    } else
         return F_SUCCESS_0;
 }
 
@@ -263,8 +281,8 @@
  */
 int MAX17055::clear_POR_bit()
 {
-    int status, ret;
-    uint16_t read_data, hibcfg_value, reg;
+    int status;
+    uint16_t read_data;
 
 
     status = readReg(STATUS_REG, read_data);
@@ -273,12 +291,12 @@
     status = write_and_verify_reg(STATUS_REG, (read_data & MAX17055_POR_MASK));
     if (status != F_SUCCESS_0)
         return F_ERROR_1; //read or write error
-    else 
+    else
         return F_SUCCESS_0;
 }
 
 /**
- * @brief      Poll Flag clear Function. 
+ * @brief      Poll Flag clear Function.
  * @par Details
  *     This function clears status flags for the MAX17055
  *
@@ -286,7 +304,7 @@
  * @param[in]  mask      - register address
  * @param[in]  timeout   - register data
  *
- * @retval     0 on success 
+ * @retval     0 on success
  * @retval    non-0 negative for errors
  */
 int MAX17055::poll_flag_clear (Registers_e reg_addr, int mask, int timeout)
@@ -313,13 +331,13 @@
  * @brief        Get Temperature Function from the MAX17055 TEMP register.
  * @par          Details
  *               This function sends a request to access the TEMP register
- *               of the MAX17055, which reflects the temperature measured for the fuel gauge. 
+ *               of the MAX17055, which reflects the temperature measured for the fuel gauge.
  *               The temperature values will reflect the Config Register (0x1D) selections for Tsel bit (D15).
  *               For this library the setting are for die temperature.
  *               The MAX32620FTHR thermistor bias pin is not connected. The biasing of the thermistor is
- *               done by the MAX77650. See MAX77650 library for how to enable the thermistor biasing.  
+ *               done by the MAX77650. See MAX77650 library for how to enable the thermistor biasing.
  *
- *   
+ *
  * @retval      temp - Temperature value from TEMP register in &deg;C
  * @retval      non-0 negative values check for errors
  */
@@ -389,7 +407,7 @@
     const int param_EZ_FG2 = 0x8000;
     uint16_t dpacc, ret;
 
-    ///STEP 2.1.2 Store the EZ Config values into the appropriate registers. 
+    ///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(ICHGTERM_REG, des_data.ichgterm);
@@ -398,7 +416,7 @@
     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); // 
+        ret = writeReg(MODELCFG_REG, param_EZ_FG1); //
     } else {
         dpacc = (des_data.designcap >> 5) * chg_V_low / des_data.designcap;
         ret = writeReg(DPACC_REG, dpacc);
@@ -407,14 +425,42 @@
     return ret;
 }
 
+
+/**
+ * @brief        Get reported Battery Capacity Function from MAX17055 Fuel Gauge
+ * @par          Details
+ *               This function sends a request to access the RepCAP register
+ *               of the MAX17055. RepCAP is the reported Battery Capacity in mAh of the battery based on the calulation by the Fuel Gauge algorithm.
+ *
+ * @retval      repcap_data - Reported SOC data from the RepSOC register in % value.
+ * @retval      non-0 negative values check for errors
+ */
+
+int MAX17055::get_battCAP(platform_data des_data)
+{
+    int ret, design_rsense;
+    uint16_t repcap_data;
+
+    ret = readReg(REPCAP_REG, repcap_data);
+    if (ret < F_SUCCESS_0)
+        return ret;
+    else
+        design_rsense = des_data.rsense;
+    ret = raw_cap_to_uAh((uint32_t)repcap_data, design_rsense);
+    if (ret < F_SUCCESS_0)
+        return ret;
+    else
+        return ret;
+}
+
 /**
  * @brief        Get reported State Of Charge(SOC) Function from MAX17055 Fuel Gauge.
  * @par          Details
  *               This function sends a request to access the RepSOC register
- *               of the MAX17055. RepSOC is the reported state-of-charge percentage output of the fuel gauge. 
+ *               of the MAX17055. RepSOC is the reported state-of-charge percentage output of the fuel gauge.
  *
  * @retval      soc_data - Reported SOC data from the RepSOC register in % value.
- * @retval      non-0 negative values check for errors 
+ * @retval      non-0 negative values check for errors
  */
 int MAX17055::get_SOC()
 {
@@ -436,12 +482,12 @@
  * @par          Details
  *               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 
+ *               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       atAvSOC_data - Average SOC data from the atAVSOC register in % value. 
- * @retval       non-0 negative values check for errors      
+ * @retval       atAvSOC_data - Average SOC data from the atAVSOC register in % value.
+ * @retval       non-0 negative values check for errors
  */
 int MAX17055::get_atAvSOC()
 {
@@ -464,8 +510,8 @@
  *               of the MAX17055. The MixSOC registers holds the calculated
  *               remaining capacity and percentage of the cell before any empty compensation
  *               adjustments are performed.
- * 
- * @retval       mixSOC_data - Mixed SOC register values from the mixSOC register in % value. 
+ *
+ * @retval       mixSOC_data - Mixed SOC register values from the mixSOC register in % value.
  * @retval       non-0 for errors
  */
 int MAX17055::get_mixSOC()
@@ -487,9 +533,9 @@
  * @par         Details
  *              This function sends a request to access the TTE register
  *              of the MAX17055
- *              The TTE register holds the estimated time to empty for the 
- *              application under present temperature and load conditions. The TTE value is 
- *              determined by relating AvCap with AvgCurrent. The corresponding AvgCurrent 
+ *              The TTE register holds the estimated time to empty for the
+ *              application under present temperature and load conditions. The TTE value is
+ *              determined by relating AvCap with AvgCurrent. The corresponding AvgCurrent
  *              filtering gives a delay in TTE, but provides more stable results.
  *
  * @retval      tte_data - Time to Empty data from the TTE register in seconds.
@@ -517,7 +563,7 @@
  *              This function sends a request to access the internal register
  *              of the MAX17055
  *
- * @retval      atTTE_data - Time to Empty data from the atTTE register in seconds. 
+ * @retval      atTTE_data - Time to Empty data from the atTTE register in seconds.
  * @retval      non-0 negative values check for errors
  */
 float MAX17055::get_atTTE()
@@ -543,11 +589,11 @@
  *             The TTF register holds the estimated time to full for the application
  *             under present conditions. The TTF value is determined by learning the
  *             constant current and constant voltage portions of the charge cycle based
- *             on experience of prior charge cycles. Time to full is then estimate 
- *             by comparing present charge current to the charge termination current. 
+ *             on experience of prior charge cycles. Time to full is then estimate
+ *             by comparing present charge current to the charge termination current.
  *             Operation of the TTF register assumes all charge profiles are consistent in the application.
  *
- * @retval     ttf_data - Time to Full data from the TTF register in seconds. 
+ * @retval     ttf_data - Time to Full data from the TTF register in seconds.
  * @retval     non-0 negative values check for errors
  */
 float MAX17055::get_TTF()
@@ -570,8 +616,8 @@
  * @brief       Get voltage of the cell Function for MAX17055 Fuel Gauge.
  * @par         Details
  *              This function sends a request to access the VCell Register
- *              of the MAX17055 to read the measured voltage from the cell. 
- *              
+ *              of the MAX17055 to read the measured voltage from the cell.
+ *
  * @retval      vcell_data  - vcell data from the VCELL_REG register in uVolts.
  * @retval      non-0 negative values check for errors
  */
@@ -590,66 +636,90 @@
 }
 
 /**
+ * @brief       Gets Average voltage of the cell Function for MAX17055 Fuel Gauge.
+ * @par         Details
+ *              This function sends a request to access the AvgVCell Register
+ *              of the MAX17055 to read the measured voltage from the cell.
+ *
+ * @retval      avgVcell_data  - avgvcell data from the AVGVCELL_REG register in uVolts.
+ * @retval      non-0 negative values check for errors
+ */
+int MAX17055::get_avgVcell()
+{
+
+    int ret;
+    uint16_t avgVcell_data;
+
+    ret = readReg(AVGVCELL_REG, avgVcell_data);
+    if (ret < F_SUCCESS_0)
+        return ret;
+    else
+        ret = lsb_to_uvolts(avgVcell_data);
+    return ret;
+}
+
+/**
  * @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 readings. 
+ *              of the MAX17055 to read the current readings.
  *
  * @param[in]   des_data - Plataform_data struct with information about the design.
- * 
- * @retval      curr_data  - current data from the CURRENT register in uAmps. 
+ *
+ * @retval      curr_data  - current data from the CURRENT register in uAmps.
  * @retval      non-0 negative values check for errors.
  */
-int MAX17055::get_Current( platform_data des_data )
+float MAX17055::get_Current( platform_data des_data )
 {
 
     int ret,design_rsense;
     uint16_t curr_data;
+    float f_ret;
 
     ret = readReg(CURRENT_REG, curr_data);
     if (ret < F_SUCCESS_0)
         return ret;
     else
-    design_rsense = des_data.rsense;
-    ret = raw_current_to_uamps((uint32_t)curr_data, design_rsense);
-    return ret;
+        design_rsense = des_data.rsense;
+    f_ret = raw_current_to_uamps((uint32_t)curr_data, design_rsense);
+    return f_ret;
 }
 
 /**
  * @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 readings. 
+ *              of the MAX17055 to read the average current readings.
  *
  * @param[in]   des_data - Plataform_data struct with information about the design.
- * 
- * @retval      aveCurr_data - current data from the AVGCURRENT register in uAmps. 
+ *
+ * @retval      aveCurr_data - current data from the AVGCURRENT register in uAmps.
  * @retval      non-0 negative values check for errors.
  */
-int MAX17055::get_AvgCurrent( platform_data des_data )
+float MAX17055::get_AvgCurrent( platform_data des_data )
 {
     int ret, design_rsense;
     uint16_t data;
-    uint32_t aveCurr_data;
+    float avgCurr_data;
 
     ret = readReg(AVGCURRENT_REG, data);
     if (ret < F_SUCCESS_0)
         return ret;
     else
-    aveCurr_data = data;
+        avgCurr_data = data;
     design_rsense = des_data.rsense;
-    aveCurr_data = raw_current_to_uamps(aveCurr_data, design_rsense);
-    return aveCurr_data;
+    avgCurr_data = raw_current_to_uamps((uint32_t)data, design_rsense);
+    return avgCurr_data;
 }
 
 /**
- * @brief        lsb_to_uvolts Conversion Function 
+ * @brief        lsb_to_uvolts Conversion Function
  * @par          Details
  *               This function takes the lsb value of the register and convert it
  *               to uvolts
  *
  * @param[in]   lsb - value of register lsb
- * @retval      conv_2_uvolts - value converted lsb to uvolts        
+ * @retval      conv_2_uvolts - value converted lsb to uvolts
  */
 int MAX17055:: lsb_to_uvolts(uint16_t lsb)
 {
@@ -659,88 +729,118 @@
 }
 
 /**
- * @brief        raw_current_to_uamp Conversion Function 
+ * @brief        raw_current_to_uamp Conversion Function
  * @par          Details
- *               This function takes the raw current value of the register and 
+ *               This function takes the raw current value of the register and
  *               converts it to uamps
  *
- * @param[in]   curr - raw current value of register 
- * @retval      res  - converted raw current to uamps (Signed 2's complement)         
+ * @param[in]   curr - raw current value of register
+ * @retval      res  - converted raw current to uamps (Signed 2's complement)
  */
-int MAX17055::raw_current_to_uamps(uint32_t curr, int rsense_value)
+float MAX17055::raw_current_to_uamps(uint32_t curr, int rsense_value)
 {
     int res = curr;
-    /* Negative */
-    if (res & 0x8000) {
+    float final_res;
+    /* Negative Check*/
+    if (res & 0x8000){
         res |= 0xFFFF0000;
-    } else {
-        res *= 1562500 /(rsense_value * 1000); //Change to interact with the rsense implemented in the design
     }
+    final_res = (float)res;
+    final_res *= 1562500 /(float)(rsense_value*1000000);
+
+    return final_res;
+}
+
+/**
+ * @brief        raw_cap_to_uAh Conversion Function
+ * @par          Details
+ *               This function takes the raw battery capacity value of the register and
+ *               converts it to uAh
+ *
+ * @param[in]   raw_cap - raw capacity value of register
+ * @retval      res  - converted raw capacity to uAh
+ */
+int MAX17055::raw_cap_to_uAh(uint32_t raw_cap, int rsense_value)
+{
+    int res = raw_cap ;
+    res *=  5000000/(rsense_value * 1000000);
     return res;
 }
 
 /**
- * @brief        Save Learned Parameters Function for battery Fuel Gauge model.  
+ * @brief        Save Learned Parameters Function for battery Fuel Gauge model.
  * @par          Details
  *               It is recommended to save the learned capacity parameters every
  *               time bit 2 of the Cycles register toggles
- *               (so that it is saved every 64% change in the battery) 
+ *               (so that it is saved every 64% change in the battery)
  *               so that if power is lost the values can easily be restored. Make sure
- *               the data is saved on a non-volatile memory.   
+ *               the data is saved on a non-volatile memory. Call this functinton after first initialization for reference in future function calls.
+ *               Max muber of cycles is 655.35 cycles with a LSB of 1% for the cycles register.
  *
- * @param[in]   FG_params Fuel Gauge Parameters based on design details. 
- * 
+ * @param[in]   FG_params Fuel Gauge Parameters based on design details.
+ *
  * @retval      0 for success
  * @retval      non-0 negative for errors
  */
 int MAX17055::save_Params(saved_FG_params_t FG_params)
 {
-    int ret, value;
-    uint16_t data[5];
+    int ret;
+    uint16_t data[5], value;
     ///STEP 1. Checks if the cycel register bit 2 has changed.
     ret = readReg(CYCLES_REG, data[3]);
-    if (ret < F_SUCCESS_0)
-        return ret;
-    else {
-        value = data[3];
-    }
-
-    ///STEP 2. Save the capacity parameters for the specific battery.
-    ret = readReg(RCOMP0_REG, data[0]);
+    value = data[3];
     if (ret < F_SUCCESS_0)
         return ret;
-    else
-        FG_params.rcomp0 = data[0];
+    //Check if the stored cycles value is different from the read Cycles_reg value
+    else if (FG_params.cycles == value)
+        return ret; //exits the function without saving, when initializing or value did not change (calculate when the function is called in you application).
+    else {
+        value = FG_params.cycles^value;
+        //check with mask
+        value = (value & MAX17055_POR_MASK);
+
+        if (value == 0)
+            return ret;
+
+        ///STEP 2. Save the capacity parameters for the specific battery.
+        ret = readReg(RCOMP0_REG, data[0]);
+        if (ret < F_SUCCESS_0)
+            return ret;
+        else
+            FG_params.rcomp0 = data[0];
 
-    ret = readReg(TEMPCO_REG, data[1]);
-    if (ret < F_SUCCESS_0)
-        return ret;
-    else
-        FG_params.temp_co = data[1];
+        ret = readReg(TEMPCO_REG, data[1]);
+        if (ret < F_SUCCESS_0)
+            return ret;
+        else
+            FG_params.temp_co = data[1];
 
-    ret = readReg(FULLCAPREP_REG, data[2]);
-    if (ret < F_SUCCESS_0)
+        ret = readReg(FULLCAPREP_REG, data[2]);
+        if (ret < F_SUCCESS_0)
+            return ret;
+        else
+            FG_params.full_cap_rep = data[2];
+
+        FG_params.cycles = data[3];
+
+        ret = readReg(FULLCAPNOM_REG, data[4]);
+        if (ret < F_SUCCESS_0)
+            return ret;
+        else
+            FG_params.full_cap_nom = data[4];
         return ret;
-    else
-        FG_params.full_cap_rep = data[2];
-
-    FG_params.cycles = data[3];
+    }
+}
 
-    ret = readReg(FULLCAPNOM_REG, data[4]);
-    if (ret < F_SUCCESS_0)
-        return ret;
-    else
-        FG_params.full_cap_nom = data[4];
-    return ret;
-}
+
 
 /**
  * @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. 
+ *               If power is lost, then the capacity information
+ *               can be easily restored with this function.
  *
- * @param[in]   FG_params Struct for Fuel Gauge Parameters 
+ * @param[in]   FG_params Struct for Fuel Gauge Parameters
  * @retval      0 for success
  * @retval      non-0 negative for errors
  */
@@ -750,13 +850,13 @@
     uint16_t temp_data, fullcapnom_data, mixCap_calc, dQacc_calc;
     uint16_t dPacc_value = 0x0C80;//Set it to 200%
 
-    ///STEP 1. Restoring capacity parameters 
+    ///STEP 1. Restoring capacity parameters
     write_and_verify_reg(RCOMP0_REG, FG_params.rcomp0);
     write_and_verify_reg(TEMPCO_REG, FG_params.temp_co);
     write_and_verify_reg(FULLCAPNOM_REG, FG_params.full_cap_nom);
-    
+
     wait_ms(350);//check the type of wait
-    
+
     ///STEP 2. Restore FullCap
     ret = readReg(FULLCAPNOM_REG, fullcapnom_data);
     if (ret < F_SUCCESS_0)
@@ -765,12 +865,12 @@
     ret = readReg(MIXSOC_REG, temp_data); //check if Error in software guide register incorrect
     if (ret < F_SUCCESS_0)
         return ret;
-    
+
     mixCap_calc = (temp_data*fullcapnom_data)/25600;
 
     write_and_verify_reg(MIXCAP_REG, mixCap_calc);
     write_and_verify_reg(FULLCAPREP_REG, FG_params.full_cap_rep);
-    
+
     ///STEP 3. Write DQACC to 200% of Capacity and DPACC to 200%
     dQacc_calc = (FG_params.full_cap_nom/ 16) ;
 
@@ -790,9 +890,9 @@
  * @brief        Function to Save Average Current to At Rate register.
  * @par          Details
  *               For User friendliness display of atTTE, atAvSOC, atAvCAP
- *               write the average current to At Rate registers every 10sec 
+ *               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. 
+ *               NOTE: do not use this function when the Battery is charging.
  *
  * @retval      0 for success
  * @retval      non-0 negative for errors
@@ -803,13 +903,13 @@
     uint16_t avCurr_data;
 
     ret = readReg(AVGCURRENT_REG, avCurr_data);
-    if (ret < F_SUCCESS_0){
+    if (ret < F_SUCCESS_0) {
         return ret = -3;
-    }      
+    }
 
     //Write avCurrent to atRate Register
     ret = writeReg(ATRATE_REG, avCurr_data);
-    if (ret < F_SUCCESS_0){
+    if (ret < F_SUCCESS_0) {
         return ret;
     }
     return F_SUCCESS_0;
--- a/max17055.h	Mon Jun 18 21:47:59 2018 +0000
+++ b/max17055.h	Tue Oct 09 20:28:41 2018 +0000
@@ -44,56 +44,113 @@
 /// Model loading options
 #define MODEL_LOADING_OPTION1           1 //EZ Config
 
-//Remove this and leave it 
+//Remove this and leave it
 
 /**
  * @brief MBED Library for the MAX17055\n
  * The MAX17055 is a low 7μA operating current fuel gauge which  \n
- * implements Maxim ModelGauge™ m5 EZ algorithm.                 \n   
+ * implements Maxim ModelGauge™ m5 EZ algorithm.                 \n
  * <a href="https://www.maximintegrated.com/en/design/partners-and-technology/design-technology/modelgauge-battery-fuel-gauge-technology.html">ModelGauge</a>
  * m5 EZ makes fuel gauge implementation easy by eliminating     \n
  * battery characterization requirements and simplifying host    \n
  * software interaction. The ModelGauge m5 EZ robust algorithm   \n
  * provides tolerance against battery diversity for most lithium \n
- * batteries and applications. Communication is through an       \n
- * SPI-compatible interface. The MAX17055 comes as part of the   \n 
- * MAX32620FTHR MBED enable development board.\n 
+ * batteries and applications. Communication is through a       \n
+ * I2C interface. The MAX17055 comes as part of the   \n
+ * MAX32620FTHR MBED enable development board.\n
  * \n
  * Visit the product page for more information:
  * <a href="https://www.maximintegrated.com/MAX17055.html">MAX17055 Product Page</a>\n
  * <a href="https://www.maximintegrated.com/MAX17055.pdf">MAX17055 Data Sheet</a>\n
  * <a href="https://www.maximintegrated.com/MAX32620FTHR.html">MAX32620FTHR Product Page</a>\n
  * <a href="https://www.maximintegrated.com/MAX32620FTHR.pdf">MAX32620FTHR Data Sheet</a>\n
- * 
+ *
  * @code
- * 
- * ///This is not the final test code. Just sample place holder. 
  * #include "mbed.h"
- * #include "MAX17055.h"
+ * #include "max17055.h"
  *
  *
- * // Hardware serial port
- * Serial serial(USBTX, USBRX);
- *
- * //SPI communications
- * I2C i2c(SCL, SDA);
- *
- * //Fuel Gauge
- * MAX17055 max17055(i2C, Sutff );//To be completed
+ *    //LED indication
+ *    DigitalOut rled(LED1);
+ *    
+ *    //I2C communications
+ *    I2C i2cBus(P5_7, P6_0);
+ *    
+ *    //Fuel Gauge
+ *    MAX17055 fuelGauge(i2cBus);
+ *    MAX17055::platform_data design_param;
+ *    MAX17055::saved_FG_params_t saved_param;
+ *    
+ *    //MAX77650 PMIC
+ *    #define POWER_HOLD_ON 1
+ *    #define POWER_HOLD_OFF 0
+ *    
+ *    DigitalOut pw_Hold(P2_2, POWER_HOLD_ON); //To enable battery operation from the PMIC
+ *    
+ *    //Battery Parameters Storage from the Fuel Gauge MAX17055
+ *    union max17055_u {
+ *        struct battery {
+ *            uint8_t avg_soc_FG;     // in Battery Percent
+ *           float tte_FG;           // in seconds
+ *            float ttf_FG;           // in seconds
+ *            int avg_vcell_FG;       // in 78.125uV per bit
+ *            float avg_curr_FG;      // in uAmps
+ *            int rep_cap;            // in mAh
+ *            int rep_SOC;            // in %
+ *        } battery;
+ *    } max17055_u;
+ *    
+ *    
+ *    int main(void)
+ *    {
+ *        rled = true;
+ *        //These are the calculated paramters for rsense 5mOhm foun in the MAX32620FTHR and a 350mAh Li+ Cell.
+ *        design_param.designcap  = 0x015E;  //Design Battery Capacity mAh thic can change depending on the batteries implemented see battery data sheet for details.
+ *        design_param.ichgterm  = 0x0070;  // Charge Termination Current for the Battery This is specified by the manufacturer.
+ *        design_param.vempty  = 0x9600;  // Battery Empty Voltage This is sepecified by design, but manufacturer has a min Empy voltage specification.
+ *        design_param.vcharge  = 4200;  // Battery Charge Voltage can be obtained from MAX77650 configuration
+ *        design_param.rsense = 5; //5mOhms for MAX32620, keep in mind the MAX17055EVKIT has a 10mOhm resistor. This is a design specific value. Used for calculation results.
  *
+ * //     //This are the parameters for an rsense = 10mOhm similar to the one found in the MAX17055EVKIT and a 350mAh Li+ Cell.
+ * //     design_param.designcap = 0x02BC;  //Design Battery Capacity mAh
+ * //     design_param.ichgterm = 0x00E0;  // Charge Termination Current for the Battery
+ * //     design_param.vempty = 0x9600;  // Battery Empty Voltage
+ * //     design_param.vcharge = 4200;  // Battery Charge Voltage  (Specified by manufacurer)
+ * //     design_param.rsense = 10; //10mOhms
  *
- * int main(void)
- * {
- *     CODE CODE TBD
- *      while(true)
- *      {
- *          CODE CODE TBD
- *      }
+ *    //Saved Parameters
+ *    //saved_param.cycles = 0; //This value is used for the save parameters function
+ *    
+ *    status = fuelGauge.init(design_param);
+ * //    printf("Init FuelGauge Function Status= %X \r\n",status);
+ *
+ *    if (status == 0)
+ *        fuelGauge.save_Params(saved_param);
+ *
+ *    while (1) {
+ *        //This code is an example to ilustrate the performance or the Fuel Gauge. This can change with the design requirements. Use this as a way to troubleshoot the FuelGauge.
+ *        rled = !rled;
+ *
+ *       max17055_u.battery.avg_vcell_FG = fuelGauge.get_avgVcell();
+ *       max17055_u.battery.avg_curr_FG  = fuelGauge.get_AvgCurrent(design_param);
+ *        max17055_u.battery.rep_cap      = fuelGauge.get_battCAP(design_param);
+ *        max17055_u.battery.rep_SOC      = fuelGauge.get_SOC();
+ *        
+ *        //This code works with Arduino Serial Plotter to visualize data
+ *        printf("%f " ,(max17055_u.battery.avg_vcell_FG/1000000.0));
+ *        printf("%f " ,max17055_u.battery.avg_curr_FG);
+ *        printf("%d " ,max17055_u.battery.rep_cap);
+ *        printf("%d " ,max17055_u.battery.rep_SOC);
+ *        printf("\n");
+ *       //printf(" ");
+ *        wait(1);
+ *     }
  * }
  * @endcode
  */
 
 
+
 /*-------------------------------------------------------------------------*//**
  * MAX17055 Class
  * @brief      Class for MAX17055 Battery Fuel Gauge
@@ -113,17 +170,17 @@
      * @brief      Register Addresses for the MAX17055
      * @details    Enumerated register addresses
      */
-    enum Registers_e {   
+    enum Registers_e {
         STATUS_REG                 = 0x00, /*!< 0x00 default value = 0x0002 */
         VALRTTH_REG                = 0x01, /*!< 0x01 */
-        TALRTTH_REG                = 0x02, /*!< 0x02 */ 
-        SALRTTH_REG                = 0x03, /*!< 0x03 */ 
+        TALRTTH_REG                = 0x02, /*!< 0x02 */
+        SALRTTH_REG                = 0x03, /*!< 0x03 */
         ATRATE_REG                 = 0x04, /*!< 0x04 write negative 2s comp of a 16-bit theoretical load */
-        REPCAP_REG                 = 0x05, /*!< 0x05 */ 
+        REPCAP_REG                 = 0x05, /*!< 0x05 */
         REPSOC_REG                 = 0x06, /*!< 0x06 */
         TEMP_REG                   = 0x08, /*!< 0x08 */
         VCELL_REG                  = 0x09, /*!< 0x09 */
-        CURRENT_REG                = 0x0A, /*!< 0x0A */ 
+        CURRENT_REG                = 0x0A, /*!< 0x0A */
         AVGCURRENT_REG             = 0x0B, /*!< 0x0B */
         MIXSOC_REG                 = 0x0D, /*!< 0x0D */
         AVSOC_REG                  = 0x0E, /*!< 0x0E */
@@ -181,12 +238,12 @@
         OCV_REG                    = 0xFB, /*!< 0x39 */
         VFSOC_REG                  = 0xFF  /*!< 0x39 */
     };
-      
+
     /**
      * @brief      Saved Platform Data for Fuel Gauge Model
      * @details    Struct with fuel Gauge Platform Data for Fuel Gauge Model based on the final design.
      */
-    struct platform_data{  //to clarify if Part of the class
+    struct platform_data { //to clarify if Part of the class
         uint16_t designcap;/*!< struct value 1 */
         uint16_t ichgterm; /*!< struct value 2 */
         uint16_t vempty;   /*!< struct value 3 */
@@ -196,11 +253,11 @@
          * rsense in miliOhms.
          * default 10 (if rsense = 0) as it is the recommended value by
          * the datasheet although it can be changed by board designers.
-         * MAX32620FTHR has a 5mOhm sense resitor installed. 
+         * MAX32620FTHR has a 5mOhm sense resitor installed.
          */
-        unsigned int rsense; 
-     } ;
-    
+        unsigned int rsense;
+    } ;
+
     /**
      * @brief      Saved Fuel Gauge Parameters
      * @details    It is recommended to save the learned capacity parameters
@@ -208,14 +265,14 @@
      *             is saved every 64% change in the battery) so that if power is
      *             lost the values can easily be restored.
      */
-    struct saved_FG_params_t{
+    struct saved_FG_params_t {
         int rcomp0;              /**< The RComp0 is the characterization information critical to computing the open-circuit voltage of a cell under loaded conditions. */
         int temp_co;             /**< The TempCo value is the temperature compensation information based on the RComp0 value*/
         int full_cap_rep;        /**< The full capacity in relation with RepCap for reporting to the GUI. A new full-capacity value is calculated at the end of every charge cycle in the application. */
         int cycles;              /**< The Cycles value maintains a total count of the number of charge/discharge cycles of the cell that have occurred */
         int full_cap_nom;        /**< This is the calculated full capacity of the cell, not including temperature and empty compensation. A new full-capacity nominal value
                                     is calculated each time a cell relaxation event is detected. This values is used to generate other outputs of the ModelGauge m5 algorithm. */
-     } ;
+    } ;
 
     /**
      * @brief       max17055 Constructor
@@ -226,7 +283,7 @@
      * @brief       Fuel Gauge Destructor
      */
     ~MAX17055();
-    
+
     /**
      * @brief      Poll Flag clear Function.
      */
@@ -260,13 +317,18 @@
     /**
      * @brief       Forced Exit Hibernate Mode Function for MAX17055
      */
-    uint16_t forcedExitHiberMode();// Hibernate spelling 
-    
+    uint16_t forcedExitHiberMode();// Hibernate spelling
+
     /**
      * @brief       EZ Config function
      */
     uint16_t EZconfig(platform_data des_data);
-      
+
+    /**
+     * @brief       Get reported Battery Capacity Function from MAX17055 Fuel Gauge
+     */
+    int get_battCAP(platform_data des_data);
+
     /**
      * @brief       Get reported State Of Charge(SOC) Function from MAX17055 Fuel Gauge
      */
@@ -279,12 +341,12 @@
 
     /**
      * @brief       Get the Time to Empty(TTE) Function form MAX17055 Fuel Gauge.
-     */   
+     */
     float get_TTE();
 
     /**
      * @brief       Get the at Time to Empty(atTTE) value Function for MAX17055 Fuel Gauge.
-     */   
+     */
     float get_atTTE();
 
     /**
@@ -296,31 +358,41 @@
      * @brief      Get the Time to Full(TTE) values Function for MAX17055 Fuel Gauge.
      */
     float get_TTF();
-    
+
     /**
      * @brief       Get voltage of the cell Function for MAX17055 Fuel Gauge.
      */
     int get_Vcell();
 
     /**
+     * @brief       Get average voltage of the cell Function for MAX17055 Fuel Gauge.
+     */
+    int get_avgVcell();
+
+    /**
      * @brief       Get current Function for MAX17055 Fuel Gauge.
      */
-    int get_Current(platform_data des_data);
-    
+    float get_Current(platform_data des_data);
+
     /**
      * @brief       Get average current Function for MAX17055 Fuel Gauge.
-     */  
-    int get_AvgCurrent(platform_data des_data);
-    
+     */
+    float get_AvgCurrent(platform_data des_data);
+
     /**
-     * @brief        lsb_to_uvolts Conversion Function         
+     * @brief        lsb_to_uvolts Conversion Function
      */
     int lsb_to_uvolts(uint16_t lsb);
-    
+
     /**
-     * @brief        raw_current_to_uamp Conversion Function         
+     * @brief        raw_current_to_uamp Conversion Function
      */
-    int raw_current_to_uamps(uint32_t curr, int rsense_value);
+    float raw_current_to_uamps(uint32_t curr, int rsense_value);
+
+    /**
+     * @brief        raw_cap_to_uAh Conversion Function
+     */
+    int raw_cap_to_uAh(uint32_t raw_cap, int rsense_value);
 
     /**
      * @brief        Save Learned Parameters Function for battery Fuel Gauge model.
@@ -337,7 +409,12 @@
      */
     int avCurr_2_atRate();
 
-protected:
+    /**
+     * @brief       Get specified register info Function for MAX17055 Fuel Gauge.
+     */
+    int16_t get_regInfo(Registers_e reg_addr);
+
+//protected:
 
     /**
      * @brief      Writes to MAX17055 register.