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 14 04:52:16 2013 +0000
Revision:
2:0a98e081b48c
Parent:
1:734b1a089a9c
Child:
3:32087cca331f
Modified library so as to be more compliant with C++ conventions

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