|
| 1 | +//! SoC manager service. |
| 2 | +#![no_std] |
| 3 | + |
| 4 | +pub mod power_guard; |
| 5 | + |
| 6 | +use embassy_sync::mutex::Mutex; |
| 7 | +use embassy_sync::watch::{Receiver, Watch}; |
| 8 | +use embedded_power_sequence::PowerSequence; |
| 9 | +use embedded_services::GlobalRawMutex; |
| 10 | + |
| 11 | +/// SoC manager service error. |
| 12 | +#[derive(Clone, Copy, Debug)] |
| 13 | +#[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| 14 | +pub enum Error { |
| 15 | + /// Unspecified error, likely some invariant was violated. |
| 16 | + Other, |
| 17 | + /// A power sequence error occurred. |
| 18 | + PowerSequence, |
| 19 | + /// An invalid power state transition was requested. |
| 20 | + InvalidStateTransition, |
| 21 | + /// No more power state listeners are available. |
| 22 | + ListenersNotAvailable, |
| 23 | +} |
| 24 | + |
| 25 | +/// An ACPI power state. |
| 26 | +#[derive(Clone, Copy, Debug, PartialEq)] |
| 27 | +#[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| 28 | +pub enum PowerState { |
| 29 | + /// Working state. |
| 30 | + S0, |
| 31 | + /// Modern standby state. |
| 32 | + S0ix, |
| 33 | + /// Sleep state. |
| 34 | + S3, |
| 35 | + /// Hibernate state. |
| 36 | + S4, |
| 37 | + /// Soft off state. |
| 38 | + S5, |
| 39 | +} |
| 40 | + |
| 41 | +/// A power state listener struct. |
| 42 | +pub struct PowerStateListener<'a, const MAX_LISTENERS: usize> { |
| 43 | + rx: Receiver<'a, GlobalRawMutex, PowerState, MAX_LISTENERS>, |
| 44 | +} |
| 45 | + |
| 46 | +impl<'a, const MAX_LISTENERS: usize> PowerStateListener<'a, MAX_LISTENERS> { |
| 47 | + /// Waits for any power state change, returning the new power state. |
| 48 | + pub fn wait_state_change(&mut self) -> impl Future<Output = PowerState> { |
| 49 | + self.rx.changed() |
| 50 | + } |
| 51 | + |
| 52 | + /// Waits for a transition to a specific power state. |
| 53 | + pub async fn wait_for_state(&mut self, power_state: PowerState) { |
| 54 | + self.rx.changed_and(|p| *p == power_state).await; |
| 55 | + } |
| 56 | + |
| 57 | + /// Returns the current power state. |
| 58 | + /// |
| 59 | + /// # Errors |
| 60 | + /// |
| 61 | + /// Returns [`Error::Other`] if the power state is uninitialized. |
| 62 | + pub fn current_state(&mut self) -> Result<PowerState, Error> { |
| 63 | + self.rx.try_get().ok_or(Error::Other) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +/// SoC manager. |
| 68 | +pub struct SocManager<T: PowerSequence, const MAX_LISTENERS: usize> { |
| 69 | + soc: Mutex<GlobalRawMutex, T>, |
| 70 | + power_state: Watch<GlobalRawMutex, PowerState, MAX_LISTENERS>, |
| 71 | +} |
| 72 | + |
| 73 | +impl<T: PowerSequence, const MAX_LISTENERS: usize> SocManager<T, MAX_LISTENERS> { |
| 74 | + /// Creates a new SoC manager instance. |
| 75 | + /// |
| 76 | + /// The `initial_state` should capture the power state the SoC is ALREADY in, not the desired state |
| 77 | + /// to transition to on initilization. |
| 78 | + /// |
| 79 | + /// This will usually be [`PowerState::S5`] (powered off) but not always. |
| 80 | + pub fn new(soc: T, initial_state: PowerState) -> Self { |
| 81 | + let soc_manager = Self { |
| 82 | + soc: Mutex::new(soc), |
| 83 | + power_state: Watch::new(), |
| 84 | + }; |
| 85 | + |
| 86 | + soc_manager.power_state.sender().send(initial_state); |
| 87 | + soc_manager |
| 88 | + } |
| 89 | + |
| 90 | + /// Creates a new power state listener. |
| 91 | + /// |
| 92 | + /// # Errors |
| 93 | + /// |
| 94 | + /// Returns [`Error::ListenersNotAvailable`] if `MAX_LISTENERS` or greater are already in use. |
| 95 | + pub fn new_pwr_listener(&self) -> Result<PowerStateListener<'_, MAX_LISTENERS>, Error> { |
| 96 | + Ok(PowerStateListener { |
| 97 | + rx: self.power_state.receiver().ok_or(Error::InvalidStateTransition)?, |
| 98 | + }) |
| 99 | + } |
| 100 | + |
| 101 | + /// Returns the current power state. |
| 102 | + /// |
| 103 | + /// This method is also available on `PowerStateListener`. |
| 104 | + pub fn current_state(&self) -> Result<PowerState, Error> { |
| 105 | + self.power_state.try_get().ok_or(Error::Other) |
| 106 | + } |
| 107 | + |
| 108 | + /// Sets the current power state. |
| 109 | + /// |
| 110 | + /// # Errors |
| 111 | + /// |
| 112 | + /// Returns [`Error::PowerSequence`] if an error is encountered while transitioning power state. |
| 113 | + /// |
| 114 | + /// Returns [`Error::InvalidStateTransition`] if the requested state is not valid based on current state. |
| 115 | + pub async fn set_power_state(&self, state: PowerState) -> Result<(), Error> { |
| 116 | + // Revisit: Check with other services to see if we are too hot or don't have enough power for requested transition |
| 117 | + // Need to think more about how that will look though |
| 118 | + let cur_state = self.power_state.try_get().ok_or(Error::Other)?; |
| 119 | + let mut soc = self.soc.lock().await; |
| 120 | + match (cur_state, state) { |
| 121 | + // Any sleeping state must first transition to S0 before we can transition to another state |
| 122 | + (PowerState::S0ix, PowerState::S0) => soc.wake_up().await, |
| 123 | + (PowerState::S3, PowerState::S0) => soc.resume().await, |
| 124 | + (PowerState::S4, PowerState::S0) => soc.activate().await, |
| 125 | + (PowerState::S5, PowerState::S0) => soc.power_on().await, |
| 126 | + |
| 127 | + // S0 can then transition to any sleep state |
| 128 | + (PowerState::S0, PowerState::S0ix) => soc.idle().await, |
| 129 | + (PowerState::S0, PowerState::S3) => soc.suspend().await, |
| 130 | + (PowerState::S0, PowerState::S4) => soc.hibernate().await, |
| 131 | + (PowerState::S0, PowerState::S5) => soc.power_off().await, |
| 132 | + |
| 133 | + // Anything else is an invalid transition |
| 134 | + _ => return Err(Error::InvalidStateTransition), |
| 135 | + } |
| 136 | + .map_err(|_| Error::PowerSequence)?; |
| 137 | + |
| 138 | + self.power_state.sender().send(state); |
| 139 | + Ok(()) |
| 140 | + } |
| 141 | +} |
0 commit comments