SLEEP

Detailed Description

Sleep Management Driver.


Introduction

This is a sleep management module consisting of sleep.c and sleep.h source files. The main purpose of the module is to make it easy for an application to always enter the lowest possible energy mode using a simple API.

The module works by providing an API for defining "sleep blocks" in the application code. A "sleep block" will block the MCU from entering a certain energy mode. A "sleep block" can for instance block EM2 entry because an EM1 only peripheral is in use. These "sleep blocks" are created by the calls to SLEEP_SleepBlockBegin() and end with SLEEP_SleepBlockEnd() .

When an application wants to enter a low energy mode it can call SLEEP_Sleep() to enter the lowest possible energy mode. This module will use the "sleep blocks" to figure out the lowest possible energy mode.

Here is an example of how the sleep driver is initialized and how it can be used to enter EM2.


Sleep and Wakeup Events/Callbacks

This module also provides a way to add application callbacks to notify the application that the MCU is entering sleep, or waking up from sleep. These callbacks can be provided to the driver when calling SLEEP_InitEx() .

The sleepCallback function is called before entering sleep and the wakeupCallback is called after waking up from sleep. The sleepCallback function has a bool return value. This return value can be used to control if the MCU should really go to sleep or not. Returning true will make the MCU enter the selected energy mode, while returning false will force the sleep driver to return without entering a sleep.

Here is an example of how the sleep and wakeup callbacks are used.

static bool beforeSleep( SLEEP_EnergyMode_t mode)
{
printf( "sleep\n" );
return true ;
}
static void afterWakeup( SLEEP_EnergyMode_t mode)
{
printf( "wakeup\n" );
(void) mode;
}
void main( void )
{
SLEEP_Init_t sleepConfig = {
. sleepCallback = beforeSleep,
.wakeupCallback = afterWakeup
};
SLEEP_InitEx (&sleepConfig);
SLEEP_SleepBlockBegin ( sleepEM3 ); // Block EM3 entry
while ( true ) {
}
}

Data Structures

struct SLEEP_Init_t

Macros

#define SLEEP_EM4_WAKEUP_CALLBACK_ENABLED true
#define SLEEP_FLAG_NO_CLOCK_RESTORE 0x1u
#define SLEEP_FLAG_NONE 0x0
#define SLEEP_HW_LOW_ENERGY_BLOCK_ENABLED false
#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.
void SLEEP_Init ( SLEEP_CbFuncPtr_t pSleepCb, SLEEP_CbFuncPtr_t pWakeUpCb)
Initialize the Sleep module.
void SLEEP_InitEx (const SLEEP_Init_t *init)
Initialize the Sleep module.
SLEEP_EnergyMode_t SLEEP_LowestEnergyModeGet (void)
Gets the lowest energy mode that the system is allowed to be set to.
SLEEP_EnergyMode_t SLEEP_Sleep (void)
Sets the system to sleep into the lowest possible energy mode.
void SLEEP_SleepBlockBegin ( SLEEP_EnergyMode_t eMode)
Begin sleep block in the requested energy mode.
void SLEEP_SleepBlockEnd ( SLEEP_EnergyMode_t eMode)
End sleep block in the requested energy mode.

Macro Definition Documentation

#define SLEEP_EM4_WAKEUP_CALLBACK_ENABLED   true

Enable/disable calling wakeup callback after EM4 reset.

Definition at line 156 of file sleep.h .

#define SLEEP_FLAG_NO_CLOCK_RESTORE   0x1u

This flag can be returned from the restoreCallback function in order to signal to the sleep driver that HF clocks should not be restored and that the sleep driver should go right back to sleep again.

Definition at line 140 of file sleep.h .

Referenced by SLEEP_Sleep() .

#define SLEEP_FLAG_NONE   0x0

This flag can be returned from the restoreCallback function in order to signal that the sleep driver should continue as normal.

Definition at line 134 of file sleep.h .

#define SLEEP_HW_LOW_ENERGY_BLOCK_ENABLED   false

Enable/disable the HW block for protecting accidental setting of low energy modes. Note that some devices do not support blocking energy mode entry in hardware, on these devices SLEEP_HW_LOW_ENERGY_BLOCK_ENABLED will have no effect.

Definition at line 151 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 166 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 192 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 174 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 253 of file sleep.c .

References 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] pSleepCb Pointer to the callback function that is being called before the device is going to sleep.
[in] pWakeUpCb Pointer to the callback function that is being called after wake up.
Deprecated:
New code should use the SLEEP_InitEx function for initializing the sleep module.

Definition at line 144 of file sleep.c .

References RMU_ResetCauseClear() , RMU_ResetCauseGet() , SLEEP_Init_t::sleepCallback , sleepEM4 , and SLEEP_Init_t::wakeupCallback .

Referenced by SLEEP_InitEx() .

void SLEEP_InitEx ( const SLEEP_Init_t * init )

Initialize the Sleep module.

Use this function to initialize the Sleep module.

Parameters
[in] init Pointer to the sleep module init structure containing callback function and configuration parameters.

Definition at line 179 of file sleep.c .

References SLEEP_Init_t::restoreCallback , SLEEP_Init() , SLEEP_Init_t::sleepCallback , and SLEEP_Init_t::wakeupCallback .

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:
  • sleepEM1
  • sleepEM2
  • sleepEM3

Definition at line 365 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:
  • sleepEM0
  • sleepEM1
  • sleepEM2
  • sleepEM3

Definition at line 206 of file sleep.c .

References CORE_DECLARE_IRQ_STATE , CORE_ENTER_CRITICAL , CORE_EXIT_CRITICAL , EMU_Restore() , EMU_Save() , SLEEP_Init_t::restoreCallback , SLEEP_FLAG_NO_CLOCK_RESTORE , SLEEP_LowestEnergyModeGet() , sleepEM0 , sleepEM1 , sleepEM2 , 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 limit of maximum blocks nesting to 255.
Parameters
[in] eMode Energy 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 288 of file sleep.c .

References 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 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] eMode Energy 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 332 of file sleep.c .

References sleepEM2 , sleepEM3 , and sleepEM4 .

Referenced by RTCDRV_DeInit() .