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:
Thu Nov 07 17:53:44 2013 +0000
Revision:
5:ffce4fe12ed1
Parent:
4:e61b2723d2cf
Child:
7:bf6972a21c61
Minor improvements

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 4:e61b2723d2cf 39 * //Print the current state of charge
neilt6 4:e61b2723d2cf 40 * printf("SOC = %f%%\n", (float)gauge);
neilt6 3:32087cca331f 41 *
neilt6 3:32087cca331f 42 * //Sleep for 0.5 seconds
neilt6 3:32087cca331f 43 * wait(0.5);
neilt6 3:32087cca331f 44 * }
neilt6 3:32087cca331f 45 * } else {
neilt6 4:e61b2723d2cf 46 * error("Device not detected!\n");
neilt6 0:abc480f8eeab 47 * }
neilt6 0:abc480f8eeab 48 * }
neilt6 0:abc480f8eeab 49 * @endcode
neilt6 0:abc480f8eeab 50 */
neilt6 0:abc480f8eeab 51 class MAX17048
neilt6 0:abc480f8eeab 52 {
neilt6 0:abc480f8eeab 53 public:
neilt6 0:abc480f8eeab 54 /** Represents the different alert flags for the MAX17048
neilt6 0:abc480f8eeab 55 */
neilt6 0:abc480f8eeab 56 enum AlertFlags {
neilt6 0:abc480f8eeab 57 ALERT_RI = (1 << 0), /**< Reset indicator */
neilt6 0:abc480f8eeab 58 ALERT_VH = (1 << 1), /**< Voltage high alert */
neilt6 0:abc480f8eeab 59 ALERT_VL = (1 << 2), /**< Voltage low alert */
neilt6 0:abc480f8eeab 60 ALERT_VR = (1 << 3), /**< Voltage reset alert */
neilt6 0:abc480f8eeab 61 ALERT_HD = (1 << 4), /**< SOC low alert */
neilt6 0:abc480f8eeab 62 ALERT_SC = (1 << 5) /**< SOC change alert */
neilt6 0:abc480f8eeab 63 };
neilt6 0:abc480f8eeab 64
neilt6 0:abc480f8eeab 65 /** Create a MAX17048 object connected to the specified I2C pins
neilt6 0:abc480f8eeab 66 *
neilt6 1:734b1a089a9c 67 * @param sda The I2C data pin.
neilt6 1:734b1a089a9c 68 * @param scl The I2C clock pin.
neilt6 0:abc480f8eeab 69 */
neilt6 0:abc480f8eeab 70 MAX17048(PinName sda, PinName scl);
neilt6 0:abc480f8eeab 71
neilt6 3:32087cca331f 72 /** Probe for the MAX17048 and load the default RCOMP value if present
neilt6 3:32087cca331f 73 *
neilt6 3:32087cca331f 74 * @returns
neilt6 3:32087cca331f 75 * 'true' if the device exists on the bus,
neilt6 3:32087cca331f 76 * 'false' if the device doesn't exist on the bus.
neilt6 3:32087cca331f 77 */
neilt6 5:ffce4fe12ed1 78 bool open();
neilt6 3:32087cca331f 79
neilt6 0:abc480f8eeab 80 /** Command the MAX17048 to perform a power-on reset
neilt6 0:abc480f8eeab 81 */
neilt6 5:ffce4fe12ed1 82 void reset();
neilt6 0:abc480f8eeab 83
neilt6 0:abc480f8eeab 84 /** Command the MAX17048 to perform a QuickStart
neilt6 0:abc480f8eeab 85 */
neilt6 5:ffce4fe12ed1 86 void quickStart();
neilt6 0:abc480f8eeab 87
neilt6 0:abc480f8eeab 88 /** Determine whether sleep mode is enabled on the MAX17048
neilt6 0:abc480f8eeab 89 *
neilt6 0:abc480f8eeab 90 * @returns
neilt6 0:abc480f8eeab 91 * 'true' if sleep mode is enabled,
neilt6 0:abc480f8eeab 92 * 'false' if sleep mode is disabled.
neilt6 0:abc480f8eeab 93 */
neilt6 5:ffce4fe12ed1 94 bool sleepEnabled();
neilt6 0:abc480f8eeab 95
neilt6 0:abc480f8eeab 96 /** Enable or disable sleep mode on the MAX17048
neilt6 0:abc480f8eeab 97 *
neilt6 0:abc480f8eeab 98 * @param enabled Whether or not sleep mode is enabled.
neilt6 0:abc480f8eeab 99 */
neilt6 2:0a98e081b48c 100 void sleepEnabled(bool enabled);
neilt6 2:0a98e081b48c 101
neilt6 0:abc480f8eeab 102 /** Determine whether or not the MAX17048 is hibernating
neilt6 0:abc480f8eeab 103 *
neilt6 0:abc480f8eeab 104 * @returns
neilt6 0:abc480f8eeab 105 * 'true' if hibernating,
neilt6 0:abc480f8eeab 106 * 'false' if not hibernating.
neilt6 0:abc480f8eeab 107 */
neilt6 5:ffce4fe12ed1 108 bool hibernating();
neilt6 2:0a98e081b48c 109
neilt6 0:abc480f8eeab 110 /** Get the current hibernate threshold of the MAX17048
neilt6 0:abc480f8eeab 111 *
neilt6 0:abc480f8eeab 112 * @returns The current hibernate threshold in \%/hr.
neilt6 0:abc480f8eeab 113 */
neilt6 5:ffce4fe12ed1 114 float hibernateThreshold();
neilt6 2:0a98e081b48c 115
neilt6 0:abc480f8eeab 116 /** Set the hibernate threshold of the MAX17048
neilt6 0:abc480f8eeab 117 *
neilt6 0:abc480f8eeab 118 * @param threshold The new hibernate threshold in \%/hr.
neilt6 0:abc480f8eeab 119 */
neilt6 2:0a98e081b48c 120 void hibernateThreshold(float threshold);
neilt6 2:0a98e081b48c 121
neilt6 0:abc480f8eeab 122 /** Get the current active threshold of the MAX17048
neilt6 0:abc480f8eeab 123 *
neilt6 0:abc480f8eeab 124 * @returns The current active threshold in volts.
neilt6 0:abc480f8eeab 125 */
neilt6 5:ffce4fe12ed1 126 float activeThreshold();
neilt6 2:0a98e081b48c 127
neilt6 0:abc480f8eeab 128 /** Set the active threshold of the MAX17048
neilt6 0:abc480f8eeab 129 *
neilt6 0:abc480f8eeab 130 * @param threshold The new active threshold in volts.
neilt6 0:abc480f8eeab 131 */
neilt6 2:0a98e081b48c 132 void activeThreshold(float threshold);
neilt6 2:0a98e081b48c 133
neilt6 0:abc480f8eeab 134 /** Get the production version of the MAX17048
neilt6 0:abc480f8eeab 135 *
neilt6 0:abc480f8eeab 136 * @returns The 16-bit production version.
neilt6 0:abc480f8eeab 137 */
neilt6 5:ffce4fe12ed1 138 unsigned short version();
neilt6 2:0a98e081b48c 139
neilt6 0:abc480f8eeab 140 /** Set the cell temperature compensation of the MAX17048
neilt6 0:abc480f8eeab 141 *
neilt6 0:abc480f8eeab 142 * @param temp The current cell temperature in °C.
neilt6 0:abc480f8eeab 143 */
neilt6 2:0a98e081b48c 144 void tempCompensation(float temp);
neilt6 2:0a98e081b48c 145
neilt6 0:abc480f8eeab 146 /** Determine whether or not the MAX17048 is in sleep mode
neilt6 0:abc480f8eeab 147 *
neilt6 0:abc480f8eeab 148 * @returns
neilt6 0:abc480f8eeab 149 * 'true' if in sleep mode,
neilt6 0:abc480f8eeab 150 * 'false' if not in sleep mode.
neilt6 0:abc480f8eeab 151 */
neilt6 5:ffce4fe12ed1 152 bool sleeping();
neilt6 2:0a98e081b48c 153
neilt6 0:abc480f8eeab 154 /** Enter or exit sleep mode on the MAX17048 (sleep mode must be enabled first)
neilt6 0:abc480f8eeab 155 *
neilt6 0:abc480f8eeab 156 * @param sleep Whether or not to sleep.
neilt6 0:abc480f8eeab 157 */
neilt6 2:0a98e081b48c 158 void sleep(bool sleep);
neilt6 2:0a98e081b48c 159
neilt6 0:abc480f8eeab 160 /** Determine whether or not the SOC 1% change alert is enabled on the MAX17048
neilt6 0:abc480f8eeab 161 *
neilt6 0:abc480f8eeab 162 * @returns
neilt6 0:abc480f8eeab 163 * 'true' if enabled,
neilt6 0:abc480f8eeab 164 * 'false' if not enabled.
neilt6 0:abc480f8eeab 165 */
neilt6 5:ffce4fe12ed1 166 bool socChangeAlertEnabled();
neilt6 2:0a98e081b48c 167
neilt6 0:abc480f8eeab 168 /** Enable or disable the SOC 1% change alert on the MAX17048
neilt6 0:abc480f8eeab 169 *
neilt6 0:abc480f8eeab 170 * @param enabled Whether or the SOC 1% change alert is enabled.
neilt6 0:abc480f8eeab 171 */
neilt6 2:0a98e081b48c 172 void socChangeAlertEnabled(bool enabled);
neilt6 2:0a98e081b48c 173
neilt6 0:abc480f8eeab 174 /** Determine whether or not the MAX17048 is asserting the ALRT pin
neilt6 0:abc480f8eeab 175 *
neilt6 0:abc480f8eeab 176 * @returns
neilt6 0:abc480f8eeab 177 * 'true' if alerting,
neilt6 0:abc480f8eeab 178 * 'false' if not alerting.
neilt6 0:abc480f8eeab 179 */
neilt6 5:ffce4fe12ed1 180 bool alerting();
neilt6 2:0a98e081b48c 181
neilt6 0:abc480f8eeab 182 /** Command the MAX17048 to de-assert the ALRT pin
neilt6 0:abc480f8eeab 183 */
neilt6 5:ffce4fe12ed1 184 void clearAlert();
neilt6 2:0a98e081b48c 185
neilt6 0:abc480f8eeab 186 /** Get the current SOC empty alert threshold of the MAX17048
neilt6 0:abc480f8eeab 187 *
neilt6 0:abc480f8eeab 188 * @returns The current SOC empty alert threshold in %.
neilt6 0:abc480f8eeab 189 */
neilt6 5:ffce4fe12ed1 190 char emptyAlertThreshold();
neilt6 2:0a98e081b48c 191
neilt6 0:abc480f8eeab 192 /** Set the SOC empty alert threshold of the MAX17048
neilt6 0:abc480f8eeab 193 *
neilt6 0:abc480f8eeab 194 * @param threshold The new SOC empty alert threshold in %.
neilt6 0:abc480f8eeab 195 */
neilt6 2:0a98e081b48c 196 void emptyAlertThreshold(char threshold);
neilt6 2:0a98e081b48c 197
neilt6 0:abc480f8eeab 198 /** Get the current low voltage alert threshold of the MAX17048
neilt6 0:abc480f8eeab 199 *
neilt6 0:abc480f8eeab 200 * @returns The current low voltage alert threshold in volts.
neilt6 0:abc480f8eeab 201 */
neilt6 5:ffce4fe12ed1 202 float vAlertMinThreshold();
neilt6 2:0a98e081b48c 203
neilt6 0:abc480f8eeab 204 /** Set the low voltage alert threshold of the MAX17048
neilt6 0:abc480f8eeab 205 *
neilt6 0:abc480f8eeab 206 * @param threshold The new low voltage alert threshold in volts.
neilt6 0:abc480f8eeab 207 */
neilt6 2:0a98e081b48c 208 void vAlertMinThreshold(float threshold);
neilt6 2:0a98e081b48c 209
neilt6 0:abc480f8eeab 210 /** Get the current high voltage alert threshold of the MAX17048
neilt6 0:abc480f8eeab 211 *
neilt6 0:abc480f8eeab 212 * @returns The current high voltage alert threshold in volts.
neilt6 0:abc480f8eeab 213 */
neilt6 5:ffce4fe12ed1 214 float vAlertMaxThreshold();
neilt6 2:0a98e081b48c 215
neilt6 0:abc480f8eeab 216 /** Set the high voltage alert threshold of the MAX17048
neilt6 0:abc480f8eeab 217 *
neilt6 0:abc480f8eeab 218 * @param threshold The new high voltage alert threshold in volts.
neilt6 0:abc480f8eeab 219 */
neilt6 2:0a98e081b48c 220 void vAlertMaxThreshold(float threshold);
neilt6 2:0a98e081b48c 221
neilt6 0:abc480f8eeab 222 /** Get the current reset voltage threshold of the MAX17048
neilt6 0:abc480f8eeab 223 *
neilt6 0:abc480f8eeab 224 * @returns The current reset voltage threshold in volts.
neilt6 0:abc480f8eeab 225 */
neilt6 5:ffce4fe12ed1 226 float vResetThreshold();
neilt6 2:0a98e081b48c 227
neilt6 0:abc480f8eeab 228 /** Set the reset voltage threshold of the MAX17048
neilt6 0:abc480f8eeab 229 *
neilt6 0:abc480f8eeab 230 * @param threshold The new reset voltage threshold in volts.
neilt6 0:abc480f8eeab 231 */
neilt6 2:0a98e081b48c 232 void vResetThreshold(float threshold);
neilt6 2:0a98e081b48c 233
neilt6 0:abc480f8eeab 234 /** Determine whether or not the reset voltage comparator is enabled on the MAX17048
neilt6 0:abc480f8eeab 235 *
neilt6 0:abc480f8eeab 236 * @returns
neilt6 0:abc480f8eeab 237 * 'true' if enabled,
neilt6 0:abc480f8eeab 238 * 'false' if not enabled.
neilt6 0:abc480f8eeab 239 */
neilt6 5:ffce4fe12ed1 240 bool comparatorEnabled();
neilt6 2:0a98e081b48c 241
neilt6 0:abc480f8eeab 242 /** Enable or disable the reset voltage comparator on the MAX17048
neilt6 0:abc480f8eeab 243 *
neilt6 0:abc480f8eeab 244 * @param enabled Whether or not the reset voltage comparator is enabled.
neilt6 0:abc480f8eeab 245 */
neilt6 2:0a98e081b48c 246 void comparatorEnabled(bool enabled);
neilt6 2:0a98e081b48c 247
neilt6 0:abc480f8eeab 248 /** Get the factory programmed 8-bit ID of the MAX17048
neilt6 0:abc480f8eeab 249 *
neilt6 0:abc480f8eeab 250 * @returns The 8-bit ID.
neilt6 0:abc480f8eeab 251 */
neilt6 5:ffce4fe12ed1 252 char id();
neilt6 2:0a98e081b48c 253
neilt6 0:abc480f8eeab 254 /** Determine whether or not the voltage reset alert is enabled on the MAX17048
neilt6 0:abc480f8eeab 255 *
neilt6 0:abc480f8eeab 256 * @returns
neilt6 0:abc480f8eeab 257 * 'true' if enabled,
neilt6 0:abc480f8eeab 258 * 'false' if not enabled.
neilt6 0:abc480f8eeab 259 */
neilt6 5:ffce4fe12ed1 260 bool vResetAlertEnabled();
neilt6 2:0a98e081b48c 261
neilt6 0:abc480f8eeab 262 /** Enable or disable the voltage reset alert on the MAX17048
neilt6 0:abc480f8eeab 263 *
neilt6 0:abc480f8eeab 264 * @param enabled Whether or the voltage reset alert is enabled.
neilt6 0:abc480f8eeab 265 */
neilt6 2:0a98e081b48c 266 void vResetAlertEnabled(bool enabled);
neilt6 2:0a98e081b48c 267
neilt6 0:abc480f8eeab 268 /** Get the current alert flags on the MAX17048
neilt6 0:abc480f8eeab 269 *
neilt6 0:abc480f8eeab 270 * @returns The current alert flags as AlertFlags enum values OR'd together.
neilt6 0:abc480f8eeab 271 */
neilt6 5:ffce4fe12ed1 272 char alertFlags();
neilt6 2:0a98e081b48c 273
neilt6 0:abc480f8eeab 274 /** Clear the specified alert flags on the MAX17048
neilt6 0:abc480f8eeab 275 *
neilt6 0:abc480f8eeab 276 * @param flags The alert flags to clear as AlertFlags enum values OR'd together.
neilt6 0:abc480f8eeab 277 */
neilt6 0:abc480f8eeab 278 void clearAlertFlags(char flags);
neilt6 0:abc480f8eeab 279
neilt6 0:abc480f8eeab 280 /** Get the current cell voltage measurement of the MAX17048
neilt6 0:abc480f8eeab 281 *
neilt6 0:abc480f8eeab 282 * @returns The cell voltage measurement as a float.
neilt6 0:abc480f8eeab 283 */
neilt6 5:ffce4fe12ed1 284 float vcell();
neilt6 0:abc480f8eeab 285
neilt6 4:e61b2723d2cf 286 /** Get the current state of charge measurement of the MAX17048 as a float
neilt6 0:abc480f8eeab 287 *
neilt6 0:abc480f8eeab 288 * @returns The state of charge measurement as a float.
neilt6 0:abc480f8eeab 289 */
neilt6 5:ffce4fe12ed1 290 float soc();
neilt6 0:abc480f8eeab 291
neilt6 4:e61b2723d2cf 292 /** Get the current state of charge measurement of the MAX17048 as an int
neilt6 4:e61b2723d2cf 293 *
neilt6 4:e61b2723d2cf 294 * @returns The state of charge measurement as an int.
neilt6 4:e61b2723d2cf 295 */
neilt6 5:ffce4fe12ed1 296 int soc_int();
neilt6 4:e61b2723d2cf 297
neilt6 0:abc480f8eeab 298 /** Get the current C rate measurement of the MAX17048
neilt6 0:abc480f8eeab 299 *
neilt6 0:abc480f8eeab 300 * @returns The C rate measurement as a float.
neilt6 0:abc480f8eeab 301 */
neilt6 5:ffce4fe12ed1 302 float crate();
neilt6 0:abc480f8eeab 303
neilt6 4:e61b2723d2cf 304 #ifdef MBED_OPERATORS
neilt6 4:e61b2723d2cf 305 /** A shorthand for soc()
neilt6 4:e61b2723d2cf 306 *
neilt6 4:e61b2723d2cf 307 * @returns The state of charge measurement as a float.
neilt6 4:e61b2723d2cf 308 */
neilt6 5:ffce4fe12ed1 309 operator float();
neilt6 4:e61b2723d2cf 310
neilt6 5:ffce4fe12ed1 311 /** A shorthand for soc_int()
neilt6 4:e61b2723d2cf 312 *
neilt6 4:e61b2723d2cf 313 * @returns The state of charge measurement as an int.
neilt6 4:e61b2723d2cf 314 */
neilt6 5:ffce4fe12ed1 315 operator int();
neilt6 4:e61b2723d2cf 316 #endif
neilt6 4:e61b2723d2cf 317
neilt6 0:abc480f8eeab 318 private:
neilt6 2:0a98e081b48c 319 //I2C register addresses
neilt6 2:0a98e081b48c 320 enum Register {
neilt6 2:0a98e081b48c 321 REG_VCELL = 0x02,
neilt6 2:0a98e081b48c 322 REG_SOC = 0x04,
neilt6 2:0a98e081b48c 323 REG_MODE = 0x06,
neilt6 2:0a98e081b48c 324 REG_VERSION = 0x08,
neilt6 2:0a98e081b48c 325 REG_HIBRT = 0x0A,
neilt6 2:0a98e081b48c 326 REG_CONFIG = 0x0C,
neilt6 2:0a98e081b48c 327 REG_VALRT = 0x14,
neilt6 2:0a98e081b48c 328 REG_CRATE = 0x16,
neilt6 2:0a98e081b48c 329 REG_VRESET_ID = 0x18,
neilt6 2:0a98e081b48c 330 REG_STATUS = 0x1A,
neilt6 2:0a98e081b48c 331 REG_TABLE = 0x40,
neilt6 2:0a98e081b48c 332 REG_CMD = 0xFE
neilt6 2:0a98e081b48c 333 };
neilt6 2:0a98e081b48c 334
neilt6 2:0a98e081b48c 335 //Member constants
neilt6 2:0a98e081b48c 336 static const int m_ADDR = (0x36 << 1);
neilt6 2:0a98e081b48c 337 static const int m_RCOMP0 = 0x97;
neilt6 2:0a98e081b48c 338
neilt6 2:0a98e081b48c 339 //Member variables
neilt6 2:0a98e081b48c 340 I2C m_I2C;
neilt6 2:0a98e081b48c 341
neilt6 2:0a98e081b48c 342 //Internal functions
neilt6 2:0a98e081b48c 343 unsigned short read(char reg);
neilt6 2:0a98e081b48c 344 void write(char reg, unsigned short data);
neilt6 2:0a98e081b48c 345 void writeRCOMP(char rcomp);
neilt6 0:abc480f8eeab 346 };
neilt6 0:abc480f8eeab 347
neilt6 0:abc480f8eeab 348 #endif