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:
Fri Aug 09 22:22:48 2013 +0000
Revision:
1:734b1a089a9c
Parent:
0:abc480f8eeab
Child:
2:0a98e081b48c
Minor documentation 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 //i2c address definition
neilt6 0:abc480f8eeab 23 #define __MAX17048_ADDR (0x36 << 1)
neilt6 0:abc480f8eeab 24
neilt6 0:abc480f8eeab 25 //i2c register definitions
neilt6 0:abc480f8eeab 26 #define __MAX17048_REG_VCELL 0x02
neilt6 0:abc480f8eeab 27 #define __MAX17048_REG_SOC 0x04
neilt6 0:abc480f8eeab 28 #define __MAX17048_REG_MODE 0x06
neilt6 0:abc480f8eeab 29 #define __MAX17048_REG_VERSION 0x08
neilt6 0:abc480f8eeab 30 #define __MAX17048_REG_HIBRT 0x0A
neilt6 0:abc480f8eeab 31 #define __MAX17048_REG_CONFIG 0x0C
neilt6 0:abc480f8eeab 32 #define __MAX17048_REG_VALRT 0x14
neilt6 0:abc480f8eeab 33 #define __MAX17048_REG_CRATE 0x16
neilt6 0:abc480f8eeab 34 #define __MAX17048_REG_VRESET_ID 0x18
neilt6 0:abc480f8eeab 35 #define __MAX17048_REG_STATUS 0x1A
neilt6 0:abc480f8eeab 36 #define __MAX17048_REG_TABLE 0x40
neilt6 0:abc480f8eeab 37 #define __MAX17048_REG_CMD 0xFE
neilt6 0:abc480f8eeab 38
neilt6 0:abc480f8eeab 39 //RCOMP0 value definition
neilt6 0:abc480f8eeab 40 #define __MAX17048_RCOMP0 0x97
neilt6 0:abc480f8eeab 41
neilt6 0:abc480f8eeab 42 /** MAX17048 class.
neilt6 0:abc480f8eeab 43 * Used for controlling a MAX17048 fuel gauge connected via I2C.
neilt6 0:abc480f8eeab 44 *
neilt6 0:abc480f8eeab 45 * Example:
neilt6 0:abc480f8eeab 46 * @code
neilt6 0:abc480f8eeab 47 * #include "mbed.h"
neilt6 0:abc480f8eeab 48 * #include "MAX17048.h"
neilt6 0:abc480f8eeab 49 *
neilt6 0:abc480f8eeab 50 * Serial pc(USBTX, USBRX);
neilt6 0:abc480f8eeab 51 * MAX17048 gauge(p28, p27);
neilt6 0:abc480f8eeab 52 *
neilt6 0:abc480f8eeab 53 * int main() {
neilt6 0:abc480f8eeab 54 * while (1) {
neilt6 0:abc480f8eeab 55 * //Read the cell voltage
neilt6 0:abc480f8eeab 56 * float vcell = gauge.getVcell();
neilt6 0:abc480f8eeab 57 *
neilt6 0:abc480f8eeab 58 * //Print the cell voltage
neilt6 0:abc480f8eeab 59 * pc.printf("Vcell = %f\n", vcell);
neilt6 0:abc480f8eeab 60 *
neilt6 0:abc480f8eeab 61 * //Sleep for 0.5 seconds
neilt6 0:abc480f8eeab 62 * wait(0.5);
neilt6 0:abc480f8eeab 63 * }
neilt6 0:abc480f8eeab 64 * }
neilt6 0:abc480f8eeab 65 * @endcode
neilt6 0:abc480f8eeab 66 */
neilt6 0:abc480f8eeab 67 class MAX17048
neilt6 0:abc480f8eeab 68 {
neilt6 0:abc480f8eeab 69 public:
neilt6 0:abc480f8eeab 70 /** Represents the different alert flags for the MAX17048
neilt6 0:abc480f8eeab 71 */
neilt6 0:abc480f8eeab 72 enum AlertFlags {
neilt6 0:abc480f8eeab 73 ALERT_RI = (1 << 0), /**< Reset indicator */
neilt6 0:abc480f8eeab 74 ALERT_VH = (1 << 1), /**< Voltage high alert */
neilt6 0:abc480f8eeab 75 ALERT_VL = (1 << 2), /**< Voltage low alert */
neilt6 0:abc480f8eeab 76 ALERT_VR = (1 << 3), /**< Voltage reset alert */
neilt6 0:abc480f8eeab 77 ALERT_HD = (1 << 4), /**< SOC low alert */
neilt6 0:abc480f8eeab 78 ALERT_SC = (1 << 5) /**< SOC change alert */
neilt6 0:abc480f8eeab 79 };
neilt6 0:abc480f8eeab 80
neilt6 0:abc480f8eeab 81 /** Create a MAX17048 object connected to the specified I2C pins
neilt6 0:abc480f8eeab 82 *
neilt6 1:734b1a089a9c 83 * @param sda The I2C data pin.
neilt6 1:734b1a089a9c 84 * @param scl The I2C clock pin.
neilt6 0:abc480f8eeab 85 */
neilt6 0:abc480f8eeab 86 MAX17048(PinName sda, PinName scl);
neilt6 0:abc480f8eeab 87
neilt6 0:abc480f8eeab 88 /** Command the MAX17048 to perform a power-on reset
neilt6 0:abc480f8eeab 89 */
neilt6 0:abc480f8eeab 90 void reset(void);
neilt6 0:abc480f8eeab 91
neilt6 0:abc480f8eeab 92 /** Command the MAX17048 to perform a QuickStart
neilt6 0:abc480f8eeab 93 */
neilt6 0:abc480f8eeab 94 void quickStart(void);
neilt6 0:abc480f8eeab 95
neilt6 0:abc480f8eeab 96 /** Determine whether sleep mode is enabled on the MAX17048
neilt6 0:abc480f8eeab 97 *
neilt6 0:abc480f8eeab 98 * @returns
neilt6 0:abc480f8eeab 99 * 'true' if sleep mode is enabled,
neilt6 0:abc480f8eeab 100 * 'false' if sleep mode is disabled.
neilt6 0:abc480f8eeab 101 */
neilt6 0:abc480f8eeab 102 bool isSleepEnabled(void);
neilt6 0:abc480f8eeab 103
neilt6 0:abc480f8eeab 104 /** Enable or disable sleep mode on the MAX17048
neilt6 0:abc480f8eeab 105 *
neilt6 0:abc480f8eeab 106 * @param enabled Whether or not sleep mode is enabled.
neilt6 0:abc480f8eeab 107 */
neilt6 0:abc480f8eeab 108 void setSleepEnabled(bool enabled);
neilt6 0:abc480f8eeab 109
neilt6 0:abc480f8eeab 110 /** Determine whether or not the MAX17048 is hibernating
neilt6 0:abc480f8eeab 111 *
neilt6 0:abc480f8eeab 112 * @returns
neilt6 0:abc480f8eeab 113 * 'true' if hibernating,
neilt6 0:abc480f8eeab 114 * 'false' if not hibernating.
neilt6 0:abc480f8eeab 115 */
neilt6 0:abc480f8eeab 116 bool isHibernating(void);
neilt6 0:abc480f8eeab 117
neilt6 0:abc480f8eeab 118 /** Get the current hibernate threshold of the MAX17048
neilt6 0:abc480f8eeab 119 *
neilt6 0:abc480f8eeab 120 * @returns The current hibernate threshold in \%/hr.
neilt6 0:abc480f8eeab 121 */
neilt6 0:abc480f8eeab 122 float getHibernateThreshold(void);
neilt6 0:abc480f8eeab 123
neilt6 0:abc480f8eeab 124 /** Set the hibernate threshold of the MAX17048
neilt6 0:abc480f8eeab 125 *
neilt6 0:abc480f8eeab 126 * @param threshold The new hibernate threshold in \%/hr.
neilt6 0:abc480f8eeab 127 */
neilt6 0:abc480f8eeab 128 void setHibernateThreshold(float threshold);
neilt6 0:abc480f8eeab 129
neilt6 0:abc480f8eeab 130 /** Get the current active threshold of the MAX17048
neilt6 0:abc480f8eeab 131 *
neilt6 0:abc480f8eeab 132 * @returns The current active threshold in volts.
neilt6 0:abc480f8eeab 133 */
neilt6 0:abc480f8eeab 134 float getActiveThreshold(void);
neilt6 0:abc480f8eeab 135
neilt6 0:abc480f8eeab 136 /** Set the active threshold of the MAX17048
neilt6 0:abc480f8eeab 137 *
neilt6 0:abc480f8eeab 138 * @param threshold The new active threshold in volts.
neilt6 0:abc480f8eeab 139 */
neilt6 0:abc480f8eeab 140 void setActiveThreshold(float threshold);
neilt6 0:abc480f8eeab 141
neilt6 0:abc480f8eeab 142 /** Get the production version of the MAX17048
neilt6 0:abc480f8eeab 143 *
neilt6 0:abc480f8eeab 144 * @returns The 16-bit production version.
neilt6 0:abc480f8eeab 145 */
neilt6 0:abc480f8eeab 146 unsigned short getVersion(void);
neilt6 0:abc480f8eeab 147
neilt6 0:abc480f8eeab 148 /** Set the cell temperature compensation of the MAX17048
neilt6 0:abc480f8eeab 149 *
neilt6 0:abc480f8eeab 150 * @param temp The current cell temperature in °C.
neilt6 0:abc480f8eeab 151 */
neilt6 0:abc480f8eeab 152 void setTempCompensation(float temp);
neilt6 0:abc480f8eeab 153
neilt6 0:abc480f8eeab 154 /** Determine whether or not the MAX17048 is in sleep mode
neilt6 0:abc480f8eeab 155 *
neilt6 0:abc480f8eeab 156 * @returns
neilt6 0:abc480f8eeab 157 * 'true' if in sleep mode,
neilt6 0:abc480f8eeab 158 * 'false' if not in sleep mode.
neilt6 0:abc480f8eeab 159 */
neilt6 0:abc480f8eeab 160 bool isSleeping(void);
neilt6 0:abc480f8eeab 161
neilt6 0:abc480f8eeab 162 /** Enter or exit sleep mode on the MAX17048 (sleep mode must be enabled first)
neilt6 0:abc480f8eeab 163 *
neilt6 0:abc480f8eeab 164 * @param sleep Whether or not to sleep.
neilt6 0:abc480f8eeab 165 */
neilt6 0:abc480f8eeab 166 void setSleep(bool sleep);
neilt6 0:abc480f8eeab 167
neilt6 0:abc480f8eeab 168 /** Determine whether or not the SOC 1% change alert is enabled on the MAX17048
neilt6 0:abc480f8eeab 169 *
neilt6 0:abc480f8eeab 170 * @returns
neilt6 0:abc480f8eeab 171 * 'true' if enabled,
neilt6 0:abc480f8eeab 172 * 'false' if not enabled.
neilt6 0:abc480f8eeab 173 */
neilt6 0:abc480f8eeab 174 bool isSOCChangeAlertEnabled(void);
neilt6 0:abc480f8eeab 175
neilt6 0:abc480f8eeab 176 /** Enable or disable the SOC 1% change alert on the MAX17048
neilt6 0:abc480f8eeab 177 *
neilt6 0:abc480f8eeab 178 * @param enabled Whether or the SOC 1% change alert is enabled.
neilt6 0:abc480f8eeab 179 */
neilt6 0:abc480f8eeab 180 void setSOCChangeAlertEnabled(bool enabled);
neilt6 0:abc480f8eeab 181
neilt6 0:abc480f8eeab 182 /** Determine whether or not the MAX17048 is asserting the ALRT pin
neilt6 0:abc480f8eeab 183 *
neilt6 0:abc480f8eeab 184 * @returns
neilt6 0:abc480f8eeab 185 * 'true' if alerting,
neilt6 0:abc480f8eeab 186 * 'false' if not alerting.
neilt6 0:abc480f8eeab 187 */
neilt6 0:abc480f8eeab 188 bool isAlerting(void);
neilt6 0:abc480f8eeab 189
neilt6 0:abc480f8eeab 190 /** Command the MAX17048 to de-assert the ALRT pin
neilt6 0:abc480f8eeab 191 */
neilt6 0:abc480f8eeab 192 void clearAlert(void);
neilt6 0:abc480f8eeab 193
neilt6 0:abc480f8eeab 194 /** Get the current SOC empty alert threshold of the MAX17048
neilt6 0:abc480f8eeab 195 *
neilt6 0:abc480f8eeab 196 * @returns The current SOC empty alert threshold in %.
neilt6 0:abc480f8eeab 197 */
neilt6 0:abc480f8eeab 198 char getEmptyAlertThreshold(void);
neilt6 0:abc480f8eeab 199
neilt6 0:abc480f8eeab 200 /** Set the SOC empty alert threshold of the MAX17048
neilt6 0:abc480f8eeab 201 *
neilt6 0:abc480f8eeab 202 * @param threshold The new SOC empty alert threshold in %.
neilt6 0:abc480f8eeab 203 */
neilt6 0:abc480f8eeab 204 void setEmptyAlertThreshold(char threshold);
neilt6 0:abc480f8eeab 205
neilt6 0:abc480f8eeab 206 /** Get the current low voltage alert threshold of the MAX17048
neilt6 0:abc480f8eeab 207 *
neilt6 0:abc480f8eeab 208 * @returns The current low voltage alert threshold in volts.
neilt6 0:abc480f8eeab 209 */
neilt6 0:abc480f8eeab 210 float getVAlertMinThreshold(void);
neilt6 0:abc480f8eeab 211
neilt6 0:abc480f8eeab 212 /** Set the low voltage alert threshold of the MAX17048
neilt6 0:abc480f8eeab 213 *
neilt6 0:abc480f8eeab 214 * @param threshold The new low voltage alert threshold in volts.
neilt6 0:abc480f8eeab 215 */
neilt6 0:abc480f8eeab 216 void setVAlertMinThreshold(float threshold);
neilt6 0:abc480f8eeab 217
neilt6 0:abc480f8eeab 218 /** Get the current high voltage alert threshold of the MAX17048
neilt6 0:abc480f8eeab 219 *
neilt6 0:abc480f8eeab 220 * @returns The current high voltage alert threshold in volts.
neilt6 0:abc480f8eeab 221 */
neilt6 0:abc480f8eeab 222 float getVAlertMaxThreshold(void);
neilt6 0:abc480f8eeab 223
neilt6 0:abc480f8eeab 224 /** Set the high voltage alert threshold of the MAX17048
neilt6 0:abc480f8eeab 225 *
neilt6 0:abc480f8eeab 226 * @param threshold The new high voltage alert threshold in volts.
neilt6 0:abc480f8eeab 227 */
neilt6 0:abc480f8eeab 228 void setVAlertMaxThreshold(float threshold);
neilt6 0:abc480f8eeab 229
neilt6 0:abc480f8eeab 230 /** Get the current reset voltage threshold of the MAX17048
neilt6 0:abc480f8eeab 231 *
neilt6 0:abc480f8eeab 232 * @returns The current reset voltage threshold in volts.
neilt6 0:abc480f8eeab 233 */
neilt6 0:abc480f8eeab 234 float getVResetThreshold(void);
neilt6 0:abc480f8eeab 235
neilt6 0:abc480f8eeab 236 /** Set the reset voltage threshold of the MAX17048
neilt6 0:abc480f8eeab 237 *
neilt6 0:abc480f8eeab 238 * @param threshold The new reset voltage threshold in volts.
neilt6 0:abc480f8eeab 239 */
neilt6 0:abc480f8eeab 240 void setVResetThreshold(float threshold);
neilt6 0:abc480f8eeab 241
neilt6 0:abc480f8eeab 242 /** Determine whether or not the reset voltage comparator is enabled on the MAX17048
neilt6 0:abc480f8eeab 243 *
neilt6 0:abc480f8eeab 244 * @returns
neilt6 0:abc480f8eeab 245 * 'true' if enabled,
neilt6 0:abc480f8eeab 246 * 'false' if not enabled.
neilt6 0:abc480f8eeab 247 */
neilt6 0:abc480f8eeab 248 bool isComparatorEnabled(void);
neilt6 0:abc480f8eeab 249
neilt6 0:abc480f8eeab 250 /** Enable or disable the reset voltage comparator on the MAX17048
neilt6 0:abc480f8eeab 251 *
neilt6 0:abc480f8eeab 252 * @param enabled Whether or not the reset voltage comparator is enabled.
neilt6 0:abc480f8eeab 253 */
neilt6 0:abc480f8eeab 254 void setComparatorEnabled(bool enabled);
neilt6 0:abc480f8eeab 255
neilt6 0:abc480f8eeab 256 /** Get the factory programmed 8-bit ID of the MAX17048
neilt6 0:abc480f8eeab 257 *
neilt6 0:abc480f8eeab 258 * @returns The 8-bit ID.
neilt6 0:abc480f8eeab 259 */
neilt6 0:abc480f8eeab 260 char getID(void);
neilt6 0:abc480f8eeab 261
neilt6 0:abc480f8eeab 262 /** Determine whether or not the voltage reset alert is enabled on the MAX17048
neilt6 0:abc480f8eeab 263 *
neilt6 0:abc480f8eeab 264 * @returns
neilt6 0:abc480f8eeab 265 * 'true' if enabled,
neilt6 0:abc480f8eeab 266 * 'false' if not enabled.
neilt6 0:abc480f8eeab 267 */
neilt6 0:abc480f8eeab 268 bool isVResetAlertEnabled(void);
neilt6 0:abc480f8eeab 269
neilt6 0:abc480f8eeab 270 /** Enable or disable the voltage reset alert on the MAX17048
neilt6 0:abc480f8eeab 271 *
neilt6 0:abc480f8eeab 272 * @param enabled Whether or the voltage reset alert is enabled.
neilt6 0:abc480f8eeab 273 */
neilt6 0:abc480f8eeab 274 void setVResetAlertEnabled(bool enabled);
neilt6 0:abc480f8eeab 275
neilt6 0:abc480f8eeab 276 /** Get the current alert flags on the MAX17048
neilt6 0:abc480f8eeab 277 *
neilt6 0:abc480f8eeab 278 * @returns The current alert flags as AlertFlags enum values OR'd together.
neilt6 0:abc480f8eeab 279 */
neilt6 0:abc480f8eeab 280 char getAlertFlags(void);
neilt6 0:abc480f8eeab 281
neilt6 0:abc480f8eeab 282 /** Clear the specified alert flags on the MAX17048
neilt6 0:abc480f8eeab 283 *
neilt6 0:abc480f8eeab 284 * @param flags The alert flags to clear as AlertFlags enum values OR'd together.
neilt6 0:abc480f8eeab 285 */
neilt6 0:abc480f8eeab 286 void clearAlertFlags(char flags);
neilt6 0:abc480f8eeab 287
neilt6 0:abc480f8eeab 288 /** Get the current cell voltage measurement of the MAX17048
neilt6 0:abc480f8eeab 289 *
neilt6 0:abc480f8eeab 290 * @returns The cell voltage measurement as a float.
neilt6 0:abc480f8eeab 291 */
neilt6 0:abc480f8eeab 292 float getVcell(void);
neilt6 0:abc480f8eeab 293
neilt6 0:abc480f8eeab 294 /** Get the current state of charge measurement of the MAX17048
neilt6 0:abc480f8eeab 295 *
neilt6 0:abc480f8eeab 296 * @returns The state of charge measurement as a float.
neilt6 0:abc480f8eeab 297 */
neilt6 0:abc480f8eeab 298 float getSOC(void);
neilt6 0:abc480f8eeab 299
neilt6 0:abc480f8eeab 300 /** Get the current C rate measurement of the MAX17048
neilt6 0:abc480f8eeab 301 *
neilt6 0:abc480f8eeab 302 * @returns The C rate measurement as a float.
neilt6 0:abc480f8eeab 303 */
neilt6 0:abc480f8eeab 304 float getCRate(void);
neilt6 0:abc480f8eeab 305
neilt6 0:abc480f8eeab 306 private:
neilt6 0:abc480f8eeab 307 I2C _i2c;
neilt6 0:abc480f8eeab 308 unsigned short _read(char reg);
neilt6 0:abc480f8eeab 309 void _write(char reg, unsigned short data);
neilt6 0:abc480f8eeab 310 void _writeRCOMP(char rcomp);
neilt6 0:abc480f8eeab 311 };
neilt6 0:abc480f8eeab 312
neilt6 0:abc480f8eeab 313 #endif