Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
[package]
name = "bq40z50-rx"
version = "0.8.0"
version = "0.8.1"
repository = "https://github.com/OpenDevicePartnership/bq40z50"
license = "MIT"
authors = ["Matteo Tullo <matteotullo@microsoft.com>"]
rust-version = "1.85"
description = "Platform-agnostic Rust driver for the Texas Instruments BQ40Z50 battery fuel (gas) gauge."
readme = "README.md"
Expand Down
11 changes: 9 additions & 2 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ pub struct Config {
pub timeout: embassy_time::Duration,
}

impl Default for Config {
fn default() -> Self {
impl Config {
#[must_use]
pub const fn new() -> Self {
Self {
max_bus_retries: crate::consts::DEFAULT_BUS_RETRIES,
pec_read: false,
Expand All @@ -25,6 +26,12 @@ impl Default for Config {
}
}

impl Default for Config {
fn default() -> Self {
Self::new()
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) enum CapacityModeState {
Milliamps = 0,
Expand Down
10 changes: 8 additions & 2 deletions src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,19 @@ pub struct DeviceInterface<I2C: I2cTrait, DELAY: DelayTrait> {
}

impl<I2C: I2cTrait, DELAY: DelayTrait> DeviceInterface<I2C, DELAY> {
pub fn new(i2c: I2C, delay: DELAY) -> Self {
#[must_use]
pub const fn new(i2c: I2C, delay: DELAY) -> Self {
DeviceInterface {
i2c,
delay,
config: Config::default(),
config: Config::new(),
}
}

#[must_use]
pub const fn new_with_config(i2c: I2C, delay: DELAY, config: Config) -> Self {
DeviceInterface { i2c, delay, config }
}
}

impl<I2C: I2cTrait, DELAY: DelayTrait> DeviceInterface<I2C, DELAY> {
Expand Down
57 changes: 49 additions & 8 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,47 @@ macro_rules! bq40z50_tests {
EXECUTOR.init(embassy_executor::Executor::new())
}

#[tokio::test]
async fn update_config() {
let expectations = vec![
Transaction::write(BQ_ADDR, vec![0x44, 0x02, 0x02, 0x00, 0x46]),
Transaction::write_read(
BQ_ADDR,
vec![0x44],
vec![
0x0A, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0,
],
),
Transaction::write(BQ_ADDR, vec![0x44, 0x02, 0x02, 0x00]),
Transaction::write_read(
BQ_ADDR,
vec![0x44],
vec![
0x0A, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
],
),
];
let i2c = Mock::new(&expectations);
let mut bq = Bq40z50::new_with_config(
i2c,
NoopDelay::new(),
Config {
pec_read: true,
..Default::default()
},
);

bq.device.mac_firmware_version().dispatch_async().await.unwrap();

// Change the device config to not use PEC.
let mut config = bq.config();
config.pec_read = false;
bq.update_config(config);
bq.device.mac_firmware_version().dispatch_async().await.unwrap();

bq.device.interface.i2c.done();
}

#[tokio::test]
async fn read_chip_id() {
let expectations = vec![Transaction::write(BQ_ADDR, vec![0x44, 0x02, 0x21, 0x00])];
Expand All @@ -36,14 +77,14 @@ macro_rules! bq40z50_tests {
async fn read_chip_id_pec() {
let expectations = vec![Transaction::write(BQ_ADDR, vec![0x44, 0x02, 0x21, 0x00, 0xD7])];
let i2c = Mock::new(&expectations);
let mut bq = Device::new(DeviceInterface {
let mut bq = Device::new(DeviceInterface::new_with_config(
i2c,
delay: NoopDelay::new(),
config: Config {
NoopDelay::new(),
Config {
pec_write: true,
..Default::default()
},
});
));

bq.mac_gauging().dispatch_async().await.unwrap();

Expand Down Expand Up @@ -95,14 +136,14 @@ macro_rules! bq40z50_tests {
),
];
let i2c = Mock::new(&expectations);
let mut bq = Device::new(DeviceInterface {
let mut bq = Device::new(DeviceInterface::new_with_config(
i2c,
delay: NoopDelay::new(),
config: Config {
NoopDelay::new(),
Config {
pec_read: true,
..Default::default()
},
});
));

bq.mac_firmware_version().dispatch_async().await.unwrap();
bq.interface.i2c.done();
Expand Down
14 changes: 13 additions & 1 deletion src/versions/r1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,23 @@ impl<I2C: I2cTrait, DELAY: DelayTrait> Bq40z50R1<I2C, DELAY> {

pub fn new_with_config(i2c: I2C, delay: DELAY, config: Config) -> Self {
Bq40z50R1 {
device: Device::new(DeviceInterface { i2c, delay, config }),
device: Device::new(DeviceInterface::new_with_config(i2c, delay, config)),
capacity_mode_state: Cell::new(CapacityModeState::Milliamps),
}
}

/// Change interface config.
///
/// Concurrency is guaranteed by the mutable borrow, ensuring the config cannot change
/// while a register transaction is in flight.
pub fn update_config(&mut self, config: Config) {
self.device.interface.config = config;
}

pub fn config(&self) -> Config {
self.device.interface.config
}

fn set_capacity_mode_state(&self, battery_mode_fields: BatteryModeFields) {
self.capacity_mode_state.set(if battery_mode_fields.capacity_mode() {
CapacityModeState::Centiwatt
Expand Down
14 changes: 13 additions & 1 deletion src/versions/r3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,23 @@ impl<I2C: I2cTrait, DELAY: DelayTrait> Bq40z50R3<I2C, DELAY> {

pub fn new_with_config(i2c: I2C, delay: DELAY, config: Config) -> Self {
Bq40z50R3 {
device: Device::new(DeviceInterface { i2c, delay, config }),
device: Device::new(DeviceInterface::new_with_config(i2c, delay, config)),
capacity_mode_state: Cell::new(CapacityModeState::Milliamps),
}
}

/// Change interface config.
///
/// Concurrency is guaranteed by the mutable borrow, ensuring the config cannot change
/// while a register transaction is in flight.
pub fn update_config(&mut self, config: Config) {
self.device.interface.config = config;
}

pub fn config(&self) -> Config {
self.device.interface.config
}

fn set_capacity_mode_state(&self, battery_mode_fields: BatteryModeFields) {
self.capacity_mode_state.set(if battery_mode_fields.capacity_mode() {
CapacityModeState::Centiwatt
Expand Down
14 changes: 13 additions & 1 deletion src/versions/r4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,23 @@ impl<I2C: I2cTrait, DELAY: DelayTrait> Bq40z50R4<I2C, DELAY> {

pub fn new_with_config(i2c: I2C, delay: DELAY, config: Config) -> Self {
Bq40z50R4 {
device: Device::new(DeviceInterface { i2c, delay, config }),
device: Device::new(DeviceInterface::new_with_config(i2c, delay, config)),
capacity_mode_state: Cell::new(CapacityModeState::Milliamps),
}
}

/// Change interface config.
///
/// Concurrency is guaranteed by the mutable borrow, ensuring the config cannot change
/// while a register transaction is in flight.
pub fn update_config(&mut self, config: Config) {
self.device.interface.config = config;
}

pub fn config(&self) -> Config {
self.device.interface.config
}

fn set_capacity_mode_state(&self, battery_mode_fields: BatteryModeFields) {
self.capacity_mode_state.set(if battery_mode_fields.capacity_mode() {
CapacityModeState::Centiwatt
Expand Down
14 changes: 13 additions & 1 deletion src/versions/r5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,23 @@ impl<I2C: I2cTrait, DELAY: DelayTrait> Bq40z50R5<I2C, DELAY> {

pub fn new_with_config(i2c: I2C, delay: DELAY, config: Config) -> Self {
Bq40z50R5 {
device: Device::new(DeviceInterface { i2c, delay, config }),
device: Device::new(DeviceInterface::new_with_config(i2c, delay, config)),
capacity_mode_state: Cell::new(CapacityModeState::Milliamps),
}
}

/// Change interface config.
///
/// Concurrency is guaranteed by the mutable borrow, ensuring the config cannot change
/// while a register transaction is in flight.
pub fn update_config(&mut self, config: Config) {
self.device.interface.config = config;
}

pub fn config(&self) -> Config {
self.device.interface.config
}

fn set_capacity_mode_state(&self, battery_mode_fields: BatteryModeFields) {
self.capacity_mode_state.set(if battery_mode_fields.capacity_mode() {
CapacityModeState::Centiwatt
Expand Down