Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 27 additions & 90 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 13 additions & 11 deletions battery-service/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use crate::device::Device;
use crate::device::{self, DeviceId};
use embassy_sync::channel::Channel;
use embassy_sync::channel::TrySendError;
use embassy_sync::mutex::Mutex;
use embassy_sync::{blocking_mutex::raw::NoopRawMutex, channel::TrySendError};
use embassy_time::{with_timeout, Duration};
use embedded_services::GlobalRawMutex;
use embedded_services::{debug, error, info, intrusive_list, trace, warn, IntrusiveList};

use core::cell::Cell;
use core::ops::DerefMut;
use core::sync::atomic::AtomicUsize;

/// Battery service states.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down Expand Up @@ -104,10 +105,10 @@ pub struct BatteryEvent {
/// Battery service context, hardware agnostic state.
pub struct Context {
fuel_gauges: IntrusiveList,
state: Mutex<NoopRawMutex, State>,
battery_event: Channel<NoopRawMutex, BatteryEvent, 1>,
battery_response: Channel<NoopRawMutex, BatteryResponse, 1>,
no_op_retry_count: Cell<usize>,
state: Mutex<GlobalRawMutex, State>,
battery_event: Channel<GlobalRawMutex, BatteryEvent, 1>,
battery_response: Channel<GlobalRawMutex, BatteryResponse, 1>,
no_op_retry_count: AtomicUsize,
config: Config,
}

Expand All @@ -133,7 +134,7 @@ impl Context {
state: Mutex::new(State::NotPresent),
battery_event: Channel::new(),
battery_response: Channel::new(),
no_op_retry_count: Cell::new(0),
no_op_retry_count: AtomicUsize::new(0),
config: Default::default(),
}
}
Expand All @@ -144,7 +145,7 @@ impl Context {
state: Mutex::new(State::NotPresent),
battery_event: Channel::new(),
battery_response: Channel::new(),
no_op_retry_count: Cell::new(0),
no_op_retry_count: AtomicUsize::new(0),
config,
}
}
Expand All @@ -161,12 +162,13 @@ impl Context {

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

/// Set global state machine NotOperational retry count.
fn set_state_machine_retry_count(&self, retry_count: usize) {
self.no_op_retry_count.set(retry_count)
self.no_op_retry_count
.store(retry_count, core::sync::atomic::Ordering::Relaxed)
}

/// Main processing function.
Expand All @@ -185,7 +187,7 @@ impl Context {
},
Err(_) => {
error!("Battery state machine timeout!");
// Should be infalliable
// Should be infallible
self.do_state_machine(BatteryEvent {
event: BatteryEventInner::Timeout,
device_id: event.device_id,
Expand Down
7 changes: 3 additions & 4 deletions battery-service/src/device.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use core::cell::Cell;

use embassy_sync::blocking_mutex::raw::NoopRawMutex;
use embassy_sync::channel::Channel;
use embassy_time::Duration;
use embedded_services::{Node, NodeContainer};
use embedded_services::{GlobalRawMutex, Node, NodeContainer};

#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
Expand Down Expand Up @@ -115,8 +114,8 @@ pub struct DeviceId(pub u8);
pub struct Device {
node: embedded_services::Node,
id: DeviceId,
command: Channel<NoopRawMutex, Command, 1>,
response: Channel<NoopRawMutex, Response, 1>,
command: Channel<GlobalRawMutex, Command, 1>,
response: Channel<GlobalRawMutex, Response, 1>,
dynamic_battery_cache: Cell<DynamicBatteryMsgs>,
static_battery_cache: Cell<StaticBatteryMsgs>,
timeout: Cell<Duration>,
Expand Down
Loading