Skip to content

Commit 0429e21

Browse files
committed
bq27441: add driver for the LiPo Fuel Gauge
1 parent aaec802 commit 0429e21

7 files changed

Lines changed: 844 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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from bq27441.device import BQ27441
2+
3+
__all__ = [ 'BQ27441', ]

lib/bq27441/bq27441/const.py

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

0 commit comments

Comments
 (0)