Skip to content

Commit ce05184

Browse files
authored
type-c-service: Pull in PPM state machine changes (#487)
1 parent 1e331a3 commit ce05184

File tree

8 files changed

+226
-223
lines changed

8 files changed

+226
-223
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: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,15 +1174,18 @@ pub(super) async fn execute_external_controller_command(
11741174
}
11751175

11761176
/// Execute an external UCSI command
1177-
pub(super) async fn execute_external_ucsi_command(
1178-
command: ucsi::GlobalCommand,
1179-
) -> Result<external::UcsiResponse, PdError> {
1177+
pub(super) async fn execute_external_ucsi_command(command: ucsi::GlobalCommand) -> external::UcsiResponse {
11801178
let context = CONTEXT.get().await;
11811179
match context.external_command.execute(external::Command::Ucsi(command)).await {
11821180
external::Response::Ucsi(response) => response,
11831181
r => {
11841182
error!("Invalid response: expected external UCSI, got {:?}", r);
1185-
Err(PdError::InvalidResponse)
1183+
external::UcsiResponse {
1184+
// Always notify OPM of an error
1185+
notify_opm: true,
1186+
cci: ucsi::cci::GlobalCci::new_error(),
1187+
data: Err(PdError::InvalidResponse),
1188+
}
11861189
}
11871190
}
11881191
}

embedded-service/src/type_c/external.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,22 @@ pub enum Command {
132132
pub struct UcsiResponse {
133133
/// Notify the OPM, the function call
134134
pub notify_opm: bool,
135-
/// UCSI response
136-
pub response: ucsi::GlobalResponse,
135+
/// Response CCI
136+
pub cci: ucsi::cci::GlobalCci,
137+
/// UCSI response data
138+
pub data: Result<Option<ucsi::ResponseData>, PdError>,
139+
}
140+
141+
/// Alias to help simplify conversion into a result
142+
pub type UcsiResponseResult = Result<ucsi::GlobalResponse, PdError>;
143+
144+
impl From<UcsiResponse> for UcsiResponseResult {
145+
fn from(value: UcsiResponse) -> Self {
146+
match value.data {
147+
Ok(data) => Ok(ucsi::GlobalResponse { cci: value.cci, data }),
148+
Err(err) => Err(err),
149+
}
150+
}
137151
}
138152

139153
/// External command response for type-C service
@@ -145,7 +159,7 @@ pub enum Response<'a> {
145159
/// Controller command response
146160
Controller(ControllerResponse<'a>),
147161
/// UCSI command response
148-
Ucsi(Result<UcsiResponse, PdError>),
162+
Ucsi(UcsiResponse),
149163
}
150164

151165
/// Get the status of the given port.
@@ -327,7 +341,7 @@ pub async fn reconfigure_retimer(port: GlobalPortId) -> Result<(), PdError> {
327341
}
328342

329343
/// Execute a UCSI command
330-
pub async fn execute_ucsi_command(command: ucsi::GlobalCommand) -> Result<UcsiResponse, PdError> {
344+
pub async fn execute_ucsi_command(command: ucsi::GlobalCommand) -> UcsiResponse {
331345
execute_external_ucsi_command(command).await
332346
}
333347

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/ucsi.rs

Lines changed: 75 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use embassy_executor::{Executor, Spawner};
22
use embedded_services::type_c::controller;
3-
use embedded_services::type_c::external::execute_ucsi_command;
3+
use embedded_services::type_c::external::{UcsiResponseResult, execute_ucsi_command};
4+
use embedded_usb_pd::GlobalPortId;
5+
use embedded_usb_pd::ucsi::ppm::ack_cc_ci::Ack;
46
use embedded_usb_pd::ucsi::ppm::get_capability::ResponseData as UcsiCapabilities;
57
use embedded_usb_pd::ucsi::ppm::set_notification_enable::NotificationEnable;
6-
use embedded_usb_pd::ucsi::{Command, ppm};
8+
use embedded_usb_pd::ucsi::{Command, lpm, ppm};
79
use log::*;
810
use static_cell::StaticCell;
911
use type_c_service::service::config::Config;
@@ -15,39 +17,98 @@ async fn task(_spawner: Spawner) {
1517
controller::init();
1618

1719
info!("Resetting PPM...");
18-
let response = execute_ucsi_command(Command::PpmCommand(ppm::Command::PpmReset))
20+
let response: UcsiResponseResult = execute_ucsi_command(Command::PpmCommand(ppm::Command::PpmReset))
1921
.await
20-
.unwrap();
21-
if !response.response.cci.reset_complete() || response.response.cci.error() {
22-
error!("PPM reset failed: {:?}", response.response.cci);
22+
.into();
23+
let response = response.unwrap();
24+
if !response.cci.reset_complete() || response.cci.error() {
25+
error!("PPM reset failed: {:?}", response.cci);
2326
} else {
2427
info!("PPM reset successful");
2528
}
2629

2730
info!("Set Notification enable...");
2831
let mut notifications = NotificationEnable::default();
2932
notifications.set_cmd_complete(true);
30-
let response = execute_ucsi_command(Command::PpmCommand(ppm::Command::SetNotificationEnable(
33+
let response: UcsiResponseResult = execute_ucsi_command(Command::PpmCommand(ppm::Command::SetNotificationEnable(
3134
ppm::set_notification_enable::Args {
3235
notification_enable: notifications,
3336
},
3437
)))
3538
.await
36-
.unwrap();
37-
if !response.response.cci.cmd_complete() || response.response.cci.error() {
38-
error!("Set Notification enable failed: {:?}", response.response.cci);
39+
.into();
40+
let response = response.unwrap();
41+
if !response.cci.cmd_complete() || response.cci.error() {
42+
error!("Set Notification enable failed: {:?}", response.cci);
3943
} else {
4044
info!("Set Notification enable successful");
4145
}
4246

47+
info!("Sending command complete ack...");
48+
let response: UcsiResponseResult =
49+
execute_ucsi_command(Command::PpmCommand(ppm::Command::AckCcCi(ppm::ack_cc_ci::Args {
50+
ack: *Ack::default().set_command_complete(true),
51+
})))
52+
.await
53+
.into();
54+
let response = response.unwrap();
55+
if !response.cci.ack_command() || response.cci.error() {
56+
error!("Sending command complete ack failed: {:?}", response.cci);
57+
} else {
58+
info!("Sending command complete ack successful");
59+
}
60+
4361
info!("Get PPM capabilities...");
44-
let response = execute_ucsi_command(Command::PpmCommand(ppm::Command::GetCapability))
62+
let response: UcsiResponseResult = execute_ucsi_command(Command::PpmCommand(ppm::Command::GetCapability))
4563
.await
46-
.unwrap();
47-
if !response.response.cci.cmd_complete() || response.response.cci.error() {
64+
.into();
65+
let response = response.unwrap();
66+
if !response.cci.cmd_complete() || response.cci.error() {
4867
error!("Get PPM capabilities failed: {response:?}");
4968
} else {
50-
info!("Get PPM capabilities successful: {:?}", response.response.data);
69+
info!("Get PPM capabilities successful: {:?}", response.data);
70+
}
71+
72+
info!("Sending command complete ack...");
73+
let response: UcsiResponseResult =
74+
execute_ucsi_command(Command::PpmCommand(ppm::Command::AckCcCi(ppm::ack_cc_ci::Args {
75+
ack: *Ack::default().set_command_complete(true),
76+
})))
77+
.await
78+
.into();
79+
let response = response.unwrap();
80+
if !response.cci.ack_command() || response.cci.error() {
81+
error!("Sending command complete ack failed: {:?}", response.cci);
82+
} else {
83+
info!("Sending command complete ack successful");
84+
}
85+
86+
info!("Get connector status...");
87+
let response: UcsiResponseResult = execute_ucsi_command(Command::LpmCommand(lpm::GlobalCommand {
88+
port: GlobalPortId(0),
89+
operation: lpm::CommandData::GetConnectorStatus,
90+
}))
91+
.await
92+
.into();
93+
let response = response.unwrap();
94+
if !response.cci.cmd_complete() || response.cci.error() {
95+
error!("Get connector status failed: {:?}", response.cci);
96+
} else {
97+
info!("Get connector status successful: {:?}", response.data);
98+
}
99+
100+
info!("Sending command complete ack...");
101+
let response: UcsiResponseResult =
102+
execute_ucsi_command(Command::PpmCommand(ppm::Command::AckCcCi(ppm::ack_cc_ci::Args {
103+
ack: *Ack::default().set_command_complete(true),
104+
})))
105+
.await
106+
.into();
107+
let response = response.unwrap();
108+
if !response.cci.ack_command() || response.cci.error() {
109+
error!("Sending command complete ack failed: {:?}", response.cci);
110+
} else {
111+
info!("Sending command complete ack successful");
51112
}
52113
}
53114

0 commit comments

Comments
 (0)