Fuel Gauge lib. based on Mark Gottscho's lib.

Dependents:   weather_station_proj weather_station_project weather_station_proj_v1_2

Committer:
daniel_davvid
Date:
Sun Jul 01 12:00:52 2018 +0000
Revision:
0:4287b7d9c9ca
No significant changes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
daniel_davvid 0:4287b7d9c9ca 1 /* MAX17043.h
daniel_davvid 0:4287b7d9c9ca 2 * Tested with mbed board: FRDM-KL46Z
daniel_davvid 0:4287b7d9c9ca 3 * Author: Mark Gottscho
daniel_davvid 0:4287b7d9c9ca 4 * mgottscho@ucla.edu
daniel_davvid 0:4287b7d9c9ca 5 */
daniel_davvid 0:4287b7d9c9ca 6
daniel_davvid 0:4287b7d9c9ca 7 #ifndef MAX17043_H
daniel_davvid 0:4287b7d9c9ca 8 #define MAX17043_H
daniel_davvid 0:4287b7d9c9ca 9
daniel_davvid 0:4287b7d9c9ca 10 #include "mbed.h"
daniel_davvid 0:4287b7d9c9ca 11
daniel_davvid 0:4287b7d9c9ca 12 /**
daniel_davvid 0:4287b7d9c9ca 13 * This class allows for easy control over a MAX17043 LiPo fuel gauge IC.
daniel_davvid 0:4287b7d9c9ca 14 */
daniel_davvid 0:4287b7d9c9ca 15 class MAX17043 {
daniel_davvid 0:4287b7d9c9ca 16 public:
daniel_davvid 0:4287b7d9c9ca 17 /**
daniel_davvid 0:4287b7d9c9ca 18 * @param sda the pin identifier for SDA I2C signal
daniel_davvid 0:4287b7d9c9ca 19 * @param scl the pin identifier for SCL I2C signal
daniel_davvid 0:4287b7d9c9ca 20 * @param i2c_addr the 8-bit I2C address for this device. Note that LSB is a don't care.
daniel_davvid 0:4287b7d9c9ca 21 */
daniel_davvid 0:4287b7d9c9ca 22 MAX17043(PinName sda, PinName scl, int i2c_addr=0x6c);
daniel_davvid 0:4287b7d9c9ca 23 MAX17043(I2C &i2c, int i2c_addr=0x6c);
daniel_davvid 0:4287b7d9c9ca 24
daniel_davvid 0:4287b7d9c9ca 25 /**
daniel_davvid 0:4287b7d9c9ca 26 *
daniel_davvid 0:4287b7d9c9ca 27 */
daniel_davvid 0:4287b7d9c9ca 28 ~MAX17043();
daniel_davvid 0:4287b7d9c9ca 29
daniel_davvid 0:4287b7d9c9ca 30 /**
daniel_davvid 0:4287b7d9c9ca 31 * Initializes the device to some preferred state.
daniel_davvid 0:4287b7d9c9ca 32 */
daniel_davvid 0:4287b7d9c9ca 33 void selfInit();
daniel_davvid 0:4287b7d9c9ca 34
daniel_davvid 0:4287b7d9c9ca 35 /**
daniel_davvid 0:4287b7d9c9ca 36 * Performs a software reset of the device.
daniel_davvid 0:4287b7d9c9ca 37 */
daniel_davvid 0:4287b7d9c9ca 38 void reset();
daniel_davvid 0:4287b7d9c9ca 39
daniel_davvid 0:4287b7d9c9ca 40 /**
daniel_davvid 0:4287b7d9c9ca 41 * @returns the IC version code
daniel_davvid 0:4287b7d9c9ca 42 */
daniel_davvid 0:4287b7d9c9ca 43 uint16_t getVersion();
daniel_davvid 0:4287b7d9c9ca 44
daniel_davvid 0:4287b7d9c9ca 45 /**
daniel_davvid 0:4287b7d9c9ca 46 * @param sampleNow if true, queries the device for the sample and returns it. if false, gets the last queried value.
daniel_davvid 0:4287b7d9c9ca 47 * The latter is preferred if this object is set up to sample using interrupts.
daniel_davvid 0:4287b7d9c9ca 48 * @returns the battery voltage raw ADC value
daniel_davvid 0:4287b7d9c9ca 49 */
daniel_davvid 0:4287b7d9c9ca 50 uint16_t getVCell();
daniel_davvid 0:4287b7d9c9ca 51
daniel_davvid 0:4287b7d9c9ca 52 /**
daniel_davvid 0:4287b7d9c9ca 53 * @param sampleNow if true, queries the device for the sample and returns it. if false, gets the last queried value.
daniel_davvid 0:4287b7d9c9ca 54 * The latter is preferred if this object is set up to sample using interrupts.
daniel_davvid 0:4287b7d9c9ca 55 * @returns the battery voltage as floating point
daniel_davvid 0:4287b7d9c9ca 56 */
daniel_davvid 0:4287b7d9c9ca 57 float getFloatVCell();
daniel_davvid 0:4287b7d9c9ca 58
daniel_davvid 0:4287b7d9c9ca 59 /**
daniel_davvid 0:4287b7d9c9ca 60 * @param sampleNow if true, queries the device for the sample and returns it. if false, gets the last queried value.
daniel_davvid 0:4287b7d9c9ca 61 * The latter is preferred if this object is set up to sample using interrupts.
daniel_davvid 0:4287b7d9c9ca 62 * @returns the battery state of charge as computed by the ModelGauge algorithm. High byte: units of %. Low byte: units of 1/256%.
daniel_davvid 0:4287b7d9c9ca 63 */
daniel_davvid 0:4287b7d9c9ca 64 uint16_t getSOC();
daniel_davvid 0:4287b7d9c9ca 65
daniel_davvid 0:4287b7d9c9ca 66 /**
daniel_davvid 0:4287b7d9c9ca 67 * @param sampleNow if true, queries the device for the sample and returns it. if false, gets the last queried value.
daniel_davvid 0:4287b7d9c9ca 68 * The latter is preferred if this object is set up to sample using interrupts.
daniel_davvid 0:4287b7d9c9ca 69 * @returns the battery state of charge in %, as a floating point #
daniel_davvid 0:4287b7d9c9ca 70 */
daniel_davvid 0:4287b7d9c9ca 71 float getFloatSOC();
daniel_davvid 0:4287b7d9c9ca 72
daniel_davvid 0:4287b7d9c9ca 73 uint8_t getRegister(const uint8_t reg_addr);
daniel_davvid 0:4287b7d9c9ca 74 uint16_t getRegister16b(const uint8_t reg_addr);
daniel_davvid 0:4287b7d9c9ca 75 void setRegister(const uint8_t reg_addr, const uint8_t data);
daniel_davvid 0:4287b7d9c9ca 76 void setRegister16b(const uint8_t reg_addr, const uint16_t data);
daniel_davvid 0:4287b7d9c9ca 77 int __writeReg(const uint8_t *data, int total_len);
daniel_davvid 0:4287b7d9c9ca 78 int __readReg(const uint8_t reg_addr, uint8_t *data, int len);
daniel_davvid 0:4287b7d9c9ca 79
daniel_davvid 0:4287b7d9c9ca 80 private:
daniel_davvid 0:4287b7d9c9ca 81
daniel_davvid 0:4287b7d9c9ca 82 I2C &i2c_;
daniel_davvid 0:4287b7d9c9ca 83 char i2cRaw[sizeof(I2C)];
daniel_davvid 0:4287b7d9c9ca 84
daniel_davvid 0:4287b7d9c9ca 85 int __i2c_addr;
daniel_davvid 0:4287b7d9c9ca 86 uint16_t __soc;
daniel_davvid 0:4287b7d9c9ca 87 uint16_t __vcell;
daniel_davvid 0:4287b7d9c9ca 88
daniel_davvid 0:4287b7d9c9ca 89 ///////////////// CONSTANTS /////////////////////
daniel_davvid 0:4287b7d9c9ca 90
daniel_davvid 0:4287b7d9c9ca 91 //Device register addresses
daniel_davvid 0:4287b7d9c9ca 92 static const uint8_t VCELL_MSB = 0x02; //Read only
daniel_davvid 0:4287b7d9c9ca 93 static const uint8_t VCELL_LSB = 0x03; //Read only
daniel_davvid 0:4287b7d9c9ca 94 static const uint8_t SOC_MSB = 0x04; //Read only
daniel_davvid 0:4287b7d9c9ca 95 static const uint8_t SOC_LSB = 0x05; //Read only
daniel_davvid 0:4287b7d9c9ca 96 static const uint8_t MODE_MSB = 0x06; //Write only
daniel_davvid 0:4287b7d9c9ca 97 static const uint8_t MODE_LSB = 0x07; //Write only
daniel_davvid 0:4287b7d9c9ca 98 static const uint8_t VERSION_MSB = 0x08; //Read only
daniel_davvid 0:4287b7d9c9ca 99 static const uint8_t VERSION_LSB = 0x09; //Read only
daniel_davvid 0:4287b7d9c9ca 100 static const uint8_t CONFIG_MSB = 0x0C; //Read/write
daniel_davvid 0:4287b7d9c9ca 101 static const uint8_t CONFIG_LSB = 0x0D; //Read/write
daniel_davvid 0:4287b7d9c9ca 102 static const uint8_t COMMAND_MSB = 0xFE; //Write only
daniel_davvid 0:4287b7d9c9ca 103 static const uint8_t COMMAND_LSB = 0xFF; //Write only
daniel_davvid 0:4287b7d9c9ca 104
daniel_davvid 0:4287b7d9c9ca 105 static const uint16_t RST_CODE = 0x5400; //reset code for COMMAND 16-bit register
daniel_davvid 0:4287b7d9c9ca 106
daniel_davvid 0:4287b7d9c9ca 107 //Levels
daniel_davvid 0:4287b7d9c9ca 108 static const float DIV_VCELL = 1.25e-3; //1.25 mV/level
daniel_davvid 0:4287b7d9c9ca 109 static const float DIV_SOC = 0.00390625; //1/256% / level
daniel_davvid 0:4287b7d9c9ca 110 };
daniel_davvid 0:4287b7d9c9ca 111
daniel_davvid 0:4287b7d9c9ca 112
daniel_davvid 0:4287b7d9c9ca 113 #endif