Skip to content

Commit

Permalink
PoC unit implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
fmckeogh committed May 20, 2024
1 parent dabad9d commit b8a1515
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 35 deletions.
37 changes: 37 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 10 additions & 8 deletions pd-interceptor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ fn handle_event(event: Event) -> Option<Request> {
.filter_map(|(i, cap)| {
if let PowerDataObject::FixedSupply(supply) = cap {
debug!(
"supply @ {}: {}mV {}mA",
"supply @ {}: {}V {}A",
i,
supply.voltage_mv(),
supply.max_current_ma()
supply.voltage().value,
supply.max_current().value
);
Some((i, supply))
} else {
Expand All @@ -122,12 +122,14 @@ fn handle_event(event: Event) -> Option<Request> {
.max_by(|(_, x), (_, y)| x.raw_voltage().cmp(&y.raw_voltage()))
.unwrap();

info!("requesting supply {:?}@{}", supply, index);

return Some(Request::RequestPower {
let req = Request::RequestPower {
index,
current: supply.max_current_ma(),
});
current: supply.raw_max_current(),
};

info!("requesting {}", req);

return Some(req);
}
Event::PowerReady => info!("power ready"),
Event::ProtocolChanged => info!("protocol changed"),
Expand Down
1 change: 1 addition & 0 deletions usb-pd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ byteorder = { version = "1.5.0", default-features = false }
defmt = "0.3.6"
heapless = { version = "0.8.0", features = ["defmt-03"] }
embassy-time = "0.3.0"
uom = { version = "0.36.0", default-features = false, features = ["autoconvert", "si", "u16", "u32"] }
65 changes: 38 additions & 27 deletions usb-pd/src/messages/pdo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ use {
defmt::Format,
heapless::Vec,
proc_bitfield::bitfield,
uom::si::{
electric_current::milliampere,
electric_potential::millivolt,
power::{milliwatt, watt},
u16::{ElectricCurrent, ElectricPotential},
u32::Power,
},
};

#[derive(Clone, Copy, Debug, Format)]
Expand Down Expand Up @@ -50,12 +57,12 @@ bitfield! {
}

impl FixedSupply {
pub fn voltage_mv(&self) -> u16 {
self.raw_voltage() * 50
pub fn voltage(&self) -> ElectricPotential {
ElectricPotential::new::<millivolt>(self.raw_voltage() * 50)
}

pub fn max_current_ma(&self) -> u16 {
self.raw_max_current() * 10
pub fn max_current(&self) -> ElectricCurrent {
ElectricCurrent::new::<milliampere>(self.raw_max_current() * 10)
}
}

Expand All @@ -74,16 +81,16 @@ bitfield! {
}

impl Battery {
pub fn max_voltage_mv(&self) -> u16 {
u16::from(self.raw_max_voltage()) * 50
pub fn max_voltage(&self) -> ElectricPotential {
ElectricPotential::new::<millivolt>(self.raw_max_voltage() * 50)
}

pub fn min_voltage_mv(&self) -> u16 {
u16::from(self.raw_min_voltage()) * 50
pub fn min_voltage(&self) -> ElectricPotential {
ElectricPotential::new::<millivolt>(self.raw_min_voltage() * 50)
}

pub fn max_power_mw(&self) -> u32 {
u32::from(self.raw_max_power()) * 250
pub fn max_power(&self) -> Power {
Power::new::<milliwatt>(u32::from(self.raw_max_power()) * 250)
}
}

Expand All @@ -102,16 +109,16 @@ bitfield! {
}

impl VariableSupply {
pub fn max_voltage_mv(&self) -> u16 {
u16::from(self.raw_max_voltage()) * 50
pub fn max_voltage(&self) -> ElectricPotential {
ElectricPotential::new::<millivolt>(self.raw_max_voltage() * 50)
}

pub fn min_voltage_mv(&self) -> u16 {
u16::from(self.raw_min_voltage()) * 50
pub fn min_voltage(&self) -> ElectricPotential {
ElectricPotential::new::<millivolt>(self.raw_min_voltage() * 50)
}

pub fn max_current_ma(&self) -> u16 {
u16::from(self.raw_max_current()) * 10
pub fn max_current(&self) -> ElectricCurrent {
ElectricCurrent::new::<milliampere>(self.raw_max_current() * 10)
}
}

Expand Down Expand Up @@ -150,16 +157,16 @@ bitfield! {
}

impl SPRProgrammablePowerSupply {
pub fn max_voltage_mv(&self) -> u16 {
u16::from(self.raw_max_voltage()) * 100
pub fn max_voltage(&self) -> ElectricPotential {
ElectricPotential::new::<millivolt>(u16::from(self.raw_max_voltage()) * 100)
}

pub fn min_voltage_mv(&self) -> u16 {
u16::from(self.raw_min_voltage()) * 100
pub fn min_voltage(&self) -> ElectricPotential {
ElectricPotential::new::<millivolt>(u16::from(self.raw_min_voltage()) * 100)
}

pub fn max_current_ma(&self) -> u16 {
u16::from(self.raw_max_current()) * 50
pub fn max_current(&self) -> ElectricCurrent {
ElectricCurrent::new::<milliampere>(u16::from(self.raw_max_current()) * 50)
}
}

Expand All @@ -176,17 +183,21 @@ bitfield! {
/// Minimum Voltage in 100mV increments
pub raw_min_voltage: u8 @ 8..=15,
/// PDP in 1W increments
pub pd_power: u8 @ 0..=7,
pub raw_pd_power: u8 @ 0..=7,
}
}

impl EPRAdjustableVoltageSupply {
pub fn max_voltage_mv(&self) -> u16 {
u16::from(self.raw_max_voltage()) * 100
pub fn max_voltage(&self) -> ElectricPotential {
ElectricPotential::new::<millivolt>(u16::from(self.raw_max_voltage()) * 100)
}

pub fn min_voltage_mv(&self) -> u16 {
u16::from(self.raw_min_voltage()) * 100
pub fn min_voltage(&self) -> ElectricPotential {
ElectricPotential::new::<millivolt>(u16::from(self.raw_min_voltage()) * 100)
}

pub fn pd_power(&self) -> Power {
Power::new::<watt>(u32::from(self.raw_pd_power()))
}
}

Expand Down

0 comments on commit b8a1515

Please sign in to comment.