This is a library for the MAX17055 Li+ Battery Fuel Gauge.
Fork of max17055 by
max17055.cpp@9:f29d5e49b190, 2018-02-06 (annotated)
- Committer:
- fneirab
- Date:
- Tue Feb 06 21:10:30 2018 +0000
- Revision:
- 9:f29d5e49b190
- Parent:
- 8:ca8765c30ed2
- Child:
- 11:bdbd3104995b
latest version 1.2 with new function additions and documentation.
Who changed what in which revision?
User | Revision | Line number | New 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 | 9:f29d5e49b190 | 6 | * @version 1.2 |
fneirab | 4:a4d6ae2182c2 | 7 | * |
fneirab | 9:f29d5e49b190 | 8 | * Started: 9JAN18 |
fneirab | 4:a4d6ae2182c2 | 9 | * |
fneirab | 9:f29d5e49b190 | 10 | * Updated: |
fneirab | 9:f29d5e49b190 | 11 | * New functions improved for continious display. Change copyright notice for 2018. |
fneirab | 9:f29d5e49b190 | 12 | * |
fneirab | 9:f29d5e49b190 | 13 | * |
fneirab | 4:a4d6ae2182c2 | 14 | * |
fneirab | 9:f29d5e49b190 | 15 | /******************************************************************************* |
fneirab | 9:f29d5e49b190 | 16 | * Copyright (C) 2018 Maxim Integrated Products, Inc., All Rights Reserved. |
fneirab | 4:a4d6ae2182c2 | 17 | * |
fneirab | 4:a4d6ae2182c2 | 18 | * Permission is hereby granted, free of charge, to any person obtaining a |
fneirab | 4:a4d6ae2182c2 | 19 | * copy of this software and associated documentation files (the "Software"), |
fneirab | 4:a4d6ae2182c2 | 20 | * to deal in the Software without restriction, including without limitation |
fneirab | 4:a4d6ae2182c2 | 21 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
fneirab | 4:a4d6ae2182c2 | 22 | * and/or sell copies of the Software, and to permit persons to whom the |
fneirab | 4:a4d6ae2182c2 | 23 | * Software is furnished to do so, subject to the following conditions: |
fneirab | 4:a4d6ae2182c2 | 24 | * |
fneirab | 4:a4d6ae2182c2 | 25 | * The above copyright notice and this permission notice shall be included |
fneirab | 4:a4d6ae2182c2 | 26 | * in all copies or substantial portions of the Software. |
fneirab | 4:a4d6ae2182c2 | 27 | * |
fneirab | 4:a4d6ae2182c2 | 28 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
fneirab | 4:a4d6ae2182c2 | 29 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
fneirab | 4:a4d6ae2182c2 | 30 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
fneirab | 4:a4d6ae2182c2 | 31 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
fneirab | 4:a4d6ae2182c2 | 32 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
fneirab | 4:a4d6ae2182c2 | 33 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
fneirab | 4:a4d6ae2182c2 | 34 | * OTHER DEALINGS IN THE SOFTWARE. |
fneirab | 4:a4d6ae2182c2 | 35 | * |
fneirab | 4:a4d6ae2182c2 | 36 | * Except as contained in this notice, the name of Maxim Integrated |
fneirab | 4:a4d6ae2182c2 | 37 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
fneirab | 4:a4d6ae2182c2 | 38 | * Products, Inc. Branding Policy. |
fneirab | 4:a4d6ae2182c2 | 39 | * |
fneirab | 4:a4d6ae2182c2 | 40 | * The mere transfer of this software does not imply any licenses |
fneirab | 4:a4d6ae2182c2 | 41 | * of trade secrets, proprietary technology, copyrights, patents, |
fneirab | 4:a4d6ae2182c2 | 42 | * trademarks, maskwork rights, or any other form of intellectual |
fneirab | 4:a4d6ae2182c2 | 43 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
fneirab | 4:a4d6ae2182c2 | 44 | * ownership rights. |
fneirab | 9:f29d5e49b190 | 45 | ******************************************************************************* |
fneirab | 9:f29d5e49b190 | 46 | */ |
fneirab | 0:80c39eb8f3ba | 47 | |
fneirab | 0:80c39eb8f3ba | 48 | #include "mbed.h" |
fneirab | 0:80c39eb8f3ba | 49 | #include "max17055.h" |
fneirab | 0:80c39eb8f3ba | 50 | |
fneirab | 5:a18a189588dc | 51 | #define DRV_NAME "max17055" |
fneirab | 5:a18a189588dc | 52 | |
fneirab | 5:a18a189588dc | 53 | /* CONFIG register bits */ |
fneirab | 5:a18a189588dc | 54 | #define MAX17055_CONFIG_ALRT_EN (1 << 2) |
fneirab | 5:a18a189588dc | 55 | #define MAX17055_CONFIG2_LDMDL (1 << 5) |
fneirab | 5:a18a189588dc | 56 | |
fneirab | 5:a18a189588dc | 57 | /* STATUS register bits */ |
fneirab | 5:a18a189588dc | 58 | #define MAX17055_STATUS_BST (1 << 3) |
fneirab | 5:a18a189588dc | 59 | #define MAX17055_STATUS_POR (1 << 1) |
fneirab | 5:a18a189588dc | 60 | |
fneirab | 9:f29d5e49b190 | 61 | /* POR Mask */ |
fneirab | 9:f29d5e49b190 | 62 | #define MAX17055_POR_MASK (0xFFFD) |
fneirab | 9:f29d5e49b190 | 63 | |
fneirab | 5:a18a189588dc | 64 | /* MODELCFG register bits */ |
fneirab | 5:a18a189588dc | 65 | #define MAX17055_MODELCFG_REFRESH (1 << 15) |
fneirab | 5:a18a189588dc | 66 | |
fneirab | 5:a18a189588dc | 67 | /* TALRTTH register bits */ |
fneirab | 5:a18a189588dc | 68 | #define MIN_TEMP_ALERT 0 |
fneirab | 5:a18a189588dc | 69 | #define MAX_TEMP_ALERT 8 |
fneirab | 5:a18a189588dc | 70 | |
fneirab | 5:a18a189588dc | 71 | /* FSTAT register bits */ |
fneirab | 5:a18a189588dc | 72 | #define MAX17055_FSTAT_DNR (1) |
fneirab | 5:a18a189588dc | 73 | |
fneirab | 5:a18a189588dc | 74 | /* STATUS interrupt status bits */ |
fneirab | 5:a18a189588dc | 75 | #define MAX17055_STATUS_ALRT_CLR_MASK (0x88BB) |
fneirab | 5:a18a189588dc | 76 | #define MAX17055_STATUS_SOC_MAX_ALRT (1 << 14) |
fneirab | 5:a18a189588dc | 77 | #define MAX17055_STATUS_TEMP_MAX_ALRT (1 << 13) |
fneirab | 5:a18a189588dc | 78 | #define MAX17055_STATUS_VOLT_MAX_ALRT (1 << 12) |
fneirab | 5:a18a189588dc | 79 | #define MAX17055_STATUS_SOC_MIN_ALRT (1 << 10) |
fneirab | 5:a18a189588dc | 80 | #define MAX17055_STATUS_TEMP_MIN_ALRT (1 << 9) |
fneirab | 5:a18a189588dc | 81 | #define MAX17055_STATUS_VOLT_MIN_ALRT (1 << 8) |
fneirab | 5:a18a189588dc | 82 | #define MAX17055_STATUS_CURR_MAX_ALRT (1 << 6) |
fneirab | 5:a18a189588dc | 83 | #define MAX17055_STATUS_CURR_MIN_ALRT (1 << 2) |
fneirab | 5:a18a189588dc | 84 | |
fneirab | 5:a18a189588dc | 85 | #define MAX17055_VMAX_TOLERANCE 50 /* 50 mV */ |
fneirab | 4:a4d6ae2182c2 | 86 | |
fneirab | 0:80c39eb8f3ba | 87 | |
fneirab | 6:5ced10109ebf | 88 | /////////////////////////////////////////////////////////////////////////////// |
fneirab | 5:a18a189588dc | 89 | |
fneirab | 5:a18a189588dc | 90 | MAX17055::MAX17055(I2C &i2c): |
fneirab | 5:a18a189588dc | 91 | m_i2cBus(i2c) |
fneirab | 0:80c39eb8f3ba | 92 | { |
fneirab | 5:a18a189588dc | 93 | //empty block |
fneirab | 0:80c39eb8f3ba | 94 | } |
fneirab | 0:80c39eb8f3ba | 95 | |
fneirab | 6:5ced10109ebf | 96 | /////////////////////////////////////////////////////////////////////////////// |
fneirab | 0:80c39eb8f3ba | 97 | MAX17055::~MAX17055() |
fneirab | 0:80c39eb8f3ba | 98 | { |
fneirab | 0:80c39eb8f3ba | 99 | //empty block |
fneirab | 0:80c39eb8f3ba | 100 | } |
fneirab | 5:a18a189588dc | 101 | |
fneirab | 5:a18a189588dc | 102 | |
fneirab | 1:a031f0c6a71e | 103 | /////////////////////////////////////////////////////////////////////////////// |
fneirab | 0:80c39eb8f3ba | 104 | |
fneirab | 0:80c39eb8f3ba | 105 | /** |
fneirab | 0:80c39eb8f3ba | 106 | * \brief Write a value to a MAX17055 register |
fneirab | 0:80c39eb8f3ba | 107 | * \par Details |
fneirab | 0:80c39eb8f3ba | 108 | * This function writes a value to a MAX17055 register |
fneirab | 0:80c39eb8f3ba | 109 | * |
fneirab | 1:a031f0c6a71e | 110 | * \param[in] reg_addr - register address |
fneirab | 1:a031f0c6a71e | 111 | * \param[in] reg_data - register data |
fneirab | 0:80c39eb8f3ba | 112 | * |
fneirab | 1:a031f0c6a71e | 113 | * \retval 1 on success |
fneirab | 0:80c39eb8f3ba | 114 | */ |
fneirab | 0:80c39eb8f3ba | 115 | |
fneirab | 0:80c39eb8f3ba | 116 | int MAX17055::writeReg(Registers_e reg_addr, uint16_t reg_data) |
fneirab | 0:80c39eb8f3ba | 117 | { |
fneirab | 0:80c39eb8f3ba | 118 | |
fneirab | 7:479a36909ced | 119 | uint16_t mask = 0x00FF; |
fneirab | 1:a031f0c6a71e | 120 | uint8_t dataLSB; |
fneirab | 1:a031f0c6a71e | 121 | uint8_t dataMSB; |
fneirab | 5:a18a189588dc | 122 | |
fneirab | 7:479a36909ced | 123 | dataLSB = reg_data & mask; |
fneirab | 7:479a36909ced | 124 | dataMSB = (reg_data >> 8) & mask; |
fneirab | 5:a18a189588dc | 125 | |
fneirab | 7:479a36909ced | 126 | char addr_plus_data[3] = {reg_addr, dataLSB, dataMSB}; |
fneirab | 5:a18a189588dc | 127 | |
fneirab | 7:479a36909ced | 128 | if ( m_i2cBus.write(I2C_W_ADRS, addr_plus_data, 3, false) == 0) |
fneirab | 0:80c39eb8f3ba | 129 | return 1; |
fneirab | 0:80c39eb8f3ba | 130 | else |
fneirab | 0:80c39eb8f3ba | 131 | return 0; |
fneirab | 0:80c39eb8f3ba | 132 | |
fneirab | 0:80c39eb8f3ba | 133 | } |
fneirab | 1:a031f0c6a71e | 134 | /////////////////////////////////////////////////////////////////////////////// |
fneirab | 0:80c39eb8f3ba | 135 | /** |
fneirab | 0:80c39eb8f3ba | 136 | * \brief Read a MAX17055 register |
fneirab | 0:80c39eb8f3ba | 137 | * \par Details |
fneirab | 0:80c39eb8f3ba | 138 | * This function reads a MAX17055 register |
fneirab | 0:80c39eb8f3ba | 139 | * |
fneirab | 1:a031f0c6a71e | 140 | * \param[in] reg_addr - register address |
fneirab | 1:a031f0c6a71e | 141 | * \param[out] &value - pointer that stores the register data |
fneirab | 0:80c39eb8f3ba | 142 | * |
fneirab | 0:80c39eb8f3ba | 143 | * \retval 1 on success |
fneirab | 0:80c39eb8f3ba | 144 | */ |
fneirab | 0:80c39eb8f3ba | 145 | |
fneirab | 0:80c39eb8f3ba | 146 | |
fneirab | 1:a031f0c6a71e | 147 | int32_t MAX17055::readReg(Registers_e reg_addr, uint16_t &value) |
fneirab | 0:80c39eb8f3ba | 148 | { |
fneirab | 0:80c39eb8f3ba | 149 | int32_t result; |
fneirab | 7:479a36909ced | 150 | uint16_t mask = 0x00FF; |
fneirab | 1:a031f0c6a71e | 151 | char local_data[1]; |
fneirab | 0:80c39eb8f3ba | 152 | local_data[0] = reg_addr; |
fneirab | 1:a031f0c6a71e | 153 | char read_data[2]; |
fneirab | 5:a18a189588dc | 154 | |
fneirab | 0:80c39eb8f3ba | 155 | result = m_i2cBus.write(I2C_W_ADRS, local_data, 1); |
fneirab | 5:a18a189588dc | 156 | if(result == 0) { |
fneirab | 1:a031f0c6a71e | 157 | result = m_i2cBus.read(I2C_R_ADRS, read_data , 2, false); |
fneirab | 5:a18a189588dc | 158 | if (result == 0) { |
fneirab | 7:479a36909ced | 159 | value = ( ((read_data[1] & mask) << 8) + (read_data[0])); |
fneirab | 1:a031f0c6a71e | 160 | result = 1; |
fneirab | 5:a18a189588dc | 161 | } |
fneirab | 0:80c39eb8f3ba | 162 | } |
fneirab | 5:a18a189588dc | 163 | |
fneirab | 0:80c39eb8f3ba | 164 | return result; |
fneirab | 5:a18a189588dc | 165 | |
fneirab | 0:80c39eb8f3ba | 166 | } |
fneirab | 0:80c39eb8f3ba | 167 | |
fneirab | 2:ff7db397b70f | 168 | /////////////////////////////////////////////////////////////////////////////// |
fneirab | 1:a031f0c6a71e | 169 | /** |
fneirab | 1:a031f0c6a71e | 170 | * \brief Write and Verify a MAX17055 register |
fneirab | 1:a031f0c6a71e | 171 | * \par Details |
fneirab | 1:a031f0c6a71e | 172 | * This function wites and verifies if the writing process was successful |
fneirab | 1:a031f0c6a71e | 173 | * |
fneirab | 1:a031f0c6a71e | 174 | * \param[in] reg_addr - register address |
fneirab | 5:a18a189588dc | 175 | * \param[out] reg_data - the variable that contains the data to write |
fneirab | 1:a031f0c6a71e | 176 | * to the register address |
fneirab | 1:a031f0c6a71e | 177 | * |
fneirab | 1:a031f0c6a71e | 178 | * \retval 1 on success |
fneirab | 1:a031f0c6a71e | 179 | */ |
fneirab | 1:a031f0c6a71e | 180 | |
fneirab | 1:a031f0c6a71e | 181 | |
fneirab | 5:a18a189588dc | 182 | int MAX17055::write_and_verify_reg(Registers_e reg_addr, uint16_t reg_data) |
fneirab | 5:a18a189588dc | 183 | { |
fneirab | 1:a031f0c6a71e | 184 | int retries = 8; |
fneirab | 1:a031f0c6a71e | 185 | int ret; |
fneirab | 1:a031f0c6a71e | 186 | int statusRead; |
fneirab | 1:a031f0c6a71e | 187 | int statusWrite; |
fneirab | 1:a031f0c6a71e | 188 | uint16_t read_data; |
fneirab | 5:a18a189588dc | 189 | |
fneirab | 1:a031f0c6a71e | 190 | do { |
fneirab | 1:a031f0c6a71e | 191 | statusWrite = writeReg(reg_addr, reg_data); |
fneirab | 1:a031f0c6a71e | 192 | if (statusWrite != 1) |
fneirab | 1:a031f0c6a71e | 193 | ret = -1; |
fneirab | 1:a031f0c6a71e | 194 | wait_ms(3); |
fneirab | 5:a18a189588dc | 195 | statusRead = readReg(reg_addr, read_data); |
fneirab | 1:a031f0c6a71e | 196 | if (statusRead != 1) |
fneirab | 1:a031f0c6a71e | 197 | ret = -2; |
fneirab | 5:a18a189588dc | 198 | if (read_data != reg_data) { |
fneirab | 1:a031f0c6a71e | 199 | ret = -3; |
fneirab | 1:a031f0c6a71e | 200 | retries--; |
fneirab | 1:a031f0c6a71e | 201 | } |
fneirab | 5:a18a189588dc | 202 | } while (retries && read_data != reg_data); |
fneirab | 5:a18a189588dc | 203 | |
fneirab | 6:5ced10109ebf | 204 | if (ret<0) |
fneirab | 1:a031f0c6a71e | 205 | return ret; |
fneirab | 7:479a36909ced | 206 | else |
fneirab | 1:a031f0c6a71e | 207 | return 1; |
fneirab | 1:a031f0c6a71e | 208 | } |
fneirab | 2:ff7db397b70f | 209 | |
fneirab | 2:ff7db397b70f | 210 | //////////////////////////////////////////////////////////////////////////////// |
fneirab | 2:ff7db397b70f | 211 | |
fneirab | 2:ff7db397b70f | 212 | /** |
fneirab | 3:f77a8345b0e3 | 213 | * \brief Initialise Function for MAX17055 |
fneirab | 3:f77a8345b0e3 | 214 | * \par Details |
fneirab | 3:f77a8345b0e3 | 215 | * This function intitializes the MAX17055 |
fneirab | 3:f77a8345b0e3 | 216 | * |
fneirab | 5:a18a189588dc | 217 | * \retval 1 on success |
fneirab | 3:f77a8345b0e3 | 218 | * 0 if device is not present |
fneirab | 3:f77a8345b0e3 | 219 | * -1 if errors exist |
fneirab | 9:f29d5e49b190 | 220 | * -2 if POR is not set |
fneirab | 3:f77a8345b0e3 | 221 | */ |
fneirab | 5:a18a189588dc | 222 | |
fneirab | 5:a18a189588dc | 223 | |
fneirab | 7:479a36909ced | 224 | int MAX17055::init(platform_data des_data) |
fneirab | 2:ff7db397b70f | 225 | { |
fneirab | 6:5ced10109ebf | 226 | |
fneirab | 5:a18a189588dc | 227 | int status, ret; |
fneirab | 9:f29d5e49b190 | 228 | int time_out = 10; |
fneirab | 7:479a36909ced | 229 | uint16_t read_data, hibcfg_value, reg; |
fneirab | 5:a18a189588dc | 230 | |
fneirab | 5:a18a189588dc | 231 | |
fneirab | 7:479a36909ced | 232 | status = readReg(VERSION_REG, read_data); |
fneirab | 2:ff7db397b70f | 233 | if (status == 0) |
fneirab | 2:ff7db397b70f | 234 | return status; //Device is not present in the i2c Bus |
fneirab | 5:a18a189588dc | 235 | |
fneirab | 2:ff7db397b70f | 236 | /* Step 0: Check for POR */ |
fneirab | 2:ff7db397b70f | 237 | /* Skip load model if POR bit is cleared */ |
fneirab | 5:a18a189588dc | 238 | |
fneirab | 9:f29d5e49b190 | 239 | if (check_POR_func() == -1) |
fneirab | 9:f29d5e49b190 | 240 | return -2; //POR is not set. Skip Initialization. |
fneirab | 2:ff7db397b70f | 241 | |
fneirab | 2:ff7db397b70f | 242 | /* Step 1: Check if FStat.DNR == 0 */ |
fneirab | 3:f77a8345b0e3 | 243 | // Do not continue until FSTAT.DNR == 0 |
fneirab | 7:479a36909ced | 244 | ret = poll_flag_clear(FSTAT_REG, MAX17055_FSTAT_DNR, time_out); |
fneirab | 7:479a36909ced | 245 | if (ret < 0) { |
fneirab | 9:f29d5e49b190 | 246 | //printf("Unsuccessful init: Data Not Ready!\n"); |
fneirab | 6:5ced10109ebf | 247 | return ret; |
fneirab | 3:f77a8345b0e3 | 248 | } |
fneirab | 5:a18a189588dc | 249 | |
fneirab | 5:a18a189588dc | 250 | /* Force exit from hibernate */ |
fneirab | 5:a18a189588dc | 251 | hibcfg_value = forcedExitHyberMode(); |
fneirab | 7:479a36909ced | 252 | |
fneirab | 9:f29d5e49b190 | 253 | //printf("step 1 check \r\n"); |
fneirab | 5:a18a189588dc | 254 | |
fneirab | 5:a18a189588dc | 255 | /* Step 2: Initialize configuration */ |
fneirab | 5:a18a189588dc | 256 | switch (1) { |
fneirab | 5:a18a189588dc | 257 | case MODEL_LOADING_OPTION1: |
fneirab | 5:a18a189588dc | 258 | /* Step 2.1: Option 1 EZ Config */ |
fneirab | 7:479a36909ced | 259 | EZconfig_init(des_data); |
fneirab | 5:a18a189588dc | 260 | |
fneirab | 5:a18a189588dc | 261 | /* Poll ModelCFG.ModelRefresh bit for clear */ |
fneirab | 7:479a36909ced | 262 | ret = poll_flag_clear(MODELCFG_REG, MAX17055_MODELCFG_REFRESH, time_out); |
fneirab | 5:a18a189588dc | 263 | if(ret < 0) { |
fneirab | 5:a18a189588dc | 264 | //dev_err(priv->dev, "Option1 model refresh not completed!\n"); |
fneirab | 5:a18a189588dc | 265 | return ret; |
fneirab | 5:a18a189588dc | 266 | } |
fneirab | 7:479a36909ced | 267 | |
fneirab | 9:f29d5e49b190 | 268 | break; |
fneirab | 5:a18a189588dc | 269 | } |
fneirab | 5:a18a189588dc | 270 | /* Restore original HibCfg */ |
fneirab | 7:479a36909ced | 271 | writeReg(HIBCFG_REG, hibcfg_value); |
fneirab | 9:f29d5e49b190 | 272 | //printf("Last section check \r\n"); |
fneirab | 6:5ced10109ebf | 273 | |
fneirab | 5:a18a189588dc | 274 | |
fneirab | 5:a18a189588dc | 275 | /* Optional step - alert threshold initialization */ |
fneirab | 7:479a36909ced | 276 | //set_alert_thresholds(priv); |
fneirab | 5:a18a189588dc | 277 | |
fneirab | 5:a18a189588dc | 278 | /* Clear Status.POR */ |
fneirab | 9:f29d5e49b190 | 279 | ret = clear_POR_bit(); |
fneirab | 9:f29d5e49b190 | 280 | if (ret == -1) |
fneirab | 9:f29d5e49b190 | 281 | return ret; |
fneirab | 2:ff7db397b70f | 282 | return 1; |
fneirab | 5:a18a189588dc | 283 | } |
fneirab | 5:a18a189588dc | 284 | |
fneirab | 9:f29d5e49b190 | 285 | //////////////////////////////////////////////////////////////////////////////// |
fneirab | 9:f29d5e49b190 | 286 | |
fneirab | 9:f29d5e49b190 | 287 | /** |
fneirab | 9:f29d5e49b190 | 288 | * \brief Check POR function |
fneirab | 9:f29d5e49b190 | 289 | * \par Details |
fneirab | 9:f29d5e49b190 | 290 | * This function check is there was a power on reset event for the MAX17055 |
fneirab | 9:f29d5e49b190 | 291 | * |
fneirab | 9:f29d5e49b190 | 292 | * \retval 1 for no POR detected |
fneirab | 9:f29d5e49b190 | 293 | * -1 for POR detected |
fneirab | 9:f29d5e49b190 | 294 | */ |
fneirab | 9:f29d5e49b190 | 295 | int MAX17055::check_POR_func() |
fneirab | 9:f29d5e49b190 | 296 | { |
fneirab | 9:f29d5e49b190 | 297 | uint16_t read_data; |
fneirab | 9:f29d5e49b190 | 298 | |
fneirab | 9:f29d5e49b190 | 299 | readReg(STATUS_REG, read_data); |
fneirab | 9:f29d5e49b190 | 300 | |
fneirab | 9:f29d5e49b190 | 301 | if (!(read_data & MAX17055_STATUS_POR ) ) |
fneirab | 9:f29d5e49b190 | 302 | return -1; //POR is not set. |
fneirab | 9:f29d5e49b190 | 303 | else |
fneirab | 9:f29d5e49b190 | 304 | return 1; |
fneirab | 9:f29d5e49b190 | 305 | } |
fneirab | 9:f29d5e49b190 | 306 | |
fneirab | 9:f29d5e49b190 | 307 | //////////////////////////////////////////////////////////////////////////////// |
fneirab | 9:f29d5e49b190 | 308 | |
fneirab | 9:f29d5e49b190 | 309 | /** |
fneirab | 9:f29d5e49b190 | 310 | * \brief clear POR bit function |
fneirab | 9:f29d5e49b190 | 311 | * \par Details |
fneirab | 9:f29d5e49b190 | 312 | * This function clear the idicating bit for POR - MAX17055 |
fneirab | 9:f29d5e49b190 | 313 | * |
fneirab | 9:f29d5e49b190 | 314 | * \retval 1 for Success |
fneirab | 9:f29d5e49b190 | 315 | * -1 for errors |
fneirab | 9:f29d5e49b190 | 316 | */ |
fneirab | 9:f29d5e49b190 | 317 | int MAX17055::clear_POR_bit() |
fneirab | 9:f29d5e49b190 | 318 | { |
fneirab | 9:f29d5e49b190 | 319 | int status, ret; |
fneirab | 9:f29d5e49b190 | 320 | uint16_t read_data, hibcfg_value, reg; |
fneirab | 9:f29d5e49b190 | 321 | |
fneirab | 9:f29d5e49b190 | 322 | |
fneirab | 9:f29d5e49b190 | 323 | status = readReg(STATUS_REG, read_data); |
fneirab | 9:f29d5e49b190 | 324 | if (status != 1) |
fneirab | 9:f29d5e49b190 | 325 | return -1; //Device is not present in the i2c Bus |
fneirab | 9:f29d5e49b190 | 326 | status = write_and_verify_reg(STATUS_REG, (read_data & MAX17055_POR_MASK)); |
fneirab | 9:f29d5e49b190 | 327 | if (status != 1) |
fneirab | 9:f29d5e49b190 | 328 | return -1; |
fneirab | 9:f29d5e49b190 | 329 | else |
fneirab | 9:f29d5e49b190 | 330 | return 1; |
fneirab | 9:f29d5e49b190 | 331 | |
fneirab | 9:f29d5e49b190 | 332 | |
fneirab | 9:f29d5e49b190 | 333 | } |
fneirab | 5:a18a189588dc | 334 | |
fneirab | 5:a18a189588dc | 335 | /////////////////////////////////////////////////////////////////////////////// |
fneirab | 5:a18a189588dc | 336 | |
fneirab | 5:a18a189588dc | 337 | /** |
fneirab | 5:a18a189588dc | 338 | * \brief Poll Flag clear |
fneirab | 5:a18a189588dc | 339 | * \par Details |
fneirab | 5:a18a189588dc | 340 | * This function clears status flags for the MAX17055 |
fneirab | 5:a18a189588dc | 341 | * |
fneirab | 5:a18a189588dc | 342 | * \param[in] reg_addr - register address |
fneirab | 5:a18a189588dc | 343 | * \param[in] mask - register address |
fneirab | 5:a18a189588dc | 344 | * \param[in] timeout - register data |
fneirab | 5:a18a189588dc | 345 | * |
fneirab | 5:a18a189588dc | 346 | * \retval 1 on success |
fneirab | 5:a18a189588dc | 347 | * -1 on Failure |
fneirab | 5:a18a189588dc | 348 | */ |
fneirab | 5:a18a189588dc | 349 | |
fneirab | 7:479a36909ced | 350 | int MAX17055::poll_flag_clear (Registers_e reg_addr, int mask, int timeout) |
fneirab | 5:a18a189588dc | 351 | { |
fneirab | 5:a18a189588dc | 352 | uint16_t data; |
fneirab | 5:a18a189588dc | 353 | int ret; |
fneirab | 5:a18a189588dc | 354 | |
fneirab | 9:f29d5e49b190 | 355 | |
fneirab | 5:a18a189588dc | 356 | do { |
fneirab | 9:f29d5e49b190 | 357 | wait_ms(1); |
fneirab | 6:5ced10109ebf | 358 | ret = readReg(reg_addr, data); |
fneirab | 5:a18a189588dc | 359 | if(ret < 0) |
fneirab | 5:a18a189588dc | 360 | return ret; |
fneirab | 5:a18a189588dc | 361 | |
fneirab | 5:a18a189588dc | 362 | if(!(data & mask)) |
fneirab | 5:a18a189588dc | 363 | return 1; |
fneirab | 5:a18a189588dc | 364 | |
fneirab | 9:f29d5e49b190 | 365 | timeout -= 1; |
fneirab | 5:a18a189588dc | 366 | } while(timeout > 0); |
fneirab | 5:a18a189588dc | 367 | |
fneirab | 5:a18a189588dc | 368 | return -1; |
fneirab | 5:a18a189588dc | 369 | } |
fneirab | 5:a18a189588dc | 370 | |
fneirab | 5:a18a189588dc | 371 | |
fneirab | 2:ff7db397b70f | 372 | |
fneirab | 2:ff7db397b70f | 373 | //////////////////////////////////////////////////////////////////////////////// |
fneirab | 2:ff7db397b70f | 374 | |
fneirab | 2:ff7db397b70f | 375 | /** |
fneirab | 3:f77a8345b0e3 | 376 | * \brief Get Internal Temperature Function for MAX17055 |
fneirab | 3:f77a8345b0e3 | 377 | * \par Details |
fneirab | 3:f77a8345b0e3 | 378 | * This function sends a request to access the internal |
fneirab | 3:f77a8345b0e3 | 379 | * of the MAX17055 |
fneirab | 3:f77a8345b0e3 | 380 | * |
fneirab | 5:a18a189588dc | 381 | * |
fneirab | 7:479a36909ced | 382 | * \retval temperature value |
fneirab | 3:f77a8345b0e3 | 383 | * -1 if errors exist |
fneirab | 3:f77a8345b0e3 | 384 | */ |
fneirab | 5:a18a189588dc | 385 | |
fneirab | 5:a18a189588dc | 386 | |
fneirab | 7:479a36909ced | 387 | int MAX17055::get_temperature() |
fneirab | 2:ff7db397b70f | 388 | { |
fneirab | 5:a18a189588dc | 389 | |
fneirab | 2:ff7db397b70f | 390 | int ret; |
fneirab | 2:ff7db397b70f | 391 | uint16_t data; |
fneirab | 5:a18a189588dc | 392 | |
fneirab | 7:479a36909ced | 393 | ret = readReg(TEMP_REG, data); |
fneirab | 2:ff7db397b70f | 394 | if (ret < 0) |
fneirab | 2:ff7db397b70f | 395 | return ret; |
fneirab | 2:ff7db397b70f | 396 | |
fneirab | 2:ff7db397b70f | 397 | /* The value is signed. */ |
fneirab | 7:479a36909ced | 398 | if (data & 0x8000) |
fneirab | 7:479a36909ced | 399 | data |= 0xFFFF0000; |
fneirab | 2:ff7db397b70f | 400 | |
fneirab | 2:ff7db397b70f | 401 | /* The value is converted into centigrade scale */ |
fneirab | 2:ff7db397b70f | 402 | /* Units of LSB = 1 / 256 degree Celsius */ |
fneirab | 7:479a36909ced | 403 | data >>= 8; |
fneirab | 2:ff7db397b70f | 404 | |
fneirab | 7:479a36909ced | 405 | return data ; |
fneirab | 5:a18a189588dc | 406 | } |
fneirab | 3:f77a8345b0e3 | 407 | |
fneirab | 3:f77a8345b0e3 | 408 | |
fneirab | 3:f77a8345b0e3 | 409 | //////////////////////////////////////////////////////////////////////////////// |
fneirab | 3:f77a8345b0e3 | 410 | |
fneirab | 3:f77a8345b0e3 | 411 | /** |
fneirab | 3:f77a8345b0e3 | 412 | * \brief Forced Exit Hibernate Mode Function for MAX17055 |
fneirab | 3:f77a8345b0e3 | 413 | * \par Details |
fneirab | 5:a18a189588dc | 414 | * This function executes a force exit from hibernate mode. |
fneirab | 3:f77a8345b0e3 | 415 | * |
fneirab | 3:f77a8345b0e3 | 416 | * \retval returns HibCFG original value before forced Exit Hybernate mode |
fneirab | 3:f77a8345b0e3 | 417 | * |
fneirab | 3:f77a8345b0e3 | 418 | */ |
fneirab | 5:a18a189588dc | 419 | |
fneirab | 5:a18a189588dc | 420 | |
fneirab | 3:f77a8345b0e3 | 421 | uint16_t MAX17055::forcedExitHyberMode() |
fneirab | 3:f77a8345b0e3 | 422 | { |
fneirab | 3:f77a8345b0e3 | 423 | uint16_t hibcfg; |
fneirab | 5:a18a189588dc | 424 | |
fneirab | 5:a18a189588dc | 425 | /* Force exit from hibernate */ |
fneirab | 3:f77a8345b0e3 | 426 | //STEP 0: Store original HibCFG value |
fneirab | 7:479a36909ced | 427 | readReg(HIBCFG_REG, hibcfg); |
fneirab | 3:f77a8345b0e3 | 428 | |
fneirab | 3:f77a8345b0e3 | 429 | //STEP 1: Write to Soft-Wakeup Commannd Register |
fneirab | 7:479a36909ced | 430 | writeReg(VFSOC0_QH0_LOCK_REG, 0x90); //Soft-Wakeup from hybernate |
fneirab | 5:a18a189588dc | 431 | |
fneirab | 3:f77a8345b0e3 | 432 | //STEP 2: Write to Hibernate Configuration register |
fneirab | 7:479a36909ced | 433 | writeReg(HIBCFG_REG, 0x0); //disable hibernate mode |
fneirab | 5:a18a189588dc | 434 | |
fneirab | 3:f77a8345b0e3 | 435 | //STEP 3:Write to Soft-Wakeup Commannd Register |
fneirab | 7:479a36909ced | 436 | writeReg(VFSOC0_QH0_LOCK_REG, 0x0); //Clear All commnads |
fneirab | 5:a18a189588dc | 437 | |
fneirab | 3:f77a8345b0e3 | 438 | return hibcfg; |
fneirab | 5:a18a189588dc | 439 | } |
fneirab | 7:479a36909ced | 440 | |
fneirab | 7:479a36909ced | 441 | |
fneirab | 7:479a36909ced | 442 | //////////////////////////////////////////////////////////////////////////////// |
fneirab | 7:479a36909ced | 443 | |
fneirab | 7:479a36909ced | 444 | /** |
fneirab | 7:479a36909ced | 445 | * \brief EZ COnfing Init function |
fneirab | 7:479a36909ced | 446 | * \par Details |
fneirab | 7:479a36909ced | 447 | * This function implements the steps for the EZ confing m5 FuelGauge |
fneirab | 7:479a36909ced | 448 | * |
fneirab | 9:f29d5e49b190 | 449 | * \retval 1 on success |
fneirab | 7:479a36909ced | 450 | * |
fneirab | 7:479a36909ced | 451 | */ |
fneirab | 7:479a36909ced | 452 | |
fneirab | 7:479a36909ced | 453 | |
fneirab | 7:479a36909ced | 454 | uint16_t MAX17055::EZconfig_init(platform_data des_data) |
fneirab | 7:479a36909ced | 455 | { |
fneirab | 7:479a36909ced | 456 | const int charger_th = 4275; |
fneirab | 7:479a36909ced | 457 | const int chg_V_high = 51200; |
fneirab | 7:479a36909ced | 458 | const int chg_V_low = 44138; |
fneirab | 7:479a36909ced | 459 | const int param_EZ_FG1 = 0x8400; |
fneirab | 7:479a36909ced | 460 | const int param_EZ_FG2 = 0x8000; |
fneirab | 8:ca8765c30ed2 | 461 | uint16_t dpacc, ret; |
fneirab | 7:479a36909ced | 462 | |
fneirab | 7:479a36909ced | 463 | |
fneirab | 7:479a36909ced | 464 | /* Step 2.1: Option 1 EZ Config */ |
fneirab | 9:f29d5e49b190 | 465 | ret = writeReg(DESIGNCAP_REG, des_data.designcap); |
fneirab | 9:f29d5e49b190 | 466 | ret = writeReg(DQACC_REG, des_data.designcap >> 5); //DesignCap divide by 32 |
fneirab | 9:f29d5e49b190 | 467 | ret = writeReg(ICHGTERM_REG, des_data.ichgterm); |
fneirab | 9:f29d5e49b190 | 468 | ret = writeReg(VEMPTY_REG, des_data.vempty); |
fneirab | 7:479a36909ced | 469 | |
fneirab | 7:479a36909ced | 470 | if (des_data.vcharge > charger_th) { |
fneirab | 7:479a36909ced | 471 | dpacc = (des_data.designcap >> 5) * chg_V_high / des_data.designcap; |
fneirab | 9:f29d5e49b190 | 472 | ret = writeReg(DPACC_REG, dpacc); |
fneirab | 9:f29d5e49b190 | 473 | ret = writeReg(MODELCFG_REG, param_EZ_FG1); //Why 0x8400?? |
fneirab | 7:479a36909ced | 474 | } else { |
fneirab | 7:479a36909ced | 475 | dpacc = (des_data.designcap >> 5) * chg_V_low / des_data.designcap; |
fneirab | 9:f29d5e49b190 | 476 | ret = writeReg(DPACC_REG, dpacc); |
fneirab | 9:f29d5e49b190 | 477 | ret = writeReg(MODELCFG_REG, param_EZ_FG2); |
fneirab | 7:479a36909ced | 478 | } |
fneirab | 7:479a36909ced | 479 | |
fneirab | 7:479a36909ced | 480 | |
fneirab | 7:479a36909ced | 481 | return ret; |
fneirab | 7:479a36909ced | 482 | |
fneirab | 7:479a36909ced | 483 | } |
fneirab | 7:479a36909ced | 484 | |
fneirab | 7:479a36909ced | 485 | |
fneirab | 7:479a36909ced | 486 | //////////////////////////////////////////////////////////////////////////////// |
fneirab | 7:479a36909ced | 487 | |
fneirab | 7:479a36909ced | 488 | /** |
fneirab | 7:479a36909ced | 489 | * \brief Get State Of Charge(SOC) Function for MAX17055 |
fneirab | 7:479a36909ced | 490 | * \par Details |
fneirab | 7:479a36909ced | 491 | * This function sends a request to access the internal |
fneirab | 7:479a36909ced | 492 | * of the MAX17055 |
fneirab | 7:479a36909ced | 493 | * |
fneirab | 7:479a36909ced | 494 | * \param[in] reg_addr - register address |
fneirab | 7:479a36909ced | 495 | * \param[out] reg_data - SOC data from the REPSOC_REG register |
fneirab | 7:479a36909ced | 496 | * \retval 1 on success |
fneirab | 7:479a36909ced | 497 | * |
fneirab | 7:479a36909ced | 498 | * -1 if errors exist |
fneirab | 7:479a36909ced | 499 | */ |
fneirab | 7:479a36909ced | 500 | |
fneirab | 7:479a36909ced | 501 | |
fneirab | 7:479a36909ced | 502 | int MAX17055::get_SOC() |
fneirab | 7:479a36909ced | 503 | { |
fneirab | 7:479a36909ced | 504 | |
fneirab | 7:479a36909ced | 505 | int ret; |
fneirab | 7:479a36909ced | 506 | uint16_t data; |
fneirab | 7:479a36909ced | 507 | |
fneirab | 7:479a36909ced | 508 | ret = readReg(REPSOC_REG, data); |
fneirab | 7:479a36909ced | 509 | if (ret < 0) |
fneirab | 7:479a36909ced | 510 | return ret; |
fneirab | 7:479a36909ced | 511 | |
fneirab | 7:479a36909ced | 512 | data = data >> 8; /* RepSOC LSB: 1/256 % */ |
fneirab | 7:479a36909ced | 513 | |
fneirab | 7:479a36909ced | 514 | return data; |
fneirab | 7:479a36909ced | 515 | } |
fneirab | 7:479a36909ced | 516 | |
fneirab | 8:ca8765c30ed2 | 517 | //////////////////////////////////////////////////////////////////////////////// |
fneirab | 8:ca8765c30ed2 | 518 | |
fneirab | 8:ca8765c30ed2 | 519 | /** |
fneirab | 8:ca8765c30ed2 | 520 | * \brief Get Average State Of Charge(SOC) Function for MAX17055 |
fneirab | 8:ca8765c30ed2 | 521 | * \par Details |
fneirab | 8:ca8765c30ed2 | 522 | * This function sends a request to access the internal |
fneirab | 8:ca8765c30ed2 | 523 | * of the MAX17055 |
fneirab | 8:ca8765c30ed2 | 524 | * |
fneirab | 8:ca8765c30ed2 | 525 | * \param[in] reg_addr - register address |
fneirab | 8:ca8765c30ed2 | 526 | * \param[out] |
fneirab | 8:ca8765c30ed2 | 527 | * \retval data - avSOC data from the AVSOC_REG register |
fneirab | 8:ca8765c30ed2 | 528 | * |
fneirab | 8:ca8765c30ed2 | 529 | */ |
fneirab | 8:ca8765c30ed2 | 530 | |
fneirab | 8:ca8765c30ed2 | 531 | |
fneirab | 8:ca8765c30ed2 | 532 | int MAX17055::get_avSOC() |
fneirab | 8:ca8765c30ed2 | 533 | { |
fneirab | 8:ca8765c30ed2 | 534 | |
fneirab | 8:ca8765c30ed2 | 535 | int ret; |
fneirab | 8:ca8765c30ed2 | 536 | uint16_t data; |
fneirab | 8:ca8765c30ed2 | 537 | |
fneirab | 8:ca8765c30ed2 | 538 | ret = readReg(AVSOC_REG, data); |
fneirab | 8:ca8765c30ed2 | 539 | if (ret < 0) |
fneirab | 8:ca8765c30ed2 | 540 | return ret; |
fneirab | 8:ca8765c30ed2 | 541 | |
fneirab | 8:ca8765c30ed2 | 542 | data = data >> 8; /* avSOC LSB: 1/256 % */ |
fneirab | 8:ca8765c30ed2 | 543 | |
fneirab | 8:ca8765c30ed2 | 544 | return data; |
fneirab | 8:ca8765c30ed2 | 545 | } |
fneirab | 7:479a36909ced | 546 | |
fneirab | 9:f29d5e49b190 | 547 | //////////////////////////////////////////////////////////////////////////////// |
fneirab | 9:f29d5e49b190 | 548 | |
fneirab | 9:f29d5e49b190 | 549 | /** |
fneirab | 9:f29d5e49b190 | 550 | * \brief Get State Of Charge(SOC) Function for MAX17055 |
fneirab | 9:f29d5e49b190 | 551 | * \par Details |
fneirab | 9:f29d5e49b190 | 552 | * This function sends a request to access the internal |
fneirab | 9:f29d5e49b190 | 553 | * of the MAX17055 |
fneirab | 9:f29d5e49b190 | 554 | * |
fneirab | 9:f29d5e49b190 | 555 | * \param[in] reg_addr - register address |
fneirab | 9:f29d5e49b190 | 556 | * \param[out] reg_data - SOC data from the REPSOC_REG register |
fneirab | 9:f29d5e49b190 | 557 | * \retval 1 on success |
fneirab | 9:f29d5e49b190 | 558 | * |
fneirab | 9:f29d5e49b190 | 559 | * -1 if errors exist |
fneirab | 9:f29d5e49b190 | 560 | */ |
fneirab | 9:f29d5e49b190 | 561 | |
fneirab | 9:f29d5e49b190 | 562 | |
fneirab | 9:f29d5e49b190 | 563 | int MAX17055::get_mixSOC() |
fneirab | 9:f29d5e49b190 | 564 | { |
fneirab | 9:f29d5e49b190 | 565 | |
fneirab | 9:f29d5e49b190 | 566 | int ret; |
fneirab | 9:f29d5e49b190 | 567 | uint16_t data; |
fneirab | 9:f29d5e49b190 | 568 | |
fneirab | 9:f29d5e49b190 | 569 | ret = readReg(MIXSOC_REG, data); |
fneirab | 9:f29d5e49b190 | 570 | if (ret < 0) |
fneirab | 9:f29d5e49b190 | 571 | return ret; |
fneirab | 9:f29d5e49b190 | 572 | |
fneirab | 9:f29d5e49b190 | 573 | data = data >> 8; /* RepSOC LSB: 1/256 % */ |
fneirab | 9:f29d5e49b190 | 574 | |
fneirab | 9:f29d5e49b190 | 575 | return data; |
fneirab | 9:f29d5e49b190 | 576 | } |
fneirab | 7:479a36909ced | 577 | /////////////////////////////////////////////////////////////////////////////// |
fneirab | 7:479a36909ced | 578 | |
fneirab | 7:479a36909ced | 579 | /** |
fneirab | 7:479a36909ced | 580 | * \brief Get the remaining Time to Empty(TTE) Function for MAX17055 |
fneirab | 7:479a36909ced | 581 | * \par Details |
fneirab | 7:479a36909ced | 582 | * This function sends a request to access the internal register |
fneirab | 7:479a36909ced | 583 | * of the MAX17055 |
fneirab | 7:479a36909ced | 584 | * |
fneirab | 7:479a36909ced | 585 | * \retval tte_data - Time to Empty data from the TTE_REG register |
fneirab | 7:479a36909ced | 586 | * |
fneirab | 7:479a36909ced | 587 | * -1 if errors exist |
fneirab | 7:479a36909ced | 588 | */ |
fneirab | 7:479a36909ced | 589 | |
fneirab | 9:f29d5e49b190 | 590 | int MAX17055::get_atTTE() |
fneirab | 7:479a36909ced | 591 | { |
fneirab | 7:479a36909ced | 592 | |
fneirab | 7:479a36909ced | 593 | int ret; |
fneirab | 7:479a36909ced | 594 | uint16_t data; |
fneirab | 7:479a36909ced | 595 | |
fneirab | 9:f29d5e49b190 | 596 | ret = readReg(ATTTE_REG, data); |
fneirab | 7:479a36909ced | 597 | if (ret < 0) |
fneirab | 7:479a36909ced | 598 | return ret; |
fneirab | 7:479a36909ced | 599 | else |
fneirab | 7:479a36909ced | 600 | data = (data * 45) >> 3; /* TTE LSB: 5.625 sec */ |
fneirab | 7:479a36909ced | 601 | |
fneirab | 7:479a36909ced | 602 | return data; |
fneirab | 7:479a36909ced | 603 | } |
fneirab | 7:479a36909ced | 604 | |
fneirab | 8:ca8765c30ed2 | 605 | /////////////////////////////////////////////////////////////////////////////// |
fneirab | 8:ca8765c30ed2 | 606 | |
fneirab | 8:ca8765c30ed2 | 607 | /** |
fneirab | 8:ca8765c30ed2 | 608 | * \brief Get the remaining Time to Full(TTF) Function for MAX17055 |
fneirab | 8:ca8765c30ed2 | 609 | * \par Details |
fneirab | 8:ca8765c30ed2 | 610 | * This function sends a request to access the internal register |
fneirab | 8:ca8765c30ed2 | 611 | * of the MAX17055 |
fneirab | 8:ca8765c30ed2 | 612 | * |
fneirab | 8:ca8765c30ed2 | 613 | * \retval ttf_data - Time to Empty data from the TTF_REG register |
fneirab | 8:ca8765c30ed2 | 614 | * |
fneirab | 8:ca8765c30ed2 | 615 | * -1 if errors exist |
fneirab | 8:ca8765c30ed2 | 616 | */ |
fneirab | 8:ca8765c30ed2 | 617 | |
fneirab | 8:ca8765c30ed2 | 618 | int MAX17055::get_TTF() |
fneirab | 8:ca8765c30ed2 | 619 | { |
fneirab | 8:ca8765c30ed2 | 620 | |
fneirab | 8:ca8765c30ed2 | 621 | int ret; |
fneirab | 8:ca8765c30ed2 | 622 | uint16_t ttf_data; |
fneirab | 8:ca8765c30ed2 | 623 | |
fneirab | 8:ca8765c30ed2 | 624 | ret = readReg(TTF_REG, ttf_data); |
fneirab | 8:ca8765c30ed2 | 625 | if (ret < 0) |
fneirab | 8:ca8765c30ed2 | 626 | return ret; |
fneirab | 8:ca8765c30ed2 | 627 | else |
fneirab | 8:ca8765c30ed2 | 628 | ttf_data = (ttf_data * 45) >> 3; /* TTF LSB: 5.625 sec */ |
fneirab | 8:ca8765c30ed2 | 629 | |
fneirab | 8:ca8765c30ed2 | 630 | return ttf_data; |
fneirab | 8:ca8765c30ed2 | 631 | } |
fneirab | 7:479a36909ced | 632 | |
fneirab | 7:479a36909ced | 633 | //////////////////////////////////////////////////////////////////////////// |
fneirab | 7:479a36909ced | 634 | |
fneirab | 7:479a36909ced | 635 | /** |
fneirab | 7:479a36909ced | 636 | * \brief Get voltage of the cell Function for MAX17055 |
fneirab | 7:479a36909ced | 637 | * \par Details |
fneirab | 7:479a36909ced | 638 | * This function sends a request to access the internal register |
fneirab | 7:479a36909ced | 639 | * of the MAX17055 to read the voltage of the cell |
fneirab | 7:479a36909ced | 640 | * |
fneirab | 7:479a36909ced | 641 | * \retval vcell_data - vcell data from the VCELL_REG register |
fneirab | 7:479a36909ced | 642 | * |
fneirab | 7:479a36909ced | 643 | * -1 if errors exist |
fneirab | 7:479a36909ced | 644 | */ |
fneirab | 7:479a36909ced | 645 | |
fneirab | 7:479a36909ced | 646 | |
fneirab | 7:479a36909ced | 647 | int MAX17055::get_Vcell() |
fneirab | 7:479a36909ced | 648 | { |
fneirab | 7:479a36909ced | 649 | |
fneirab | 7:479a36909ced | 650 | int ret; |
fneirab | 7:479a36909ced | 651 | uint16_t vcell_data; |
fneirab | 7:479a36909ced | 652 | |
fneirab | 7:479a36909ced | 653 | ret = readReg(VCELL_REG, vcell_data); |
fneirab | 7:479a36909ced | 654 | if (ret < 0) |
fneirab | 7:479a36909ced | 655 | return ret; |
fneirab | 7:479a36909ced | 656 | else |
fneirab | 7:479a36909ced | 657 | //printf("Vcell Reg= %d \r\n",vcell_data); |
fneirab | 7:479a36909ced | 658 | ret = lsb_to_uvolts(vcell_data); |
fneirab | 7:479a36909ced | 659 | //printf("Vcell Conv= %d \r\n",ret); |
fneirab | 7:479a36909ced | 660 | return ret; |
fneirab | 7:479a36909ced | 661 | |
fneirab | 7:479a36909ced | 662 | } |
fneirab | 7:479a36909ced | 663 | |
fneirab | 7:479a36909ced | 664 | |
fneirab | 7:479a36909ced | 665 | //////////////////////////////////////////////////////////////////////////// |
fneirab | 7:479a36909ced | 666 | |
fneirab | 7:479a36909ced | 667 | /** |
fneirab | 7:479a36909ced | 668 | * \brief Get current Function for MAX17055 |
fneirab | 7:479a36909ced | 669 | * \par Details |
fneirab | 7:479a36909ced | 670 | * This function sends a request to access the internal register |
fneirab | 7:479a36909ced | 671 | * of the MAX17055 to read the current register. |
fneirab | 7:479a36909ced | 672 | * |
fneirab | 7:479a36909ced | 673 | * \retval curr_data - vcell data from the VCELL_REG register |
fneirab | 7:479a36909ced | 674 | * |
fneirab | 7:479a36909ced | 675 | * -1 if errors exist |
fneirab | 7:479a36909ced | 676 | */ |
fneirab | 7:479a36909ced | 677 | |
fneirab | 7:479a36909ced | 678 | |
fneirab | 7:479a36909ced | 679 | int MAX17055::get_Current( platform_data des_data ) |
fneirab | 7:479a36909ced | 680 | { |
fneirab | 7:479a36909ced | 681 | |
fneirab | 7:479a36909ced | 682 | int ret,design_rsense; |
fneirab | 7:479a36909ced | 683 | uint16_t data; |
fneirab | 7:479a36909ced | 684 | |
fneirab | 7:479a36909ced | 685 | ret = readReg(CURRENT_REG, data); |
fneirab | 7:479a36909ced | 686 | if (ret < 0) |
fneirab | 7:479a36909ced | 687 | return ret; |
fneirab | 7:479a36909ced | 688 | else |
fneirab | 7:479a36909ced | 689 | design_rsense = des_data.rsense; |
fneirab | 7:479a36909ced | 690 | ret = raw_current_to_uamps((uint32_t)data, design_rsense); |
fneirab | 7:479a36909ced | 691 | return ret; |
fneirab | 7:479a36909ced | 692 | |
fneirab | 7:479a36909ced | 693 | } |
fneirab | 7:479a36909ced | 694 | |
fneirab | 7:479a36909ced | 695 | |
fneirab | 7:479a36909ced | 696 | //////////////////////////////////////////////////////////////////////////// |
fneirab | 7:479a36909ced | 697 | |
fneirab | 7:479a36909ced | 698 | /** |
fneirab | 7:479a36909ced | 699 | * \brief Get Average Current Function for MAX17055 |
fneirab | 7:479a36909ced | 700 | * \par Details |
fneirab | 7:479a36909ced | 701 | * This function sends a request to access the internal register |
fneirab | 7:479a36909ced | 702 | * of the MAX17055 to read the average current register. |
fneirab | 7:479a36909ced | 703 | * |
fneirab | 7:479a36909ced | 704 | * \retval curr_data - vcell data from the AVGCURRENT_REG register |
fneirab | 7:479a36909ced | 705 | * |
fneirab | 7:479a36909ced | 706 | * -1 if errors exist |
fneirab | 7:479a36909ced | 707 | */ |
fneirab | 7:479a36909ced | 708 | |
fneirab | 7:479a36909ced | 709 | |
fneirab | 7:479a36909ced | 710 | int MAX17055::get_AvgCurrent( platform_data des_data ) |
fneirab | 7:479a36909ced | 711 | { |
fneirab | 7:479a36909ced | 712 | |
fneirab | 7:479a36909ced | 713 | int ret, design_rsense; |
fneirab | 7:479a36909ced | 714 | uint16_t data; |
fneirab | 7:479a36909ced | 715 | uint32_t aveCurr_data; |
fneirab | 7:479a36909ced | 716 | |
fneirab | 7:479a36909ced | 717 | ret = readReg(AVGCURRENT_REG, data); |
fneirab | 7:479a36909ced | 718 | if (ret < 0) |
fneirab | 7:479a36909ced | 719 | return ret; |
fneirab | 7:479a36909ced | 720 | else |
fneirab | 7:479a36909ced | 721 | aveCurr_data = data; |
fneirab | 7:479a36909ced | 722 | design_rsense = des_data.rsense; |
fneirab | 7:479a36909ced | 723 | aveCurr_data = raw_current_to_uamps(aveCurr_data, design_rsense); |
fneirab | 7:479a36909ced | 724 | return aveCurr_data; |
fneirab | 7:479a36909ced | 725 | |
fneirab | 7:479a36909ced | 726 | } |
fneirab | 7:479a36909ced | 727 | |
fneirab | 7:479a36909ced | 728 | /////////////////////////////////////////////////////////////////////////////// |
fneirab | 7:479a36909ced | 729 | |
fneirab | 7:479a36909ced | 730 | /** |
fneirab | 7:479a36909ced | 731 | * \brief lsb_to_uvolts Converssion Function |
fneirab | 7:479a36909ced | 732 | * \par Details |
fneirab | 7:479a36909ced | 733 | * This function takes the lsb value of the register and convert it |
fneirab | 7:479a36909ced | 734 | * to uvolts |
fneirab | 7:479a36909ced | 735 | * |
fneirab | 7:479a36909ced | 736 | * \param[in] lsb - value of register lsb |
fneirab | 7:479a36909ced | 737 | * \retval lsb - converted lsb to uvolts |
fneirab | 7:479a36909ced | 738 | * |
fneirab | 7:479a36909ced | 739 | */ |
fneirab | 7:479a36909ced | 740 | |
fneirab | 7:479a36909ced | 741 | int MAX17055:: lsb_to_uvolts(uint16_t lsb) |
fneirab | 7:479a36909ced | 742 | { |
fneirab | 7:479a36909ced | 743 | int store; |
fneirab | 7:479a36909ced | 744 | store = (lsb * 625) / 8; /* 78.125uV per bit */ |
fneirab | 7:479a36909ced | 745 | return store; |
fneirab | 7:479a36909ced | 746 | } |
fneirab | 7:479a36909ced | 747 | |
fneirab | 7:479a36909ced | 748 | |
fneirab | 7:479a36909ced | 749 | /////////////////////////////////////////////////////////////////////////////// |
fneirab | 7:479a36909ced | 750 | |
fneirab | 7:479a36909ced | 751 | /** |
fneirab | 7:479a36909ced | 752 | * \brief raw_current_to_uamp Converssion Function |
fneirab | 7:479a36909ced | 753 | * \par Details |
fneirab | 7:479a36909ced | 754 | * This function takes the raw current value of the register and |
fneirab | 7:479a36909ced | 755 | * converts it to uamps |
fneirab | 7:479a36909ced | 756 | * |
fneirab | 7:479a36909ced | 757 | * \param[in] curr - raw current value of register |
fneirab | 7:479a36909ced | 758 | * \retval res - converted raw current to uamps - Signed 2's complement |
fneirab | 7:479a36909ced | 759 | * |
fneirab | 7:479a36909ced | 760 | */ |
fneirab | 7:479a36909ced | 761 | |
fneirab | 7:479a36909ced | 762 | int MAX17055::raw_current_to_uamps(uint32_t curr, int rsense_value) |
fneirab | 7:479a36909ced | 763 | { |
fneirab | 7:479a36909ced | 764 | int res = curr; |
fneirab | 7:479a36909ced | 765 | /* Negative */ |
fneirab | 7:479a36909ced | 766 | if (res & 0x8000) { |
fneirab | 7:479a36909ced | 767 | res |= 0xFFFF0000; |
fneirab | 7:479a36909ced | 768 | } else { |
fneirab | 7:479a36909ced | 769 | res *= 1562500 /(rsense_value * 1000); //Change to interact with the rsense of customer |
fneirab | 7:479a36909ced | 770 | } |
fneirab | 7:479a36909ced | 771 | return res; |
fneirab | 9:f29d5e49b190 | 772 | } |
fneirab | 9:f29d5e49b190 | 773 | |
fneirab | 9:f29d5e49b190 | 774 | /////////////////////////////////////////////////////////////////////////////// |
fneirab | 9:f29d5e49b190 | 775 | |
fneirab | 9:f29d5e49b190 | 776 | /** |
fneirab | 9:f29d5e49b190 | 777 | * \brief Save Learned Parameters Function |
fneirab | 9:f29d5e49b190 | 778 | * \par Details |
fneirab | 9:f29d5e49b190 | 779 | * It is recommended to save the learned capacity parameters every |
fneirab | 9:f29d5e49b190 | 780 | * time bit 2 of the Cycles register toggles |
fneirab | 9:f29d5e49b190 | 781 | * (so that it is saved every 64% change in the battery) |
fneirab | 9:f29d5e49b190 | 782 | * so that if power is lost the values can easily be restored. |
fneirab | 9:f29d5e49b190 | 783 | * |
fneirab | 9:f29d5e49b190 | 784 | * \retval ret int -1 if fail |
fneirab | 9:f29d5e49b190 | 785 | * |
fneirab | 9:f29d5e49b190 | 786 | */ |
fneirab | 9:f29d5e49b190 | 787 | |
fneirab | 9:f29d5e49b190 | 788 | int MAX17055::save_Params(saved_FG_params_t FG_params) |
fneirab | 9:f29d5e49b190 | 789 | { |
fneirab | 9:f29d5e49b190 | 790 | int ret; |
fneirab | 9:f29d5e49b190 | 791 | uint16_t data[5]; |
fneirab | 9:f29d5e49b190 | 792 | ret = readReg(RCOMP0_REG, data[0]); |
fneirab | 9:f29d5e49b190 | 793 | if (ret < 0) |
fneirab | 9:f29d5e49b190 | 794 | return ret; |
fneirab | 9:f29d5e49b190 | 795 | else |
fneirab | 9:f29d5e49b190 | 796 | FG_params.rcomp0 = data[0]; |
fneirab | 9:f29d5e49b190 | 797 | |
fneirab | 9:f29d5e49b190 | 798 | ret = readReg(TEMPCO_REG, data[1]); |
fneirab | 9:f29d5e49b190 | 799 | if (ret < 0) |
fneirab | 9:f29d5e49b190 | 800 | return ret; |
fneirab | 9:f29d5e49b190 | 801 | else |
fneirab | 9:f29d5e49b190 | 802 | FG_params.temp_co = data[1]; |
fneirab | 9:f29d5e49b190 | 803 | |
fneirab | 9:f29d5e49b190 | 804 | ret = readReg(FULLCAPREP_REG, data[2]); |
fneirab | 9:f29d5e49b190 | 805 | if (ret < 0) |
fneirab | 9:f29d5e49b190 | 806 | return ret; |
fneirab | 9:f29d5e49b190 | 807 | else |
fneirab | 9:f29d5e49b190 | 808 | FG_params.full_cap_rep = data[2]; |
fneirab | 9:f29d5e49b190 | 809 | |
fneirab | 9:f29d5e49b190 | 810 | ret = readReg(CYCLES_REG, data[3]); |
fneirab | 9:f29d5e49b190 | 811 | if (ret < 0) |
fneirab | 9:f29d5e49b190 | 812 | return ret; |
fneirab | 9:f29d5e49b190 | 813 | else |
fneirab | 9:f29d5e49b190 | 814 | FG_params.cycles = data[3]; |
fneirab | 9:f29d5e49b190 | 815 | |
fneirab | 9:f29d5e49b190 | 816 | ret = readReg(FULLCAPNOM_REG, data[4]); |
fneirab | 9:f29d5e49b190 | 817 | if (ret < 0) |
fneirab | 9:f29d5e49b190 | 818 | return ret; |
fneirab | 9:f29d5e49b190 | 819 | else |
fneirab | 9:f29d5e49b190 | 820 | FG_params.full_cap_nom = data[4]; |
fneirab | 9:f29d5e49b190 | 821 | return ret; |
fneirab | 9:f29d5e49b190 | 822 | } |
fneirab | 9:f29d5e49b190 | 823 | |
fneirab | 9:f29d5e49b190 | 824 | /////////////////////////////////////////////////////////////////////////////// |
fneirab | 9:f29d5e49b190 | 825 | |
fneirab | 9:f29d5e49b190 | 826 | /** |
fneirab | 9:f29d5e49b190 | 827 | * \brief Resotore Parameters Function |
fneirab | 9:f29d5e49b190 | 828 | * \par Details |
fneirab | 9:f29d5e49b190 | 829 | * If power is lost, then the capacity information |
fneirab | 9:f29d5e49b190 | 830 | * can be easily restored with this function |
fneirab | 9:f29d5e49b190 | 831 | * |
fneirab | 9:f29d5e49b190 | 832 | * \param[in] struct saved_FG_params_t |
fneirab | 9:f29d5e49b190 | 833 | * \retval void |
fneirab | 9:f29d5e49b190 | 834 | * |
fneirab | 9:f29d5e49b190 | 835 | */ |
fneirab | 9:f29d5e49b190 | 836 | |
fneirab | 9:f29d5e49b190 | 837 | int MAX17055::restore_Params(saved_FG_params_t FG_params) |
fneirab | 9:f29d5e49b190 | 838 | { |
fneirab | 9:f29d5e49b190 | 839 | int ret; |
fneirab | 9:f29d5e49b190 | 840 | uint16_t temp_data, fullcapnom_data, mixCap_calc, dQacc_calc; |
fneirab | 9:f29d5e49b190 | 841 | uint16_t dPacc_value = 0x0C80;//Why this vcalue? |
fneirab | 9:f29d5e49b190 | 842 | |
fneirab | 9:f29d5e49b190 | 843 | //Restoring capacity parameters |
fneirab | 9:f29d5e49b190 | 844 | write_and_verify_reg(RCOMP0_REG, FG_params.rcomp0); |
fneirab | 9:f29d5e49b190 | 845 | write_and_verify_reg(TEMPCO_REG, FG_params.temp_co); |
fneirab | 9:f29d5e49b190 | 846 | write_and_verify_reg(FULLCAPNOM_REG, FG_params.full_cap_nom); |
fneirab | 9:f29d5e49b190 | 847 | |
fneirab | 9:f29d5e49b190 | 848 | wait_ms(350); |
fneirab | 9:f29d5e49b190 | 849 | //Restore FullCap |
fneirab | 9:f29d5e49b190 | 850 | |
fneirab | 9:f29d5e49b190 | 851 | ret = readReg(FULLCAPNOM_REG, fullcapnom_data); |
fneirab | 9:f29d5e49b190 | 852 | if (ret < 0) |
fneirab | 9:f29d5e49b190 | 853 | return ret; |
fneirab | 9:f29d5e49b190 | 854 | |
fneirab | 9:f29d5e49b190 | 855 | ret = readReg(MIXSOC_REG, temp_data); //check if Error in sofware guide register incorrect |
fneirab | 9:f29d5e49b190 | 856 | if (ret < 0) |
fneirab | 9:f29d5e49b190 | 857 | return ret; |
fneirab | 9:f29d5e49b190 | 858 | |
fneirab | 9:f29d5e49b190 | 859 | mixCap_calc = (temp_data*fullcapnom_data)/25600; |
fneirab | 9:f29d5e49b190 | 860 | |
fneirab | 9:f29d5e49b190 | 861 | write_and_verify_reg(MIXCAP_REG, mixCap_calc); |
fneirab | 9:f29d5e49b190 | 862 | write_and_verify_reg(FULLCAPREP_REG, FG_params.full_cap_rep); |
fneirab | 9:f29d5e49b190 | 863 | |
fneirab | 9:f29d5e49b190 | 864 | //Write DQACC to 200% of Capacity and DPACC to 200% |
fneirab | 9:f29d5e49b190 | 865 | dQacc_calc = (FG_params.full_cap_nom/ 16) ; |
fneirab | 9:f29d5e49b190 | 866 | |
fneirab | 9:f29d5e49b190 | 867 | write_and_verify_reg(DPACC_REG, dPacc_value); |
fneirab | 9:f29d5e49b190 | 868 | write_and_verify_reg(DQACC_REG, dQacc_calc); |
fneirab | 9:f29d5e49b190 | 869 | |
fneirab | 9:f29d5e49b190 | 870 | wait_ms(350); |
fneirab | 9:f29d5e49b190 | 871 | |
fneirab | 9:f29d5e49b190 | 872 | //Restore Cycles register |
fneirab | 9:f29d5e49b190 | 873 | ret = write_and_verify_reg(CYCLES_REG, FG_params.cycles); |
fneirab | 9:f29d5e49b190 | 874 | if (ret < 0) |
fneirab | 9:f29d5e49b190 | 875 | return ret; |
fneirab | 9:f29d5e49b190 | 876 | return ret; |
fneirab | 7:479a36909ced | 877 | } |