A feature complete driver for the MAX17048 lithium fuel gauge from Maxim.

Dependents:   MAX17048_HelloWorld ECGAFE_copy MAX17048_HelloWorld Orion_newPCB_test_LV ... more

Now fully tested!

Committer:
neilt6
Date:
Wed Aug 28 17:53:53 2013 +0000
Revision:
3:32087cca331f
Parent:
2:0a98e081b48c
Child:
4:e61b2723d2cf
Added open() method to probe for devices and load the default RCOMP value

Who changed what in which revision?

UserRevisionLine numberNew contents of line
neilt6 0:abc480f8eeab 1 /* MAX17048 Driver Library
neilt6 0:abc480f8eeab 2 * Copyright (c) 2013 Neil Thiessen
neilt6 0:abc480f8eeab 3 *
neilt6 0:abc480f8eeab 4 * Licensed under the Apache License, Version 2.0 (the "License");
neilt6 0:abc480f8eeab 5 * you may not use this file except in compliance with the License.
neilt6 0:abc480f8eeab 6 * You may obtain a copy of the License at
neilt6 0:abc480f8eeab 7 *
neilt6 0:abc480f8eeab 8 * http://www.apache.org/licenses/LICENSE-2.0
neilt6 0:abc480f8eeab 9 *
neilt6 0:abc480f8eeab 10 * Unless required by applicable law or agreed to in writing, software
neilt6 0:abc480f8eeab 11 * distributed under the License is distributed on an "AS IS" BASIS,
neilt6 0:abc480f8eeab 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
neilt6 0:abc480f8eeab 13 * See the License for the specific language governing permissions and
neilt6 0:abc480f8eeab 14 * limitations under the License.
neilt6 0:abc480f8eeab 15 */
neilt6 0:abc480f8eeab 16
neilt6 0:abc480f8eeab 17 #ifndef MAX17048_H
neilt6 0:abc480f8eeab 18 #define MAX17048_H
neilt6 0:abc480f8eeab 19
neilt6 0:abc480f8eeab 20 #include "mbed.h"
neilt6 0:abc480f8eeab 21
neilt6 0:abc480f8eeab 22 /** MAX17048 class.
neilt6 0:abc480f8eeab 23 * Used for controlling a MAX17048 fuel gauge connected via I2C.
neilt6 0:abc480f8eeab 24 *
neilt6 0:abc480f8eeab 25 * Example:
neilt6 0:abc480f8eeab 26 * @code
neilt6 0:abc480f8eeab 27 * #include "mbed.h"
neilt6 0:abc480f8eeab 28 * #include "MAX17048.h"
neilt6 0:abc480f8eeab 29 *
neilt6 0:abc480f8eeab 30 * MAX17048 gauge(p28, p27);
neilt6 0:abc480f8eeab 31 *
neilt6 3:32087cca331f 32 * int main()
neilt6 3:32087cca331f 33 * {
neilt6 3:32087cca331f 34 * //Try to open the MAX17048
neilt6 3:32087cca331f 35 * if (gauge.open()) {
neilt6 3:32087cca331f 36 * printf("Device detected!\n");
neilt6 0:abc480f8eeab 37 *
neilt6 3:32087cca331f 38 * while (1) {
neilt6 3:32087cca331f 39 * //Read the cell voltage
neilt6 3:32087cca331f 40 * float vcell = gauge.vcell();
neilt6 0:abc480f8eeab 41 *
neilt6 3:32087cca331f 42 * //Print the cell voltage
neilt6 3:32087cca331f 43 * printf("Vcell = %f\n", vcell);
neilt6 3:32087cca331f 44 *
neilt6 3:32087cca331f 45 * //Sleep for 0.5 seconds
neilt6 3:32087cca331f 46 * wait(0.5);
neilt6 3:32087cca331f 47 * }
neilt6 3:32087cca331f 48 * } else {
neilt6 3:32087cca331f 49 * printf("Device not detected!\n");
neilt6 0:abc480f8eeab 50 * }
neilt6 0:abc480f8eeab 51 * }
neilt6 0:abc480f8eeab 52 * @endcode
neilt6 0:abc480f8eeab 53 */
neilt6 0:abc480f8eeab 54 class MAX17048
neilt6 0:abc480f8eeab 55 {
neilt6 0:abc480f8eeab 56 public:
neilt6 0:abc480f8eeab 57 /** Represents the different alert flags for the MAX17048
neilt6 0:abc480f8eeab 58 */
neilt6 0:abc480f8eeab 59 enum AlertFlags {
neilt6 0:abc480f8eeab 60 ALERT_RI = (1 << 0), /**< Reset indicator */
neilt6 0:abc480f8eeab 61 ALERT_VH = (1 << 1), /**< Voltage high alert */
neilt6 0:abc480f8eeab 62 ALERT_VL = (1 << 2), /**< Voltage low alert */
neilt6 0:abc480f8eeab 63 ALERT_VR = (1 << 3), /**< Voltage reset alert */
neilt6 0:abc480f8eeab 64 ALERT_HD = (1 << 4), /**< SOC low alert */
neilt6 0:abc480f8eeab 65 ALERT_SC = (1 << 5) /**< SOC change alert */
neilt6 0:abc480f8eeab 66 };
neilt6 0:abc480f8eeab 67
neilt6 0:abc480f8eeab 68 /** Create a MAX17048 object connected to the specified I2C pins
neilt6 0:abc480f8eeab 69 *
neilt6 1:734b1a089a9c 70 * @param sda The I2C data pin.
neilt6 1:734b1a089a9c 71 * @param scl The I2C clock pin.
neilt6 0:abc480f8eeab 72 */
neilt6 0:abc480f8eeab 73 MAX17048(PinName sda, PinName scl);
neilt6 0:abc480f8eeab 74
neilt6 3:32087cca331f 75 /** Probe for the MAX17048 and load the default RCOMP value if present
neilt6 3:32087cca331f 76 *
neilt6 3:32087cca331f 77 * @returns
neilt6 3:32087cca331f 78 * 'true' if the device exists on the bus,
neilt6 3:32087cca331f 79 * 'false' if the device doesn't exist on the bus.
neilt6 3:32087cca331f 80 */
neilt6 3:32087cca331f 81 bool open(void);
neilt6 3:32087cca331f 82
neilt6 0:abc480f8eeab 83 /** Command the MAX17048 to perform a power-on reset
neilt6 0:abc480f8eeab 84 */
neilt6 0:abc480f8eeab 85 void reset(void);
neilt6 0:abc480f8eeab 86
neilt6 0:abc480f8eeab 87 /** Command the MAX17048 to perform a QuickStart
neilt6 0:abc480f8eeab 88 */
neilt6 0:abc480f8eeab 89 void quickStart(void);
neilt6 0:abc480f8eeab 90
neilt6 0:abc480f8eeab 91 /** Determine whether sleep mode is enabled on the MAX17048
neilt6 0:abc480f8eeab 92 *
neilt6 0:abc480f8eeab 93 * @returns
neilt6 0:abc480f8eeab 94 * 'true' if sleep mode is enabled,
neilt6 0:abc480f8eeab 95 * 'false' if sleep mode is disabled.
neilt6 0:abc480f8eeab 96 */
neilt6 2:0a98e081b48c 97 bool sleepEnabled(void);
neilt6 0:abc480f8eeab 98
neilt6 0:abc480f8eeab 99 /** Enable or disable sleep mode on the MAX17048
neilt6 0:abc480f8eeab 100 *
neilt6 0:abc480f8eeab 101 * @param enabled Whether or not sleep mode is enabled.
neilt6 0:abc480f8eeab 102 */
neilt6 2:0a98e081b48c 103 void sleepEnabled(bool enabled);
neilt6 2:0a98e081b48c 104
neilt6 0:abc480f8eeab 105 /** Determine whether or not the MAX17048 is hibernating
neilt6 0:abc480f8eeab 106 *
neilt6 0:abc480f8eeab 107 * @returns
neilt6 0:abc480f8eeab 108 * 'true' if hibernating,
neilt6 0:abc480f8eeab 109 * 'false' if not hibernating.
neilt6 0:abc480f8eeab 110 */
neilt6 2:0a98e081b48c 111 bool hibernating(void);
neilt6 2:0a98e081b48c 112
neilt6 0:abc480f8eeab 113 /** Get the current hibernate threshold of the MAX17048
neilt6 0:abc480f8eeab 114 *
neilt6 0:abc480f8eeab 115 * @returns The current hibernate threshold in \%/hr.
neilt6 0:abc480f8eeab 116 */
neilt6 2:0a98e081b48c 117 float hibernateThreshold(void);
neilt6 2:0a98e081b48c 118
neilt6 0:abc480f8eeab 119 /** Set the hibernate threshold of the MAX17048
neilt6 0:abc480f8eeab 120 *
neilt6 0:abc480f8eeab 121 * @param threshold The new hibernate threshold in \%/hr.
neilt6 0:abc480f8eeab 122 */
neilt6 2:0a98e081b48c 123 void hibernateThreshold(float threshold);
neilt6 2:0a98e081b48c 124
neilt6 0:abc480f8eeab 125 /** Get the current active threshold of the MAX17048
neilt6 0:abc480f8eeab 126 *
neilt6 0:abc480f8eeab 127 * @returns The current active threshold in volts.
neilt6 0:abc480f8eeab 128 */
neilt6 2:0a98e081b48c 129 float activeThreshold(void);
neilt6 2:0a98e081b48c 130
neilt6 0:abc480f8eeab 131 /** Set the active threshold of the MAX17048
neilt6 0:abc480f8eeab 132 *
neilt6 0:abc480f8eeab 133 * @param threshold The new active threshold in volts.
neilt6 0:abc480f8eeab 134 */
neilt6 2:0a98e081b48c 135 void activeThreshold(float threshold);
neilt6 2:0a98e081b48c 136
neilt6 0:abc480f8eeab 137 /** Get the production version of the MAX17048
neilt6 0:abc480f8eeab 138 *
neilt6 0:abc480f8eeab 139 * @returns The 16-bit production version.
neilt6 0:abc480f8eeab 140 */
neilt6 2:0a98e081b48c 141 unsigned short version(void);
neilt6 2:0a98e081b48c 142
neilt6 0:abc480f8eeab 143 /** Set the cell temperature compensation of the MAX17048
neilt6 0:abc480f8eeab 144 *
neilt6 0:abc480f8eeab 145 * @param temp The current cell temperature in °C.
neilt6 0:abc480f8eeab 146 */
neilt6 2:0a98e081b48c 147 void tempCompensation(float temp);
neilt6 2:0a98e081b48c 148
neilt6 0:abc480f8eeab 149 /** Determine whether or not the MAX17048 is in sleep mode
neilt6 0:abc480f8eeab 150 *
neilt6 0:abc480f8eeab 151 * @returns
neilt6 0:abc480f8eeab 152 * 'true' if in sleep mode,
neilt6 0:abc480f8eeab 153 * 'false' if not in sleep mode.
neilt6 0:abc480f8eeab 154 */
neilt6 2:0a98e081b48c 155 bool sleeping(void);
neilt6 2:0a98e081b48c 156
neilt6 0:abc480f8eeab 157 /** Enter or exit sleep mode on the MAX17048 (sleep mode must be enabled first)
neilt6 0:abc480f8eeab 158 *
neilt6 0:abc480f8eeab 159 * @param sleep Whether or not to sleep.
neilt6 0:abc480f8eeab 160 */
neilt6 2:0a98e081b48c 161 void sleep(bool sleep);
neilt6 2:0a98e081b48c 162
neilt6 0:abc480f8eeab 163 /** Determine whether or not the SOC 1% change alert is enabled on the MAX17048
neilt6 0:abc480f8eeab 164 *
neilt6 0:abc480f8eeab 165 * @returns
neilt6 0:abc480f8eeab 166 * 'true' if enabled,
neilt6 0:abc480f8eeab 167 * 'false' if not enabled.
neilt6 0:abc480f8eeab 168 */
neilt6 2:0a98e081b48c 169 bool socChangeAlertEnabled(void);
neilt6 2:0a98e081b48c 170
neilt6 0:abc480f8eeab 171 /** Enable or disable the SOC 1% change alert on the MAX17048
neilt6 0:abc480f8eeab 172 *
neilt6 0:abc480f8eeab 173 * @param enabled Whether or the SOC 1% change alert is enabled.
neilt6 0:abc480f8eeab 174 */
neilt6 2:0a98e081b48c 175 void socChangeAlertEnabled(bool enabled);
neilt6 2:0a98e081b48c 176
neilt6 0:abc480f8eeab 177 /** Determine whether or not the MAX17048 is asserting the ALRT pin
neilt6 0:abc480f8eeab 178 *
neilt6 0:abc480f8eeab 179 * @returns
neilt6 0:abc480f8eeab 180 * 'true' if alerting,
neilt6 0:abc480f8eeab 181 * 'false' if not alerting.
neilt6 0:abc480f8eeab 182 */
neilt6 2:0a98e081b48c 183 bool alerting(void);
neilt6 2:0a98e081b48c 184
neilt6 0:abc480f8eeab 185 /** Command the MAX17048 to de-assert the ALRT pin
neilt6 0:abc480f8eeab 186 */
neilt6 0:abc480f8eeab 187 void clearAlert(void);
neilt6 2:0a98e081b48c 188
neilt6 0:abc480f8eeab 189 /** Get the current SOC empty alert threshold of the MAX17048
neilt6 0:abc480f8eeab 190 *
neilt6 0:abc480f8eeab 191 * @returns The current SOC empty alert threshold in %.
neilt6 0:abc480f8eeab 192 */
neilt6 2:0a98e081b48c 193 char emptyAlertThreshold(void);
neilt6 2:0a98e081b48c 194
neilt6 0:abc480f8eeab 195 /** Set the SOC empty alert threshold of the MAX17048
neilt6 0:abc480f8eeab 196 *
neilt6 0:abc480f8eeab 197 * @param threshold The new SOC empty alert threshold in %.
neilt6 0:abc480f8eeab 198 */
neilt6 2:0a98e081b48c 199 void emptyAlertThreshold(char threshold);
neilt6 2:0a98e081b48c 200
neilt6 0:abc480f8eeab 201 /** Get the current low voltage alert threshold of the MAX17048
neilt6 0:abc480f8eeab 202 *
neilt6 0:abc480f8eeab 203 * @returns The current low voltage alert threshold in volts.
neilt6 0:abc480f8eeab 204 */
neilt6 2:0a98e081b48c 205 float vAlertMinThreshold(void);
neilt6 2:0a98e081b48c 206
neilt6 0:abc480f8eeab 207 /** Set the low voltage alert threshold of the MAX17048
neilt6 0:abc480f8eeab 208 *
neilt6 0:abc480f8eeab 209 * @param threshold The new low voltage alert threshold in volts.
neilt6 0:abc480f8eeab 210 */
neilt6 2:0a98e081b48c 211 void vAlertMinThreshold(float threshold);
neilt6 2:0a98e081b48c 212
neilt6 0:abc480f8eeab 213 /** Get the current high voltage alert threshold of the MAX17048
neilt6 0:abc480f8eeab 214 *
neilt6 0:abc480f8eeab 215 * @returns The current high voltage alert threshold in volts.
neilt6 0:abc480f8eeab 216 */
neilt6 2:0a98e081b48c 217 float vAlertMaxThreshold(void);
neilt6 2:0a98e081b48c 218
neilt6 0:abc480f8eeab 219 /** Set the high voltage alert threshold of the MAX17048
neilt6 0:abc480f8eeab 220 *
neilt6 0:abc480f8eeab 221 * @param threshold The new high voltage alert threshold in volts.
neilt6 0:abc480f8eeab 222 */
neilt6 2:0a98e081b48c 223 void vAlertMaxThreshold(float threshold);
neilt6 2:0a98e081b48c 224
neilt6 0:abc480f8eeab 225 /** Get the current reset voltage threshold of the MAX17048
neilt6 0:abc480f8eeab 226 *
neilt6 0:abc480f8eeab 227 * @returns The current reset voltage threshold in volts.
neilt6 0:abc480f8eeab 228 */
neilt6 2:0a98e081b48c 229 float vResetThreshold(void);
neilt6 2:0a98e081b48c 230
neilt6 0:abc480f8eeab 231 /** Set the reset voltage threshold of the MAX17048
neilt6 0:abc480f8eeab 232 *
neilt6 0:abc480f8eeab 233 * @param threshold The new reset voltage threshold in volts.
neilt6 0:abc480f8eeab 234 */
neilt6 2:0a98e081b48c 235 void vResetThreshold(float threshold);
neilt6 2:0a98e081b48c 236
neilt6 0:abc480f8eeab 237 /** Determine whether or not the reset voltage comparator is enabled on the MAX17048
neilt6 0:abc480f8eeab 238 *
neilt6 0:abc480f8eeab 239 * @returns
neilt6 0:abc480f8eeab 240 * 'true' if enabled,
neilt6 0:abc480f8eeab 241 * 'false' if not enabled.
neilt6 0:abc480f8eeab 242 */
neilt6 2:0a98e081b48c 243 bool comparatorEnabled(void);
neilt6 2:0a98e081b48c 244
neilt6 0:abc480f8eeab 245 /** Enable or disable the reset voltage comparator on the MAX17048
neilt6 0:abc480f8eeab 246 *
neilt6 0:abc480f8eeab 247 * @param enabled Whether or not the reset voltage comparator is enabled.
neilt6 0:abc480f8eeab 248 */
neilt6 2:0a98e081b48c 249 void comparatorEnabled(bool enabled);
neilt6 2:0a98e081b48c 250
neilt6 0:abc480f8eeab 251 /** Get the factory programmed 8-bit ID of the MAX17048
neilt6 0:abc480f8eeab 252 *
neilt6 0:abc480f8eeab 253 * @returns The 8-bit ID.
neilt6 0:abc480f8eeab 254 */
neilt6 2:0a98e081b48c 255 char id(void);
neilt6 2:0a98e081b48c 256
neilt6 0:abc480f8eeab 257 /** Determine whether or not the voltage reset alert is enabled on the MAX17048
neilt6 0:abc480f8eeab 258 *
neilt6 0:abc480f8eeab 259 * @returns
neilt6 0:abc480f8eeab 260 * 'true' if enabled,
neilt6 0:abc480f8eeab 261 * 'false' if not enabled.
neilt6 0:abc480f8eeab 262 */
neilt6 2:0a98e081b48c 263 bool vResetAlertEnabled(void);
neilt6 2:0a98e081b48c 264
neilt6 0:abc480f8eeab 265 /** Enable or disable the voltage reset alert on the MAX17048
neilt6 0:abc480f8eeab 266 *
neilt6 0:abc480f8eeab 267 * @param enabled Whether or the voltage reset alert is enabled.
neilt6 0:abc480f8eeab 268 */
neilt6 2:0a98e081b48c 269 void vResetAlertEnabled(bool enabled);
neilt6 2:0a98e081b48c 270
neilt6 0:abc480f8eeab 271 /** Get the current alert flags on the MAX17048
neilt6 0:abc480f8eeab 272 *
neilt6 0:abc480f8eeab 273 * @returns The current alert flags as AlertFlags enum values OR'd together.
neilt6 0:abc480f8eeab 274 */
neilt6 2:0a98e081b48c 275 char alertFlags(void);
neilt6 2:0a98e081b48c 276
neilt6 0:abc480f8eeab 277 /** Clear the specified alert flags on the MAX17048
neilt6 0:abc480f8eeab 278 *
neilt6 0:abc480f8eeab 279 * @param flags The alert flags to clear as AlertFlags enum values OR'd together.
neilt6 0:abc480f8eeab 280 */
neilt6 0:abc480f8eeab 281 void clearAlertFlags(char flags);
neilt6 0:abc480f8eeab 282
neilt6 0:abc480f8eeab 283 /** Get the current cell voltage measurement of the MAX17048
neilt6 0:abc480f8eeab 284 *
neilt6 0:abc480f8eeab 285 * @returns The cell voltage measurement as a float.
neilt6 0:abc480f8eeab 286 */
neilt6 2:0a98e081b48c 287 float vcell(void);
neilt6 0:abc480f8eeab 288
neilt6 0:abc480f8eeab 289 /** Get the current state of charge measurement of the MAX17048
neilt6 0:abc480f8eeab 290 *
neilt6 0:abc480f8eeab 291 * @returns The state of charge measurement as a float.
neilt6 0:abc480f8eeab 292 */
neilt6 2:0a98e081b48c 293 float soc(void);
neilt6 0:abc480f8eeab 294
neilt6 0:abc480f8eeab 295 /** Get the current C rate measurement of the MAX17048
neilt6 0:abc480f8eeab 296 *
neilt6 0:abc480f8eeab 297 * @returns The C rate measurement as a float.
neilt6 0:abc480f8eeab 298 */
neilt6 2:0a98e081b48c 299 float crate(void);
neilt6 0:abc480f8eeab 300
neilt6 0:abc480f8eeab 301 private:
neilt6 2:0a98e081b48c 302 //I2C register addresses
neilt6 2:0a98e081b48c 303 enum Register {
neilt6 2:0a98e081b48c 304 REG_VCELL = 0x02,
neilt6 2:0a98e081b48c 305 REG_SOC = 0x04,
neilt6 2:0a98e081b48c 306 REG_MODE = 0x06,
neilt6 2:0a98e081b48c 307 REG_VERSION = 0x08,
neilt6 2:0a98e081b48c 308 REG_HIBRT = 0x0A,
neilt6 2:0a98e081b48c 309 REG_CONFIG = 0x0C,
neilt6 2:0a98e081b48c 310 REG_VALRT = 0x14,
neilt6 2:0a98e081b48c 311 REG_CRATE = 0x16,
neilt6 2:0a98e081b48c 312 REG_VRESET_ID = 0x18,
neilt6 2:0a98e081b48c 313 REG_STATUS = 0x1A,
neilt6 2:0a98e081b48c 314 REG_TABLE = 0x40,
neilt6 2:0a98e081b48c 315 REG_CMD = 0xFE
neilt6 2:0a98e081b48c 316 };
neilt6 2:0a98e081b48c 317
neilt6 2:0a98e081b48c 318 //Member constants
neilt6 2:0a98e081b48c 319 static const int m_ADDR = (0x36 << 1);
neilt6 2:0a98e081b48c 320 static const int m_RCOMP0 = 0x97;
neilt6 2:0a98e081b48c 321
neilt6 2:0a98e081b48c 322 //Member variables
neilt6 2:0a98e081b48c 323 I2C m_I2C;
neilt6 2:0a98e081b48c 324
neilt6 2:0a98e081b48c 325 //Internal functions
neilt6 2:0a98e081b48c 326 unsigned short read(char reg);
neilt6 2:0a98e081b48c 327 void write(char reg, unsigned short data);
neilt6 2:0a98e081b48c 328 void writeRCOMP(char rcomp);
neilt6 0:abc480f8eeab 329 };
neilt6 0:abc480f8eeab 330
neilt6 0:abc480f8eeab 331 #endif