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

Dependents:   weather_station_proj weather_station_project weather_station_proj_v1_2

Files at this revision

API Documentation at this revision

Comitter:
daniel_davvid
Date:
Sun Jul 01 12:00:52 2018 +0000
Commit message:
No significant changes

Changed in this revision

MAX17043.cpp Show annotated file Show diff for this revision Revisions of this file
MAX17043.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 4287b7d9c9ca MAX17043.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MAX17043.cpp	Sun Jul 01 12:00:52 2018 +0000
@@ -0,0 +1,118 @@
+/* MAX17043.cpp
+ * Tested with mbed board: FRDM-KL46Z
+ * Author: Mark Gottscho
+ * mgottscho@ucla.edu
+ */
+
+
+#include "mbed.h"
+#include "MAX17043.h"
+#include <new>
+
+MAX17043::MAX17043(PinName sda, PinName scl, int i2c_addr) : i2c_(*reinterpret_cast<I2C*>(i2cRaw)), __i2c_addr(i2c_addr)
+{
+    // Placement new to avoid additional heap memory allocation.
+    new(i2cRaw) I2C(sda, scl);
+
+    selfInit();
+}
+
+MAX17043::MAX17043(I2C &i2c, int i2c_addr): i2c_(i2c), __i2c_addr(i2c_addr)
+{
+    selfInit();
+}
+
+MAX17043::~MAX17043() { }
+
+void MAX17043::selfInit()
+{
+    __soc = 0;
+    __vcell = 0;
+    reset();
+}
+
+void MAX17043::reset()
+{
+    uint16_t data = RST_CODE;
+    setRegister16b(COMMAND_MSB, data);
+    wait(0.130); //wait 130ms until first readings are valid (125ms est)
+}
+
+uint16_t MAX17043::getVersion()
+{
+    uint8_t data = getRegister(VERSION_MSB);
+    return (data << 8) | (getRegister(VERSION_LSB));
+}
+
+uint16_t MAX17043::getVCell()
+{
+    uint16_t data = getRegister16b(VCELL_MSB);
+    __vcell = data >> 4; //right shift by 4 to throw out the don't care bits
+    return __vcell;
+}
+
+float MAX17043::getFloatVCell()
+{
+    return getVCell() * DIV_VCELL;
+}
+
+uint16_t MAX17043::getSOC()
+{
+    uint8_t data = getRegister(SOC_MSB);
+    __soc =  (data << 8) | (getRegister(SOC_LSB));
+    return __soc;
+}
+
+float MAX17043::getFloatSOC()
+{
+    return getSOC() * DIV_SOC;
+}
+
+
+void MAX17043::setRegister16b(const uint8_t reg_addr, const uint16_t data)
+{
+    uint8_t dataMSB = (data >> 8) & 0x00FF;
+    uint8_t dataLSB = data & 0x00FF;
+    uint8_t payload[3] = {reg_addr, dataMSB, dataLSB};
+    __writeReg(payload, 3);
+}
+void MAX17043::setRegister(const uint8_t reg_addr, const uint8_t data)
+{
+    uint8_t payload[2] = {reg_addr, data};
+    __writeReg(payload, 2);
+}
+
+uint8_t MAX17043::getRegister(const uint8_t reg_addr)
+{
+    uint8_t data;
+    __readReg(reg_addr, &data, 1);
+    return data;
+}
+
+uint16_t MAX17043::getRegister16b(const uint8_t reg_addr)
+{
+    uint8_t payload[2];
+    __readReg(reg_addr, payload, 2);
+    uint16_t data = (payload[0] << 8) | (payload[1]);
+    return data;
+}
+int MAX17043::__readReg(const uint8_t reg_addr, uint8_t *data, int len)
+{
+    int retval = 0;
+
+    retval = i2c_.write(__i2c_addr, (char *) &reg_addr, 1, true);
+    if (retval != 0) {
+        return retval;
+    }
+    retval = i2c_.read(__i2c_addr, (char *) data, len);
+
+    return retval;
+}
+
+int MAX17043::__writeReg(const uint8_t *data, int total_len)
+{
+    int retval = 0;
+    retval = i2c_.write(__i2c_addr, (char *) data, total_len);
+
+    return retval;
+}
diff -r 000000000000 -r 4287b7d9c9ca MAX17043.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MAX17043.h	Sun Jul 01 12:00:52 2018 +0000
@@ -0,0 +1,113 @@
+/* MAX17043.h
+ * Tested with mbed board: FRDM-KL46Z
+ * Author: Mark Gottscho
+ * mgottscho@ucla.edu
+ */
+ 
+#ifndef MAX17043_H
+#define MAX17043_H
+
+#include "mbed.h"
+ 
+/**
+* This class allows for easy control over a MAX17043 LiPo fuel gauge IC.
+*/
+class MAX17043 {
+    public:
+        /**
+         * @param sda the pin identifier for SDA I2C signal
+         * @param scl the pin identifier for SCL I2C signal
+         * @param i2c_addr the 8-bit I2C address for this device. Note that LSB is a don't care.
+         */
+        MAX17043(PinName sda, PinName scl, int i2c_addr=0x6c);
+        MAX17043(I2C &i2c, int i2c_addr=0x6c);
+        
+        /**
+         *
+         */
+        ~MAX17043();
+        
+        /**
+         * Initializes the device to some preferred state.
+         */
+        void selfInit();
+        
+        /**
+         * Performs a software reset of the device.
+         */
+        void reset();
+        
+        /**
+         * @returns the IC version code
+         */
+        uint16_t getVersion();
+        
+        /**
+         * @param sampleNow if true, queries the device for the sample and returns it. if false, gets the last queried value.
+         * The latter is preferred if this object is set up to sample using interrupts.
+         * @returns the battery voltage raw ADC value
+         */
+        uint16_t getVCell();
+        
+        /**
+         * @param sampleNow if true, queries the device for the sample and returns it. if false, gets the last queried value.
+         * The latter is preferred if this object is set up to sample using interrupts.
+         * @returns the battery voltage as floating point
+         */
+        float getFloatVCell();
+        
+        /**
+         * @param sampleNow if true, queries the device for the sample and returns it. if false, gets the last queried value.
+         * The latter is preferred if this object is set up to sample using interrupts.
+         * @returns the battery state of charge as computed by the ModelGauge algorithm. High byte: units of %. Low byte: units of 1/256%.
+         */
+        uint16_t getSOC();
+        
+        /**
+         * @param sampleNow if true, queries the device for the sample and returns it. if false, gets the last queried value.
+         * The latter is preferred if this object is set up to sample using interrupts.
+         * @returns the battery state of charge in %, as a floating point #
+         */
+        float getFloatSOC(); 
+        
+        uint8_t getRegister(const uint8_t reg_addr);
+        uint16_t getRegister16b(const uint8_t reg_addr);
+        void setRegister(const uint8_t reg_addr, const uint8_t data);
+        void setRegister16b(const uint8_t reg_addr, const uint16_t data);
+        int __writeReg(const uint8_t *data, int total_len);
+        int __readReg(const uint8_t reg_addr, uint8_t *data, int len);
+        
+    private:
+    
+        I2C &i2c_;
+        char i2cRaw[sizeof(I2C)];
+        
+        int __i2c_addr;
+        uint16_t __soc;
+        uint16_t __vcell;
+    
+        ///////////////// CONSTANTS /////////////////////
+        
+        //Device register addresses
+        static const uint8_t VCELL_MSB =            0x02; //Read only
+        static const uint8_t VCELL_LSB =            0x03; //Read only
+        static const uint8_t SOC_MSB =              0x04; //Read only
+        static const uint8_t SOC_LSB =              0x05; //Read only
+        static const uint8_t MODE_MSB =             0x06; //Write only
+        static const uint8_t MODE_LSB =             0x07; //Write only
+        static const uint8_t VERSION_MSB =          0x08; //Read only
+        static const uint8_t VERSION_LSB =          0x09; //Read only
+        static const uint8_t CONFIG_MSB =           0x0C; //Read/write
+        static const uint8_t CONFIG_LSB =           0x0D; //Read/write
+        static const uint8_t COMMAND_MSB =          0xFE; //Write only
+        static const uint8_t COMMAND_LSB =          0xFF; //Write only
+        
+        static const uint16_t RST_CODE =            0x5400; //reset code for COMMAND 16-bit register
+    
+        //Levels
+        static const float DIV_VCELL =              1.25e-3; //1.25 mV/level
+        static const float DIV_SOC =                0.00390625; //1/256% / level
+};
+
+ 
+ #endif
\ No newline at end of file