A no_std Rust driver for the Texas Instruments BQ27441-G1 battery fuel gauge IC.
- I²C Communication: Full support for blocking and async I²C operations
- Comprehensive Battery Monitoring:
- State of Charge (SOC) - 0-100%
- Voltage measurements (mV)
- Current measurements (mA)
- Temperature readings (°C or 0.1K)
- Remaining capacity (mAh)
- Full charge capacity (mAh)
- State of Health (SOH) percentage
- Average power (mW)
- Power Management: Support for NORMAL, SLEEP, HIBERNATE, and SHUTDOWN modes
- Configuration: Data Memory access for customization
- Security: SEALED/UNSEALED modes
- YAML-Based Register Definitions: Uses
device-drivercrate for type-safe register access - Async Support: Optional async/await support via
embedded-hal-async - Embassy Compatible: Works seamlessly with the Embassy framework
- defmt Support: Optional defmt logging for debugging
The BQ27441-G1 is a System-Side Impedance Track™ Fuel Gauge for single-cell Li-Ion batteries.
- I²C Address: 0x55 (default)
- Supply Voltage: 1.8V - 3.6V
- Sense Resistor: Typically 10mΩ between SRN and SRP pins
- Variants:
- BQ27441-G1A: For 4.2V maximum charge voltage batteries
- BQ27441-G1B: For 4.3V or 4.35V maximum charge voltage batteries
use bq27441::Bq27441;
use embedded_hal::i2c::I2c;
// Create driver with default I2C address (0x55)
let mut gauge = Bq27441::new(i2c)?;
// Read battery status
let voltage = gauge.voltage()?; // mV
let soc = gauge.state_of_charge()?; // %
let capacity = gauge.remaining_capacity()?; // mAh
let current = gauge.average_current()?; // mA (signed)
let temp = gauge.temperature_celsius()?; // °C
// Check charging status
if gauge.is_charging()? {
println!("Battery is charging");
}
if gauge.is_full_charged()? {
println!("Battery is fully charged");
}use bq27441::Bq27441Async;
use embedded_hal_async::i2c::I2c;
// Create async driver
let mut gauge = Bq27441Async::new(i2c).await?;
// Read battery status asynchronously
let voltage = gauge.voltage().await?;
let soc = gauge.state_of_charge().await?;
let capacity = gauge.remaining_capacity().await?;// Unseal device for configuration
gauge.unseal()?;
// Enter config update mode
gauge.enter_config_mode()?;
// Modify configuration via Data Memory
// (See datasheet for details)
// Exit config mode
gauge.exit_config_mode()?;
// Seal device to protect configuration
gauge.seal()?;See the examples/ directory for complete working examples:
stm32wba65ri_embassy.rs- Embassy async example for STM32WBA65RI
To run an example:
cargo build --example stm32wba65ri_embassy --features embassy --target thumbv8m.main-none-eabihfvoltage()- Read battery voltage in mVstate_of_charge()- Read SOC percentage (0-100%)remaining_capacity()- Read remaining capacity in mAhfull_charge_capacity()- Read full charge capacity in mAhaverage_current()- Read average current in mA (signed)average_power()- Read average power in mW (signed)temperature_celsius()- Read temperature in °Cstate_of_health()- Read SOH percentage
is_battery_detected()- Check if battery is connectedis_charging()- Check if battery is chargingis_discharging()- Check if battery is dischargingis_full_charged()- Check if battery is fully chargedflags()- Read all status flags
control_read(cmd)- Send control subcommand and read responsecontrol_write(cmd)- Send control subcommandfirmware_version()- Read firmware versionchemistry_id()- Read chemistry IDseal()- Enter SEALED modeunseal()- Exit SEALED modeenter_config_mode()- Enter CONFIG UPDATE modeexit_config_mode()- Exit CONFIG UPDATE modeset_hibernate()/clear_hibernate()- Hibernate mode control
Enable optional features in your Cargo.toml:
[dependencies]
bq27441 = { version = "0.1", features = ["async", "defmt-03"] }Available features:
async- Enable async/await supportdefmt-03- Enable defmt loggingembassy- Enable both async and defmt (convenience feature)
For low-level register access, use the .device() method:
// Access generated device API directly
let flags = gauge.device().flags().read()?;
if flags.bat_det() {
println!("Battery detected");
}# Build for embedded target
cargo build --target thumbv8m.main-none-eabihf
# Build with async support
cargo build --target thumbv8m.main-none-eabihf --features async
# Build with Embassy + defmt
cargo build --target thumbv8m.main-none-eabihf --features embassyRegister definitions are maintained in src/bq27441.yaml using the device-driver crate's YAML format. The build script (build.rs) generates type-safe register access code at compile time.
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.