Skip to content

Commit c2c48d9

Browse files
committed
PAC195X: Calculate correct VBUS voltage
Signed-off-by: Daniel Schaefer <dhs@frame.work>
1 parent ff2e67b commit c2c48d9

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

src/lib.rs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,25 @@ pub enum ProductId {
7575
PAC1952_2,
7676
}
7777

78+
impl ProductId {
79+
fn max_voltage(&self) -> f32 {
80+
match self {
81+
Self::PAC1941_1 => 9.0,
82+
Self::PAC1942_1 => 9.0,
83+
Self::PAC1943_1 => 9.0,
84+
Self::PAC1944_1 => 9.0,
85+
Self::PAC1941_2 => 9.0,
86+
Self::PAC1942_2 => 9.0,
87+
Self::PAC1951_1 => 9.0,
88+
Self::PAC1952_1 => 32.0,
89+
Self::PAC1953_1 => 32.0,
90+
Self::PAC1954_1 => 32.0,
91+
Self::PAC1951_2 => 32.0,
92+
Self::PAC1952_2 => 32.0,
93+
}
94+
}
95+
}
96+
7897
/// A PAC194X power monitor on the I2C bus `I`.
7998
pub struct PAC194X<I>
8099
where
@@ -164,8 +183,8 @@ macro_rules! read_write_n {
164183
};
165184
}
166185

167-
fn vbus_to_real(raw: u16, fsr: VBusFSR) -> f32 {
168-
9.0 * match fsr {
186+
fn vbus_to_real(raw: u16, max: f32, fsr: VBusFSR) -> f32 {
187+
max * match fsr {
169188
VBusFSR::Unipolar => (raw as f32) / 65536.0,
170189
VBusFSR::BipolarHV => (i16::from_ne_bytes(raw.to_le_bytes()) as f32) / 65536.0,
171190
VBusFSR::BipolarLV => (i16::from_ne_bytes(raw.to_le_bytes()) as f32) / 32768.0,
@@ -311,7 +330,11 @@ where
311330
4 => fsr_reg.cfg_vb4,
312331
_ => unreachable!(),
313332
};
314-
Ok(vbus_to_real(self.read_vbusn(n)?.voltage, fsr))
333+
Ok(vbus_to_real(
334+
self.read_vbusn(n)?.voltage,
335+
self.product_id.max_voltage(),
336+
fsr,
337+
))
315338
}
316339

317340
/// High level API for retrieving the sense voltage of channel `n`
@@ -340,7 +363,11 @@ where
340363
4 => fsr_reg.cfg_vb4,
341364
_ => unreachable!(),
342365
};
343-
Ok(vbus_to_real(self.read_vbusn_avg(n)?.voltage, fsr))
366+
Ok(vbus_to_real(
367+
self.read_vbusn_avg(n)?.voltage,
368+
self.product_id.max_voltage(),
369+
fsr,
370+
))
344371
}
345372

346373
/// Same as [read_sense_voltage_n()], but using the accumulator-based rolling average

0 commit comments

Comments
 (0)