Skip to content

Commit b18ef8b

Browse files
committed
Update battery-service device object to use Mutexes and SyncCell instead of Cell
1 parent 606ed52 commit b18ef8b

File tree

5 files changed

+47
-47
lines changed

5 files changed

+47
-47
lines changed

battery-service/src/device.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
use core::cell::Cell;
2-
3-
use embassy_sync::channel::Channel;
1+
use embassy_sync::{channel::Channel, mutex::Mutex};
42
use embassy_time::Duration;
5-
use embedded_services::{GlobalRawMutex, Node, NodeContainer};
3+
use embedded_services::{GlobalRawMutex, Node, NodeContainer, SyncCell};
64

75
#[derive(Debug, Clone, Copy)]
86
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
@@ -116,9 +114,9 @@ pub struct Device {
116114
id: DeviceId,
117115
command: Channel<GlobalRawMutex, Command, 1>,
118116
response: Channel<GlobalRawMutex, Response, 1>,
119-
dynamic_battery_cache: Cell<DynamicBatteryMsgs>,
120-
static_battery_cache: Cell<StaticBatteryMsgs>,
121-
timeout: Cell<Duration>,
117+
dynamic_battery_cache: Mutex<GlobalRawMutex, DynamicBatteryMsgs>,
118+
static_battery_cache: Mutex<GlobalRawMutex, StaticBatteryMsgs>,
119+
timeout: SyncCell<Duration>,
122120
}
123121

124122
impl Device {
@@ -128,9 +126,9 @@ impl Device {
128126
id,
129127
command: Channel::new(),
130128
response: Channel::new(),
131-
dynamic_battery_cache: Cell::default(),
132-
static_battery_cache: Cell::default(),
133-
timeout: Cell::new(Duration::from_secs(60)),
129+
dynamic_battery_cache: Mutex::default(),
130+
static_battery_cache: Mutex::default(),
131+
timeout: SyncCell::new(Duration::from_secs(60)),
134132
}
135133
}
136134

@@ -166,23 +164,23 @@ impl Device {
166164
}
167165

168166
/// Set dynamic battery cache with updated values.
169-
pub fn set_dynamic_battery_cache(&self, new_values: DynamicBatteryMsgs) {
170-
self.dynamic_battery_cache.set(new_values);
167+
pub async fn set_dynamic_battery_cache(&self, new_values: DynamicBatteryMsgs) {
168+
*self.dynamic_battery_cache.lock().await = new_values;
171169
}
172170

173171
/// Set static battery cache with updated values.
174-
pub fn set_static_battery_cache(&self, new_values: StaticBatteryMsgs) {
175-
self.static_battery_cache.set(new_values);
172+
pub async fn set_static_battery_cache(&self, new_values: StaticBatteryMsgs) {
173+
*self.static_battery_cache.lock().await = new_values;
176174
}
177175

178176
/// Get dynamic battery cache.
179-
pub fn get_dynamic_battery_cache(&self) -> DynamicBatteryMsgs {
180-
self.dynamic_battery_cache.get()
177+
pub async fn get_dynamic_battery_cache(&self) -> DynamicBatteryMsgs {
178+
*self.dynamic_battery_cache.lock().await
181179
}
182180

183181
/// Get static battery cache.
184-
pub fn get_static_battery_cache(&self) -> StaticBatteryMsgs {
185-
self.static_battery_cache.get()
182+
pub async fn get_static_battery_cache(&self) -> StaticBatteryMsgs {
183+
*self.static_battery_cache.lock().await
186184
}
187185

188186
/// Set device timeout.

battery-service/src/wrapper.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl<'a, C: Controller> Wrapper<'a, C> {
7676
},
7777
Command::UpdateStaticCache => match controller.get_static_data().await {
7878
Ok(static_data) => {
79-
device.set_static_battery_cache(static_data);
79+
device.set_static_battery_cache(static_data).await;
8080
device
8181
.send_response(Ok(crate::device::InternalResponse::Complete))
8282
.await;
@@ -88,7 +88,7 @@ impl<'a, C: Controller> Wrapper<'a, C> {
8888
},
8989
Command::UpdateDynamicCache => match controller.get_dynamic_data().await {
9090
Ok(dynamic_data) => {
91-
device.set_dynamic_battery_cache(dynamic_data);
91+
device.set_dynamic_battery_cache(dynamic_data).await;
9292
device
9393
.send_response(Ok(crate::device::InternalResponse::Complete))
9494
.await;

examples/rt633/src/bin/espi_battery.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ async fn battery_publish_task(fg_device: &'static Device) {
217217
loop {
218218
Timer::after_secs(1).await;
219219
// Get dynamic cache
220-
let cache = fg_device.get_dynamic_battery_cache();
220+
let cache = fg_device.get_dynamic_battery_cache().await;
221221

222222
// Send cache data to eSpi service
223223
battery_service::comms_send(

examples/std/src/bin/battery.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,17 @@ use static_cell::StaticCell;
1818
mod espi_service {
1919
use battery_service::context::{BatteryEvent, BatteryEventInner};
2020
use battery_service::device::DeviceId;
21-
use embassy_sync::blocking_mutex::raw::NoopRawMutex;
2221
use embassy_sync::once_lock::OnceLock;
2322
use embassy_sync::signal::Signal;
2423
use embassy_time::Timer;
2524
use embedded_services::comms::{self, EndpointID, External};
2625
use embedded_services::ec_type::message::BatteryMessage;
27-
use embedded_services::error;
26+
use embedded_services::{GlobalRawMutex, error};
2827
use log::info;
2928

3029
pub struct Service {
3130
endpoint: comms::Endpoint,
32-
_signal: Signal<NoopRawMutex, BatteryMessage>,
31+
_signal: Signal<GlobalRawMutex, BatteryMessage>,
3332
}
3433

3534
impl Service {

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

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ use embassy_time::Timer;
44
use embedded_cfu_protocol::protocol_definitions::{FwUpdateOfferResponse, HostToken};
55
use embedded_services::comms;
66
use 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};
98
use embedded_usb_pd::Error;
109
use embedded_usb_pd::GlobalPortId;
1110
use embedded_usb_pd::PortId as LocalPortId;
11+
use embedded_usb_pd::type_c::Current;
1212
use log::*;
1313
use static_cell::StaticCell;
1414

@@ -19,30 +19,33 @@ const POWER0: power::policy::DeviceId = power::policy::DeviceId(0);
1919
mod 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

Comments
 (0)