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
Diff: max17055.h
- Revision:
- 17:7447aaa9c121
- Parent:
- 16:839186d0693a
- Child:
- 19:20590e00feab
diff -r 839186d0693a -r 7447aaa9c121 max17055.h --- 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.