| Crates.io | bq25887 |
| lib.rs | bq25887 |
| version | 0.1.2 |
| created_at | 2025-12-10 05:07:38.671077+00 |
| updated_at | 2025-12-12 01:15:15.753134+00 |
| description | Device driver for the Texas Instruments BQ25887 linear battery chargers. |
| homepage | |
| repository | https://github.com/leftger/bq25887 |
| max_upload_size | |
| id | 1977477 |
| size | 152,915 |
bq25887Platform-agnostic Rust driver for the Texas Instruments BQ25887 synchronous battery charger. The crate targets no_std environments, provides an async-friendly I²C interface built on embedded-hal-async, and generates its register API at build time from a YAML manifest.
Bq25887Driver) built on embedded-hal-async::i2c::I2c.src/bq25887.yaml).defmt-03 and log feature flags.no_std compatible (only depends on alloc-free crates by default).Add the crate to your Cargo.toml:
[dependencies]
bq25887 = "0.1"
embedded-hal-async = "1.0"
# Optional feature flags:
# bq25887 = { version = "0.1", features = ["defmt-03"] }
# bq25887 = { version = "0.1", features = ["log"] }
Use any I²C type that implements embedded_hal_async::i2c::I2c to talk to the charger:
use bq25887::Bq25887Driver;
pub async fn configure_charger<I2C>(i2c: I2C) -> Result<(), bq25887::BQ25887Error<I2C::Error>>
where
I2C: embedded_hal_async::i2c::I2c,
{
let mut driver = Bq25887Driver::new(i2c);
// Refresh cached register view, including the cell voltage limit (register 0x00)
driver.refresh_configuration_cache().await?;
let limit = driver
.configuration_cache()
.cell_voltage_limit
.expect("cache populated");
// Raise the regulation voltage slightly (example value only!)
let new_limit = limit.with_vcellreg(limit.vcellreg().saturating_add(1));
driver.write_voltage_regulation_limit(new_limit).await?;
// Check charger status after applying the new limit
let status = driver.read_charger_status_1().await?;
if status.chrg_stat() != 0 {
// handle charger state change here (for example, log telemetry or notify a power-management state machine)
}
Ok(())
}
The driver exposes high-level methods for each register group, e.g. read_charge_current_limit, write_input_voltage_limit, and friends. These helpers wrap the generated register accessors and convert conversion errors into the crate’s custom error type.
Enable the embassy feature to pull in the bq25887::embassy module. It provides thin wrappers around embassy_embedded_hal::shared_bus::asynch::i2c so you can build a driver from a shared Mutex-guarded bus via new_driver, or apply per-device bus settings with new_driver_with_config. These helpers make it straightforward to share one I²C peripheral across multiple Embassy tasks while keeping charger operations serialized.
use bq25887::embassy::{new_driver, SharedBus};
use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex;
use embassy_sync::mutex::Mutex;
// Replace `MyAsyncI2c` with your HAL's async I²C peripheral.
static BUS: SharedBus<ThreadModeRawMutex, MyAsyncI2c> = Mutex::new(MyAsyncI2c::new());
async fn poll_charger() -> Result<(), bq25887::BQ25887Error<MyAsyncI2c::Error>> {
let mut driver = new_driver(&BUS);
let status = driver.read_charger_status_1().await?;
// Inspect `status` or update application state.
Ok(())
}
| Feature | Default | Description |
|---|---|---|
defmt-03 |
✗ | Enables defmt formatting support for logging-friendly targets |
embassy |
✗ | Enables Embassy shared-bus helpers via the bq25887::embassy module |
log |
✗ | Enables log crate integration for environments that use log backends |
Rust 1.85 or newer is required. The MSRV will only increase in minor releases and will be documented in the changelog.
During cargo build, build.rs reads src/bq25887.yaml, generates the register accessor API, and writes it into $OUT_DIR. No manual steps are necessary for downstream users.
cargo fmt --all
cargo clippy --all-targets --all-features
cargo test --all-features
cargo publish --dry-run
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the project by you shall be dual licensed as above, without any additional terms or conditions.