Skip to content

Commit 5f21bbd

Browse files
committed
added some hibernation and alert stuff
1 parent a696788 commit 5f21bbd

File tree

2 files changed

+115
-9
lines changed

2 files changed

+115
-9
lines changed

adafruit_max1704x.py

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@
5050
_MAX1704X_STATUS_REG = const(0x1A)
5151
_MAX1704X_CMD_REG = const(0xFE)
5252

53+
ALERTFLAG_SOC_CHANGE = 0x20
54+
ALERTFLAG_SOC_LOW = 0x10
55+
ALERTFLAG_VOLTAGE_RESET = 0x08
56+
ALERTFLAG_VOLTAGE_LOW = 0x04
57+
ALERTFLAG_VOLTAGE_HIGH = 0x02
58+
ALERTFLAG_RESET_INDICATOR = 0x01
59+
5360
class MAX17048:
5461
"""Driver for the MAX1704X battery fuel gauge.
5562
:param ~busio.I2C i2c_bus: The I2C bus the MAX1704X is connected to.
@@ -58,18 +65,28 @@ class MAX17048:
5865

5966
chip_version = ROUnaryStruct(_MAX1704X_VERSION_REG, ">H")
6067
_config = ROUnaryStruct(_MAX1704X_CONFIG_REG, ">H")
68+
# expose the config bits
69+
sleep = RWBit(_MAX1704X_CONFIG_REG+1, 7, register_width=2, lsb_first=False)
70+
_alert_status = RWBit(_MAX1704X_CONFIG_REG+1, 5, register_width=2, lsb_first=False)
71+
enable_sleep = RWBit(_MAX1704X_MODE_REG, 5)
72+
hibernating = ROBit(_MAX1704X_MODE_REG, 4)
73+
6174
_cmd = UnaryStruct(_MAX1704X_CMD_REG, ">H")
6275
_status = ROUnaryStruct(_MAX1704X_STATUS_REG, ">B")
6376
_cell_voltage = ROUnaryStruct(_MAX1704X_VCELL_REG, ">H")
6477
_cell_SOC = ROUnaryStruct(_MAX1704X_SOC_REG, ">B")
6578
_hibrt_actthr = UnaryStruct(_MAX1704X_HIBRT_REG+1, ">B")
6679
_hibrt_hibthr = UnaryStruct(_MAX1704X_HIBRT_REG, ">B")
80+
_valrt_min = UnaryStruct(_MAX1704X_VALERT_REG, ">B")
81+
_valrt_max = UnaryStruct(_MAX1704X_VALERT_REG+1, ">B")
82+
6783
# expose the alert bits
68-
reset_alert = ROBit(_MAX1704X_STATUS_REG, 0)
69-
voltage_high_alert = ROBit(_MAX1704X_STATUS_REG, 1)
70-
voltage_low_alert = ROBit(_MAX1704X_STATUS_REG, 2)
71-
voltage_reset_alert = ROBit(_MAX1704X_STATUS_REG, 3)
72-
SOC_change_alert = ROBit(_MAX1704X_STATUS_REG, 5)
84+
reset_alert = RWBit(_MAX1704X_STATUS_REG, 0)
85+
voltage_high_alert = RWBit(_MAX1704X_STATUS_REG, 1)
86+
voltage_low_alert = RWBit(_MAX1704X_STATUS_REG, 2)
87+
voltage_reset_alert = RWBit(_MAX1704X_STATUS_REG, 3)
88+
SOC_low_alert = RWBit(_MAX1704X_STATUS_REG, 4)
89+
SOC_change_alert = RWBit(_MAX1704X_STATUS_REG, 5)
7390

7491
chip_id = ROUnaryStruct(_MAX1704X_CHIPID_REG, ">B")
7592

@@ -80,14 +97,18 @@ def __init__(self, i2c_bus, address=MAX1704X_I2CADDR_DEFAULT):
8097
if self.chip_version & 0xFFF0 != 0x0010:
8198
raise RuntimeError("Failed to find MAX1704X - check your wiring!")
8299
self.reset()
100+
self.enable_sleep = False
101+
self.sleep = False
83102

84103
def reset(self):
85104
try:
86105
self._cmd = 0x5400
87106
except OSError:
88107
# aha! we NACKed, which is CORRECT!
89-
return self.reset_alert # we should have reset
90-
raise RuntimeException("Reset did not succeed")
108+
pass
109+
else:
110+
raise RuntimeError("Reset did not succeed")
111+
self.reset_alert = False # clean up RI alert
91112

92113
@property
93114
def cell_voltage(self):
@@ -97,6 +118,36 @@ def cell_voltage(self):
97118
def cell_percent(self):
98119
return self._cell_SOC
99120

121+
122+
@property
123+
def voltage_alert_min(self):
124+
return self._valrt_min * .02 # 20mV / LSB
125+
126+
@voltage_alert_min.setter
127+
def voltage_alert_min(self, minvoltage):
128+
if (not 0 <= minvoltage <= (255 * 0.02)):
129+
raise ValueError("Alert voltage must be between 0 and 5.1 Volts")
130+
self._valrt_min = int(minvoltage / .02) # 20mV / LSB
131+
132+
@property
133+
def voltage_alert_max(self):
134+
return self._valrt_max * .02 # 20mV / LSB
135+
136+
@voltage_alert_max.setter
137+
def voltage_alert_max(self, maxvoltage):
138+
if (not 0 <= maxvoltage <= (255 * 0.02)):
139+
raise ValueError("Alert voltage must be between 0 and 5.1 Volts")
140+
self._valrt_max = int(maxvoltage / .02) # 20mV / LSB
141+
142+
@property
143+
def active_alert(self):
144+
return self._alert_status
145+
146+
@property
147+
def alert_reason(self):
148+
return self._status & 0x3F
149+
150+
100151
@property
101152
def activity_threshold(self):
102153
return self._hibrt_actthr * 0.00125 # 1.25mV per LSB
@@ -118,3 +169,19 @@ def hibernation_threshold(self, threshold_percent):
118169
raise ValueError("Activity percentage/hour change must be between 0 and 53%")
119170
self._hibrt_hibthr = int(threshold_percent / 0.208) # 0.208% per hour
120171

172+
173+
def hibernate(self):
174+
"""Setup thresholds for hibernation to go into hibernation mode immediately.
175+
See datasheet: HIBRT Register (0x0A) To disable hibernate mode, set HIBRT = 0x0000. To
176+
always use hibernate mode, set HIBRT = 0xFFFF. Can check status with `self.hibernating`"""
177+
self._hibrt_hibthr = 0xFF
178+
self._hibrt_actthr = 0xFF
179+
180+
def disable_hibernation(self):
181+
"""Setup thresholds for hibernation to leave hibernation mode immediately.
182+
See datasheet: HIBRT Register (0x0A) To disable hibernate mode, set HIBRT = 0x0000. To
183+
always use hibernate mode, set HIBRT = 0xFFFF. Can check status with `self.hibernating`"""
184+
self._hibrt_hibthr = 0
185+
self._hibrt_actthr = 0
186+
187+

examples/max1704x_simpletest.py

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,50 @@
1919
#max17.activity_threshold = 0.15
2020
#print("MAX1704x activity threshold = %0.2f V" % max17.activity_threshold)
2121

22-
max17.hibernation_threshold = 5
23-
print("MAX1704x hibernation threshold = %0.2f %%" % max17.hibernation_threshold)
22+
#max17.hibernation_threshold = 5
23+
#print("MAX1704x hibernation threshold = %0.2f %%" % max17.hibernation_threshold)
2424

25+
#max17.hibernate()
26+
27+
#max17.enable_sleep = True
28+
#max17.sleep = True
29+
30+
max17.voltage_alert_min = 3.7
31+
print("Voltage alert minimum = %0.2f V" % max17.voltage_alert_min)
32+
#max17.voltage_alert_max = 3.8
33+
#print("Voltage alert maximum = %0.2f V" % max17.voltage_alert_max)
2534

2635
while True:
2736
print("Battery voltage:", max17.cell_voltage, "V")
2837
print("Battery state :", max17.cell_percent, "%")
38+
39+
if max17.hibernating:
40+
print("Hibernating!")
41+
42+
if max17.active_alert:
43+
print("Alert!")
44+
if max17.reset_alert:
45+
print(" Reset indicator")
46+
max17.reset_alert = False # clear the alert
47+
48+
if max17.voltage_high_alert:
49+
print(" Voltage high")
50+
max17.voltage_high_alert = False # clear the alert
51+
52+
if max17.voltage_low_alert:
53+
print(" Voltage low")
54+
max17.voltage_low_alert = False # clear the alert
55+
56+
if max17.voltage_reset_alert:
57+
print(" Voltage reset")
58+
max17.voltage_reset_alert = False # clear the alert
59+
60+
if max17.SOC_low_alert:
61+
print(" Charge low")
62+
max17.SOC_low_alert = False # clear the alert
63+
64+
if max17.SOC_change_alert:
65+
print(" Charge changed")
66+
max17.SOC_change_alert = False # clear the alert
67+
2968
time.sleep(1)

0 commit comments

Comments
 (0)