11use crate :: device:: Device ;
22use crate :: device:: { self , DeviceId } ;
33use embassy_sync:: channel:: Channel ;
4+ use embassy_sync:: channel:: TrySendError ;
45use embassy_sync:: mutex:: Mutex ;
5- use embassy_sync:: { blocking_mutex:: raw:: NoopRawMutex , channel:: TrySendError } ;
66use embassy_time:: { with_timeout, Duration } ;
7+ use embedded_services:: GlobalRawMutex ;
78use embedded_services:: { debug, error, info, intrusive_list, trace, warn, IntrusiveList } ;
89
9- use core:: cell:: Cell ;
1010use 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.
105106pub 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 ,
0 commit comments