State of Charge - MAX17261
Driver Initialization
To initialize the max17261 driver you need to provide the the max17261_init function with a reference to a Max17261Storage struct and a populated Max17261Settings struct.
Calculating Max17261Settings Values
// found in fwxv/libraries/ms-drivers/inc/max17261_fuel_gauge.h
typedef struct {
I2CPort i2c_port;
I2CAddress i2c_address;
uint16_t design_capacity; // LSB = 5.0 (micro Volt Hours / R Sense)
uint16_t empty_voltage; // Only a 9-bit field, LSB = 78.125 (micro Volts)
uint16_t charge_term_current; // LSB = 1.5625 (micro Volts / R Sense)
uint16_t r_sense_ohms;
} Max17261Settings;
The first two members of the struct will just be the I2C port and address corresponding to the device. The slave address for the Max17261 is 0x6C. Note that the last bit is used to indicate whether you are reading or writing so only the first 7 bits need to match 0x6C.
The following 3 members are battery characteristics used by the fuel gauge. They do not map directly to real world units but instead each field has a LSB (least significant bit) value which indicates the value in real world units of a single bit.
Taking design capacity as an example. First we need to determine the value of the LSB. If the sense resistor (a resistor used for calculating current) has a value of 2 ohms then our LSB would have a value of 5/2 = 2.5. The units for the LSB is micro volts hours / ohms which is just micro amp hours. If we have a battery that has a capacity of 1000000 micro amp hours, the value of the design_capacity field will be (1000000 / 2.5) = 400000.
Using Driver
The drivers functions follow the same format.
StatusCode max17261_characteristic(Max17261Storage* storage, uint16_t* return_value_units)
You supply a reference to an already initialized Max17261Storage struct and a reference to a variable that will be used to store the given characteristic of the battery and a status code is returned by the function indicating success or failure. The units of the return variable are indicated by the name of the variable but a table summary is shown below.
Function | Units | Description |
---|---|---|
max17261_state_of_charge | percentage (0-100) | current state of charge in percentage |
max17261_remaining_capacity | micro amp hours | current remaining capacity of the battery |
max17261_full_capacity | micro amp hours | capacity of the battery at full charge |
max17261_time_to_empty | milliseconds | Time until battery becomes empty (only valid when battery is discharging) |
max17261_time_to_full | milliseconds | Time until battery becomes full (only valid when battery is charging) |