Skip to content

Commit 8d761e7

Browse files
committed
Add fuel gauge driver
1 parent 5eb4791 commit 8d761e7

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

src/MAX1726Driver.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include "WireUtils.h"
2+
// TODO extract into a separate header file and check how PF1550Driver is implemented
3+
#include "BatteryConstants.h"
4+
5+
enum class FuelGaugeOperationMode
6+
{
7+
hibernate,
8+
shutdown,
9+
active
10+
};
11+
12+
// TODO Extract into a separate header file or use this file in Battery.h
13+
constexpr uint8_t I2C_ADDRESS = 0x36; // I2C address of the fuel gauge
14+
15+
class MAX1726Driver {
16+
private:
17+
TwoWire *wire;
18+
public:
19+
bool setOperationMode(FuelGaugeOperationMode mode);
20+
MAX1726Driver(TwoWire *wire);
21+
~MAX1726Driver();
22+
};
23+
24+
MAX1726Driver::MAX1726Driver(TwoWire *wire) {
25+
this->wire = wire;
26+
}
27+
28+
MAX1726Driver::~MAX1726Driver(){}
29+
30+
bool MAX1726Driver::setOperationMode(FuelGaugeOperationMode mode) {
31+
if(mode == FuelGaugeOperationMode::active){
32+
// See section "Soft-Wakeup" in user manual https://www.analog.com/media/en/technical-documentation/user-guides/max1726x-modelgauge-m5-ez-user-guide.pdf
33+
writeRegister16Bits(this->wire, I2C_ADDRESS, HIB_CFG_REG, 0x0); // Exit Hibernate Mode
34+
writeRegister16Bits(this->wire, I2C_ADDRESS, SOFT_WAKEUP_REG, 0x90); // Wakes up the fuel gauge from hibernate mode to reduce the response time of the IC to configuration changes
35+
writeRegister16Bits(this->wire, I2C_ADDRESS, SOFT_WAKEUP_REG, 0x0); // This command must be manually cleared (0x0000) afterward to keep proper fuel gauge timing
36+
} else if(mode == FuelGaugeOperationMode::hibernate){
37+
// Enters hibernate mode somewhere between 2.812s and 5.625s if the threshold conditions are met
38+
replaceRegisterBit(this->wire, I2C_ADDRESS, HIB_CFG_REG, 1, EN_HIBERNATION_BIT); // Enter Hibernate Mode
39+
} else if(mode == FuelGaugeOperationMode::shutdown){
40+
// Enter active mode
41+
// TODO use function instead
42+
replaceRegisterBit(this->wire, I2C_ADDRESS, HIB_CFG_REG, 0, EN_HIBERNATION_BIT); // Exit Hibernate Mode
43+
// TODO check if we can enter shutdown quicker by setting CTR manually in ShdnTimer
44+
// The default shutdown timeout is 45s
45+
replaceRegisterBit(this->wire, I2C_ADDRESS, CONFIG_REG, 1, SHDN_BIT); // Command shutdown mode
46+
}
47+
48+
return false;
49+
}

0 commit comments

Comments
 (0)