Skip to content

Commit 64afc39

Browse files
committed
fix: improve example
1 parent 00862b8 commit 64afc39

File tree

4 files changed

+64
-27
lines changed

4 files changed

+64
-27
lines changed

examples/embassy-stm32-g431cb/.cargo/config.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
2-
runner = 'probe-rs run --chip STM32G431CBTx'
2+
runner = 'probe-rs run --chip STM32G431CBTx --connect-under-reset'
33

44
[build]
55
target = "thumbv7em-none-eabihf"

examples/embassy-stm32-g431cb/src/power.rs

Lines changed: 60 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Handles USB PD negotiation.
2-
use defmt::{error, info, warn, Format};
2+
use defmt::{info, warn, Format};
33
use embassy_futures::select::{select, Either};
44
use embassy_stm32::gpio::Output;
55
use embassy_stm32::ucpd::{self, CcPhy, CcPull, CcSel, CcVState, PdPhy, Ucpd};
@@ -119,20 +119,32 @@ impl SinkTimer for EmbassySinkTimer {
119119
}
120120
}
121121

122+
/// Capabilities that are tested (cycled through each second).
123+
#[derive(Format)]
124+
enum TestCapabilities {
125+
Safe5V,
126+
Pps3V6,
127+
Pps5V5,
128+
Fixed9V,
129+
Max,
130+
}
131+
122132
struct Device {
123-
target_voltage: ElectricPotential,
133+
test_capabilities: TestCapabilities,
124134
}
125135

126136
impl Default for Device {
127137
fn default() -> Self {
128138
Self {
129-
target_voltage: ElectricPotential::new::<electric_potential::volt>(5),
139+
test_capabilities: TestCapabilities::Safe5V,
130140
}
131141
}
132142
}
133143

134144
impl DevicePolicyManager for Device {
135145
async fn request(&mut self, source_capabilities: &SourceCapabilities) -> request::PowerSource {
146+
info!("Found capabilities: {}", source_capabilities);
147+
136148
request::PowerSource::new_fixed(
137149
request::CurrentRequest::Highest,
138150
request::VoltageRequest::Safe5V,
@@ -143,32 +155,54 @@ impl DevicePolicyManager for Device {
143155

144156
async fn get_event(&mut self, source_capabilities: &SourceCapabilities) -> Event {
145157
// Periodically request another power level.
146-
Timer::after_secs(5).await;
147-
148-
let power_source = match request::PowerSource::new_pps(
149-
request::CurrentRequest::Highest,
150-
self.target_voltage,
151-
source_capabilities,
152-
) {
153-
Ok(power_source) => {
154-
self.target_voltage += ElectricPotential::new::<electric_potential::millivolt>(100);
155-
info!("Requesting {}", power_source,);
158+
Timer::after_secs(1).await;
159+
160+
info!("Test capabilities: {}", self.test_capabilities);
161+
let (power_source, new_test_capabilities) = match self.test_capabilities {
162+
TestCapabilities::Safe5V => (
163+
request::PowerSource::new_fixed(CurrentRequest::Highest, VoltageRequest::Safe5V, source_capabilities),
164+
TestCapabilities::Fixed9V,
165+
),
166+
TestCapabilities::Fixed9V => (
167+
request::PowerSource::new_fixed(
168+
CurrentRequest::Highest,
169+
VoltageRequest::Specific(ElectricPotential::new::<electric_potential::volt>(9)),
170+
source_capabilities,
171+
),
172+
TestCapabilities::Pps3V6,
173+
),
174+
TestCapabilities::Pps3V6 => (
175+
request::PowerSource::new_pps(
176+
request::CurrentRequest::Highest,
177+
ElectricPotential::new::<electric_potential::millivolt>(3600),
178+
source_capabilities,
179+
),
180+
TestCapabilities::Pps5V5,
181+
),
182+
TestCapabilities::Pps5V5 => (
183+
request::PowerSource::new_pps(
184+
request::CurrentRequest::Highest,
185+
ElectricPotential::new::<electric_potential::millivolt>(5500),
186+
source_capabilities,
187+
),
188+
TestCapabilities::Max,
189+
),
190+
TestCapabilities::Max => (
191+
request::PowerSource::new_fixed(CurrentRequest::Highest, VoltageRequest::Highest, source_capabilities),
192+
TestCapabilities::Safe5V,
193+
),
194+
};
156195

157-
power_source
158-
}
159-
Err(_) => {
160-
error!(
161-
"Cannot request {} mV - fall back to default voltage",
162-
self.target_voltage.get::<electric_potential::millivolt>()
163-
);
164-
self.target_voltage = ElectricPotential::new::<electric_potential::volt>(5);
165-
166-
request::PowerSource::new_fixed(CurrentRequest::Highest, VoltageRequest::Safe5V, source_capabilities)
167-
.unwrap()
168-
}
196+
let event = if let Ok(power_source) = power_source {
197+
info!("Requesting power source {}", power_source);
198+
Event::RequestPower(power_source)
199+
} else {
200+
warn!("Capabilities not available {}", self.test_capabilities);
201+
Event::None
169202
};
170203

171-
Event::RequestPower(power_source)
204+
self.test_capabilities = new_test_capabilities;
205+
event
172206
}
173207
}
174208

usbpd/src/sink/device_policy_manager.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ use crate::protocol_layer::message::request;
1010
/// Events that the device policy manager can send to the policy engine.
1111
#[derive(Debug)]
1212
pub enum Event {
13+
/// Empty event.
14+
None,
1315
/// Request source capabilities (again).
1416
RequestSourceCapabilities,
1517
/// Request a certain power level.

usbpd/src/sink/policy_engine.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ impl<DRIVER: Driver, TIMER: Timer, DPM: DevicePolicyManager> Sink<DRIVER, TIMER,
302302
Either::Right((event, _)) => match event {
303303
Event::RequestSourceCapabilities => State::EventRequestSourceCapabilities,
304304
Event::RequestPower(power) => State::SelectCapability(power),
305+
Event::None => State::Ready,
305306
},
306307
}
307308
}

0 commit comments

Comments
 (0)