bq27441 0.1.0

Blocking and async driver for the BQ27441 battery fuel gauge with I2C support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
//! High-level BQ27441 battery fuel gauge driver with synchronous and asynchronous support.
//!
//! This driver provides access to the BQ27441-G1 battery fuel gauge IC from Texas Instruments.
//! It supports I²C communication with both blocking and async modes.
//!
//! # Features
//!
//! - State of Charge (SOC) monitoring
//! - Voltage, current, and temperature measurements
//! - Remaining capacity and full charge capacity reporting
//! - Power and current averaging
//! - State of Health (SOH) reporting
//! - Configuration via Data Memory
//! - SEALED/UNSEALED access modes
//! - Multiple power modes (NORMAL, SLEEP, HIBERNATE, SHUTDOWN)
//!
//! # Examples
//!
//! ## Blocking I2C
//!
//! ```no_run
//! use bq27441::Bq27441;
//! # use embedded_hal::i2c::I2c;
//! # fn example<I2C: I2c>(i2c: I2C) -> Result<(), bq27441::Error<I2C::Error>>
//! # where I2C::Error: core::fmt::Debug {
//!
//! // Create driver with default I2C address (0x55)
//! let mut gauge = Bq27441::new(i2c)?;
//!
//! // Read battery voltage
//! let voltage_mv = gauge.voltage()?;
//!
//! // Read state of charge (0-100%)
//! let soc = gauge.state_of_charge()?;
//!
//! // Read remaining capacity
//! let capacity_mah = gauge.remaining_capacity()?;
//!
//! # Ok(())
//! # }
//! ```
//!
//! ## Async I2C
//!
//! ```ignore
//! use bq27441::Bq27441Async;
//! # use embedded_hal_async::i2c::I2c;
//! # async fn example<I2C: I2c>(i2c: I2C) -> Result<(), bq27441::Error<I2C::Error>>
//! # where I2C::Error: core::fmt::Debug {
//!
//! // 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?;
//!
//! # Ok(())
//! # }
//! ```

#![no_std]
#![deny(missing_docs)]
#![deny(warnings)]
#![allow(clippy::missing_errors_doc)]

use core::fmt::Debug;

#[cfg(feature = "async")]
use device_driver::AsyncRegisterInterface;
use device_driver::RegisterInterface;
use embedded_hal as hal;
#[cfg(feature = "async")]
use embedded_hal_async as hal_async;

#[allow(unsafe_code)]
#[allow(missing_docs)]
#[allow(clippy::doc_markdown, clippy::missing_errors_doc, clippy::identity_op)]
mod generated {
    device_driver::create_device!(
        device_name: Bq27441Device,
        manifest: "src/bq27441.yaml"
    );
}

pub use generated::{Bq27441Device, field_sets};

/// Default I²C address for BQ27441.
pub const DEFAULT_I2C_ADDRESS: u8 = 0x55;

/// Unseal key value (sent twice to unseal device).
pub const UNSEAL_KEY: u16 = 0x8000;

/// Expected device type ID.
pub const DEVICE_TYPE_ID: u16 = 0x0421;

/// Control subcommand codes.
#[repr(u16)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ControlCmd {
    /// Read control status.
    ControlStatus = 0x0000,
    /// Read device type (should return 0x0421).
    DeviceType = 0x0001,
    /// Read firmware version.
    FwVersion = 0x0002,
    /// Read data memory code.
    DmCode = 0x0004,
    /// Read previous MAC write command.
    PrevMacWrite = 0x0007,
    /// Read chemistry ID.
    ChemId = 0x0008,
    /// Signal battery insertion.
    BatInsert = 0x000C,
    /// Signal battery removal.
    BatRemove = 0x000D,
    /// Set hibernate mode.
    SetHibernate = 0x0011,
    /// Clear hibernate mode.
    ClearHibernate = 0x0012,
    /// Enter config update mode.
    SetCfgUpdate = 0x0013,
    /// Enable shutdown mode.
    ShutdownEnable = 0x001B,
    /// Enter shutdown mode.
    Shutdown = 0x001C,
    /// Enter sealed mode.
    Sealed = 0x0020,
    /// Pulse GPOUT pin.
    PulseGpout = 0x0023,
    /// Full device reset.
    Reset = 0x0041,
    /// Soft reset (exit config mode with OCV).
    SoftReset = 0x0042,
    /// Exit config mode without OCV.
    ExitCfgUpdate = 0x0043,
    /// Exit config mode with resimulation.
    ExitResim = 0x0044,
}

/// Error type for BQ27441 operations.
#[derive(Debug)]
pub enum Error<E> {
    /// I²C bus error.
    I2c(E),
    /// Device ID mismatch.
    InvalidDevice,
    /// Invalid parameter.
    InvalidParam,
}

impl<E> From<E> for Error<E> {
    fn from(err: E) -> Self {
        Error::I2c(err)
    }
}

/// Battery chemistry variant.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ChemId {
    /// G1A variant (4.2V max charge, CHEM_ID = 0x0128).
    G1A = 0x0128,
    /// G1B variant (4.3V/4.35V max charge, CHEM_ID = 0x0312).
    G1B = 0x0312,
}

/// Blocking I²C interface wrapper.
pub struct DeviceInterface<I2C> {
    /// Underlying I²C bus.
    pub i2c: I2C,
    /// Device I²C address.
    pub address: u8,
}

/// Asynchronous I²C interface wrapper.
#[cfg(feature = "async")]
pub struct DeviceInterfaceAsync<I2C> {
    /// Underlying async I²C bus.
    pub i2c: I2C,
    /// Device I²C address.
    pub address: u8,
}

impl<I2C> RegisterInterface for DeviceInterface<I2C>
where
    I2C: hal::i2c::I2c,
{
    type Error = I2C::Error;
    type AddressType = u8;

    fn write_register(&mut self, address: Self::AddressType, _size_bits: u32, data: &[u8]) -> Result<(), Self::Error> {
        let mut buf = [0u8; 1 + 8];
        buf[0] = address;
        let end = 1 + data.len();
        buf[1..end].copy_from_slice(data);
        self.i2c.write(self.address, &buf[..end])
    }

    fn read_register(
        &mut self,
        address: Self::AddressType,
        _size_bits: u32,
        data: &mut [u8],
    ) -> Result<(), Self::Error> {
        self.i2c.write_read(self.address, &[address], data)
    }
}

#[cfg(feature = "async")]
impl<I2C> AsyncRegisterInterface for DeviceInterfaceAsync<I2C>
where
    I2C: hal_async::i2c::I2c,
{
    type Error = I2C::Error;
    type AddressType = u8;

    async fn write_register(
        &mut self,
        address: Self::AddressType,
        _size_bits: u32,
        data: &[u8],
    ) -> Result<(), Self::Error> {
        let mut buf = [0u8; 1 + 8];
        buf[0] = address;
        let end = 1 + data.len();
        buf[1..end].copy_from_slice(data);
        self.i2c.write(self.address, &buf[..end]).await
    }

    async fn read_register(
        &mut self,
        address: Self::AddressType,
        _size_bits: u32,
        data: &mut [u8],
    ) -> Result<(), Self::Error> {
        self.i2c.write_read(self.address, &[address], data).await
    }
}

/// Blocking BQ27441 driver.
pub struct Bq27441<I2C> {
    device: Bq27441Device<DeviceInterface<I2C>>,
}

impl<I2C> Bq27441<I2C>
where
    I2C: hal::i2c::I2c,
    I2C::Error: Debug,
{
    /// Create a new driver with default I²C address (0x55).
    pub fn new(i2c: I2C) -> Result<Self, Error<I2C::Error>> {
        Self::new_with_address(i2c, DEFAULT_I2C_ADDRESS)
    }

    /// Create a new driver with a custom I²C address.
    pub fn new_with_address(i2c: I2C, address: u8) -> Result<Self, Error<I2C::Error>> {
        let interface = DeviceInterface { i2c, address };
        let device = Bq27441Device::new(interface);
        let mut this = Self { device };
        this.verify_device()?;
        Ok(this)
    }

    /// Verify device ID matches expected value.
    fn verify_device(&mut self) -> Result<(), Error<I2C::Error>> {
        let device_type = self.control_read(ControlCmd::DeviceType)?;
        if device_type != DEVICE_TYPE_ID {
            return Err(Error::InvalidDevice);
        }
        Ok(())
    }

    /// Send a control subcommand and read the 2-byte response.
    pub fn control_read(&mut self, cmd: ControlCmd) -> Result<u16, Error<I2C::Error>> {
        // Write subcommand to control register
        let cmd_bytes = (cmd as u16).to_le_bytes();
        self.device.control()
            .write(|w| *w = field_sets::Control::from(cmd_bytes))
            .map_err(Error::I2c)?;

        // Small delay to allow device to process command
        // (In real implementation, might want to use a timer)

        // Read response from control register
        let result = self.device.control().read().map_err(Error::I2c)?;
        let bytes: [u8; 2] = result.into();
        Ok(u16::from_le_bytes(bytes))
    }

    /// Send a control subcommand (write-only, no response).
    pub fn control_write(&mut self, cmd: ControlCmd) -> Result<(), Error<I2C::Error>> {
        let cmd_bytes = (cmd as u16).to_le_bytes();
        self.device.control()
            .write(|w| *w = field_sets::Control::from(cmd_bytes))
            .map_err(Error::I2c)
    }

    /// Read battery voltage in millivolts.
    pub fn voltage(&mut self) -> Result<u16, Error<I2C::Error>> {
        let val = self.device.voltage().read().map_err(Error::I2c)?;
        let bytes: [u8; 2] = val.into();
        Ok(u16::from_le_bytes(bytes))
    }

    /// Read temperature in 0.1 Kelvin units.
    pub fn temperature_raw(&mut self) -> Result<u16, Error<I2C::Error>> {
        let val = self.device.temperature().read().map_err(Error::I2c)?;
        let bytes: [u8; 2] = val.into();
        Ok(u16::from_le_bytes(bytes))
    }

    /// Read temperature in degrees Celsius.
    pub fn temperature_celsius(&mut self) -> Result<f32, Error<I2C::Error>> {
        let raw = self.temperature_raw()?;
        Ok((raw as f32 * 0.1) - 273.15)
    }

    /// Read state of charge (0-100%).
    pub fn state_of_charge(&mut self) -> Result<u16, Error<I2C::Error>> {
        let val = self.device.state_of_charge().read().map_err(Error::I2c)?;
        let bytes: [u8; 2] = val.into();
        Ok(u16::from_le_bytes(bytes))
    }

    /// Read remaining capacity in mAh.
    pub fn remaining_capacity(&mut self) -> Result<u16, Error<I2C::Error>> {
        let val = self.device.remaining_capacity().read().map_err(Error::I2c)?;
        let bytes: [u8; 2] = val.into();
        Ok(u16::from_le_bytes(bytes))
    }

    /// Read full charge capacity in mAh.
    pub fn full_charge_capacity(&mut self) -> Result<u16, Error<I2C::Error>> {
        let val = self.device.full_charge_capacity().read().map_err(Error::I2c)?;
        let bytes: [u8; 2] = val.into();
        Ok(u16::from_le_bytes(bytes))
    }

    /// Read average current in mA (signed).
    pub fn average_current(&mut self) -> Result<i16, Error<I2C::Error>> {
        let val = self.device.average_current().read().map_err(Error::I2c)?;
        let bytes: [u8; 2] = val.into();
        Ok(i16::from_le_bytes(bytes))
    }

    /// Read average power in mW (signed).
    pub fn average_power(&mut self) -> Result<i16, Error<I2C::Error>> {
        let val = self.device.average_power().read().map_err(Error::I2c)?;
        let bytes: [u8; 2] = val.into();
        Ok(i16::from_le_bytes(bytes))
    }

    /// Read state of health percentage.
    pub fn state_of_health(&mut self) -> Result<u8, Error<I2C::Error>> {
        let val = self.device.state_of_health().read().map_err(Error::I2c)?;
        let bytes: [u8; 2] = val.into();
        Ok(bytes[0]) // Lower byte is percentage
    }

    /// Read status flags.
    pub fn flags(&mut self) -> Result<field_sets::Flags, Error<I2C::Error>> {
        self.device.flags().read().map_err(Error::I2c)
    }

    /// Check if battery is detected.
    pub fn is_battery_detected(&mut self) -> Result<bool, Error<I2C::Error>> {
        let flags = self.flags()?;
        Ok(flags.bat_det())
    }

    /// Check if battery is charging.
    pub fn is_charging(&mut self) -> Result<bool, Error<I2C::Error>> {
        let flags = self.flags()?;
        Ok(flags.chg())
    }

    /// Check if battery is discharging.
    pub fn is_discharging(&mut self) -> Result<bool, Error<I2C::Error>> {
        let flags = self.flags()?;
        Ok(flags.dsg())
    }

    /// Check if battery is fully charged.
    pub fn is_full_charged(&mut self) -> Result<bool, Error<I2C::Error>> {
        let flags = self.flags()?;
        Ok(flags.fc())
    }

    /// Read firmware version.
    pub fn firmware_version(&mut self) -> Result<u16, Error<I2C::Error>> {
        self.control_read(ControlCmd::FwVersion)
    }

    /// Read chemistry ID.
    pub fn chemistry_id(&mut self) -> Result<u16, Error<I2C::Error>> {
        self.control_read(ControlCmd::ChemId)
    }

    /// Enter SEALED mode (protects configuration).
    pub fn seal(&mut self) -> Result<(), Error<I2C::Error>> {
        self.control_write(ControlCmd::Sealed)
    }

    /// Unseal the device (allows configuration changes).
    /// Requires sending the unseal key (0x8000) twice.
    pub fn unseal(&mut self) -> Result<(), Error<I2C::Error>> {
        let key_bytes = UNSEAL_KEY.to_le_bytes();
        // Send key first time
        self.device.control()
            .write(|w| *w = field_sets::Control::from(key_bytes))
            .map_err(Error::I2c)?;
        // Send key second time
        self.device.control()
            .write(|w| *w = field_sets::Control::from(key_bytes))
            .map_err(Error::I2c)
    }

    /// Enter config update mode (device must be unsealed).
    pub fn enter_config_mode(&mut self) -> Result<(), Error<I2C::Error>> {
        self.control_write(ControlCmd::SetCfgUpdate)
    }

    /// Exit config update mode with soft reset.
    pub fn exit_config_mode(&mut self) -> Result<(), Error<I2C::Error>> {
        self.control_write(ControlCmd::SoftReset)
    }

    /// Perform a soft reset.
    pub fn soft_reset(&mut self) -> Result<(), Error<I2C::Error>> {
        self.control_write(ControlCmd::SoftReset)
    }

    /// Set hibernate mode.
    pub fn set_hibernate(&mut self) -> Result<(), Error<I2C::Error>> {
        self.control_write(ControlCmd::SetHibernate)
    }

    /// Clear hibernate mode.
    pub fn clear_hibernate(&mut self) -> Result<(), Error<I2C::Error>> {
        self.control_write(ControlCmd::ClearHibernate)
    }

    /// Access the generated register API directly.
    pub fn device(&mut self) -> &mut Bq27441Device<DeviceInterface<I2C>> {
        &mut self.device
    }

    /// Consume the driver and return the underlying I²C bus.
    pub fn destroy(self) -> I2C {
        self.device.interface.i2c
    }
}

/// Asynchronous BQ27441 driver.
#[cfg(feature = "async")]
pub struct Bq27441Async<I2C> {
    device: Bq27441Device<DeviceInterfaceAsync<I2C>>,
}

#[cfg(feature = "async")]
impl<I2C> Bq27441Async<I2C>
where
    I2C: hal_async::i2c::I2c,
    I2C::Error: Debug,
{
    /// Create a new async driver with default I²C address (0x55).
    pub async fn new(i2c: I2C) -> Result<Self, Error<I2C::Error>> {
        Self::new_with_address(i2c, DEFAULT_I2C_ADDRESS).await
    }

    /// Create a new async driver with a custom I²C address.
    pub async fn new_with_address(i2c: I2C, address: u8) -> Result<Self, Error<I2C::Error>> {
        let interface = DeviceInterfaceAsync { i2c, address };
        let device = Bq27441Device::new(interface);
        let mut this = Self { device };
        this.verify_device().await?;
        Ok(this)
    }

    /// Verify device ID matches expected value.
    async fn verify_device(&mut self) -> Result<(), Error<I2C::Error>> {
        let device_type = self.control_read(ControlCmd::DeviceType).await?;
        if device_type != DEVICE_TYPE_ID {
            return Err(Error::InvalidDevice);
        }
        Ok(())
    }

    /// Send a control subcommand and read the 2-byte response.
    pub async fn control_read(&mut self, cmd: ControlCmd) -> Result<u16, Error<I2C::Error>> {
        let cmd_bytes = (cmd as u16).to_le_bytes();
        self.device.control()
            .write_async(|w| *w = field_sets::Control::from(cmd_bytes))
            .await
            .map_err(Error::I2c)?;
        let result = self.device.control().read_async().await.map_err(Error::I2c)?;
        let bytes: [u8; 2] = result.into();
        Ok(u16::from_le_bytes(bytes))
    }

    /// Send a control subcommand (write-only).
    pub async fn control_write(&mut self, cmd: ControlCmd) -> Result<(), Error<I2C::Error>> {
        let cmd_bytes = (cmd as u16).to_le_bytes();
        self.device.control()
            .write_async(|w| *w = field_sets::Control::from(cmd_bytes))
            .await
            .map_err(Error::I2c)
    }

    /// Read battery voltage in millivolts.
    pub async fn voltage(&mut self) -> Result<u16, Error<I2C::Error>> {
        let val = self.device.voltage().read_async().await.map_err(Error::I2c)?;
        let bytes: [u8; 2] = val.into();
        Ok(u16::from_le_bytes(bytes))
    }

    /// Read temperature in 0.1 Kelvin units.
    pub async fn temperature_raw(&mut self) -> Result<u16, Error<I2C::Error>> {
        let val = self.device.temperature().read_async().await.map_err(Error::I2c)?;
        let bytes: [u8; 2] = val.into();
        Ok(u16::from_le_bytes(bytes))
    }

    /// Read temperature in degrees Celsius.
    pub async fn temperature_celsius(&mut self) -> Result<f32, Error<I2C::Error>> {
        let raw = self.temperature_raw().await?;
        Ok((raw as f32 * 0.1) - 273.15)
    }

    /// Read state of charge (0-100%).
    pub async fn state_of_charge(&mut self) -> Result<u16, Error<I2C::Error>> {
        let val = self.device.state_of_charge().read_async().await.map_err(Error::I2c)?;
        let bytes: [u8; 2] = val.into();
        Ok(u16::from_le_bytes(bytes))
    }

    /// Read remaining capacity in mAh.
    pub async fn remaining_capacity(&mut self) -> Result<u16, Error<I2C::Error>> {
        let val = self.device.remaining_capacity().read_async().await.map_err(Error::I2c)?;
        let bytes: [u8; 2] = val.into();
        Ok(u16::from_le_bytes(bytes))
    }

    /// Read full charge capacity in mAh.
    pub async fn full_charge_capacity(&mut self) -> Result<u16, Error<I2C::Error>> {
        let val = self.device.full_charge_capacity().read_async().await.map_err(Error::I2c)?;
        let bytes: [u8; 2] = val.into();
        Ok(u16::from_le_bytes(bytes))
    }

    /// Read average current in mA (signed).
    pub async fn average_current(&mut self) -> Result<i16, Error<I2C::Error>> {
        let val = self.device.average_current().read_async().await.map_err(Error::I2c)?;
        let bytes: [u8; 2] = val.into();
        Ok(i16::from_le_bytes(bytes))
    }

    /// Read average power in mW (signed).
    pub async fn average_power(&mut self) -> Result<i16, Error<I2C::Error>> {
        let val = self.device.average_power().read_async().await.map_err(Error::I2c)?;
        let bytes: [u8; 2] = val.into();
        Ok(i16::from_le_bytes(bytes))
    }

    /// Read state of health percentage.
    pub async fn state_of_health(&mut self) -> Result<u8, Error<I2C::Error>> {
        let val = self.device.state_of_health().read_async().await.map_err(Error::I2c)?;
        let bytes: [u8; 2] = val.into();
        Ok(bytes[0])
    }

    /// Read status flags.
    pub async fn flags(&mut self) -> Result<field_sets::Flags, Error<I2C::Error>> {
        self.device.flags().read_async().await.map_err(Error::I2c)
    }

    /// Check if battery is detected.
    pub async fn is_battery_detected(&mut self) -> Result<bool, Error<I2C::Error>> {
        let flags = self.flags().await?;
        Ok(flags.bat_det())
    }

    /// Check if battery is charging.
    pub async fn is_charging(&mut self) -> Result<bool, Error<I2C::Error>> {
        let flags = self.flags().await?;
        Ok(flags.chg())
    }

    /// Check if battery is discharging.
    pub async fn is_discharging(&mut self) -> Result<bool, Error<I2C::Error>> {
        let flags = self.flags().await?;
        Ok(flags.dsg())
    }

    /// Check if battery is fully charged.
    pub async fn is_full_charged(&mut self) -> Result<bool, Error<I2C::Error>> {
        let flags = self.flags().await?;
        Ok(flags.fc())
    }

    /// Read firmware version.
    pub async fn firmware_version(&mut self) -> Result<u16, Error<I2C::Error>> {
        self.control_read(ControlCmd::FwVersion).await
    }

    /// Read chemistry ID.
    pub async fn chemistry_id(&mut self) -> Result<u16, Error<I2C::Error>> {
        self.control_read(ControlCmd::ChemId).await
    }

    /// Enter SEALED mode.
    pub async fn seal(&mut self) -> Result<(), Error<I2C::Error>> {
        self.control_write(ControlCmd::Sealed).await
    }

    /// Unseal the device.
    pub async fn unseal(&mut self) -> Result<(), Error<I2C::Error>> {
        let key_bytes = UNSEAL_KEY.to_le_bytes();
        // Send key first time
        self.device.control()
            .write_async(|w| *w = field_sets::Control::from(key_bytes))
            .await
            .map_err(Error::I2c)?;
        // Send key second time
        self.device.control()
            .write_async(|w| *w = field_sets::Control::from(key_bytes))
            .await
            .map_err(Error::I2c)
    }

    /// Enter config update mode.
    pub async fn enter_config_mode(&mut self) -> Result<(), Error<I2C::Error>> {
        self.control_write(ControlCmd::SetCfgUpdate).await
    }

    /// Exit config update mode with soft reset.
    pub async fn exit_config_mode(&mut self) -> Result<(), Error<I2C::Error>> {
        self.control_write(ControlCmd::SoftReset).await
    }

    /// Perform a soft reset.
    pub async fn soft_reset(&mut self) -> Result<(), Error<I2C::Error>> {
        self.control_write(ControlCmd::SoftReset).await
    }

    /// Set hibernate mode.
    pub async fn set_hibernate(&mut self) -> Result<(), Error<I2C::Error>> {
        self.control_write(ControlCmd::SetHibernate).await
    }

    /// Clear hibernate mode.
    pub async fn clear_hibernate(&mut self) -> Result<(), Error<I2C::Error>> {
        self.control_write(ControlCmd::ClearHibernate).await
    }

    /// Access the generated register API directly.
    pub fn device(&mut self) -> &mut Bq27441Device<DeviceInterfaceAsync<I2C>> {
        &mut self.device
    }

    /// Consume the driver and return the underlying I²C bus.
    pub fn destroy(self) -> I2C {
        self.device.interface.i2c
    }
}