Interface for changing power states and restricting when they are allowed.
There are three supported MCU Power States:
Some devices support multiple modes for the DeepSleep power state. The currently selected DeepSleep mode, which will be entered by mtb_hal_syspm_deepsleep, can be determined via the mtb_hal_syspm_get_deepsleep_mode function. Configuration of the DeepSleep mode can be performed via platform specific APIs (e.g. PDL).
Any time a power state transition is requested a series of callbacks are invoked. This allows peripherals, or other parts of the application, to confirm they are not currently doing something that would not work in the requested power state. Middleware has the option to register a callback function(s) to be called on requested state transitions by callling mtb_hal_syspm_register_callback. Each callback registered can specify the exact set of states ( mtb_hal_syspm_callback_state_t ) that it should be called for. Each callback is invoked multiple times as part of the transition process as defined by mtb_hal_syspm_callback_mode_t.
At any point the code can lock the ability to enter deep sleep by calling mtb_hal_syspm_lock_deepsleep. This should be done in critical blocks that need to continue remain active. When the critical work is complete, and the lock is no longer needed, it can be released by calling mtb_hal_syspm_unlock_deepsleep. The lock is a counter with a max count of USHRT_MAX. It must be locked/unlocked an equal number of times before the device is actually able to enter deep sleep.
This driver provides control over multiple different types of power management and when those transitions are allowed:
Unlike most HAL drivers this does not require initializing an instance object. The APIs provided here can be called at anytime. See the snippets below for examples of how to use this driver.
The following snippet shows how to use the deep sleep locking APIs to restrict when the device can enter deep sleep. In between the lock/unlock calls any attempt to change power modes will automatically be canceled.
The following snippet shows the different functions that exist to change power states on the device and how they can each be called.
The following snippet shows how to use the callback mechanisms to manage whether it is safe to enter low power modes.
API Reference | |
SYSPM HAL Results | |
SYSPM specific return codes. | |
Data Structures | |
struct | mtb_hal_syspm_callback_params_t |
Power management callback data object. More... | |
Typedefs | |
typedef bool(* | mtb_hal_syspm_callback_t) (mtb_hal_syspm_callback_state_t state, mtb_hal_syspm_callback_mode_t mode, void *callback_arg) |
The system wide custom action power callback type. More... | |
Enumerations | |
enum | mtb_hal_syspm_system_deep_sleep_mode_t { MTB_HAL_SYSPM_SYSTEM_DEEPSLEEP_NONE , MTB_HAL_SYSPM_SYSTEM_DEEPSLEEP , MTB_HAL_SYSPM_SYSTEM_DEEPSLEEP_RAM , MTB_HAL_SYSPM_SYSTEM_DEEPSLEEP_OFF } |
Enumeration of the system Deep Sleep modes. More... | |
enum | mtb_hal_syspm_callback_state_t { MTB_HAL_SYSPM_CB_CPU_SLEEP = MTB_HAL_MAP_SYSPM_CB_SLEEP , MTB_HAL_SYSPM_CB_CPU_DEEPSLEEP = MTB_HAL_MAP_SYSPM_CB_DEEPSLEEP , MTB_HAL_SYSPM_CB_CPU_DEEPSLEEP_RAM = MTB_HAL_MAP_SYSPM_CB_DEEPSLEEP_RAM , MTB_HAL_SYSPM_CB_SYSTEM_HIBERNATE = MTB_HAL_MAP_SYSPM_CB_HIBERNATE , MTB_HAL_SYSPM_CB_SYSTEM_NORMAL = MTB_HAL_MAP_SYSPM_CB_NORMAL , MTB_HAL_SYSPM_CB_SYSTEM_LOW = MTB_HAL_MAP_SYSPM_CB_LOW , MTB_HAL_SYSPM_CB_SYSTEM_HIGH = MTB_HAL_MAP_SYSPM_CB_HIGH } |
Flags enum for the cpu state in a custom callback. More... | |
enum | mtb_hal_syspm_callback_mode_t { MTB_HAL_SYSPM_CHECK_READY = MTB_HAL_MAP_SYSPM_CHECK_READY , MTB_HAL_SYSPM_CHECK_FAIL = MTB_HAL_MAP_SYSPM_CHECK_FAIL , MTB_HAL_SYSPM_BEFORE_TRANSITION = MTB_HAL_MAP_SYSPM_BEFORE_TRANSITION , MTB_HAL_SYSPM_AFTER_TRANSITION = MTB_HAL_MAP_SYSPM_AFTER_TRANSITION , MTB_HAL_SYSPM_AFTER_DS_WFI_TRANSITION = MTB_HAL_MAP_SYSPM_AFTER_DS_WFI_TRANSITION } |
Enumeration of the transition modes in custom callback. More... | |
Functions | |
cy_rslt_t | mtb_hal_syspm_register_callback (mtb_hal_syspm_callback_data_t *obj, mtb_hal_syspm_callback_params_t *params) |
Register the specified handler with the power manager to be notified of power state changes. More... | |
cy_rslt_t | mtb_hal_syspm_unregister_callback (mtb_hal_syspm_callback_data_t *obj) |
Removes the registered handler from the power manager so no future notifications are made. More... | |
cy_rslt_t | mtb_hal_syspm_sleep (void) |
Set CPU to sleep mode. More... | |
cy_rslt_t | mtb_hal_syspm_deepsleep (void) |
Set CPU to deep sleep mode. More... | |
void | mtb_hal_syspm_lock_deepsleep (void) |
Lock deep sleep. More... | |
void | mtb_hal_syspm_unlock_deepsleep (void) |
Unlock deep sleep. More... | |
cy_rslt_t | mtb_hal_syspm_tickless_deepsleep (mtb_hal_lptimer_t *lptimer_obj, uint32_t desired_ms, uint32_t *actual_ms) |
Timed deep-sleep without system timer. More... | |
cy_rslt_t | mtb_hal_syspm_tickless_sleep (mtb_hal_lptimer_t *lptimer_obj, uint32_t desired_ms, uint32_t *actual_ms) |
Timed sleep without system timer. More... | |
mtb_hal_syspm_system_deep_sleep_mode_t | mtb_hal_syspm_get_deepsleep_mode (void) |
Get current deep sleep mode. More... | |
struct mtb_hal_syspm_callback_params_t |
Data Fields | ||
---|---|---|
mtb_hal_syspm_callback_t | callback | Callback to run on power state change. |
mtb_hal_syspm_callback_state_t | state | Power states that should trigger calling the callback. |
mtb_hal_syspm_callback_mode_t | ignore_modes |
Modes to ignore invoking the callback for. Multiple mtb_hal_syspm_callback_mode_t values can be ored together. |
void * | arg | Argument value to provide to the callback. |
typedef bool(* mtb_hal_syspm_callback_t) (mtb_hal_syspm_callback_state_t state, mtb_hal_syspm_callback_mode_t mode, void *callback_arg) |
The system wide custom action power callback type.
[in] | state | State the system or CPU is being transitioned into. |
[in] | mode | Callback mode. |
[in] | callback_arg | User argument passed as part of registering callback. |
Enumeration of the system Deep Sleep modes.
These modes are device specifc and may not be supported on all devices. Refer to the device specific documentation or the Data Sheet to determine what is allowed.
Flags enum for the cpu state in a custom callback.
This is used to indicate what states a callback should be called in. When a callback is called, only one of these will be set at a time.
Enumeration of the transition modes in custom callback.
The general sequence is: CHECK_READY, BEFORE_TRANSITION, AFTER_TRANSITION. If any callback indicates that it is not able to change state as part of CHECK_READY, CHECK_FAIL will be run instead of the BEFORE/AFTER_TRANSITION.
cy_rslt_t mtb_hal_syspm_register_callback | ( | mtb_hal_syspm_callback_data_t * | obj, |
mtb_hal_syspm_callback_params_t * | params | ||
) |
Register the specified handler with the power manager to be notified of power state changes.
This callback will be called before any of the peripheral callbacks for MTB_HAL_SYSPM_CHECK_READY and MTB_HAL_SYSPM_BEFORE_TRANSITION.
[out] | obj | The callback object. The caller must allocate the memory for this object, but the HAL will populate its |
[in] | params | The paramers for the callback to register |
cy_rslt_t mtb_hal_syspm_unregister_callback | ( | mtb_hal_syspm_callback_data_t * | obj | ) |
Removes the registered handler from the power manager so no future notifications are made.
[in] | obj | The callback object to unregister. |
cy_rslt_t mtb_hal_syspm_sleep | ( | void | ) |
Set CPU to sleep mode.
cy_rslt_t mtb_hal_syspm_deepsleep | ( | void | ) |
Set CPU to deep sleep mode.
void mtb_hal_syspm_lock_deepsleep | ( | void | ) |
Lock deep sleep.
This function prevents the CPU from going to deep sleep. The lock is implemented as a counter from 0 to USHRT_MAX. Each call to this function increments the counter by 1. mtb_hal_syspm_unlock_deepsleep must be called an equal number of times to fully unlock. Deepsleep will only be allowed when the counter is equal to 0.
void mtb_hal_syspm_unlock_deepsleep | ( | void | ) |
Unlock deep sleep.
This function decrements the lock counter by 1 and must be called an equal number of times as mtb_hal_syspm_lock_deepsleep is called to fully unlock. Deepsleep will only be allowed when the counter is equal to 0.
cy_rslt_t mtb_hal_syspm_tickless_deepsleep | ( | mtb_hal_lptimer_t * | lptimer_obj, |
uint32_t | desired_ms, | ||
uint32_t * | actual_ms | ||
) |
Timed deep-sleep without system timer.
Provides a way to deep-sleep for a desired number of milliseconds(ms) with the system timer disabled. The system timer is disabled before sleeping and a low power timer is setup to wake the device from deep-sleep after the desired number of ms have elapsed.
[in] | lptimer_obj | Pre-Initialized LPTimer object. |
[in] | desired_ms | Desired number of ms to deep-sleep. |
[out] | actual_ms | Actual number of ms in which systick timer was disabled. This value can range from 0 to desired_ms - 1 depending on how long the device was able to deep-sleep. |
cy_rslt_t mtb_hal_syspm_tickless_sleep | ( | mtb_hal_lptimer_t * | lptimer_obj, |
uint32_t | desired_ms, | ||
uint32_t * | actual_ms | ||
) |
Timed sleep without system timer.
Provides a way to sleep for a desired number of milliseconds(ms) with the system timer disabled. The system timer is disabled before sleeping and a low power timer is setup to wake the device from sleep after the desired number of ms have elapsed.
[in] | lptimer_obj | Pre-Initialized LPTimer object. |
[in] | desired_ms | Desired number of ms to sleep. |
[out] | actual_ms | Actual number of ms that was spent in sleep. This value can range from 0 to desired_ms - 1 depending on how long the device was able to sleep. |
mtb_hal_syspm_system_deep_sleep_mode_t mtb_hal_syspm_get_deepsleep_mode | ( | void | ) |
Get current deep sleep mode.
Provides a way to get the current deep sleep mode.