EFM32 Gecko Software Documentation  efm32g-doc-5.1.2
SLEEP

Detailed Description

Energy Modes management driver.

Energy Modes Management Driver.

This is a energy modes management module consisting of sleep.c and sleep.h source files. The main purpose of the module is to ease energy optimization with a simple API. The module allows the system to always sleep in the lowest possible energy mode. Users could set up callbacks that are being called before and after each and every sleep. A counting semaphore is available for each low energy mode (EM2/EM3) to protect certain system states from being corrupted. This semaphore has limit set to maximum 255 locks.

Macros

#define SLEEP_EM4_WAKEUP_CALLBACK_ENABLED   true
 
#define SLEEP_HW_LOW_ENERGY_BLOCK_ENABLED   true
 
#define SLEEP_LOWEST_ENERGY_MODE_DEFAULT   sleepEM3
 

Typedefs

typedef void(* SLEEP_CbFuncPtr_t) (SLEEP_EnergyMode_t)
 

Enumerations

enum  SLEEP_EnergyMode_t {
  sleepEM0 = 0,
  sleepEM1 = 1,
  sleepEM2 = 2,
  sleepEM3 = 3,
  sleepEM4 = 4
}
 

Functions

void SLEEP_ForceSleepInEM4 (void)
 Force the device to go to EM4 without doing any checks. More...
 
void SLEEP_Init (SLEEP_CbFuncPtr_t pSleepCb, SLEEP_CbFuncPtr_t pWakeUpCb)
 Initialize the Sleep module. More...
 
SLEEP_EnergyMode_t SLEEP_LowestEnergyModeGet (void)
 Gets the lowest energy mode that the system is allowed to be set to. More...
 
SLEEP_EnergyMode_t SLEEP_Sleep (void)
 Sets the system to sleep into the lowest possible energy mode. More...
 
void SLEEP_SleepBlockBegin (SLEEP_EnergyMode_t eMode)
 Begin sleep block in the requested energy mode. More...
 
void SLEEP_SleepBlockEnd (SLEEP_EnergyMode_t eMode)
 End sleep block in the requested energy mode. More...
 

Macro Definition Documentation

#define SLEEP_EM4_WAKEUP_CALLBACK_ENABLED   true

Enable/disable calling wakeup callback after EM4 reset.

Definition at line 80 of file sleep.h.

#define SLEEP_HW_LOW_ENERGY_BLOCK_ENABLED   true

Enable/disable the HW block for protecting accidental setting of low energy modes (recommended to be set to true).

Definition at line 75 of file sleep.h.

#define SLEEP_LOWEST_ENERGY_MODE_DEFAULT   sleepEM3

Configure default lowest energy mode that the system can be set to. Possible values:

  • sleepEM2 - EM2, CPU core is turned off, all HF clocks are turned off, LF clocks are on.
  • sleepEM3 - EM3, like EM2 + LF clocks are off, RAM retention, GPIO and ACMP interrupt is on.

Definition at line 90 of file sleep.h.

Referenced by SLEEP_LowestEnergyModeGet().

Typedef Documentation

typedef void(* SLEEP_CbFuncPtr_t) (SLEEP_EnergyMode_t)

Callback function pointer type.

Definition at line 117 of file sleep.h.

Enumeration Type Documentation

Status value used for showing the Energy Mode the device is currently in.

Enumerator
sleepEM0 

Status value for EM0.

sleepEM1 

Status value for EM1.

sleepEM2 

Status value for EM2.

sleepEM3 

Status value for EM3.

sleepEM4 

Status value for EM4.

Definition at line 98 of file sleep.h.

Function Documentation

void SLEEP_ForceSleepInEM4 ( void  )

Force the device to go to EM4 without doing any checks.

This function unblocks the low energy sleep block then goes to EM4.

Note
Regular RAM is not retained in EM4 and the wake up causes a reset. If the configuration option SLEEP_EM4_WAKEUP_CALLBACK_ENABLED is set to true, the SLEEP_Init() function checks for the reset cause and calls the EM4 wakeup callback.

Definition at line 214 of file sleep.c.

References EMU_EM2UnBlock(), and sleepEM4.

void SLEEP_Init ( SLEEP_CbFuncPtr_t  pSleepCb,
SLEEP_CbFuncPtr_t  pWakeUpCb 
)

Initialize the Sleep module.

Use this function to initialize the Sleep module, should be called only once! Pointers to sleep and wake-up callback functions shall be provided when calling this function. If SLEEP_EM4_WAKEUP_CALLBACK_ENABLED is set to true, this function checks for the cause of the reset that implicitly called it and calls the wakeup callback if the reset was a wakeup from EM4 (does not work on Gecko MCU).

Parameters
[in]pSleepCbPointer to the callback function that is being called before the device is going to sleep.
[in]pWakeUpCbPointer to the callback function that is being called after wake up.

Definition at line 131 of file sleep.c.

References RMU_ResetCauseClear(), RMU_ResetCauseGet(), and sleepEM4.

SLEEP_EnergyMode_t SLEEP_LowestEnergyModeGet ( void  )

Gets the lowest energy mode that the system is allowed to be set to.

This function uses the low energy mode block counters to determine the lowest possible that the system is allowed to be set to.

Returns
Lowest energy mode that the system can be set to. Possible values:
  • sleepEM2
  • sleepEM3
This function uses the low energy mode block counters to determine the lowest possible that the system is allowed to be set to.
Lowest energy mode that the system can be set to. Possible values:
  • sleepEM1
  • sleepEM2
  • sleepEM3

Definition at line 331 of file sleep.c.

References SLEEP_LOWEST_ENERGY_MODE_DEFAULT, sleepEM1, sleepEM2, and sleepEM3.

Referenced by SLEEP_Sleep().

SLEEP_EnergyMode_t SLEEP_Sleep ( void  )

Sets the system to sleep into the lowest possible energy mode.

This function takes care of the system states protected by the sleep block provided by SLEEP_SleepBlockBegin() / SLEEP_SleepBlockEnd(). It allows the system to go into the lowest possible energy mode that the device can be set into at the time of the call of this function. This function will not go lower than EM3 because leaving EM4 requires resetting MCU. To enter into EM4 call SLEEP_ForceSleepInEM4().

Returns
Energy Mode that was entered. Possible values:
  • sleepEM2
  • sleepEM3
This function takes care of the system states protected by the sleep block provided by SLEEP_SleepBlockBegin() / SLEEP_SleepBlockEnd(). It allows the system to go into the lowest possible energy mode that the device can be set into at the time of the call of this function. This function will not go lower than EM3 because leaving EM4 requires resetting MCU. To enter into EM4 call SLEEP_ForceSleepInEM4().
Energy Mode that was entered. Possible values:
  • sleepEM1
  • sleepEM2
  • sleepEM3

Definition at line 179 of file sleep.c.

References CORE_DECLARE_IRQ_STATE, CORE_ENTER_CRITICAL, CORE_EXIT_CRITICAL, SLEEP_LowestEnergyModeGet(), sleepEM0, sleepEM1, and sleepEM3.

void SLEEP_SleepBlockBegin ( SLEEP_EnergyMode_t  eMode)

Begin sleep block in the requested energy mode.

Blocking a critical system state from a certain energy mode makes sure that the system is not set to that energy mode while the block is not being released. Every SLEEP_SleepBlockBegin() increases the corresponding counter and every SLEEP_SleepBlockEnd() decreases it.

Example:

1 SLEEP_SleepBlockBegin(sleepEM2); // do not allow EM2 or higher
2 // do some stuff that requires EM1 at least, like ADC sampling
3 SLEEP_SleepBlockEnd(sleepEM2); // remove restriction for EM2
Note
Be aware that there is a limit of maximum block nesting set to 255.
Parameters
[in]eModeEnergy mode to begin to block. Possible values:
  • sleepEM2 - Begin to block the system from being set to EM2/EM3/EM4.
  • sleepEM3 - Begin to block the system from being set to EM3/EM4.
Blocking a critical system state from a certain energy mode makes sure that the system is not set to that energy mode while the block is not being released. Every SLEEP_SleepBlockBegin() increases the corresponding counter and every SLEEP_SleepBlockEnd() decreases it.

Example:

1 SLEEP_SleepBlockBegin(sleepEM2); // do not allow EM2 or higher
2 // do some stuff that requires EM1 at least, like ADC sampling
3 SLEEP_SleepBlockEnd(sleepEM2); // remove restriction for EM2
Note
Be aware that there is limit of maximum blocks nesting to 255.
Parameters
[in]eModeEnergy mode to begin to block. Possible values:
  • sleepEM2 - Begin to block the system from being set to EM2/EM3/EM4.
  • sleepEM3 - Begin to block the system from being set to EM3/EM4.

Definition at line 249 of file sleep.c.

References EMU_EM2Block(), sleepEM2, sleepEM3, and sleepEM4.

Referenced by RTCDRV_Init(), and RTCDRV_StartTimer().

void SLEEP_SleepBlockEnd ( SLEEP_EnergyMode_t  eMode)

End sleep block in the requested energy mode.

Release restriction for entering certain energy mode. Every call of this function reduce the blocking counter by 1. Once the counter for specific energy mode is 0 and all counters for lower energy modes are 0 as well, using a particular energy mode is allowed. Every SLEEP_SleepBlockBegin() increases the corresponding counter and every SLEEP_SleepBlockEnd() decreases it.

Example:

1 // at start all energy modes are allowed
2 SLEEP_SleepBlockBegin(sleepEM3); // EM3 and EM4 are blocked
3 SLEEP_SleepBlockBegin(sleepEM2); // EM2, EM3 and EM4 are blocked
4 SLEEP_SleepBlockBegin(sleepEM2); // EM2, EM3 and EM4 are blocked
5 SLEEP_SleepBlockEnd(sleepEM3); // EM2, EM3 and EM4 are still blocked
6 SLEEP_SleepBlockEnd(sleepEM2); // EM2, EM3 and EM4 are still blocked
7 SLEEP_SleepBlockEnd(sleepEM2); // all energy modes are allowed now
Parameters
[in]eModeEnergy mode to end to block. Possible values:
  • sleepEM2 - End to block the system from being set to EM2/EM3/EM4.
  • sleepEM3 - End to block the system from being set to EM3/EM4.
Release restriction for entering certain energy mode. Every call of this function reduce blocking counter by 1. Once the counter for specific energy mode is 0 and all counters for lower energy modes are 0 as well, using particular energy mode is allowed. Every SLEEP_SleepBlockBegin() increases the corresponding counter and every SLEEP_SleepBlockEnd() decreases it.

Example:

1 // at start all energy modes are allowed
2 SLEEP_SleepBlockBegin(sleepEM3); // EM3 and EM4 are blocked
3 SLEEP_SleepBlockBegin(sleepEM2); // EM2, EM3 and EM4 are blocked
4 SLEEP_SleepBlockBegin(sleepEM2); // EM2, EM3 and EM4 are blocked
5 SLEEP_SleepBlockEnd(sleepEM3); // EM2, EM3 and EM4 are still blocked
6 SLEEP_SleepBlockEnd(sleepEM2); // EM2, EM3 and EM4 are still blocked
7 SLEEP_SleepBlockEnd(sleepEM2); // all energy modes are allowed now
Parameters
[in]eModeEnergy mode to end to block. Possible values:
  • sleepEM2 - End to block the system from being set to EM2/EM3/EM4.
  • sleepEM3 - End to block the system from being set to EM3/EM4.

Definition at line 295 of file sleep.c.

References EMU_EM2UnBlock(), sleepEM2, sleepEM3, and sleepEM4.

Referenced by RTCDRV_DeInit().