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

Committer:
fneirab
Date:
Thu Apr 19 22:34:45 2018 +0000
Revision:
12:519a18fc3b28
Parent:
11:bdbd3104995b
Child:
13:fc91b283e689
updates version 1.5 ; Remove necessary code; Spell check; Error check; Still need a bit more work to finish;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fneirab 4:a4d6ae2182c2 1 /******************************************************************//**
fneirab 4:a4d6ae2182c2 2 * @file max17055.cpp
fneirab 4:a4d6ae2182c2 3 *
fneirab 4:a4d6ae2182c2 4 * @author Felipe Neira - Maxim Integrated - TTS
fneirab 4:a4d6ae2182c2 5 *
fneirab 12:519a18fc3b28 6 * @version 1.5
fneirab 4:a4d6ae2182c2 7 *
fneirab 12:519a18fc3b28 8 * Started: 13DEC17
fneirab 4:a4d6ae2182c2 9 *
fneirab 12:519a18fc3b28 10 * Updated: 16APR18.
fneirab 12:519a18fc3b28 11 * Remove unnecessary code.
fneirab 12:519a18fc3b28 12 * Check spelling
fneirab 12:519a18fc3b28 13 * Error check
fneirab 12:519a18fc3b28 14 *
fneirab 4:a4d6ae2182c2 15 *
fneirab 9:f29d5e49b190 16 /*******************************************************************************
fneirab 9:f29d5e49b190 17 * Copyright (C) 2018 Maxim Integrated Products, Inc., All Rights Reserved.
fneirab 4:a4d6ae2182c2 18 *
fneirab 4:a4d6ae2182c2 19 * Permission is hereby granted, free of charge, to any person obtaining a
fneirab 4:a4d6ae2182c2 20 * copy of this software and associated documentation files (the "Software"),
fneirab 4:a4d6ae2182c2 21 * to deal in the Software without restriction, including without limitation
fneirab 4:a4d6ae2182c2 22 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
fneirab 4:a4d6ae2182c2 23 * and/or sell copies of the Software, and to permit persons to whom the
fneirab 4:a4d6ae2182c2 24 * Software is furnished to do so, subject to the following conditions:
fneirab 4:a4d6ae2182c2 25 *
fneirab 4:a4d6ae2182c2 26 * The above copyright notice and this permission notice shall be included
fneirab 4:a4d6ae2182c2 27 * in all copies or substantial portions of the Software.
fneirab 4:a4d6ae2182c2 28 *
fneirab 4:a4d6ae2182c2 29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
fneirab 4:a4d6ae2182c2 30 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
fneirab 4:a4d6ae2182c2 31 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
fneirab 4:a4d6ae2182c2 32 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
fneirab 4:a4d6ae2182c2 33 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
fneirab 4:a4d6ae2182c2 34 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
fneirab 4:a4d6ae2182c2 35 * OTHER DEALINGS IN THE SOFTWARE.
fneirab 4:a4d6ae2182c2 36 *
fneirab 4:a4d6ae2182c2 37 * Except as contained in this notice, the name of Maxim Integrated
fneirab 4:a4d6ae2182c2 38 * Products, Inc. shall not be used except as stated in the Maxim Integrated
fneirab 4:a4d6ae2182c2 39 * Products, Inc. Branding Policy.
fneirab 4:a4d6ae2182c2 40 *
fneirab 4:a4d6ae2182c2 41 * The mere transfer of this software does not imply any licenses
fneirab 4:a4d6ae2182c2 42 * of trade secrets, proprietary technology, copyrights, patents,
fneirab 4:a4d6ae2182c2 43 * trademarks, maskwork rights, or any other form of intellectual
fneirab 4:a4d6ae2182c2 44 * property whatsoever. Maxim Integrated Products, Inc. retains all
fneirab 4:a4d6ae2182c2 45 * ownership rights.
fneirab 9:f29d5e49b190 46 *******************************************************************************
fneirab 9:f29d5e49b190 47 */
fneirab 0:80c39eb8f3ba 48
fneirab 0:80c39eb8f3ba 49 #include "mbed.h"
fneirab 0:80c39eb8f3ba 50 #include "max17055.h"
fneirab 0:80c39eb8f3ba 51
fneirab 9:f29d5e49b190 52 /* POR Mask */
fneirab 9:f29d5e49b190 53 #define MAX17055_POR_MASK (0xFFFD)
fneirab 9:f29d5e49b190 54
fneirab 5:a18a189588dc 55 /* MODELCFG register bits */
fneirab 5:a18a189588dc 56 #define MAX17055_MODELCFG_REFRESH (1 << 15)
fneirab 5:a18a189588dc 57
fneirab 5:a18a189588dc 58
fneirab 5:a18a189588dc 59 /* FSTAT register bits */
fneirab 5:a18a189588dc 60 #define MAX17055_FSTAT_DNR (1)
fneirab 5:a18a189588dc 61
fneirab 11:bdbd3104995b 62 /* LIBRARY FUNCTION SUCCESS*/
fneirab 11:bdbd3104995b 63 #define F_SUCCESS_0 0
fneirab 11:bdbd3104995b 64
fneirab 11:bdbd3104995b 65 /* LIBRARY FUNCTION ERROR CODES */
fneirab 12:519a18fc3b28 66 #define F_ERROR_1 -1 //-1 if I2C read/write errors exist
fneirab 11:bdbd3104995b 67 #define F_ERROR_2 -2 //-2 if device is not present
fneirab 11:bdbd3104995b 68 #define F_ERROR_3 -3 //-3 if function error
fneirab 11:bdbd3104995b 69 #define F_ERROR_4 -4 //-4 if other error
fneirab 11:bdbd3104995b 70 #define F_ERROR_5 -5 //-5 if POR not detected
fneirab 4:a4d6ae2182c2 71
fneirab 0:80c39eb8f3ba 72
fneirab 5:a18a189588dc 73
fneirab 11:bdbd3104995b 74 /**
fneirab 11:bdbd3104995b 75 * @brief max17055 Constructor
fneirab 11:bdbd3104995b 76 * @details max17055 Constructor with battery and i2c as parameters
fneirab 11:bdbd3104995b 77 */
fneirab 5:a18a189588dc 78 MAX17055::MAX17055(I2C &i2c):
fneirab 5:a18a189588dc 79 m_i2cBus(i2c)
fneirab 0:80c39eb8f3ba 80 {
fneirab 5:a18a189588dc 81 //empty block
fneirab 0:80c39eb8f3ba 82 }
fneirab 0:80c39eb8f3ba 83
fneirab 11:bdbd3104995b 84 /**
fneirab 11:bdbd3104995b 85 * @brief Fuel Gauge Destructor
fneirab 11:bdbd3104995b 86 */
fneirab 0:80c39eb8f3ba 87 MAX17055::~MAX17055()
fneirab 0:80c39eb8f3ba 88 {
fneirab 0:80c39eb8f3ba 89 //empty block
fneirab 0:80c39eb8f3ba 90 }
fneirab 5:a18a189588dc 91
fneirab 0:80c39eb8f3ba 92 /**
fneirab 11:bdbd3104995b 93 * @brief Writes a register.
fneirab 11:bdbd3104995b 94 *
fneirab 11:bdbd3104995b 95 * @param[in] reg_addr The register address
fneirab 11:bdbd3104995b 96 * @param[in] reg_data The register data
fneirab 11:bdbd3104995b 97 *
fneirab 11:bdbd3104995b 98 * @retval 0 on success
fneirab 11:bdbd3104995b 99 * @retval non-0 for errors
fneirab 11:bdbd3104995b 100 */
fneirab 0:80c39eb8f3ba 101 int MAX17055::writeReg(Registers_e reg_addr, uint16_t reg_data)
fneirab 0:80c39eb8f3ba 102 {
fneirab 0:80c39eb8f3ba 103
fneirab 7:479a36909ced 104 uint16_t mask = 0x00FF;
fneirab 1:a031f0c6a71e 105 uint8_t dataLSB;
fneirab 1:a031f0c6a71e 106 uint8_t dataMSB;
fneirab 5:a18a189588dc 107
fneirab 7:479a36909ced 108 dataLSB = reg_data & mask;
fneirab 7:479a36909ced 109 dataMSB = (reg_data >> 8) & mask;
fneirab 5:a18a189588dc 110
fneirab 7:479a36909ced 111 char addr_plus_data[3] = {reg_addr, dataLSB, dataMSB};
fneirab 5:a18a189588dc 112
fneirab 11:bdbd3104995b 113 if ( m_i2cBus.write(I2C_W_ADRS, addr_plus_data, 3, false) == F_SUCCESS_0)
fneirab 11:bdbd3104995b 114 return F_SUCCESS_0;
fneirab 0:80c39eb8f3ba 115 else
fneirab 11:bdbd3104995b 116 return F_ERROR_1;
fneirab 11:bdbd3104995b 117 }
fneirab 0:80c39eb8f3ba 118
fneirab 0:80c39eb8f3ba 119 /**
fneirab 11:bdbd3104995b 120 * @brief Reads from MAX17055 register.
fneirab 11:bdbd3104995b 121 *
fneirab 11:bdbd3104995b 122 * @param[in] reg_addr The register address
fneirab 11:bdbd3104995b 123 * @param value The value
fneirab 11:bdbd3104995b 124 *
fneirab 11:bdbd3104995b 125 * @retval 0 on success
fneirab 11:bdbd3104995b 126 * @retval non-0 for errors
fneirab 11:bdbd3104995b 127 */
fneirab 1:a031f0c6a71e 128 int32_t MAX17055::readReg(Registers_e reg_addr, uint16_t &value)
fneirab 0:80c39eb8f3ba 129 {
fneirab 0:80c39eb8f3ba 130 int32_t result;
fneirab 7:479a36909ced 131 uint16_t mask = 0x00FF;
fneirab 1:a031f0c6a71e 132 char local_data[1];
fneirab 0:80c39eb8f3ba 133 local_data[0] = reg_addr;
fneirab 1:a031f0c6a71e 134 char read_data[2];
fneirab 5:a18a189588dc 135
fneirab 0:80c39eb8f3ba 136 result = m_i2cBus.write(I2C_W_ADRS, local_data, 1);
fneirab 11:bdbd3104995b 137 if(result == F_SUCCESS_0) {
fneirab 1:a031f0c6a71e 138 result = m_i2cBus.read(I2C_R_ADRS, read_data , 2, false);
fneirab 11:bdbd3104995b 139 if (result == F_SUCCESS_0) {
fneirab 7:479a36909ced 140 value = ( ((read_data[1] & mask) << 8) + (read_data[0]));
fneirab 11:bdbd3104995b 141 result = F_SUCCESS_0;
fneirab 5:a18a189588dc 142 }
fneirab 0:80c39eb8f3ba 143 }
fneirab 0:80c39eb8f3ba 144 return result;
fneirab 0:80c39eb8f3ba 145 }
fneirab 0:80c39eb8f3ba 146
fneirab 1:a031f0c6a71e 147 /**
fneirab 11:bdbd3104995b 148 * @brief Write and Verify a MAX17055 register
fneirab 11:bdbd3104995b 149 * @par Details
fneirab 12:519a18fc3b28 150 * This function writes and verifies if the writing process was successful
fneirab 11:bdbd3104995b 151 *
fneirab 11:bdbd3104995b 152 * @param[in] reg_addr - register address
fneirab 11:bdbd3104995b 153 * @param[out] reg_data - the variable that contains the data to write
fneirab 11:bdbd3104995b 154 * to the register address
fneirab 11:bdbd3104995b 155 *
fneirab 11:bdbd3104995b 156 * @retval 0 on success
fneirab 12:519a18fc3b28 157 * @retval non-0 for errors
fneirab 11:bdbd3104995b 158 */
fneirab 5:a18a189588dc 159 int MAX17055::write_and_verify_reg(Registers_e reg_addr, uint16_t reg_data)
fneirab 5:a18a189588dc 160 {
fneirab 1:a031f0c6a71e 161 int retries = 8;
fneirab 1:a031f0c6a71e 162 int ret;
fneirab 1:a031f0c6a71e 163 int statusRead;
fneirab 1:a031f0c6a71e 164 int statusWrite;
fneirab 1:a031f0c6a71e 165 uint16_t read_data;
fneirab 5:a18a189588dc 166
fneirab 1:a031f0c6a71e 167 do {
fneirab 1:a031f0c6a71e 168 statusWrite = writeReg(reg_addr, reg_data);
fneirab 11:bdbd3104995b 169 if (statusWrite != F_SUCCESS_0)
fneirab 12:519a18fc3b28 170 ret = -6;
fneirab 1:a031f0c6a71e 171 wait_ms(3);
fneirab 5:a18a189588dc 172 statusRead = readReg(reg_addr, read_data);
fneirab 11:bdbd3104995b 173 if (statusRead != F_SUCCESS_0)
fneirab 12:519a18fc3b28 174 ret = -7;
fneirab 5:a18a189588dc 175 if (read_data != reg_data) {
fneirab 12:519a18fc3b28 176 ret = -8;
fneirab 1:a031f0c6a71e 177 retries--;
fneirab 1:a031f0c6a71e 178 }
fneirab 5:a18a189588dc 179 } while (retries && read_data != reg_data);
fneirab 5:a18a189588dc 180
fneirab 11:bdbd3104995b 181 if (ret!=F_SUCCESS_0)
fneirab 1:a031f0c6a71e 182 return ret;
fneirab 7:479a36909ced 183 else
fneirab 11:bdbd3104995b 184 return F_SUCCESS_0;
fneirab 1:a031f0c6a71e 185 }
fneirab 2:ff7db397b70f 186
fneirab 2:ff7db397b70f 187 /**
fneirab 11:bdbd3104995b 188 * @brief Initialization Function for MAX17055.
fneirab 11:bdbd3104995b 189 * @par Details
fneirab 12:519a18fc3b28 190 * This function initializes the MAX17055 for the implementation of the EZconfig model.\n
fneirab 12:519a18fc3b28 191 * The library needs to be customized for the implementation of customize model.\n
fneirab 11:bdbd3104995b 192 *
fneirab 11:bdbd3104995b 193 * @retval 0 on success
fneirab 11:bdbd3104995b 194 * @retval non-0 for errors
fneirab 11:bdbd3104995b 195 */
fneirab 7:479a36909ced 196 int MAX17055::init(platform_data des_data)
fneirab 2:ff7db397b70f 197 {
fneirab 6:5ced10109ebf 198
fneirab 5:a18a189588dc 199 int status, ret;
fneirab 9:f29d5e49b190 200 int time_out = 10;
fneirab 12:519a18fc3b28 201 uint16_t hibcfg_value,read_data;
fneirab 5:a18a189588dc 202
fneirab 5:a18a189588dc 203
fneirab 7:479a36909ced 204 status = readReg(VERSION_REG, read_data);
fneirab 11:bdbd3104995b 205 if (status != F_SUCCESS_0)
fneirab 11:bdbd3104995b 206 return status;
fneirab 5:a18a189588dc 207
fneirab 12:519a18fc3b28 208 ///STEP 0. Check for POR (Skip load model if POR bit is cleared)
fneirab 5:a18a189588dc 209
fneirab 11:bdbd3104995b 210 if (check_POR_func() == F_ERROR_5)
fneirab 11:bdbd3104995b 211 return F_ERROR_5; //POR not detected. Skip Initialization.
fneirab 12:519a18fc3b28 212 //This is not an error.
fneirab 2:ff7db397b70f 213
fneirab 12:519a18fc3b28 214 ///STEP 1. Check if FStat.DNR == 0 (Do not continue until FSTAT.DNR == 0)
fneirab 7:479a36909ced 215 ret = poll_flag_clear(FSTAT_REG, MAX17055_FSTAT_DNR, time_out);
fneirab 11:bdbd3104995b 216 if (ret < F_SUCCESS_0) {
fneirab 6:5ced10109ebf 217 return ret;
fneirab 3:f77a8345b0e3 218 }
fneirab 5:a18a189588dc 219
fneirab 12:519a18fc3b28 220 ///STEP 1.2. Force exit from hibernate
fneirab 12:519a18fc3b28 221 hibcfg_value = forcedExitHiberMode();
fneirab 7:479a36909ced 222
fneirab 9:f29d5e49b190 223 //printf("step 1 check \r\n");
fneirab 5:a18a189588dc 224
fneirab 12:519a18fc3b28 225 ///STEP 2. Initialize configuration
fneirab 5:a18a189588dc 226 switch (1) {
fneirab 5:a18a189588dc 227 case MODEL_LOADING_OPTION1:
fneirab 12:519a18fc3b28 228 ///STEP 2.1. Load EZ Config
fneirab 7:479a36909ced 229 EZconfig_init(des_data);
fneirab 5:a18a189588dc 230
fneirab 12:519a18fc3b28 231 ///STEP 2.2. Poll ModelCFG.ModelRefresh bit for clear
fneirab 7:479a36909ced 232 ret = poll_flag_clear(MODELCFG_REG, MAX17055_MODELCFG_REFRESH, time_out);
fneirab 11:bdbd3104995b 233 if(ret < F_SUCCESS_0) {
fneirab 5:a18a189588dc 234 return ret;
fneirab 5:a18a189588dc 235 }
fneirab 7:479a36909ced 236
fneirab 9:f29d5e49b190 237 break;
fneirab 5:a18a189588dc 238 }
fneirab 12:519a18fc3b28 239 ///STEP3. Restore original HibCfg
fneirab 7:479a36909ced 240 writeReg(HIBCFG_REG, hibcfg_value);
fneirab 5:a18a189588dc 241
fneirab 5:a18a189588dc 242 /* Clear Status.POR */
fneirab 9:f29d5e49b190 243 ret = clear_POR_bit();
fneirab 11:bdbd3104995b 244 if (ret < F_SUCCESS_0)
fneirab 11:bdbd3104995b 245 return ret; //See errors
fneirab 11:bdbd3104995b 246 return F_SUCCESS_0;
fneirab 5:a18a189588dc 247 }
fneirab 5:a18a189588dc 248
fneirab 9:f29d5e49b190 249 /**
fneirab 11:bdbd3104995b 250 * @brief Check POR function
fneirab 11:bdbd3104995b 251 * @par Details
fneirab 11:bdbd3104995b 252 * This function check is there was a power on reset event for the
fneirab 11:bdbd3104995b 253 * MAX17055
fneirab 11:bdbd3104995b 254 *
fneirab 11:bdbd3104995b 255 * @retval 0 on success (POR detected)
fneirab 11:bdbd3104995b 256 * @retval non-0 for errors (POR not detected)
fneirab 11:bdbd3104995b 257 *
fneirab 11:bdbd3104995b 258 */
fneirab 9:f29d5e49b190 259 int MAX17055::check_POR_func()
fneirab 9:f29d5e49b190 260 {
fneirab 9:f29d5e49b190 261 uint16_t read_data;
fneirab 9:f29d5e49b190 262
fneirab 9:f29d5e49b190 263 readReg(STATUS_REG, read_data);
fneirab 9:f29d5e49b190 264
fneirab 9:f29d5e49b190 265 if (!(read_data & MAX17055_STATUS_POR ) )
fneirab 11:bdbd3104995b 266 return F_ERROR_5; //POR not detected.
fneirab 9:f29d5e49b190 267 else
fneirab 11:bdbd3104995b 268 return F_SUCCESS_0;
fneirab 9:f29d5e49b190 269 }
fneirab 9:f29d5e49b190 270
fneirab 9:f29d5e49b190 271 /**
fneirab 11:bdbd3104995b 272 * @brief clear POR bit function
fneirab 11:bdbd3104995b 273 * @par Details
fneirab 12:519a18fc3b28 274 * This function clear the indicating bit for POR - MAX17055
fneirab 11:bdbd3104995b 275 *
fneirab 11:bdbd3104995b 276 * @retval 0 for Success
fneirab 11:bdbd3104995b 277 * @retval non-0 for errors
fneirab 11:bdbd3104995b 278 */
fneirab 9:f29d5e49b190 279 int MAX17055::clear_POR_bit()
fneirab 9:f29d5e49b190 280 {
fneirab 9:f29d5e49b190 281 int status, ret;
fneirab 9:f29d5e49b190 282 uint16_t read_data, hibcfg_value, reg;
fneirab 9:f29d5e49b190 283
fneirab 9:f29d5e49b190 284
fneirab 9:f29d5e49b190 285 status = readReg(STATUS_REG, read_data);
fneirab 11:bdbd3104995b 286 if (status != F_SUCCESS_0)
fneirab 11:bdbd3104995b 287 return F_ERROR_2; //Device is not present in the i2c Bus
fneirab 9:f29d5e49b190 288 status = write_and_verify_reg(STATUS_REG, (read_data & MAX17055_POR_MASK));
fneirab 11:bdbd3104995b 289 if (status != F_SUCCESS_0)
fneirab 11:bdbd3104995b 290 return F_ERROR_1; //read or write error
fneirab 9:f29d5e49b190 291 else
fneirab 11:bdbd3104995b 292 return F_SUCCESS_0;
fneirab 9:f29d5e49b190 293 }
fneirab 5:a18a189588dc 294
fneirab 5:a18a189588dc 295 /**
fneirab 11:bdbd3104995b 296 * @brief Poll Flag clear Function.
fneirab 11:bdbd3104995b 297 * @par Details
fneirab 11:bdbd3104995b 298 * This function clears status flags for the MAX17055
fneirab 11:bdbd3104995b 299 *
fneirab 11:bdbd3104995b 300 * @param[in] reg_addr - register address
fneirab 11:bdbd3104995b 301 * @param[in] mask - register address
fneirab 11:bdbd3104995b 302 * @param[in] timeout - register data
fneirab 11:bdbd3104995b 303 *
fneirab 11:bdbd3104995b 304 * @retval 0 on success
fneirab 11:bdbd3104995b 305 * @retval non-0 negative for errors
fneirab 11:bdbd3104995b 306 */
fneirab 7:479a36909ced 307 int MAX17055::poll_flag_clear (Registers_e reg_addr, int mask, int timeout)
fneirab 5:a18a189588dc 308 {
fneirab 5:a18a189588dc 309 uint16_t data;
fneirab 5:a18a189588dc 310 int ret;
fneirab 5:a18a189588dc 311
fneirab 5:a18a189588dc 312 do {
fneirab 9:f29d5e49b190 313 wait_ms(1);
fneirab 6:5ced10109ebf 314 ret = readReg(reg_addr, data);
fneirab 11:bdbd3104995b 315 if(ret < F_SUCCESS_0)
fneirab 11:bdbd3104995b 316 return F_ERROR_1;
fneirab 5:a18a189588dc 317
fneirab 5:a18a189588dc 318 if(!(data & mask))
fneirab 11:bdbd3104995b 319 return F_SUCCESS_0;
fneirab 5:a18a189588dc 320
fneirab 9:f29d5e49b190 321 timeout -= 1;
fneirab 5:a18a189588dc 322 } while(timeout > 0);
fneirab 5:a18a189588dc 323
fneirab 11:bdbd3104995b 324 return F_ERROR_4;
fneirab 5:a18a189588dc 325 }
fneirab 5:a18a189588dc 326
fneirab 2:ff7db397b70f 327 /**
fneirab 11:bdbd3104995b 328 * @brief Get Temperature Function from the MAX17055 TEMP register.
fneirab 11:bdbd3104995b 329 * @par Details
fneirab 11:bdbd3104995b 330 * This function sends a request to access the TEMP register
fneirab 11:bdbd3104995b 331 * of the MAX17055, which reflects the temperature measured for the fuel gauge.
fneirab 11:bdbd3104995b 332 * The temperature values will reflect the Config Register (0x1D) selections for Tsel bit (D15).
fneirab 11:bdbd3104995b 333 * For this library the setting are for die temperature.
fneirab 11:bdbd3104995b 334 * The MAX32620FTHR thermistor bias pin is not connected. The biasing of the thermistor is
fneirab 11:bdbd3104995b 335 * done by the MAX77650. See MAX77650 library for how to enable the thermistor biasing.
fneirab 11:bdbd3104995b 336 *
fneirab 11:bdbd3104995b 337 *
fneirab 11:bdbd3104995b 338 * @retval temp - Temperature value from TEMP register in &deg;C
fneirab 11:bdbd3104995b 339 * @retval non-0 negative values check for errors
fneirab 11:bdbd3104995b 340 */
fneirab 7:479a36909ced 341 int MAX17055::get_temperature()
fneirab 2:ff7db397b70f 342 {
fneirab 5:a18a189588dc 343
fneirab 2:ff7db397b70f 344 int ret;
fneirab 11:bdbd3104995b 345 uint16_t temp;
fneirab 5:a18a189588dc 346
fneirab 11:bdbd3104995b 347 ret = readReg(TEMP_REG, temp);
fneirab 11:bdbd3104995b 348 if (ret < F_SUCCESS_0)
fneirab 2:ff7db397b70f 349 return ret;
fneirab 2:ff7db397b70f 350
fneirab 2:ff7db397b70f 351 /* The value is signed. */
fneirab 11:bdbd3104995b 352 if (temp & 0x8000)
fneirab 11:bdbd3104995b 353 temp |= 0xFFFF0000;
fneirab 2:ff7db397b70f 354
fneirab 2:ff7db397b70f 355 /* The value is converted into centigrade scale */
fneirab 2:ff7db397b70f 356 /* Units of LSB = 1 / 256 degree Celsius */
fneirab 11:bdbd3104995b 357 temp >>= 8;
fneirab 2:ff7db397b70f 358
fneirab 11:bdbd3104995b 359 return temp;
fneirab 5:a18a189588dc 360 }
fneirab 3:f77a8345b0e3 361
fneirab 3:f77a8345b0e3 362 /**
fneirab 11:bdbd3104995b 363 * @brief Forced Exit Hibernate Mode Function for MAX17055
fneirab 11:bdbd3104995b 364 * @par Details
fneirab 11:bdbd3104995b 365 * This function executes a force exit from hibernate mode.
fneirab 11:bdbd3104995b 366 *
fneirab 12:519a18fc3b28 367 * @retval HibCFG original value before forced Exit Hibernate mode *
fneirab 11:bdbd3104995b 368 */
fneirab 12:519a18fc3b28 369 uint16_t MAX17055::forcedExitHiberMode()
fneirab 3:f77a8345b0e3 370 {
fneirab 3:f77a8345b0e3 371 uint16_t hibcfg;
fneirab 5:a18a189588dc 372
fneirab 5:a18a189588dc 373 /* Force exit from hibernate */
fneirab 3:f77a8345b0e3 374 //STEP 0: Store original HibCFG value
fneirab 7:479a36909ced 375 readReg(HIBCFG_REG, hibcfg);
fneirab 3:f77a8345b0e3 376
fneirab 12:519a18fc3b28 377 //STEP 1: Write to Soft-Wakeup Command Register
fneirab 12:519a18fc3b28 378 writeReg(VFSOC0_QH0_LOCK_REG, 0x90); //Soft-Wakeup from hibernate
fneirab 5:a18a189588dc 379
fneirab 3:f77a8345b0e3 380 //STEP 2: Write to Hibernate Configuration register
fneirab 7:479a36909ced 381 writeReg(HIBCFG_REG, 0x0); //disable hibernate mode
fneirab 5:a18a189588dc 382
fneirab 12:519a18fc3b28 383 //STEP 3:Write to Soft-Wakeup Command Register
fneirab 12:519a18fc3b28 384 writeReg(VFSOC0_QH0_LOCK_REG, 0x0); //Clear All commands
fneirab 5:a18a189588dc 385
fneirab 3:f77a8345b0e3 386 return hibcfg;
fneirab 5:a18a189588dc 387 }
fneirab 7:479a36909ced 388
fneirab 7:479a36909ced 389 /**
fneirab 12:519a18fc3b28 390 * @brief EZ Config Initialization function
fneirab 11:bdbd3104995b 391 * @par Details
fneirab 12:519a18fc3b28 392 * This function implements the steps for the EZ config m5 FuelGauge
fneirab 12:519a18fc3b28 393 * @param[in] des_data - Plataform_data struct with information about the design.
fneirab 11:bdbd3104995b 394 * @retval 0 on success
fneirab 11:bdbd3104995b 395 * @retval non-zero for errors
fneirab 11:bdbd3104995b 396 */
fneirab 7:479a36909ced 397 uint16_t MAX17055::EZconfig_init(platform_data des_data)
fneirab 7:479a36909ced 398 {
fneirab 12:519a18fc3b28 399 ///STEP 2.1.1 EZ config values suggested by manufacturer.
fneirab 7:479a36909ced 400 const int charger_th = 4275;
fneirab 12:519a18fc3b28 401 const int chg_V_high = 51200; // scaling factor high voltage charger
fneirab 7:479a36909ced 402 const int chg_V_low = 44138;
fneirab 12:519a18fc3b28 403 const int param_EZ_FG1 = 0x8400; // Sets config bit for the charge voltage for the m5
fneirab 7:479a36909ced 404 const int param_EZ_FG2 = 0x8000;
fneirab 8:ca8765c30ed2 405 uint16_t dpacc, ret;
fneirab 7:479a36909ced 406
fneirab 12:519a18fc3b28 407 ///STEP 2.1.2 Store the EZ Config values into the appropriate registers.
fneirab 9:f29d5e49b190 408 ret = writeReg(DESIGNCAP_REG, des_data.designcap);
fneirab 12:519a18fc3b28 409 ret = writeReg(DQACC_REG, des_data.designcap >> 5); //DesignCap divide by 32
fneirab 9:f29d5e49b190 410 ret = writeReg(ICHGTERM_REG, des_data.ichgterm);
fneirab 9:f29d5e49b190 411 ret = writeReg(VEMPTY_REG, des_data.vempty);
fneirab 7:479a36909ced 412
fneirab 7:479a36909ced 413 if (des_data.vcharge > charger_th) {
fneirab 7:479a36909ced 414 dpacc = (des_data.designcap >> 5) * chg_V_high / des_data.designcap;
fneirab 9:f29d5e49b190 415 ret = writeReg(DPACC_REG, dpacc);
fneirab 12:519a18fc3b28 416 ret = writeReg(MODELCFG_REG, param_EZ_FG1); //
fneirab 7:479a36909ced 417 } else {
fneirab 7:479a36909ced 418 dpacc = (des_data.designcap >> 5) * chg_V_low / des_data.designcap;
fneirab 9:f29d5e49b190 419 ret = writeReg(DPACC_REG, dpacc);
fneirab 9:f29d5e49b190 420 ret = writeReg(MODELCFG_REG, param_EZ_FG2);
fneirab 7:479a36909ced 421 }
fneirab 7:479a36909ced 422 return ret;
fneirab 7:479a36909ced 423 }
fneirab 7:479a36909ced 424
fneirab 7:479a36909ced 425 /**
fneirab 11:bdbd3104995b 426 * @brief Get reported State Of Charge(SOC) Function from MAX17055 Fuel Gauge.
fneirab 11:bdbd3104995b 427 * @par Details
fneirab 11:bdbd3104995b 428 * This function sends a request to access the RepSOC register
fneirab 11:bdbd3104995b 429 * of the MAX17055. RepSOC is the reported state-of-charge percentage output of the fuel gauge.
fneirab 11:bdbd3104995b 430 *
fneirab 11:bdbd3104995b 431 * @retval soc_data - Reported SOC data from the RepSOC register in % value.
fneirab 11:bdbd3104995b 432 * @retval non-0 negative values check for errors
fneirab 11:bdbd3104995b 433 */
fneirab 7:479a36909ced 434 int MAX17055::get_SOC()
fneirab 7:479a36909ced 435 {
fneirab 7:479a36909ced 436
fneirab 7:479a36909ced 437 int ret;
fneirab 11:bdbd3104995b 438 uint16_t soc_data;
fneirab 7:479a36909ced 439
fneirab 11:bdbd3104995b 440 ret = readReg(REPSOC_REG, soc_data);
fneirab 11:bdbd3104995b 441 if (ret < F_SUCCESS_0)
fneirab 7:479a36909ced 442 return ret;
fneirab 7:479a36909ced 443
fneirab 11:bdbd3104995b 444 soc_data = soc_data >> 8; /* RepSOC LSB: 1/256 % */
fneirab 7:479a36909ced 445
fneirab 11:bdbd3104995b 446 return soc_data;
fneirab 7:479a36909ced 447 }
fneirab 7:479a36909ced 448
fneirab 8:ca8765c30ed2 449 /**
fneirab 12:519a18fc3b28 450 * @brief Get at rate Average State Of Charge(SOC) Function from MAX17055 Fuel Gauge.
fneirab 11:bdbd3104995b 451 * @par Details
fneirab 12:519a18fc3b28 452 * This function sends a request to access the atAvSOC register of the MAX17055.
fneirab 11:bdbd3104995b 453 * The AvSOC registers hold the calculated available capacity and percentage of the
fneirab 11:bdbd3104995b 454 * battery based on all inputs from the ModelGauge m5 algorithm including empty
fneirab 11:bdbd3104995b 455 * compensation. These registers provide unfiltered results. Jumps in the reported
fneirab 11:bdbd3104995b 456 * values can be caused by abrupt changes in load current or temperature.
fneirab 11:bdbd3104995b 457 *
fneirab 12:519a18fc3b28 458 * @retval atAvSOC_data - Average SOC data from the atAVSOC register in % value.
fneirab 11:bdbd3104995b 459 * @retval non-0 negative values check for errors
fneirab 11:bdbd3104995b 460 */
fneirab 12:519a18fc3b28 461 int MAX17055::get_atAvSOC()
fneirab 8:ca8765c30ed2 462 {
fneirab 8:ca8765c30ed2 463 int ret;
fneirab 12:519a18fc3b28 464 uint16_t atAvSOC_data;
fneirab 8:ca8765c30ed2 465
fneirab 12:519a18fc3b28 466 ret = readReg(AVSOC_REG, atAvSOC_data);
fneirab 11:bdbd3104995b 467 if (ret < F_SUCCESS_0)
fneirab 11:bdbd3104995b 468 return ret; //Check errors if data is not correct
fneirab 8:ca8765c30ed2 469
fneirab 12:519a18fc3b28 470 atAvSOC_data = atAvSOC_data >> 8; /* avSOC LSB: 1/256 % */
fneirab 11:bdbd3104995b 471
fneirab 12:519a18fc3b28 472 return atAvSOC_data;
fneirab 8:ca8765c30ed2 473 }
fneirab 7:479a36909ced 474
fneirab 9:f29d5e49b190 475 /**
fneirab 11:bdbd3104995b 476 * @brief Get mix State Of Charge(SOC) Function for MAX17055 Fuel Gauge.
fneirab 11:bdbd3104995b 477 * @par Details
fneirab 11:bdbd3104995b 478 * This function sends a request to access mixSOC register
fneirab 11:bdbd3104995b 479 * of the MAX17055. The MixSOC registers holds the calculated
fneirab 11:bdbd3104995b 480 * remaining capacity and percentage of the cell before any empty compensation
fneirab 11:bdbd3104995b 481 * adjustments are performed.
fneirab 11:bdbd3104995b 482 *
fneirab 11:bdbd3104995b 483 * @retval mixSOC_data - Mixed SOC register values from the mixSOC register in % value.
fneirab 11:bdbd3104995b 484 * @retval non-0 for errors
fneirab 11:bdbd3104995b 485 */
fneirab 11:bdbd3104995b 486 int MAX17055::get_mixSOC()
fneirab 11:bdbd3104995b 487 {
fneirab 11:bdbd3104995b 488 int ret;
fneirab 11:bdbd3104995b 489 uint16_t mixSOC_data;
fneirab 11:bdbd3104995b 490
fneirab 11:bdbd3104995b 491 ret = readReg(MIXSOC_REG, mixSOC_data);
fneirab 11:bdbd3104995b 492 if (ret < F_SUCCESS_0)
fneirab 11:bdbd3104995b 493 return ret;
fneirab 9:f29d5e49b190 494
fneirab 11:bdbd3104995b 495 mixSOC_data = mixSOC_data >> 8; /* RepSOC LSB: 1/256 % */
fneirab 9:f29d5e49b190 496
fneirab 11:bdbd3104995b 497 return mixSOC_data;
fneirab 11:bdbd3104995b 498 }
fneirab 11:bdbd3104995b 499
fneirab 11:bdbd3104995b 500 /**
fneirab 11:bdbd3104995b 501 * @brief Get the Time to Empty(TTE) Function form MAX17055 Fuel Gauge.
fneirab 11:bdbd3104995b 502 * @par Details
fneirab 11:bdbd3104995b 503 * This function sends a request to access the TTE register
fneirab 11:bdbd3104995b 504 * of the MAX17055
fneirab 11:bdbd3104995b 505 * The TTE register holds the estimated time to empty for the
fneirab 11:bdbd3104995b 506 * application under present temperature and load conditions. The TTE value is
fneirab 11:bdbd3104995b 507 * determined by relating AvCap with AvgCurrent. The corresponding AvgCurrent
fneirab 11:bdbd3104995b 508 * filtering gives a delay in TTE, but provides more stable results.
fneirab 11:bdbd3104995b 509 *
fneirab 11:bdbd3104995b 510 * @retval tte_data - Time to Empty data from the TTE register in seconds.
fneirab 11:bdbd3104995b 511 * @retval non-0 negative values check for errors
fneirab 11:bdbd3104995b 512 */
fneirab 12:519a18fc3b28 513 float MAX17055::get_TTE()
fneirab 9:f29d5e49b190 514 {
fneirab 9:f29d5e49b190 515
fneirab 9:f29d5e49b190 516 int ret;
fneirab 11:bdbd3104995b 517 uint16_t tte_data;
fneirab 12:519a18fc3b28 518 float f_tte_data;
fneirab 9:f29d5e49b190 519
fneirab 11:bdbd3104995b 520 ret = readReg(TTE_REG, tte_data);
fneirab 11:bdbd3104995b 521 if (ret < F_SUCCESS_0)
fneirab 11:bdbd3104995b 522 return ret;
fneirab 11:bdbd3104995b 523 else
fneirab 12:519a18fc3b28 524 f_tte_data = ((float)tte_data * 5.625); /* TTE LSB: 5.625 sec */
fneirab 9:f29d5e49b190 525
fneirab 12:519a18fc3b28 526 return f_tte_data;
fneirab 9:f29d5e49b190 527 }
fneirab 7:479a36909ced 528
fneirab 7:479a36909ced 529 /**
fneirab 11:bdbd3104995b 530 * @brief Get the at Time to Empty(atTTE) value Function for MAX17055 Fuel Gauge.
fneirab 11:bdbd3104995b 531 * @par Details
fneirab 11:bdbd3104995b 532 * This function sends a request to access the internal register
fneirab 11:bdbd3104995b 533 * of the MAX17055
fneirab 11:bdbd3104995b 534 *
fneirab 11:bdbd3104995b 535 * @retval atTTE_data - Time to Empty data from the atTTE register in seconds.
fneirab 11:bdbd3104995b 536 * @retval non-0 negative values check for errors
fneirab 11:bdbd3104995b 537 */
fneirab 12:519a18fc3b28 538 float MAX17055::get_atTTE()
fneirab 7:479a36909ced 539 {
fneirab 7:479a36909ced 540
fneirab 7:479a36909ced 541 int ret;
fneirab 11:bdbd3104995b 542 uint16_t atTTE_data;
fneirab 12:519a18fc3b28 543 float f_atTTE_data;
fneirab 7:479a36909ced 544
fneirab 11:bdbd3104995b 545 ret = readReg(ATTTE_REG, atTTE_data);
fneirab 11:bdbd3104995b 546 if (ret < F_SUCCESS_0)
fneirab 11:bdbd3104995b 547 return ret; //Check for errors
fneirab 7:479a36909ced 548 else
fneirab 12:519a18fc3b28 549 f_atTTE_data = ((float)atTTE_data * 5.625); /* TTE LSB: 5.625 sec */
fneirab 7:479a36909ced 550
fneirab 12:519a18fc3b28 551 return f_atTTE_data;
fneirab 7:479a36909ced 552 }
fneirab 7:479a36909ced 553
fneirab 8:ca8765c30ed2 554 /**
fneirab 11:bdbd3104995b 555 * @brief Get the Time to Full(TTE) values Function for MAX17055 Fuel Gauge.
fneirab 11:bdbd3104995b 556 * @par Details
fneirab 11:bdbd3104995b 557 * This function sends a request to access the internal register of the MAX17055
fneirab 11:bdbd3104995b 558 * The TTF register holds the estimated time to full for the application
fneirab 11:bdbd3104995b 559 * under present conditions. The TTF value is determined by learning the
fneirab 11:bdbd3104995b 560 * constant current and constant voltage portions of the charge cycle based
fneirab 11:bdbd3104995b 561 * on experience of prior charge cycles. Time to full is then estimate
fneirab 11:bdbd3104995b 562 * by comparing present charge current to the charge termination current.
fneirab 11:bdbd3104995b 563 * Operation of the TTF register assumes all charge profiles are consistent in the application.
fneirab 11:bdbd3104995b 564 *
fneirab 11:bdbd3104995b 565 * @retval ttf_data - Time to Full data from the TTF register in seconds.
fneirab 11:bdbd3104995b 566 * @retval non-0 negative values check for errors
fneirab 11:bdbd3104995b 567 */
fneirab 12:519a18fc3b28 568 float MAX17055::get_TTF()
fneirab 8:ca8765c30ed2 569 {
fneirab 8:ca8765c30ed2 570
fneirab 8:ca8765c30ed2 571 int ret;
fneirab 8:ca8765c30ed2 572 uint16_t ttf_data;
fneirab 12:519a18fc3b28 573 float f_ttf_data;
fneirab 8:ca8765c30ed2 574
fneirab 8:ca8765c30ed2 575 ret = readReg(TTF_REG, ttf_data);
fneirab 11:bdbd3104995b 576 if (ret < F_SUCCESS_0)
fneirab 8:ca8765c30ed2 577 return ret;
fneirab 8:ca8765c30ed2 578 else
fneirab 12:519a18fc3b28 579 f_ttf_data = ((float)ttf_data * 5.625); /* TTE LSB: 5.625 sec */
fneirab 8:ca8765c30ed2 580
fneirab 12:519a18fc3b28 581 return f_ttf_data;
fneirab 8:ca8765c30ed2 582 }
fneirab 7:479a36909ced 583
fneirab 7:479a36909ced 584 /**
fneirab 11:bdbd3104995b 585 * @brief Get voltage of the cell Function for MAX17055 Fuel Gauge.
fneirab 11:bdbd3104995b 586 * @par Details
fneirab 11:bdbd3104995b 587 * This function sends a request to access the VCell Register
fneirab 11:bdbd3104995b 588 * of the MAX17055 to read the measured voltage from the cell.
fneirab 11:bdbd3104995b 589 *
fneirab 11:bdbd3104995b 590 * @retval vcell_data - vcell data from the VCELL_REG register in uVolts.
fneirab 11:bdbd3104995b 591 * @retval non-0 negative values check for errors
fneirab 11:bdbd3104995b 592 */
fneirab 7:479a36909ced 593 int MAX17055::get_Vcell()
fneirab 7:479a36909ced 594 {
fneirab 7:479a36909ced 595
fneirab 7:479a36909ced 596 int ret;
fneirab 7:479a36909ced 597 uint16_t vcell_data;
fneirab 7:479a36909ced 598
fneirab 7:479a36909ced 599 ret = readReg(VCELL_REG, vcell_data);
fneirab 11:bdbd3104995b 600 if (ret < F_SUCCESS_0)
fneirab 7:479a36909ced 601 return ret;
fneirab 7:479a36909ced 602 else
fneirab 7:479a36909ced 603 ret = lsb_to_uvolts(vcell_data);
fneirab 7:479a36909ced 604 return ret;
fneirab 7:479a36909ced 605 }
fneirab 7:479a36909ced 606
fneirab 7:479a36909ced 607 /**
fneirab 11:bdbd3104995b 608 * @brief Get current Function for MAX17055 Fuel Gauge.
fneirab 11:bdbd3104995b 609 * @par Details
fneirab 11:bdbd3104995b 610 * This function sends a request to access the CURRENT register
fneirab 12:519a18fc3b28 611 * of the MAX17055 to read the current readings.
fneirab 11:bdbd3104995b 612 *
fneirab 12:519a18fc3b28 613 * @param[in] des_data - Plataform_data struct with information about the design.
fneirab 11:bdbd3104995b 614 *
fneirab 11:bdbd3104995b 615 * @retval curr_data - current data from the CURRENT register in uAmps.
fneirab 11:bdbd3104995b 616 * @retval non-0 negative values check for errors.
fneirab 11:bdbd3104995b 617 */
fneirab 7:479a36909ced 618 int MAX17055::get_Current( platform_data des_data )
fneirab 7:479a36909ced 619 {
fneirab 7:479a36909ced 620
fneirab 7:479a36909ced 621 int ret,design_rsense;
fneirab 11:bdbd3104995b 622 uint16_t curr_data;
fneirab 7:479a36909ced 623
fneirab 11:bdbd3104995b 624 ret = readReg(CURRENT_REG, curr_data);
fneirab 11:bdbd3104995b 625 if (ret < F_SUCCESS_0)
fneirab 7:479a36909ced 626 return ret;
fneirab 7:479a36909ced 627 else
fneirab 7:479a36909ced 628 design_rsense = des_data.rsense;
fneirab 11:bdbd3104995b 629 ret = raw_current_to_uamps((uint32_t)curr_data, design_rsense);
fneirab 7:479a36909ced 630 return ret;
fneirab 7:479a36909ced 631 }
fneirab 7:479a36909ced 632
fneirab 7:479a36909ced 633 /**
fneirab 11:bdbd3104995b 634 * @brief Get average current Function for MAX17055 Fuel Gauge.
fneirab 11:bdbd3104995b 635 * @par Details
fneirab 11:bdbd3104995b 636 * This function sends a request to access the aveCURRENT register
fneirab 12:519a18fc3b28 637 * of the MAX17055 to read the average current readings.
fneirab 11:bdbd3104995b 638 *
fneirab 12:519a18fc3b28 639 * @param[in] des_data - Plataform_data struct with information about the design.
fneirab 11:bdbd3104995b 640 *
fneirab 11:bdbd3104995b 641 * @retval aveCurr_data - current data from the AVGCURRENT register in uAmps.
fneirab 11:bdbd3104995b 642 * @retval non-0 negative values check for errors.
fneirab 11:bdbd3104995b 643 */
fneirab 7:479a36909ced 644 int MAX17055::get_AvgCurrent( platform_data des_data )
fneirab 7:479a36909ced 645 {
fneirab 7:479a36909ced 646 int ret, design_rsense;
fneirab 7:479a36909ced 647 uint16_t data;
fneirab 7:479a36909ced 648 uint32_t aveCurr_data;
fneirab 7:479a36909ced 649
fneirab 7:479a36909ced 650 ret = readReg(AVGCURRENT_REG, data);
fneirab 11:bdbd3104995b 651 if (ret < F_SUCCESS_0)
fneirab 7:479a36909ced 652 return ret;
fneirab 7:479a36909ced 653 else
fneirab 7:479a36909ced 654 aveCurr_data = data;
fneirab 7:479a36909ced 655 design_rsense = des_data.rsense;
fneirab 7:479a36909ced 656 aveCurr_data = raw_current_to_uamps(aveCurr_data, design_rsense);
fneirab 7:479a36909ced 657 return aveCurr_data;
fneirab 7:479a36909ced 658 }
fneirab 7:479a36909ced 659
fneirab 7:479a36909ced 660 /**
fneirab 12:519a18fc3b28 661 * @brief lsb_to_uvolts Conversion Function
fneirab 11:bdbd3104995b 662 * @par Details
fneirab 11:bdbd3104995b 663 * This function takes the lsb value of the register and convert it
fneirab 11:bdbd3104995b 664 * to uvolts
fneirab 11:bdbd3104995b 665 *
fneirab 11:bdbd3104995b 666 * @param[in] lsb - value of register lsb
fneirab 11:bdbd3104995b 667 * @retval conv_2_uvolts - value converted lsb to uvolts
fneirab 11:bdbd3104995b 668 */
fneirab 7:479a36909ced 669 int MAX17055:: lsb_to_uvolts(uint16_t lsb)
fneirab 7:479a36909ced 670 {
fneirab 11:bdbd3104995b 671 int conv_2_uvolts;
fneirab 11:bdbd3104995b 672 conv_2_uvolts = (lsb * 625) / 8; /* 78.125uV per bit */
fneirab 11:bdbd3104995b 673 return conv_2_uvolts;
fneirab 7:479a36909ced 674 }
fneirab 7:479a36909ced 675
fneirab 7:479a36909ced 676 /**
fneirab 12:519a18fc3b28 677 * @brief raw_current_to_uamp Conversion Function
fneirab 11:bdbd3104995b 678 * @par Details
fneirab 11:bdbd3104995b 679 * This function takes the raw current value of the register and
fneirab 11:bdbd3104995b 680 * converts it to uamps
fneirab 11:bdbd3104995b 681 *
fneirab 11:bdbd3104995b 682 * @param[in] curr - raw current value of register
fneirab 11:bdbd3104995b 683 * @retval res - converted raw current to uamps (Signed 2's complement)
fneirab 11:bdbd3104995b 684 */
fneirab 7:479a36909ced 685 int MAX17055::raw_current_to_uamps(uint32_t curr, int rsense_value)
fneirab 7:479a36909ced 686 {
fneirab 7:479a36909ced 687 int res = curr;
fneirab 7:479a36909ced 688 /* Negative */
fneirab 7:479a36909ced 689 if (res & 0x8000) {
fneirab 7:479a36909ced 690 res |= 0xFFFF0000;
fneirab 7:479a36909ced 691 } else {
fneirab 12:519a18fc3b28 692 res *= 1562500 /(rsense_value * 1000); //Change to interact with the rsense implemented in the design
fneirab 7:479a36909ced 693 }
fneirab 7:479a36909ced 694 return res;
fneirab 9:f29d5e49b190 695 }
fneirab 9:f29d5e49b190 696
fneirab 9:f29d5e49b190 697 /**
fneirab 11:bdbd3104995b 698 * @brief Save Learned Parameters Function for battery Fuel Gauge model.
fneirab 11:bdbd3104995b 699 * @par Details
fneirab 11:bdbd3104995b 700 * It is recommended to save the learned capacity parameters every
fneirab 11:bdbd3104995b 701 * time bit 2 of the Cycles register toggles
fneirab 11:bdbd3104995b 702 * (so that it is saved every 64% change in the battery)
fneirab 11:bdbd3104995b 703 * so that if power is lost the values can easily be restored.
fneirab 11:bdbd3104995b 704 *
fneirab 11:bdbd3104995b 705 * @param[in] FG_params Fuel Gauge Parameters based on design details.
fneirab 11:bdbd3104995b 706 *
fneirab 11:bdbd3104995b 707 * @retval 0 for success
fneirab 11:bdbd3104995b 708 * @retval non-0 negative for errors
fneirab 11:bdbd3104995b 709 */
fneirab 9:f29d5e49b190 710 int MAX17055::save_Params(saved_FG_params_t FG_params)
fneirab 9:f29d5e49b190 711 {
fneirab 11:bdbd3104995b 712 int ret, value;
fneirab 9:f29d5e49b190 713 uint16_t data[5];
fneirab 11:bdbd3104995b 714 ///STEP 1. Checks if the cycel register bit 2 has changed.
fneirab 11:bdbd3104995b 715 ret = readReg(CYCLES_REG, data[3]);
fneirab 11:bdbd3104995b 716 if (ret < F_SUCCESS_0)
fneirab 11:bdbd3104995b 717 return ret;
fneirab 11:bdbd3104995b 718 else {
fneirab 11:bdbd3104995b 719 value = data[3];
fneirab 11:bdbd3104995b 720 }
fneirab 11:bdbd3104995b 721
fneirab 12:519a18fc3b28 722 ///STEP 2. Save the capacity parameters for the specific battery.
fneirab 9:f29d5e49b190 723 ret = readReg(RCOMP0_REG, data[0]);
fneirab 11:bdbd3104995b 724 if (ret < F_SUCCESS_0)
fneirab 9:f29d5e49b190 725 return ret;
fneirab 9:f29d5e49b190 726 else
fneirab 9:f29d5e49b190 727 FG_params.rcomp0 = data[0];
fneirab 9:f29d5e49b190 728
fneirab 9:f29d5e49b190 729 ret = readReg(TEMPCO_REG, data[1]);
fneirab 11:bdbd3104995b 730 if (ret < F_SUCCESS_0)
fneirab 9:f29d5e49b190 731 return ret;
fneirab 9:f29d5e49b190 732 else
fneirab 9:f29d5e49b190 733 FG_params.temp_co = data[1];
fneirab 9:f29d5e49b190 734
fneirab 9:f29d5e49b190 735 ret = readReg(FULLCAPREP_REG, data[2]);
fneirab 11:bdbd3104995b 736 if (ret < F_SUCCESS_0)
fneirab 9:f29d5e49b190 737 return ret;
fneirab 9:f29d5e49b190 738 else
fneirab 9:f29d5e49b190 739 FG_params.full_cap_rep = data[2];
fneirab 9:f29d5e49b190 740
fneirab 11:bdbd3104995b 741 FG_params.cycles = data[3];
fneirab 9:f29d5e49b190 742
fneirab 9:f29d5e49b190 743 ret = readReg(FULLCAPNOM_REG, data[4]);
fneirab 11:bdbd3104995b 744 if (ret < F_SUCCESS_0)
fneirab 9:f29d5e49b190 745 return ret;
fneirab 9:f29d5e49b190 746 else
fneirab 9:f29d5e49b190 747 FG_params.full_cap_nom = data[4];
fneirab 9:f29d5e49b190 748 return ret;
fneirab 9:f29d5e49b190 749 }
fneirab 9:f29d5e49b190 750
fneirab 9:f29d5e49b190 751 /**
fneirab 12:519a18fc3b28 752 * @brief Restore Parameters Function for battery Fuel Gauge model.
fneirab 11:bdbd3104995b 753 * @par Details
fneirab 11:bdbd3104995b 754 * If power is lost, then the capacity information
fneirab 11:bdbd3104995b 755 * can be easily restored with this function.
fneirab 11:bdbd3104995b 756 *
fneirab 11:bdbd3104995b 757 * @param[in] FG_params Struct for Fuel Gauge Parameters
fneirab 11:bdbd3104995b 758 * @retval 0 for success
fneirab 11:bdbd3104995b 759 * @retval non-0 negative for errors
fneirab 11:bdbd3104995b 760 */
fneirab 9:f29d5e49b190 761 int MAX17055::restore_Params(saved_FG_params_t FG_params)
fneirab 9:f29d5e49b190 762 {
fneirab 9:f29d5e49b190 763 int ret;
fneirab 9:f29d5e49b190 764 uint16_t temp_data, fullcapnom_data, mixCap_calc, dQacc_calc;
fneirab 12:519a18fc3b28 765 uint16_t dPacc_value = 0x0C80;//Set it to 200%
fneirab 9:f29d5e49b190 766
fneirab 11:bdbd3104995b 767 ///STEP 1. Restoring capacity parameters
fneirab 9:f29d5e49b190 768 write_and_verify_reg(RCOMP0_REG, FG_params.rcomp0);
fneirab 9:f29d5e49b190 769 write_and_verify_reg(TEMPCO_REG, FG_params.temp_co);
fneirab 9:f29d5e49b190 770 write_and_verify_reg(FULLCAPNOM_REG, FG_params.full_cap_nom);
fneirab 9:f29d5e49b190 771
fneirab 11:bdbd3104995b 772 wait_ms(350);//check the type of wait
fneirab 11:bdbd3104995b 773
fneirab 11:bdbd3104995b 774 ///STEP 2. Restore FullCap
fneirab 9:f29d5e49b190 775 ret = readReg(FULLCAPNOM_REG, fullcapnom_data);
fneirab 11:bdbd3104995b 776 if (ret < F_SUCCESS_0)
fneirab 9:f29d5e49b190 777 return ret;
fneirab 9:f29d5e49b190 778
fneirab 12:519a18fc3b28 779 ret = readReg(MIXSOC_REG, temp_data); //check if Error in software guide register incorrect
fneirab 11:bdbd3104995b 780 if (ret < F_SUCCESS_0)
fneirab 9:f29d5e49b190 781 return ret;
fneirab 9:f29d5e49b190 782
fneirab 9:f29d5e49b190 783 mixCap_calc = (temp_data*fullcapnom_data)/25600;
fneirab 9:f29d5e49b190 784
fneirab 9:f29d5e49b190 785 write_and_verify_reg(MIXCAP_REG, mixCap_calc);
fneirab 9:f29d5e49b190 786 write_and_verify_reg(FULLCAPREP_REG, FG_params.full_cap_rep);
fneirab 9:f29d5e49b190 787
fneirab 11:bdbd3104995b 788 ///STEP 3. Write DQACC to 200% of Capacity and DPACC to 200%
fneirab 9:f29d5e49b190 789 dQacc_calc = (FG_params.full_cap_nom/ 16) ;
fneirab 9:f29d5e49b190 790
fneirab 9:f29d5e49b190 791 write_and_verify_reg(DPACC_REG, dPacc_value);
fneirab 9:f29d5e49b190 792 write_and_verify_reg(DQACC_REG, dQacc_calc);
fneirab 9:f29d5e49b190 793
fneirab 9:f29d5e49b190 794 wait_ms(350);
fneirab 9:f29d5e49b190 795
fneirab 11:bdbd3104995b 796 ///STEP 4. Restore Cycles register
fneirab 9:f29d5e49b190 797 ret = write_and_verify_reg(CYCLES_REG, FG_params.cycles);
fneirab 11:bdbd3104995b 798 if (ret < F_SUCCESS_0)
fneirab 9:f29d5e49b190 799 return ret;
fneirab 11:bdbd3104995b 800 return ret;
fneirab 11:bdbd3104995b 801 }
fneirab 11:bdbd3104995b 802
fneirab 11:bdbd3104995b 803 /**
fneirab 11:bdbd3104995b 804 * @brief Function to Save Average Current to At Rate register.
fneirab 11:bdbd3104995b 805 * @par Details
fneirab 12:519a18fc3b28 806 * For User friendliness display of atTTE, atAvSOC, atAvCAP
fneirab 11:bdbd3104995b 807 * write the average current to At Rate registers every 10sec
fneirab 11:bdbd3104995b 808 * when the battery is in use.
fneirab 11:bdbd3104995b 809 * NOTE: do not use this function when the Battery is charging.
fneirab 11:bdbd3104995b 810 *
fneirab 11:bdbd3104995b 811 * @retval 0 for success
fneirab 11:bdbd3104995b 812 * @retval non-0 negative for errors
fneirab 11:bdbd3104995b 813 */
fneirab 11:bdbd3104995b 814 int MAX17055::avCurr_2_atRate()
fneirab 11:bdbd3104995b 815 {
fneirab 11:bdbd3104995b 816 int ret;
fneirab 11:bdbd3104995b 817 uint16_t avCurr_data;
fneirab 11:bdbd3104995b 818
fneirab 11:bdbd3104995b 819 ret = readReg(AVGCURRENT_REG, avCurr_data);
fneirab 12:519a18fc3b28 820 if (ret < F_SUCCESS_0){
fneirab 12:519a18fc3b28 821 return ret = -3;
fneirab 12:519a18fc3b28 822 }
fneirab 11:bdbd3104995b 823
fneirab 11:bdbd3104995b 824 //Write avCurrent to atRate Register
fneirab 12:519a18fc3b28 825 ret = writeReg(ATRATE_REG, avCurr_data);
fneirab 12:519a18fc3b28 826 if (ret < F_SUCCESS_0){
fneirab 11:bdbd3104995b 827 return ret;
fneirab 12:519a18fc3b28 828 }
fneirab 12:519a18fc3b28 829 return F_SUCCESS_0;
fneirab 7:479a36909ced 830 }