@@ -4,11 +4,11 @@ use embassy_time::Timer;
44use embedded_cfu_protocol:: protocol_definitions:: { FwUpdateOfferResponse , HostToken } ;
55use embedded_services:: comms;
66use embedded_services:: power:: { self , policy} ;
7- use embedded_services:: type_c:: { controller, ControllerId } ;
8- use embedded_usb_pd:: type_c:: Current ;
7+ use embedded_services:: type_c:: { ControllerId , controller} ;
98use embedded_usb_pd:: Error ;
109use embedded_usb_pd:: GlobalPortId ;
1110use embedded_usb_pd:: PortId as LocalPortId ;
11+ use embedded_usb_pd:: type_c:: Current ;
1212use log:: * ;
1313use static_cell:: StaticCell ;
1414
@@ -19,30 +19,33 @@ const POWER0: power::policy::DeviceId = power::policy::DeviceId(0);
1919mod test_controller {
2020 use std:: cell:: Cell ;
2121
22- use embassy_sync:: { blocking_mutex:: raw:: NoopRawMutex , signal:: Signal } ;
23- use embedded_services:: type_c:: {
24- controller:: { Contract , ControllerStatus , PortStatus , RetimerFwUpdateState } ,
25- event:: PortEventKind ,
22+ use embassy_sync:: { mutex:: Mutex , signal:: Signal } ;
23+ use embedded_services:: {
24+ GlobalRawMutex ,
25+ type_c:: {
26+ controller:: { Contract , ControllerStatus , PortStatus , RetimerFwUpdateState } ,
27+ event:: PortEventKind ,
28+ } ,
2629 } ;
2730
2831 use super :: * ;
2932
3033 pub struct ControllerState {
31- events : Signal < NoopRawMutex , PortEventKind > ,
32- status : Cell < PortStatus > ,
34+ events : Signal < GlobalRawMutex , PortEventKind > ,
35+ status : Mutex < GlobalRawMutex , PortStatus > ,
3336 }
3437
3538 impl ControllerState {
3639 pub fn new ( ) -> Self {
3740 Self {
3841 events : Signal :: new ( ) ,
39- status : Cell :: new ( PortStatus :: default ( ) ) ,
42+ status : Mutex :: new ( PortStatus :: default ( ) ) ,
4043 }
4144 }
4245
4346 /// Simulate a connection
44- pub fn connect ( & self , _contract : Contract ) {
45- self . status . set ( PortStatus :: new ( ) ) ;
47+ pub async fn connect ( & self , _contract : Contract ) {
48+ * self . status . lock ( ) . await = PortStatus :: new ( ) ;
4649
4750 let mut events = PortEventKind :: none ( ) ;
4851 events. set_plug_inserted_or_removed ( true ) ;
@@ -51,22 +54,22 @@ mod test_controller {
5154 }
5255
5356 /// Simulate a sink connecting
54- pub fn connect_sink ( & self , current : Current ) {
55- self . connect ( Contract :: Sink ( current. into ( ) ) ) ;
57+ pub async fn connect_sink ( & self , current : Current ) {
58+ self . connect ( Contract :: Sink ( current. into ( ) ) ) . await ;
5659 }
5760
5861 /// Simulate a disconnection
59- pub fn disconnect ( & self ) {
60- self . status . set ( PortStatus :: default ( ) ) ;
62+ pub async fn disconnect ( & self ) {
63+ * self . status . lock ( ) . await = PortStatus :: default ( ) ;
6164
6265 let mut events = PortEventKind :: none ( ) ;
6366 events. set_plug_inserted_or_removed ( true ) ;
6467 self . events . signal ( events) ;
6568 }
6669
6770 /// Simulate a debug accessory source connecting
68- pub fn connect_debug_accessory_source ( & self , _current : Current ) {
69- self . status . set ( PortStatus :: new ( ) ) ;
71+ pub async fn connect_debug_accessory_source ( & self , _current : Current ) {
72+ * self . status . lock ( ) . await = PortStatus :: new ( ) ;
7073
7174 let mut events = PortEventKind :: none ( ) ;
7275 events. set_plug_inserted_or_removed ( true ) ;
@@ -112,8 +115,8 @@ mod test_controller {
112115 }
113116
114117 async fn get_port_status ( & mut self , _port : LocalPortId ) -> Result < PortStatus , Error < Self :: BusError > > {
115- debug ! ( "Get port status: {:#?}" , self . state. status. get ( ) ) ;
116- Ok ( self . state . status . get ( ) )
118+ debug ! ( "Get port status: {:#?}" , * self . state. status. lock ( ) . await ) ;
119+ Ok ( * self . state . status . lock ( ) . await )
117120 }
118121
119122 async fn enable_sink_path ( & mut self , _port : LocalPortId , enable : bool ) -> Result < ( ) , Error < Self :: BusError > > {
@@ -267,19 +270,19 @@ async fn task(spawner: Spawner) {
267270 Timer :: after_secs ( 1 ) . await ;
268271
269272 info ! ( "Simulating connection" ) ;
270- state. connect_sink ( Current :: UsbDefault ) ;
273+ state. connect_sink ( Current :: UsbDefault ) . await ;
271274 Timer :: after_millis ( 250 ) . await ;
272275
273276 info ! ( "Simulating disconnection" ) ;
274- state. disconnect ( ) ;
277+ state. disconnect ( ) . await ;
275278 Timer :: after_millis ( 250 ) . await ;
276279
277280 info ! ( "Simulating debug accessory connection" ) ;
278- state. connect_debug_accessory_source ( Current :: UsbDefault ) ;
281+ state. connect_debug_accessory_source ( Current :: UsbDefault ) . await ;
279282 Timer :: after_millis ( 250 ) . await ;
280283
281284 info ! ( "Simulating debug accessory disconnection" ) ;
282- state. disconnect ( ) ;
285+ state. disconnect ( ) . await ;
283286 Timer :: after_millis ( 250 ) . await ;
284287}
285288
0 commit comments