a7105/registers/
battery.rs

1use super::*;
2use defmt::Format;
3
4#[derive(Format, PartialEq, Debug, Copy, Clone, Default)]
5pub struct BatteryDetectConfig {
6    /// VDD D voltage setting in Sleep mode
7    pub sleep_voltage_setting: SleepModeVoltageSetting,
8    /// VDD D and VDD A voltage setting in non-Sleep mode
9    pub nonsleep_voltage_setting: NonSleepModeVoltageSetting,
10    /// Battery voltage detect threshold
11    pub detect_threshold: DetectThreshold,
12    /// Battery detect threshold enabled
13    pub detect_enabled: bool,
14}
15
16impl Register for BatteryDetectConfig {
17    fn id() -> u8 {
18        0x27
19    }
20}
21
22impl WritableRegister for BatteryDetectConfig {}
23
24impl From<BatteryDetectConfig> for u8 {
25    fn from(val: BatteryDetectConfig) -> Self {
26        u8::from(val.detect_enabled)
27            | match val.detect_threshold {
28                DetectThreshold::V20 => 0b000,
29                DetectThreshold::V21 => 0b001,
30                DetectThreshold::V22 => 0b010,
31                DetectThreshold::V23 => 0b011,
32                DetectThreshold::V24 => 0b100,
33                DetectThreshold::V25 => 0b101,
34                DetectThreshold::V26 => 0b110,
35                DetectThreshold::V27 => 0b111,
36            } << 1
37            | match val.nonsleep_voltage_setting {
38                NonSleepModeVoltageSetting::V18 => 0b11,
39                NonSleepModeVoltageSetting::V19 => 0b10,
40                NonSleepModeVoltageSetting::V20 => 0b01,
41                NonSleepModeVoltageSetting::V21 => 0b00,
42            } << 5
43            | match val.sleep_voltage_setting {
44                SleepModeVoltageSetting::ThreeFifth => 0b0,
45                SleepModeVoltageSetting::ThreeForths => 0b1,
46            } << 7
47    }
48}
49
50#[derive(Format, PartialEq, Debug, Copy, Clone, Default)]
51pub enum SleepModeVoltageSetting {
52    /// 3/5 * REGI
53    #[default]
54    ThreeFifth,
55    /// 3/4 * REGI
56    ThreeForths,
57}
58
59#[derive(Format, PartialEq, Debug, Copy, Clone, Default)]
60pub enum NonSleepModeVoltageSetting {
61    /// 1.8V
62    V18,
63    /// 1.9V
64    V19,
65    /// 2.0V
66    V20,
67    /// 2.1V
68    #[default]
69    V21,
70}
71
72#[derive(Format, PartialEq, Debug, Copy, Clone, Default)]
73pub enum DetectThreshold {
74    /// 2.0V
75    V20,
76    /// 2.1V
77    V21,
78    /// 2.2V
79    V22,
80    /// 2.3V
81    #[default]
82    V23,
83    /// 2.4V
84    V24,
85    /// 2.5V
86    V25,
87    /// 2.6V
88    V26,
89    /// 2.7V
90    V27,
91}
92
93#[derive(Format, PartialEq, Debug, Copy, Clone, Default)]
94pub struct BatteryDetectResult {
95    /// Battery detection flag
96    pub voltage_above_threshold: bool,
97}
98
99impl Register for BatteryDetectResult {
100    fn id() -> u8 {
101        0x27
102    }
103}
104
105impl ReadableRegister for BatteryDetectResult {}
106
107impl From<u8> for BatteryDetectResult {
108    fn from(val: u8) -> Self {
109        Self {
110            voltage_above_threshold: (val & 0b0001_0000) != 0,
111        }
112    }
113}
114
115#[cfg(test)]
116mod test {
117    use super::super::Register as _;
118    use super::*;
119
120    #[test]
121    fn test_battery_detect() {
122        let default: u8 = BatteryDetectConfig::default().into();
123        assert_eq!(default, 0b0000_0110);
124
125        assert_eq!(BatteryDetectConfig::id(), 0x27);
126        assert_eq!(BatteryDetectResult::id(), 0x27);
127    }
128}