Skip to content

Commit 6311798

Browse files
zhuyi2024zhuyiAdam Sasine
authored
Add more information to PortStatus command (#389)
1. Add plug_orientation, power_role, data_role to PortStatus command 2. Fix Power switch 3. Add argument "cached" in get_port_status() to call update_port_status() to get the latest port status --------- Co-authored-by: zhuyi <zhuyi@microsoft.com> Co-authored-by: Adam Sasine <adam.sasine@gmail.com>
1 parent 889ecf8 commit 6311798

File tree

13 files changed

+89
-33
lines changed

13 files changed

+89
-33
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/type_c/controller.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use embassy_sync::signal::Signal;
77
use embassy_time::{Duration, with_timeout};
88
use embedded_usb_pd::ucsi::lpm;
99
use embedded_usb_pd::{
10-
Error, GlobalPortId, PdError, PortId as LocalPortId,
10+
DataRole, Error, GlobalPortId, PdError, PlugOrientation, PortId as LocalPortId, PowerRole,
1111
pdinfo::{AltMode, PowerPathStatus},
1212
type_c::ConnectionState,
1313
};
@@ -40,6 +40,12 @@ pub struct PortStatus {
4040
pub connection_state: Option<ConnectionState>,
4141
/// Port partner supports dual-power roles
4242
pub dual_power: bool,
43+
/// plug orientation
44+
pub plug_orientation: PlugOrientation,
45+
/// power role
46+
pub power_role: PowerRole,
47+
/// data role
48+
pub data_role: DataRole,
4349
/// Active alt-modes
4450
pub alt_mode: AltMode,
4551
/// Power path status
@@ -55,6 +61,9 @@ impl PortStatus {
5561
available_sink_contract: None,
5662
connection_state: None,
5763
dual_power: false,
64+
plug_orientation: PlugOrientation::CC1,
65+
power_role: PowerRole::Sink,
66+
data_role: DataRole::Dfp,
5867
alt_mode: AltMode::none(),
5968
power_path: PowerPathStatus::none(),
6069
}
@@ -87,7 +96,7 @@ impl Default for PortStatus {
8796
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
8897
pub enum PortCommandData {
8998
/// Get port status
90-
PortStatus,
99+
PortStatus(bool),
91100
/// Get and clear events
92101
ClearEvents,
93102
/// Get retimer fw update state
@@ -313,8 +322,12 @@ pub trait Controller {
313322
port: LocalPortId,
314323
) -> impl Future<Output = Result<PortEventKind, Error<Self::BusError>>>;
315324
/// Returns the port status
316-
fn get_port_status(&mut self, port: LocalPortId)
317-
-> impl Future<Output = Result<PortStatus, Error<Self::BusError>>>;
325+
fn get_port_status(
326+
&mut self,
327+
port: LocalPortId,
328+
cached: bool,
329+
) -> impl Future<Output = Result<PortStatus, Error<Self::BusError>>>;
330+
318331
/// Returns the retimer fw update state
319332
fn get_rt_fw_update_status(
320333
&mut self,
@@ -620,8 +633,11 @@ impl ContextToken {
620633
}
621634

622635
/// Get the current port status
623-
pub async fn get_port_status(&self, port: GlobalPortId) -> Result<PortStatus, PdError> {
624-
match self.send_port_command(port, PortCommandData::PortStatus).await? {
636+
pub async fn get_port_status(&self, port: GlobalPortId, cached: bool) -> Result<PortStatus, PdError> {
637+
match self
638+
.send_port_command(port, PortCommandData::PortStatus(cached))
639+
.await?
640+
{
625641
PortResponseData::PortStatus(status) => Ok(status),
626642
r => {
627643
error!("Invalid response: expected port status, got {:?}", r);

embedded-service/src/type_c/external.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub type ControllerResponse<'a> = Result<ControllerResponseData<'a>, PdError>;
4747
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
4848
pub enum PortCommandData {
4949
/// Get port status
50-
PortStatus,
50+
PortStatus(bool),
5151
/// Get retimer fw update status
5252
RetimerFwUpdateGetState,
5353
/// Set retimer fw update status
@@ -107,7 +107,7 @@ pub enum Response<'a> {
107107
pub async fn get_port_status(port: GlobalPortId) -> Result<PortStatus, PdError> {
108108
match execute_external_port_command(Command::Port(PortCommand {
109109
port,
110-
data: PortCommandData::PortStatus,
110+
data: PortCommandData::PortStatus(false),
111111
}))
112112
.await?
113113
{

examples/rt633/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/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/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.

examples/std/src/bin/type_c/basic.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use embassy_executor::{Executor, Spawner};
22
use embassy_sync::once_lock::OnceLock;
33
use embassy_time::Timer;
44
use embedded_services::power;
5-
use embedded_services::type_c::{controller, ControllerId};
5+
use embedded_services::type_c::{ControllerId, controller};
66
use embedded_usb_pd::ucsi::lpm;
77
use embedded_usb_pd::{GlobalPortId, PdError as Error};
88
use log::*;
@@ -82,7 +82,7 @@ mod test_controller {
8282
command: controller::PortCommand,
8383
) -> Result<controller::PortResponseData, Error> {
8484
Ok(match command.data {
85-
controller::PortCommandData::PortStatus => {
85+
controller::PortCommandData::PortStatus(true) => {
8686
info!("Port status for port {}", command.port.0);
8787
controller::PortResponseData::PortStatus(PortStatus::new())
8888
}
@@ -149,10 +149,10 @@ async fn task(spawner: Spawner) {
149149
let status = context.get_controller_status(CONTROLLER0).await.unwrap();
150150
info!("Controller 0 status: {status:#?}");
151151

152-
let status = context.get_port_status(PORT0).await.unwrap();
152+
let status = context.get_port_status(PORT0, true).await.unwrap();
153153
info!("Port 0 status: {status:#?}");
154154

155-
let status = context.get_port_status(PORT1).await.unwrap();
155+
let status = context.get_port_status(PORT1, true).await.unwrap();
156156
info!("Port 1 status: {status:#?}");
157157
}
158158

examples/std/src/bin/type_c/external.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Low-level example of external messaging with a simple type-C service
22
use embassy_executor::{Executor, Spawner};
3-
use embedded_services::type_c::{external, ControllerId};
3+
use embedded_services::type_c::{ControllerId, external};
44
use embedded_usb_pd::GlobalPortId;
55
use log::*;
66
use static_cell::StaticCell;

examples/std/src/bin/type_c/service.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,11 @@ mod test_controller {
120120
Ok(events)
121121
}
122122

123-
async fn get_port_status(&mut self, _port: LocalPortId) -> Result<PortStatus, Error<Self::BusError>> {
123+
async fn get_port_status(
124+
&mut self,
125+
_port: LocalPortId,
126+
_cached: bool,
127+
) -> Result<PortStatus, Error<Self::BusError>> {
124128
debug!("Get port status: {:#?}", *self.state.status.lock().await);
125129
Ok(*self.state.status.lock().await)
126130
}

type-c-service/src/driver/tps6699x.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use embedded_services::{debug, info, trace, type_c, warn, GlobalRawMutex};
2121
use embedded_usb_pd::pdinfo::PowerPathStatus;
2222
use embedded_usb_pd::pdo::{sink, source, Common, Rdo};
2323
use embedded_usb_pd::type_c::Current as TypecCurrent;
24-
use embedded_usb_pd::{Error, GlobalPortId, PdError, PortId as LocalPortId};
24+
use embedded_usb_pd::{DataRole, Error, GlobalPortId, PdError, PlugOrientation, PortId as LocalPortId, PowerRole};
2525
use tps6699x::asynchronous::embassy as tps6699x_drv;
2626
use tps6699x::asynchronous::fw_update::UpdateTarget;
2727
use tps6699x::asynchronous::fw_update::{
@@ -136,6 +136,22 @@ impl<'a, const N: usize, M: RawMutex, B: I2c> Tps6699x<'a, N, M, B> {
136136
port_status.available_sink_contract = new_contract;
137137
}
138138

139+
port_status.plug_orientation = if status.plug_orientation() {
140+
PlugOrientation::CC2
141+
} else {
142+
PlugOrientation::CC1
143+
};
144+
port_status.power_role = if status.port_role() {
145+
PowerRole::Source
146+
} else {
147+
PowerRole::Sink
148+
};
149+
port_status.data_role = if status.data_role() {
150+
DataRole::Dfp
151+
} else {
152+
DataRole::Ufp
153+
};
154+
139155
// Update alt-mode status
140156
let alt_mode = tps6699x.get_alt_mode_status(port).await?;
141157
debug!("Port{} alt mode: {:#?}", port.0, alt_mode);
@@ -145,12 +161,12 @@ impl<'a, const N: usize, M: RawMutex, B: I2c> Tps6699x<'a, N, M, B> {
145161
let power_path = tps6699x.get_power_path_status(port).await?;
146162
port_status.power_path = match port {
147163
PORT0 => PowerPathStatus::new(
148-
power_path.pa_ext_vbus_sw() == PpExtVbusSw::EnabledInput,
149164
power_path.pa_int_vbus_sw() == PpIntVbusSw::EnabledOutput,
165+
power_path.pa_ext_vbus_sw() == PpExtVbusSw::EnabledInput,
150166
),
151167
PORT1 => PowerPathStatus::new(
152-
power_path.pb_ext_vbus_sw() == PpExtVbusSw::EnabledInput,
153168
power_path.pb_int_vbus_sw() == PpIntVbusSw::EnabledOutput,
169+
power_path.pb_ext_vbus_sw() == PpExtVbusSw::EnabledInput,
154170
),
155171
_ => Err(PdError::InvalidPort)?,
156172
};
@@ -349,11 +365,26 @@ impl<const N: usize, M: RawMutex, B: I2c> Controller for Tps6699x<'_, N, M, B> {
349365
async fn get_port_status(
350366
&mut self,
351367
port: LocalPortId,
368+
cached: bool,
352369
) -> Result<type_c::controller::PortStatus, Error<Self::BusError>> {
353370
if port.0 >= self.port_status.len() as u8 {
354371
return PdError::InvalidPort.into();
355372
}
356373

374+
// sync port status
375+
if !cached {
376+
debug!("update port status");
377+
378+
let mut tps6699x = self
379+
.tps6699x
380+
.try_lock()
381+
.expect("Driver should not have been locked before this, thus infallible");
382+
383+
let _ = self.update_port_status(&mut tps6699x, port).await;
384+
} else {
385+
debug!("using cached port status");
386+
}
387+
357388
Ok(*self.port_status[port.0 as usize].lock().await)
358389
}
359390

0 commit comments

Comments
 (0)