Skip to content

Commit 57c10a6

Browse files
committed
Update examples
1 parent 73b626f commit 57c10a6

File tree

2 files changed

+108
-18
lines changed

2 files changed

+108
-18
lines changed

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

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,24 @@ use embassy_imxrt::gpio::{Input, Inverter, Pull};
88
use embassy_imxrt::i2c::Async;
99
use embassy_imxrt::i2c::master::{Config, I2cMaster};
1010
use embassy_imxrt::{bind_interrupts, peripherals};
11+
use embassy_sync::channel::{Channel, DynamicReceiver, DynamicSender};
1112
use embassy_sync::mutex::Mutex;
1213
use embassy_sync::pubsub::PubSubChannel;
13-
use embassy_time::{self as _, Delay};
14+
use embassy_time::{self as _, Delay, Timer};
1415
use embedded_cfu_protocol::protocol_definitions::{FwUpdateOffer, FwUpdateOfferResponse, FwVersion, HostToken};
15-
use embedded_services::power::policy::{CommsMessage, DeviceId as PowerId};
16+
use embedded_services::power::policy::{CommsMessage, DeviceId as PowerId, policy};
1617
use embedded_services::type_c::{Cached, ControllerId};
1718
use embedded_services::{GlobalRawMutex, IntrusiveList};
1819
use embedded_services::{error, info};
1920
use embedded_usb_pd::GlobalPortId;
21+
use power_policy_service::PowerPolicy;
2022
use static_cell::StaticCell;
2123
use tps6699x::asynchronous::embassy as tps6699x;
2224
use type_c_service::driver::tps6699x::{self as tps6699x_drv};
2325
use type_c_service::service::Service;
2426
use type_c_service::wrapper::ControllerWrapper;
25-
use type_c_service::wrapper::backing::{ReferencedStorage, Storage};
27+
use type_c_service::wrapper::backing::{IntermediateStorage, ReferencedStorage, Storage};
28+
use type_c_service::wrapper::proxy::PowerProxyDevice;
2629

2730
extern crate rt685s_evk_example;
2831

@@ -49,7 +52,14 @@ impl type_c_service::wrapper::FwOfferValidator for Validator {
4952
type BusMaster<'a> = I2cMaster<'a, Async>;
5053
type BusDevice<'a> = I2cDevice<'a, GlobalRawMutex, BusMaster<'a>>;
5154
type Tps6699xMutex<'a> = Mutex<GlobalRawMutex, tps6699x_drv::Tps6699x<'a, GlobalRawMutex, BusDevice<'a>>>;
52-
type Wrapper<'a> = ControllerWrapper<'a, GlobalRawMutex, Tps6699xMutex<'a>, Validator>;
55+
type Wrapper<'a> = ControllerWrapper<
56+
'a,
57+
GlobalRawMutex,
58+
Tps6699xMutex<'a>,
59+
DynamicSender<'a, policy::RequestData>,
60+
DynamicReceiver<'a, policy::RequestData>,
61+
Validator,
62+
>;
5363
type Controller<'a> = tps6699x::controller::Controller<GlobalRawMutex, BusDevice<'a>>;
5464
type Interrupt<'a> = tps6699x::Interrupt<'a, GlobalRawMutex, BusDevice<'a>>;
5565

@@ -69,7 +79,15 @@ async fn interrupt_task(mut int_in: Input<'static>, mut interrupt: Interrupt<'st
6979

7080
#[embassy_executor::task]
7181
async fn power_policy_service_task() {
72-
power_policy_service::task::task(Default::default())
82+
static POWER_POLICY: static_cell::StaticCell<
83+
PowerPolicy<Mutex<GlobalRawMutex, PowerProxyDevice<'static>>, DynamicReceiver<'static, policy::RequestData>>,
84+
> = static_cell::StaticCell::new();
85+
let power_policy =
86+
POWER_POLICY.init(PowerPolicy::create(Default::default()).expect("Failed to create power policy"));
87+
88+
// TODO: remove once power policy task accepts context
89+
Timer::after_millis(100).await;
90+
power_policy_service::task::task(power_policy)
7391
.await
7492
.expect("Failed to start power policy service task");
7593
}
@@ -157,13 +175,40 @@ async fn main(spawner: Spawner) {
157175
controller_context,
158176
CONTROLLER0_ID,
159177
0, // CFU component ID
160-
[(PORT0_ID, PORT0_PWR_ID), (PORT1_ID, PORT1_PWR_ID)],
178+
[PORT0_ID, PORT1_ID],
161179
));
162180

163-
static REFERENCED: StaticCell<ReferencedStorage<TPS66994_NUM_PORTS, GlobalRawMutex>> = StaticCell::new();
164-
let referenced = REFERENCED.init(
181+
static INTERMEDIATE: StaticCell<IntermediateStorage<TPS66994_NUM_PORTS, GlobalRawMutex>> = StaticCell::new();
182+
let intermediate = INTERMEDIATE.init(
165183
storage
166-
.create_referenced()
184+
.try_create_intermediate()
185+
.expect("Failed to create intermediate storage"),
186+
);
187+
188+
static POLICY_CHANNEL0: StaticCell<Channel<GlobalRawMutex, policy::RequestData, 1>> = StaticCell::new();
189+
let policy_channel0 = POLICY_CHANNEL0.init(Channel::new());
190+
let policy_sender0 = policy_channel0.dyn_sender();
191+
let policy_receiver0 = policy_channel0.dyn_receiver();
192+
193+
static POLICY_CHANNEL1: StaticCell<Channel<GlobalRawMutex, policy::RequestData, 1>> = StaticCell::new();
194+
let policy_channel1 = POLICY_CHANNEL1.init(Channel::new());
195+
let policy_sender1 = policy_channel1.dyn_sender();
196+
let policy_receiver1 = policy_channel1.dyn_receiver();
197+
198+
static REFERENCED: StaticCell<
199+
ReferencedStorage<
200+
TPS66994_NUM_PORTS,
201+
GlobalRawMutex,
202+
DynamicSender<'_, policy::RequestData>,
203+
DynamicReceiver<'_, policy::RequestData>,
204+
>,
205+
> = StaticCell::new();
206+
let referenced = REFERENCED.init(
207+
intermediate
208+
.try_create_referenced([
209+
(PORT0_PWR_ID, policy_sender0, policy_receiver0),
210+
(PORT1_PWR_ID, policy_sender1, policy_receiver1),
211+
])
167212
.expect("Failed to create referenced storage"),
168213
);
169214

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

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use embassy_imxrt::gpio::{Input, Inverter, Pull};
88
use embassy_imxrt::i2c::Async;
99
use embassy_imxrt::i2c::master::{Config, I2cMaster};
1010
use embassy_imxrt::{bind_interrupts, peripherals};
11+
use embassy_sync::channel::{Channel, DynamicReceiver, DynamicSender};
1112
use embassy_sync::mutex::Mutex;
1213
use embassy_sync::pubsub::PubSubChannel;
1314
use embassy_time::Timer;
@@ -16,18 +17,20 @@ use embedded_cfu_protocol::protocol_definitions::*;
1617
use embedded_cfu_protocol::protocol_definitions::{FwUpdateOffer, FwUpdateOfferResponse, FwVersion};
1718
use embedded_services::cfu::component::InternalResponseData;
1819
use embedded_services::cfu::component::RequestData;
19-
use embedded_services::power::policy::{CommsMessage, DeviceId as PowerId};
20+
use embedded_services::power::policy::{CommsMessage, DeviceId as PowerId, policy};
2021
use embedded_services::type_c::ControllerId;
2122
use embedded_services::type_c::controller::Context;
2223
use embedded_services::{GlobalRawMutex, IntrusiveList, cfu};
2324
use embedded_services::{error, info};
2425
use embedded_usb_pd::GlobalPortId;
26+
use power_policy_service::PowerPolicy;
2527
use static_cell::StaticCell;
2628
use tps6699x::asynchronous::embassy as tps6699x;
2729
use type_c_service::driver::tps6699x::{self as tps6699x_drv};
2830
use type_c_service::service::Service;
2931
use type_c_service::wrapper::ControllerWrapper;
30-
use type_c_service::wrapper::backing::{ReferencedStorage, Storage};
32+
use type_c_service::wrapper::backing::{IntermediateStorage, ReferencedStorage, Storage};
33+
use type_c_service::wrapper::proxy::PowerProxyDevice;
3134

3235
extern crate rt685s_evk_example;
3336

@@ -47,7 +50,14 @@ impl type_c_service::wrapper::FwOfferValidator for Validator {
4750
type BusMaster<'a> = I2cMaster<'a, Async>;
4851
type BusDevice<'a> = I2cDevice<'a, GlobalRawMutex, BusMaster<'a>>;
4952
type Tps6699xMutex<'a> = Mutex<GlobalRawMutex, tps6699x_drv::Tps6699x<'a, GlobalRawMutex, BusDevice<'a>>>;
50-
type Wrapper<'a> = ControllerWrapper<'a, GlobalRawMutex, Tps6699xMutex<'a>, Validator>;
53+
type Wrapper<'a> = ControllerWrapper<
54+
'a,
55+
GlobalRawMutex,
56+
Tps6699xMutex<'a>,
57+
DynamicSender<'a, policy::RequestData>,
58+
DynamicReceiver<'a, policy::RequestData>,
59+
Validator,
60+
>;
5161
type Controller<'a> = tps6699x::controller::Controller<GlobalRawMutex, BusDevice<'a>>;
5262
type Interrupt<'a> = tps6699x::Interrupt<'a, GlobalRawMutex, BusDevice<'a>>;
5363

@@ -155,7 +165,15 @@ async fn fw_update_task() {
155165

156166
#[embassy_executor::task]
157167
async fn power_policy_service_task() {
158-
power_policy_service::task::task(Default::default())
168+
static POWER_POLICY: static_cell::StaticCell<
169+
PowerPolicy<Mutex<GlobalRawMutex, PowerProxyDevice<'static>>, DynamicReceiver<'static, policy::RequestData>>,
170+
> = static_cell::StaticCell::new();
171+
let power_policy =
172+
POWER_POLICY.init(PowerPolicy::create(Default::default()).expect("Failed to create power policy"));
173+
174+
// TODO: remove once power policy task accepts context
175+
Timer::after_millis(100).await;
176+
power_policy_service::task::task(power_policy)
159177
.await
160178
.expect("Failed to start power policy service task");
161179
}
@@ -242,14 +260,41 @@ async fn main(spawner: Spawner) {
242260
let storage = STORAGE.init(Storage::new(
243261
controller_context,
244262
CONTROLLER0_ID,
245-
CONTROLLER0_CFU_ID,
246-
[(PORT0_ID, PORT0_PWR_ID), (PORT1_ID, PORT1_PWR_ID)],
263+
0, // CFU component ID
264+
[PORT0_ID, PORT1_ID],
247265
));
248266

249-
static REFERENCED: StaticCell<ReferencedStorage<TPS66994_NUM_PORTS, GlobalRawMutex>> = StaticCell::new();
250-
let referenced = REFERENCED.init(
267+
static INTERMEDIATE: StaticCell<IntermediateStorage<TPS66994_NUM_PORTS, GlobalRawMutex>> = StaticCell::new();
268+
let intermediate = INTERMEDIATE.init(
251269
storage
252-
.create_referenced()
270+
.try_create_intermediate()
271+
.expect("Failed to create intermediate storage"),
272+
);
273+
274+
static POLICY_CHANNEL0: StaticCell<Channel<GlobalRawMutex, policy::RequestData, 1>> = StaticCell::new();
275+
let policy_channel0 = POLICY_CHANNEL0.init(Channel::new());
276+
let policy_sender0 = policy_channel0.dyn_sender();
277+
let policy_receiver0 = policy_channel0.dyn_receiver();
278+
279+
static POLICY_CHANNEL1: StaticCell<Channel<GlobalRawMutex, policy::RequestData, 1>> = StaticCell::new();
280+
let policy_channel1 = POLICY_CHANNEL1.init(Channel::new());
281+
let policy_sender1 = policy_channel1.dyn_sender();
282+
let policy_receiver1 = policy_channel1.dyn_receiver();
283+
284+
static REFERENCED: StaticCell<
285+
ReferencedStorage<
286+
TPS66994_NUM_PORTS,
287+
GlobalRawMutex,
288+
DynamicSender<'_, policy::RequestData>,
289+
DynamicReceiver<'_, policy::RequestData>,
290+
>,
291+
> = StaticCell::new();
292+
let referenced = REFERENCED.init(
293+
intermediate
294+
.try_create_referenced([
295+
(PORT0_PWR_ID, policy_sender0, policy_receiver0),
296+
(PORT1_PWR_ID, policy_sender1, policy_receiver1),
297+
])
253298
.expect("Failed to create referenced storage"),
254299
);
255300

0 commit comments

Comments
 (0)