Skip to content

Commit f8f6529

Browse files
committed
bq27441: add driver for the LiPo Fuel Gauge
1 parent c913f20 commit f8f6529

7 files changed

Lines changed: 840 additions & 0 deletions

File tree

lib/bq27441/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# MicroPython BQ27441 Library
2+
3+
This library is a port of the [SparkFun BQ27441-G1A LiPo Fuel Gauge Arduino Libraryry](https://github.com/sparkfun/SparkFun_BQ27441_Arduino_Library).
4+
5+
The examples could be easily tested with [mpremote](https://docs.micropython.org/en/latest/reference/mpremote.html) with the following command :
6+
7+
```sh
8+
mpremote mount . run examples/fuel_gauge.py
9+
```

lib/bq27441/bq27441/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from bq27441.device import BQ27441
2+
3+
__all__ = [
4+
"BQ27441",
5+
]

lib/bq27441/bq27441/const.py

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
# Lipo Battery Capacity
2+
LIPO_BATTERY_CAPACITY = 650 # 850 mAh
3+
4+
5+
BQ27441_I2C_ADDRESS = 0x55 # Default I2C address of the BQ27441-G1A
6+
7+
# General Constants #
8+
BQ27441_UNSEAL_KEY = 0x8000 # Secret code to unseal the BQ27441-G1A
9+
BQ27441_DEVICE_ID = 0x0421 # Default device ID
10+
11+
# Standard Commands #
12+
13+
# The fuel gauge uses a series of 2-byte standard commands to enable system
14+
# reading and writing of battery information. Each command has an associated
15+
# sequential command-code pair.
16+
17+
BQ27441_COMMAND_CONTROL = 0x00 # Control()
18+
BQ27441_COMMAND_TEMP = 0x02 # Temperature()
19+
BQ27441_COMMAND_VOLTAGE = 0x04 # Voltage()
20+
BQ27441_COMMAND_FLAGS = 0x06 # Flags()
21+
BQ27441_COMMAND_NOM_CAPACITY = 0x08 # NominalAvailableCapacity()
22+
BQ27441_COMMAND_AVAIL_CAPACITY = 0x0A # FullAvailableCapacity()
23+
BQ27441_COMMAND_REM_CAPACITY = 0x0C # RemainingCapacity()
24+
BQ27441_COMMAND_FULL_CAPACITY = 0x0E # FullChargeCapacity()
25+
BQ27441_COMMAND_AVG_CURRENT = 0x10 # AverageCurrent()
26+
BQ27441_COMMAND_STDBY_CURRENT = 0x12 # StandbyCurrent()
27+
BQ27441_COMMAND_MAX_CURRENT = 0x14 # MaxLoadCurrent()
28+
BQ27441_COMMAND_AVG_POWER = 0x18 # AveragePower()
29+
BQ27441_COMMAND_SOC = 0x1C # StateOfCharge()
30+
BQ27441_COMMAND_INT_TEMP = 0x1E # InternalTemperature()
31+
BQ27441_COMMAND_SOH = 0x20 # StateOfHealth()
32+
BQ27441_COMMAND_REM_CAP_UNFL = 0x28 # RemainingCapacityUnfiltered()
33+
BQ27441_COMMAND_REM_CAP_FIL = 0x2A # RemainingCapacityFiltered()
34+
BQ27441_COMMAND_FULL_CAP_UNFL = 0x2C # FullChargeCapacityUnfiltered()
35+
BQ27441_COMMAND_FULL_CAP_FIL = 0x2E # FullChargeCapacityFiltered()
36+
BQ27441_COMMAND_SOC_UNFL = 0x30 # StateOfChargeUnfiltered()
37+
38+
# Control Sub-commands #
39+
40+
# Issuing a Control() command requires a subsequent 2-byte subcommand. These
41+
# additional bytes specify the particular control function desired. The
42+
# Control() command allows the system to control specific features of the fuel
43+
# gauge during normal operation and additional features when the device is in
44+
# different access modes.
45+
46+
BQ27441_CONTROL_STATUS = 0x00
47+
BQ27441_CONTROL_DEVICE_TYPE = 0x01
48+
BQ27441_CONTROL_FW_VERSION = 0x02
49+
BQ27441_CONTROL_DM_CODE = 0x04
50+
BQ27441_CONTROL_PREV_MACWRITE = 0x07
51+
BQ27441_CONTROL_CHEM_ID = 0x08
52+
BQ27441_CONTROL_BAT_INSERT = 0x0C
53+
BQ27441_CONTROL_BAT_REMOVE = 0x0D
54+
BQ27441_CONTROL_SET_HIBERNATE = 0x11
55+
BQ27441_CONTROL_CLEAR_HIBERNATE = 0x12
56+
BQ27441_CONTROL_SET_CFGUPDATE = 0x13
57+
BQ27441_CONTROL_SHUTDOWN_ENABLE = 0x1B
58+
BQ27441_CONTROL_SHUTDOWN = 0x1C
59+
BQ27441_CONTROL_SEALED = 0x20
60+
BQ27441_CONTROL_PULSE_SOC_INT = 0x23
61+
BQ27441_CONTROL_RESET = 0x41
62+
BQ27441_CONTROL_SOFT_RESET = 0x42
63+
BQ27441_CONTROL_EXIT_CFGUPDATE = 0x43
64+
BQ27441_CONTROL_EXIT_RESIM = 0x44
65+
66+
# Control Status Word - Bit Definitions #
67+
68+
# Bit positions for the 16-bit data of CONTROL_STATUS.
69+
# CONTROL_STATUS instructs the fuel gauge to return status information to
70+
# Control() addresses 0x00 and 0x01. The read-only status word contains status
71+
# bits that are set or cleared either automatically as conditions warrant or
72+
# through using specified subcommands.
73+
BQ27441_STATUS_SHUTDOWNEN = 1 << 15
74+
BQ27441_STATUS_WDRESET = 1 << 14
75+
BQ27441_STATUS_SS = 1 << 13
76+
BQ27441_STATUS_CALMODE = 1 << 12
77+
BQ27441_STATUS_CCA = 1 << 11
78+
BQ27441_STATUS_BCA = 1 << 10
79+
BQ27441_STATUS_QMAX_UP = 1 << 9
80+
BQ27441_STATUS_RES_UP = 1 << 8
81+
BQ27441_STATUS_INITCOMP = 1 << 7
82+
BQ27441_STATUS_HIBERNATE = 1 << 6
83+
BQ27441_STATUS_SLEEP = 1 << 4
84+
BQ27441_STATUS_LDMD = 1 << 3
85+
BQ27441_STATUS_RUP_DIS = 1 << 2
86+
BQ27441_STATUS_VOK = 1 << 1
87+
88+
89+
# Flag Command - Bit Definitions #
90+
91+
# Bit positions for the 16-bit data of Flags()
92+
# This read-word function returns the contents of the fuel gauging status
93+
# register, depicting the current operating status.
94+
BQ27441_FLAG_OT = 1 << 15
95+
BQ27441_FLAG_UT = 1 << 14
96+
BQ27441_FLAG_FC = 1 << 9
97+
BQ27441_FLAG_CHG = 1 << 8
98+
BQ27441_FLAG_OCVTAKEN = 1 << 7
99+
BQ27441_FLAG_ITPOR = 1 << 5
100+
BQ27441_FLAG_CFGUPMODE = 1 << 4
101+
BQ27441_FLAG_BAT_DET = 1 << 3
102+
BQ27441_FLAG_SOC1 = 1 << 2
103+
BQ27441_FLAG_SOCF = 1 << 1
104+
BQ27441_FLAG_DSG = 1 << 0
105+
106+
107+
# Extended Data Commands #
108+
109+
# Extended data commands offer additional functionality beyond the standard
110+
# set of commands. They are used in the same manner; however, unlike standard
111+
# commands, extended commands are not limited to 2-byte words.
112+
BQ27441_EXTENDED_OPCONFIG = 0x3A # OpConfig()
113+
BQ27441_EXTENDED_CAPACITY = 0x3C # DesignCapacity()
114+
BQ27441_EXTENDED_DATACLASS = 0x3E # DataClass()
115+
BQ27441_EXTENDED_DATABLOCK = 0x3F # DataBlock()
116+
BQ27441_EXTENDED_BLOCKDATA = 0x40 # BlockData()
117+
BQ27441_EXTENDED_CHECKSUM = 0x60 # BlockDataCheckSum()
118+
BQ27441_EXTENDED_CONTROL = 0x61 # BlockDataControl()
119+
120+
121+
# Configuration Class, Subclass ID's #
122+
123+
# To access a subclass of the extended data, set the DataClass() function
124+
# with one of these values.
125+
# Configuration Classes
126+
BQ27441_ID_SAFETY = 2 # Safety
127+
BQ27441_ID_CHG_TERMINATION = 36 # Charge Termination
128+
BQ27441_ID_CONFIG_DATA = 48 # Data
129+
BQ27441_ID_DISCHARGE = 49 # Discharge
130+
BQ27441_ID_REGISTERS = 64 # Registers
131+
BQ27441_ID_POWER = 68 # Power
132+
# Gas Gauging Classes
133+
BQ27441_ID_IT_CFG = 80 # IT Cfg
134+
BQ27441_ID_CURRENT_THRESH = 81 # Current Thresholds
135+
BQ27441_ID_STATE = 82 # State
136+
# Ra Tables Classes
137+
BQ27441_ID_R_A_RAM = 89 # R_a RAM
138+
# Calibration Classes
139+
BQ27441_ID_CALIB_DATA = 104 # Data
140+
BQ27441_ID_CC_CAL = 105 # CC Cal
141+
BQ27441_ID_CURRENT = 107 # Current
142+
# Security Classes
143+
BQ27441_ID_CODES = 112 # Codes
144+
145+
146+
# OpConfig Register - Bit Definitions #
147+
148+
# Bit positions of the OpConfig Register
149+
BQ27441_OPCONFIG_BIE = 1 << 13
150+
BQ27441_OPCONFIG_BI_PU_EN = 1 << 12
151+
BQ27441_OPCONFIG_GPIOPOL = 1 << 11
152+
BQ27441_OPCONFIG_SLEEP = 1 << 5
153+
BQ27441_OPCONFIG_RMFCC = 1 << 4
154+
BQ27441_OPCONFIG_BATLOWEN = 1 << 2
155+
BQ27441_OPCONFIG_TEMPS = 1 << 0
156+
157+
BQ27441_I2C_TIMEOUT = 2000 # ms

0 commit comments

Comments
 (0)