Skip to content

Commit 80c8794

Browse files
authored
Update dependencies and replace Cell and RefCell with SyncCell and Mutex, respectively (#372)
- NoopRawMutex changed to CriticalSectionRawMutex - embedded_service::Buffer fixed to use `AtomicPtr<T>` rather than `*mut [T]`
1 parent ae961a6 commit 80c8794

File tree

33 files changed

+453
-428
lines changed

33 files changed

+453
-428
lines changed

Cargo.lock

Lines changed: 27 additions & 90 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

battery-service/src/context.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use crate::device::Device;
22
use crate::device::{self, DeviceId};
33
use embassy_sync::channel::Channel;
4+
use embassy_sync::channel::TrySendError;
45
use embassy_sync::mutex::Mutex;
5-
use embassy_sync::{blocking_mutex::raw::NoopRawMutex, channel::TrySendError};
66
use embassy_time::{with_timeout, Duration};
7+
use embedded_services::GlobalRawMutex;
78
use embedded_services::{debug, error, info, intrusive_list, trace, warn, IntrusiveList};
89

9-
use core::cell::Cell;
1010
use core::ops::DerefMut;
11+
use core::sync::atomic::AtomicUsize;
1112

1213
/// Battery service states.
1314
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -104,10 +105,10 @@ pub struct BatteryEvent {
104105
/// Battery service context, hardware agnostic state.
105106
pub struct Context {
106107
fuel_gauges: IntrusiveList,
107-
state: Mutex<NoopRawMutex, State>,
108-
battery_event: Channel<NoopRawMutex, BatteryEvent, 1>,
109-
battery_response: Channel<NoopRawMutex, BatteryResponse, 1>,
110-
no_op_retry_count: Cell<usize>,
108+
state: Mutex<GlobalRawMutex, State>,
109+
battery_event: Channel<GlobalRawMutex, BatteryEvent, 1>,
110+
battery_response: Channel<GlobalRawMutex, BatteryResponse, 1>,
111+
no_op_retry_count: AtomicUsize,
111112
config: Config,
112113
}
113114

@@ -133,7 +134,7 @@ impl Context {
133134
state: Mutex::new(State::NotPresent),
134135
battery_event: Channel::new(),
135136
battery_response: Channel::new(),
136-
no_op_retry_count: Cell::new(0),
137+
no_op_retry_count: AtomicUsize::new(0),
137138
config: Default::default(),
138139
}
139140
}
@@ -144,7 +145,7 @@ impl Context {
144145
state: Mutex::new(State::NotPresent),
145146
battery_event: Channel::new(),
146147
battery_response: Channel::new(),
147-
no_op_retry_count: Cell::new(0),
148+
no_op_retry_count: AtomicUsize::new(0),
148149
config,
149150
}
150151
}
@@ -161,12 +162,13 @@ impl Context {
161162

162163
/// Get global state machine NotOperational retry count.
163164
fn get_state_machine_retry_count(&self) -> usize {
164-
self.no_op_retry_count.get()
165+
self.no_op_retry_count.load(core::sync::atomic::Ordering::Relaxed)
165166
}
166167

167168
/// Set global state machine NotOperational retry count.
168169
fn set_state_machine_retry_count(&self, retry_count: usize) {
169-
self.no_op_retry_count.set(retry_count)
170+
self.no_op_retry_count
171+
.store(retry_count, core::sync::atomic::Ordering::Relaxed)
170172
}
171173

172174
/// Main processing function.
@@ -185,7 +187,7 @@ impl Context {
185187
},
186188
Err(_) => {
187189
error!("Battery state machine timeout!");
188-
// Should be infalliable
190+
// Should be infallible
189191
self.do_state_machine(BatteryEvent {
190192
event: BatteryEventInner::Timeout,
191193
device_id: event.device_id,

battery-service/src/device.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use core::cell::Cell;
22

3-
use embassy_sync::blocking_mutex::raw::NoopRawMutex;
43
use embassy_sync::channel::Channel;
54
use embassy_time::Duration;
6-
use embedded_services::{Node, NodeContainer};
5+
use embedded_services::{GlobalRawMutex, Node, NodeContainer};
76

87
#[derive(Debug, Clone, Copy)]
98
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
@@ -115,8 +114,8 @@ pub struct DeviceId(pub u8);
115114
pub struct Device {
116115
node: embedded_services::Node,
117116
id: DeviceId,
118-
command: Channel<NoopRawMutex, Command, 1>,
119-
response: Channel<NoopRawMutex, Response, 1>,
117+
command: Channel<GlobalRawMutex, Command, 1>,
118+
response: Channel<GlobalRawMutex, Response, 1>,
120119
dynamic_battery_cache: Cell<DynamicBatteryMsgs>,
121120
static_battery_cache: Cell<StaticBatteryMsgs>,
122121
timeout: Cell<Duration>,

0 commit comments

Comments
 (0)