axp20x 0.0.3

Rust driver for axp20x Power Management Unit
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
#![no_std]

use embedded_hal::{delay::DelayNs, i2c::I2c};

use core::{
    convert::From,
    ops::{BitAnd, BitOr},
};

use bitmask_enum::bitmask;
use num_enum::{FromPrimitive, IntoPrimitive};

const DEFAULT_AXP202_SLAVE_ADDR: u8 = 0x35;
const BATTERY_VOLTAGE_STEP: f32 = 1.1;

/// Power state for the different modules
#[derive(Debug)]
pub enum PowerState {
    On,
    Off,
}

/// Power source status
#[bitmask(u8)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum PowerInputStatus {
    BootSource = Self(1 << 0),
    AcinVbusShortCircuit = Self(1 << 1),
    CurrentDirection = Self(1 << 2),
    VbusAbove = Self(1 << 3),
    VbusUsable = Self(1 << 4),
    VbusPresence = Self(1 << 5),
    AcinUsable = Self(1 << 6),
    AcinPresence = Self(1 << 7),
}

/// Power module
#[bitmask(u8)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Power {
    Exten = Self(1 << 0),
    DcDc3 = Self(1 << 1),
    Ldo2 = Self(1 << 2),
    Ldo4 = Self(1 << 3),
    DcDc2 = Self(1 << 4),
    Ldo3 = Self(1 << 6),
}

#[bitmask(u8)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Charge {
    Charging = Self(1 << 7),
}

/// Interrupt sources
#[bitmask(u64)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EventsIrq {
    PowerKeyShortPress = Self(1 << 17),

    Int1 = Self(0xFF),
    Int2 = Self(0xFF00),
    Int3 = Self(0xFF0000),
    Int4 = Self(0xFF000000),
    Int5 = Self(0xFF00000000),
}

impl EventsIrq {
    fn is_int1(&self) -> bool {
        self.intersects(Self::Int1)
    }

    fn is_int2(&self) -> bool {
        self.intersects(Self::Int2)
    }

    fn is_int3(&self) -> bool {
        self.intersects(Self::Int3)
    }

    fn is_int4(&self) -> bool {
        self.intersects(Self::Int4)
    }

    fn is_int5(&self) -> bool {
        self.intersects(Self::Int5)
    }

    fn into_int1_u8(&self) -> u8 {
        let mask: u64 = self.bitand(Self::Int1).into();
        mask as u8
    }

    fn into_int2_u8(&self) -> u8 {
        let mask: u64 = self.bitand(Self::Int2).into();
        (mask >> 8) as u8
    }

    fn into_int3_u8(&self) -> u8 {
        let mask: u64 = self.bitand(Self::Int3).into();
        (mask >> 16) as u8
    }

    fn into_int4_u8(&self) -> u8 {
        let mask: u64 = self.bitand(Self::Int4).into();
        (mask >> 24) as u8
    }

    fn into_int5_u8(&self) -> u8 {
        let mask: u64 = self.bitand(Self::Int5).into();
        (mask >> 32) as u8
    }

    fn from_int1_u8(val: u8) -> Self {
        let mask: u64 = val as u64;
        Self::Int1.bitand(mask.into())
    }

    fn from_int2_u8(val: u8) -> Self {
        let mask: u64 = (val as u64)
            .checked_shl(8)
            .expect("Source being u8, this should not overflow");
        Self::Int2.bitand(mask.into())
    }

    fn from_int3_u8(val: u8) -> Self {
        let mask: u64 = (val as u64)
            .checked_shl(16)
            .expect("Source being u8, this should not overflow");
        Self::Int3.bitand(mask.into())
    }

    fn from_int4_u8(val: u8) -> Self {
        let mask: u64 = (val as u64)
            .checked_shl(24)
            .expect("Source being u8, this should not overflow");
        Self::Int4.bitand(mask.into())
    }

    fn from_int5_u8(val: u8) -> Self {
        let mask: u64 = (val as u64)
            .checked_shl(32)
            .expect("Source being u8, this should not overflow");
        Self::Int5.bitand(mask.into())
    }

    fn toggle(self, current_mask: EventsIrq, enable: bool) -> Self {
        if enable {
            self.bitor(current_mask)
        } else {
            self.bitand(!current_mask)
        }
    }
}

/// AXP20x registers
#[allow(dead_code)]
#[repr(u8)]
#[derive(Clone, Copy, Debug, IntoPrimitive)]
enum Register {
    PowerInputStatus = 0x00,
    PowerWorkingModeChargeStatus = 0x01,
    IcType = 0x03,
    Ldo234Dc23Ctl = 0x12,
    Charge1 = 0x33,
    EnabledIrq1 = 0x40,
    EnabledIrq2 = 0x41,
    EnabledIrq3 = 0x42,
    EnabledIrq4 = 0x43,
    EnabledIrq5 = 0x45,
    StatusIrq1 = 0x48,
    StatusIrq2 = 0x49,
    StatusIrq3 = 0x4A,
    StatusIrq4 = 0x4B,
    StatusIrq5 = 0x4C,
    BatteryAverageVoltageHigh8b = 0x78,
    BatteryAverageVoltageLow4b = 0x79,
    BatteryPercentage = 0xB9,
}

/// AXP20x chip ids
#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, FromPrimitive)]
enum ChipId {
    #[default]
    Unknown = 0x00,
    Axp202 = 0x41,
    Axp192 = 0x03,
    Axp173 = 0xAD,
}

/// AXP20x errors
pub enum Error<E> {
    Uninitialized,
    I2cError(E),
}

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

impl<E> core::fmt::Debug for Error<E>
where
    E: core::fmt::Debug,
{
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::Uninitialized => write!(f, "Uninitialized"),
            Self::I2cError(arg0) => f.debug_tuple("I2cError").field(arg0).finish(),
        }
    }
}

/// AXP device representation
pub struct Axpxx<I2C>
where
    I2C: I2c,
{
    i2c: I2C,
    address: u8,
    state: State,
}

/// AXP device state
enum State {
    Uninitialized,
    Initialized(ChipId),
}

impl<I2C> Axpxx<I2C>
where
    I2C: I2c,
{
    /// Create a new Axp20x device with the default slave address
    ///
    /// # Arguments
    ///
    /// - `i2c` I2C bus used to communicate with the device
    ///
    /// # Returns
    ///
    /// - [Axp20x driver](Axpxx) created
    ///
    pub fn new(i2c: I2C) -> Self {
        Self {
            i2c,
            address: DEFAULT_AXP202_SLAVE_ADDR,
            state: State::Uninitialized,
        }
    }

    /// Create a new Axp20x device with the default slave address
    ///
    /// # Arguments
    ///
    /// - `i2c` I2C bus used to communicate with the device
    /// - `address` custom address for the device
    ///
    /// # Returns
    ///
    /// - [Axp20x driver](Axpxx) created
    ///
    pub fn new_with_address(i2c: I2C, address: u8) -> Self {
        Self {
            i2c,
            address,
            state: State::Uninitialized,
        }
    }

    /// Initialize the device
    pub fn init(&mut self) -> Result<(), Error<I2C::Error>> {
        let chip_id = self.probe_chip()?;
        self.state = State::Initialized(chip_id);
        Ok(())
    }

    fn read_reg(&mut self, reg: Register) -> Result<u8, Error<I2C::Error>> {
        let mut buf = [0u8; 1];
        let read_buf = [reg.into(); 1];
        self.i2c.write_read(self.address, &read_buf, &mut buf)?;
        Ok(buf[0])
    }

    fn write_reg(&mut self, reg: Register, val: u8) -> Result<(), Error<I2C::Error>> {
        self.i2c.write(self.address, &[reg.into(), val])?;
        Ok(())
    }

    fn probe_chip(&mut self) -> Result<ChipId, Error<I2C::Error>> {
        let chip_id = self.read_reg(Register::IcType)?;
        Ok(ChipId::from(chip_id))
    }

    /// Check if power ac is present
    ///
    /// # Returns
    ///
    /// - true if power AC is present, false otherwise
    pub fn is_acin_present(&mut self) -> Result<bool, Error<I2C::Error>> {
        let power_status = self.read_reg(Register::PowerInputStatus)?;
        let power_status = PowerInputStatus(power_status);
        Ok(power_status.intersects(PowerInputStatus::AcinPresence))
    }

    /// Check if power ac is usable
    ///
    /// # Returns
    ///
    /// - true if power AC is usable, false otherwise
    pub fn is_acin_usable(&mut self) -> Result<bool, Error<I2C::Error>> {
        let power_status = self.read_reg(Register::PowerInputStatus)?;
        let power_status = PowerInputStatus(power_status);
        Ok(power_status.intersects(PowerInputStatus::AcinUsable))
    }

    /// Check if VBus is present
    ///
    /// # Returns
    ///
    /// - true if VBus is present, false otherwise
    pub fn is_vbus_present(&mut self) -> Result<bool, Error<I2C::Error>> {
        let power_status = self.read_reg(Register::PowerInputStatus)?;
        let power_status = PowerInputStatus(power_status);
        Ok(power_status.intersects(PowerInputStatus::VbusPresence))
    }

    /// Check if VBus is usable
    ///
    /// # Returns
    ///
    /// - true if VBus is usable, false otherwise
    pub fn is_vbus_usable(&mut self) -> Result<bool, Error<I2C::Error>> {
        let power_status = self.read_reg(Register::PowerInputStatus)?;
        let power_status = PowerInputStatus(power_status);
        Ok(power_status.intersects(PowerInputStatus::VbusUsable))
    }

    pub fn is_vbus_above(&mut self) -> Result<bool, Error<I2C::Error>> {
        let power_status = self.read_reg(Register::PowerInputStatus)?;
        let power_status = PowerInputStatus(power_status);
        Ok(power_status.intersects(PowerInputStatus::VbusAbove))
    }

    /// Check if battery is charging
    ///
    /// # Returns
    ///
    /// - true if battery is charging, false otherwise
    pub fn is_battery_charging(&mut self) -> Result<bool, Error<I2C::Error>> {
        let raw_charge1 = self.read_reg(Register::Charge1)?;
        Ok(Charge(raw_charge1).intersects(Charge::Charging))
    }

    pub fn is_acin_vbus_shortcircuit(&mut self) -> Result<bool, Error<I2C::Error>> {
        let power_status = self.read_reg(Register::PowerInputStatus)?;
        let power_status = PowerInputStatus(power_status);
        Ok(power_status.intersects(PowerInputStatus::AcinVbusShortCircuit))
    }

    pub fn is_bootsource_acin_vbus(&mut self) -> Result<bool, Error<I2C::Error>> {
        let power_status = self.read_reg(Register::PowerInputStatus)?;
        let power_status = PowerInputStatus(power_status);
        Ok(power_status.intersects(PowerInputStatus::BootSource))
    }

    /// Check battery percentage
    ///
    /// # Returns
    ///
    /// - Battery percentage
    pub fn get_battery_percentage(&mut self) -> Result<u8, Error<I2C::Error>> {
        self.read_reg(Register::BatteryPercentage)
    }

    pub fn get_battery_voltage(&mut self) -> Result<f32, Error<I2C::Error>> {
        let battery_high_8b = self.read_reg(Register::BatteryAverageVoltageHigh8b)?;
        let battery_low_4b = self.read_reg(Register::BatteryAverageVoltageLow4b)?;
        Ok(
            (((battery_high_8b as u16) << 4) | (battery_low_4b & 0x0F) as u16) as f32
                * BATTERY_VOLTAGE_STEP,
        )
    }

    pub fn toggle_irq(&mut self, irqs: EventsIrq, enable: bool) -> Result<(), Error<I2C::Error>> {
        if irqs.is_int1() {
            let irq1 = self.read_reg(Register::EnabledIrq1)?;
            let irq1 = EventsIrq::from_int1_u8(irq1);
            let irqs = irqs.toggle(irq1, enable);
            self.write_reg(Register::EnabledIrq1, irqs.into_int1_u8())?;
        }
        if irqs.is_int2() {
            let irq2 = self.read_reg(Register::EnabledIrq2)?;
            let irq2 = EventsIrq::from_int2_u8(irq2).bitor(irqs);
            let irqs = irqs.toggle(irq2, enable);
            self.write_reg(Register::EnabledIrq2, irqs.into_int2_u8())?;
        }
        if irqs.is_int3() {
            let irq3 = self.read_reg(Register::EnabledIrq3)?;
            let irq3 = EventsIrq::from_int3_u8(irq3).bitor(irqs);
            let irqs = irqs.toggle(irq3, enable);
            self.write_reg(Register::EnabledIrq3, irqs.into_int3_u8())?;
        }
        if irqs.is_int4() {
            let irq4 = self.read_reg(Register::EnabledIrq4)?;
            let irq4 = EventsIrq::from_int4_u8(irq4).bitor(irqs);
            let irqs = irqs.toggle(irq4, enable);
            self.write_reg(Register::EnabledIrq4, irqs.into_int4_u8())?;
        }
        if irqs.is_int5() {
            let irq5 = self.read_reg(Register::EnabledIrq5)?;
            let irq5 = EventsIrq::from_int5_u8(irq5).bitor(irqs);
            let irqs = irqs.toggle(irq5, enable);
            self.write_reg(Register::EnabledIrq5, irqs.into_int5_u8())?;
        }
        Ok(())
    }

    pub fn clear_irq(&mut self) -> Result<(), Error<I2C::Error>> {
        self.write_reg(Register::StatusIrq1, 0xFF)?;
        self.write_reg(Register::StatusIrq2, 0xFF)?;
        self.write_reg(Register::StatusIrq3, 0xFF)?;
        self.write_reg(Register::StatusIrq4, 0xFF)?;
        self.write_reg(Register::StatusIrq5, 0xFF)?;
        Ok(())
    }

    pub fn read_irq(&mut self) -> Result<EventsIrq, Error<I2C::Error>> {
        let irq1 = self.read_reg(Register::StatusIrq1)?;
        let irq2 = self.read_reg(Register::StatusIrq2)?;
        let irq3 = self.read_reg(Register::StatusIrq3)?;
        let irq4 = self.read_reg(Register::StatusIrq4)?;
        let irq5 = self.read_reg(Register::StatusIrq5)?;
        self.clear_irq()?;
        Ok(EventsIrq::from_int1_u8(irq1)
            .bitor(EventsIrq::from_int2_u8(irq2))
            .bitor(EventsIrq::from_int3_u8(irq3))
            .bitor(EventsIrq::from_int4_u8(irq4))
            .bitor(EventsIrq::from_int5_u8(irq5)))
    }

    /// Set power output for modules
    ///
    /// # Arguments
    ///
    /// - `channel`: [Power](Power) channel to manage
    /// - `state`: [PowerState](PowerState) to set (On or Off)
    /// - `delay`: [Delay source](embedded_hal::blocking::delay::DelayNs) to use
    pub fn set_power_output(
        &mut self,
        channel: Power,
        state: PowerState,
        delay: &mut impl DelayNs,
    ) -> Result<(), Error<I2C::Error>> {
        match self.state {
            State::Uninitialized => Err(Error::Uninitialized),
            State::Initialized(chip_id) => {
                // Before setting, the output cannot be all turned off
                let mut data: u8;
                loop {
                    data = self.read_reg(Register::Ldo234Dc23Ctl)?;
                    delay.delay_ms(10);
                    if data != 0 {
                        break;
                    }
                }

                let mut data = Power::from(data);

                match state {
                    PowerState::On => {
                        data |= channel;
                    }
                    PowerState::Off => {
                        data &= !channel;
                    }
                };

                if chip_id == ChipId::Axp202 {
                    data |= Power::DcDc3.into();
                }
                self.write_reg(Register::Ldo234Dc23Ctl, u8::from(data))?;
                Ok(())
            }
        }
    }
}