Skip to content

Commit 11345b8

Browse files
zhuyi2024zhuyi
andauthored
Fix usbc charge fail issue (#329)
1. Add new event source_caps_received and sink_ready for future use. 2. Set default usb current to 900mA for now to follow G3. 3. Call set_new_power_contract_as_consumer/set_new_power_contract_as_provider only when there is int event indicating that power negotiation is complete. 4. Fix process_new_consumer_contract from ConnectedProvider state issue 5. Remove pr_swap and unused code. 6. Update some comments and function name for easy understanding. --------- Co-authored-by: zhuyi <zhuyi@microsoft.com>
1 parent ff32da9 commit 11345b8

File tree

16 files changed

+209
-230
lines changed

16 files changed

+209
-230
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

embedded-service/src/power/policy/action/device.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl<'a, S: Kind> Device<'a, S> {
6969
capability: Option<PowerCapability>,
7070
) -> Result<(), Error> {
7171
info!(
72-
"Device {} consume capability updated {:#?}",
72+
"Device {} consume capability updated: {:#?}",
7373
self.device.id().0,
7474
capability
7575
);
@@ -150,4 +150,9 @@ impl<'a> Device<'a, ConnectedProvider> {
150150
pub async fn request_provider_power_capability(&self, capability: PowerCapability) -> Result<(), Error> {
151151
self.request_provider_power_capability_internal(capability).await
152152
}
153+
154+
/// Notify the power policy service of an updated consumer power capability
155+
pub async fn notify_consumer_power_capability(&self, capability: Option<PowerCapability>) -> Result<(), Error> {
156+
self.notify_consumer_power_capability_internal(capability).await
157+
}
153158
}

embedded-service/src/power/policy/action/policy.rs

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ impl<'a, S: Kind> Policy<'a, S> {
6666
}
6767
}
6868

69-
/// Common connect provider function used by multiple states
70-
async fn connect_provider_internal_no_timeout(&self, capability: PowerCapability) -> Result<(), Error> {
69+
/// Common connect as provider function used by multiple states
70+
async fn connect_as_provider_internal_no_timeout(&self, capability: PowerCapability) -> Result<(), Error> {
7171
info!("Device {} connecting provider", self.device.id().0);
7272

7373
self.device
74-
.execute_device_command(device::CommandData::ConnectProvider(capability))
74+
.execute_device_command(device::CommandData::ConnectAsProvider(capability))
7575
.await?
7676
.complete_or_err()?;
7777

@@ -84,7 +84,12 @@ impl<'a, S: Kind> Policy<'a, S> {
8484

8585
/// Common connect provider function used by multiple states
8686
async fn connect_provider_internal(&self, capability: PowerCapability) -> Result<(), Error> {
87-
match with_timeout(DEFAULT_TIMEOUT, self.connect_provider_internal_no_timeout(capability)).await {
87+
match with_timeout(
88+
DEFAULT_TIMEOUT,
89+
self.connect_as_provider_internal_no_timeout(capability),
90+
)
91+
.await
92+
{
8893
Ok(r) => r,
8994
Err(TimeoutError) => Err(Error::Timeout),
9095
}
@@ -96,14 +101,14 @@ impl Policy<'_, Detached> {}
96101

97102
impl<'a> Policy<'a, Idle> {
98103
/// Connect this device as a consumer
99-
pub async fn connect_consumer_no_timeout(
104+
pub async fn connect_as_consumer_no_timeout(
100105
self,
101106
capability: PowerCapability,
102107
) -> Result<Policy<'a, ConnectedConsumer>, Error> {
103-
info!("Device {} connecting consumer", self.device.id().0);
108+
info!("Device {} connecting as consumer", self.device.id().0);
104109

105110
self.device
106-
.execute_device_command(device::CommandData::ConnectConsumer(capability))
111+
.execute_device_command(device::CommandData::ConnectAsConsumer(capability))
107112
.await?
108113
.complete_or_err()?;
109114

@@ -115,7 +120,7 @@ impl<'a> Policy<'a, Idle> {
115120

116121
/// Connect this device as a consumer
117122
pub async fn connect_consumer(self, capability: PowerCapability) -> Result<Policy<'a, ConnectedConsumer>, Error> {
118-
match with_timeout(DEFAULT_TIMEOUT, self.connect_consumer_no_timeout(capability)).await {
123+
match with_timeout(DEFAULT_TIMEOUT, self.connect_as_consumer_no_timeout(capability)).await {
119124
Ok(r) => r,
120125
Err(TimeoutError) => Err(Error::Timeout),
121126
}
@@ -126,7 +131,7 @@ impl<'a> Policy<'a, Idle> {
126131
self,
127132
capability: PowerCapability,
128133
) -> Result<Policy<'a, ConnectedProvider>, Error> {
129-
self.connect_provider_internal_no_timeout(capability)
134+
self.connect_as_provider_internal_no_timeout(capability)
130135
.await
131136
.map(|_| Policy::new(self.device))
132137
}
@@ -171,12 +176,38 @@ impl<'a> Policy<'a, ConnectedProvider> {
171176
}
172177
}
173178

179+
/// Connect this device as a consumer
180+
pub async fn connect_as_consumer_no_timeout(
181+
self,
182+
capability: PowerCapability,
183+
) -> Result<Policy<'a, ConnectedConsumer>, Error> {
184+
info!("Device {} connecting as consumer", self.device.id().0);
185+
186+
self.device
187+
.execute_device_command(device::CommandData::ConnectAsConsumer(capability))
188+
.await?
189+
.complete_or_err()?;
190+
191+
self.device
192+
.set_state(device::State::ConnectedConsumer(capability))
193+
.await;
194+
Ok(Policy::new(self.device))
195+
}
196+
197+
/// Connect this device as a consumer
198+
pub async fn connect_consumer(self, capability: PowerCapability) -> Result<Policy<'a, ConnectedConsumer>, Error> {
199+
match with_timeout(DEFAULT_TIMEOUT, self.connect_as_consumer_no_timeout(capability)).await {
200+
Ok(r) => r,
201+
Err(TimeoutError) => Err(Error::Timeout),
202+
}
203+
}
204+
174205
/// Connect this device as a provider
175206
pub async fn connect_provider_no_timeout(
176207
&self,
177208
capability: PowerCapability,
178209
) -> Result<Policy<'a, ConnectedProvider>, Error> {
179-
self.connect_provider_internal_no_timeout(capability)
210+
self.connect_as_provider_internal_no_timeout(capability)
180211
.await
181212
.map(|_| Policy::new(self.device))
182213
}

embedded-service/src/power/policy/device.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ pub enum StateKind {
1616
Detached,
1717
/// Device is attached
1818
Idle,
19-
/// Device is actively providing power
19+
/// Device is actively providing power, USB PD source mode
2020
ConnectedProvider,
21-
/// Device is actively consuming power
21+
/// Device is actively consuming power, USB PD sink mode
2222
ConnectedConsumer,
2323
}
2424

@@ -65,9 +65,9 @@ struct InternalState {
6565
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
6666
pub enum CommandData {
6767
/// Start consuming on this device
68-
ConnectConsumer(PowerCapability),
69-
/// Start providinig on this device
70-
ConnectProvider(PowerCapability),
68+
ConnectAsConsumer(PowerCapability),
69+
/// Start providing power to port partner on this device
70+
ConnectAsProvider(PowerCapability),
7171
/// Stop providing or consuming on this device
7272
Disconnect,
7373
}

embedded-service/src/type_c/controller.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -347,25 +347,6 @@ pub trait Controller {
347347
port: LocalPortId,
348348
enable: bool,
349349
) -> impl Future<Output = Result<(), Error<Self::BusError>>>;
350-
/// Enable or disable sourcing
351-
fn set_sourcing(
352-
&mut self,
353-
port: LocalPortId,
354-
enable: bool,
355-
) -> impl Future<Output = Result<(), Error<Self::BusError>>>;
356-
/// Set source current capability
357-
fn set_source_current(
358-
&mut self,
359-
port: LocalPortId,
360-
current: TypecCurrent,
361-
signal_event: bool,
362-
) -> impl Future<Output = Result<(), Error<Self::BusError>>>;
363-
/// Initiate a power-role swap to the given role
364-
fn request_pr_swap(
365-
&mut self,
366-
port: LocalPortId,
367-
role: PowerRole,
368-
) -> impl Future<Output = Result<(), Error<Self::BusError>>>;
369350
/// Get current controller status
370351
fn get_controller_status(
371352
&mut self,

embedded-service/src/type_c/event.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ bitfield! {
1515
pub u8, new_power_contract_as_provider, set_new_power_contract_as_provider: 2, 2;
1616
/// New power contract as consumer
1717
pub u8, new_power_contract_as_consumer, set_new_power_contract_as_consumer: 3, 3;
18+
/// Source Caps received
19+
pub u8, source_caps_received, set_source_caps_received: 4, 4;
20+
/// Sink ready
21+
pub u8, sink_ready, set_sink_ready: 5, 5;
1822
}
1923

2024
/// Type-safe wrapper for the raw port event kind
@@ -63,6 +67,26 @@ impl PortEventKind {
6367
pub fn set_new_power_contract_as_consumer(&mut self, value: bool) {
6468
self.0.set_new_power_contract_as_consumer(value.into());
6569
}
70+
71+
/// Returns true if a source caps msg received
72+
pub fn source_caps_received(self) -> bool {
73+
self.0.source_caps_received() != 0
74+
}
75+
76+
/// Sets the source caps received event
77+
pub fn set_source_caps_received(&mut self, value: bool) {
78+
self.0.set_source_caps_received(value.into());
79+
}
80+
81+
/// Returns true if a sink ready event triggered
82+
pub fn sink_ready(self) -> bool {
83+
self.0.sink_ready() != 0
84+
}
85+
86+
/// Sets the sink ready event
87+
pub fn set_sink_ready(&mut self, value: bool) {
88+
self.0.set_sink_ready(value.into());
89+
}
6690
}
6791

6892
/// Bit vector type to store pending port events

embedded-service/src/type_c/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ impl From<type_c::Current> for policy::PowerCapability {
9898
fn from(current: type_c::Current) -> Self {
9999
policy::PowerCapability {
100100
voltage_mv: 5000,
101-
// Assume lower power for now
102-
current_ma: current.to_ma(true),
101+
// Assume higher power for now
102+
current_ma: current.to_ma(false),
103103
}
104104
}
105105
}

examples/rt685s-evk/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/rt685s-evk/src/bin/type_c.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ async fn main(spawner: Spawner) {
176176
mask.set_intel_vid_status_updated(true);
177177
mask.set_usb_status_updated(true);
178178
mask.set_power_path_switch_changed(true);
179+
mask.set_sink_ready(true);
179180
*mask
180181
})
181182
.await

examples/std/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)