1 /***************************************************************************//**
\r
3 * @brief Energy Modes management driver
\r
6 * This is a energy modes management module consisting of sleep.c and sleep.h
\r
7 * source files. The main purpose of the module is to ease energy
\r
8 * optimization with a simple API. The module allows the system to always sleep
\r
9 * in the lowest possible energy mode. Users could set up callbacks that are
\r
10 * being called before and after each and every sleep. A counting semaphore is
\r
11 * available for each low energy mode (EM1/EM2/EM3) to protect certain system
\r
12 * states from being corrupted. This semaphore has limit set to maximum 255 locks.
\r
14 * The module provides the following public API to the users:
\r
17 * SLEEP_SleepBlockBegin()
\r
18 * SLEEP_SleepBlockEnd()
\r
19 * SLEEP_ForceSleepInEM4()
\r
21 *******************************************************************************
\r
23 * <b>(C) Copyright 2014 Silicon Labs, http://www.silabs.com</b>
\r
24 *******************************************************************************
\r
26 * This file is licensed under the Silabs License Agreement. See the file
\r
27 * "Silabs_License_Agreement.txt" for details. Before using this software for
\r
28 * any purpose, you must agree to the terms of that agreement.
\r
30 ******************************************************************************/
\r
36 #include <stdbool.h>
\r
38 /* Device specific header file(s). */
\r
39 #include "em_device.h"
\r
45 /***************************************************************************//**
\r
46 * @addtogroup EM_Drivers
\r
48 ******************************************************************************/
\r
50 /***************************************************************************//**
\r
52 * @brief Energy Modes management driver.
\r
54 * This is a energy modes management module consisting of sleep.c and sleep.h
\r
55 * source files. The main purpose of the module is to ease energy
\r
56 * optimization with a simple API. The module allows the system to always sleep
\r
57 * in the lowest possible energy mode. Users could set up callbacks that are
\r
58 * being called before and after each and every sleep. A counting semaphore is
\r
59 * available for each low energy mode (EM1/EM2/EM3) to protect certain system
\r
60 * states from being corrupted. This semaphore has limit set to maximum 255 locks.
\r
62 ******************************************************************************/
\r
64 /*******************************************************************************
\r
65 ******************************* MACROS ************************************
\r
66 ******************************************************************************/
\r
69 /*******************************************************************************
\r
70 **************************** CONFIGURATION ********************************
\r
71 ******************************************************************************/
\r
73 /** Enable/disable the HW block for protecting accidental setting of low energy
\r
74 * modes (recommended to be set to true). */
\r
75 #ifndef SLEEP_HW_LOW_ENERGY_BLOCK_ENABLED
\r
76 #define SLEEP_HW_LOW_ENERGY_BLOCK_ENABLED true
\r
79 /** Enable/disable calling wakeup callback after EM4 reset. */
\r
80 #ifndef SLEEP_EM4_WAKEUP_CALLBACK_ENABLED
\r
81 #define SLEEP_EM4_WAKEUP_CALLBACK_ENABLED true
\r
84 /** Configure default lowest energy mode that the system can be set to.
\r
86 * @li sleepEM1 - EM1, the CPU core is turned off.
\r
87 * @li sleepEM2 - EM2, like EM1 + all HF clocks are turned off, LF clocks are on.
\r
88 * @li sleepEM3 - EM3, like EM2 + LF clocks are off, RAM retention, GPIO and ACMP
\r
89 * interrupt is on. */
\r
90 #ifndef SLEEP_LOWEST_ENERGY_MODE_DEFAULT
\r
91 #define SLEEP_LOWEST_ENERGY_MODE_DEFAULT sleepEM3
\r
94 /*******************************************************************************
\r
95 ****************************** TYPEDEFS ***********************************
\r
96 ******************************************************************************/
\r
98 /** Status value used for showing the Energy Mode the device is currently in. */
\r
101 /** Status value for EM0. */
\r
104 /** Status value for EM1. */
\r
107 /** Status value for EM2. */
\r
110 /** Status value for EM3. */
\r
113 /** Status value for EM4. */
\r
115 } SLEEP_EnergyMode_t;
\r
117 /** Callback function pointer type. */
\r
118 typedef void (*SLEEP_CbFuncPtr_t)(SLEEP_EnergyMode_t);
\r
121 /*******************************************************************************
\r
122 ****************************** PROTOTYPES *********************************
\r
123 ******************************************************************************/
\r
125 /***************************************************************************//**
\r
127 * Initialize the Sleep module.
\r
130 * Use this function to initialize the Sleep module, should be called
\r
131 * only once! Pointers to sleep and wake-up callback functions shall be
\r
132 * provided when calling this function.
\r
133 * If SLEEP_EM4_WAKEUP_CALLBACK_ENABLED is set to true, this function checks
\r
134 * for the cause of the reset that implicitly called it and calls the wakeup
\r
135 * callback if the reset was a wakeup from EM4 (does not work on Gecko MCU).
\r
137 * @param[in] pSleepCb
\r
138 * Pointer to the callback function that is being called before the device is
\r
141 * @param[in] pWakeUpCb
\r
142 * Pointer to the callback function that is being called after wake up.
\r
143 ******************************************************************************/
\r
144 void SLEEP_Init(SLEEP_CbFuncPtr_t pSleepCb, SLEEP_CbFuncPtr_t pWakeUpCb);
\r
146 /***************************************************************************//**
\r
148 * Gets the lowest energy mode that the system is allowed to be set to.
\r
151 * This function uses the low energy mode block counters to determine the
\r
152 * lowest possible that the system is allowed to be set to.
\r
155 * Lowest energy mode that the system can be set to. Possible values:
\r
160 ******************************************************************************/
\r
161 SLEEP_EnergyMode_t SLEEP_LowestEnergyModeGet(void);
\r
163 /***************************************************************************//**
\r
165 * Sets the system to sleep into the lowest possible energy mode.
\r
168 * This function takes care of the system states protected by the sleep block
\r
169 * provided by SLEEP_SleepBlockBegin() / SLEEP_SleepBlockEnd(). It allows
\r
170 * the system to go into the lowest possible energy mode that the device can
\r
171 * be set into at the time of the call of this function.
\r
172 * This function will not go lower than EM3 because leaving EM4 requires
\r
173 * resetting MCU. To enter into EM4 call SLEEP_ForceSleepInEM4().
\r
176 * Energy Mode that was entered. Possible values:
\r
181 ******************************************************************************/
\r
182 SLEEP_EnergyMode_t SLEEP_Sleep(void);
\r
185 /***************************************************************************//**
\r
187 * Force the device to go to EM4 without doing any checks.
\r
190 * This function unblocks the low energy sleep block then goes to EM4.
\r
193 * Regular RAM is not retained in EM4 and the wake up causes a reset.
\r
194 * If the configuration option SLEEP_EM4_WAKEUP_CALLBACK_ENABLED is set to
\r
195 * true, the SLEEP_Init() function checks for the reset cause and calls the
\r
196 * EM4 wakeup callback.
\r
197 ******************************************************************************/
\r
198 void SLEEP_ForceSleepInEM4(void);
\r
201 /***************************************************************************//**
\r
203 * Begin sleep block in the requested energy mode.
\r
206 * Blocking a critical system state from a certain energy mode makes sure that
\r
207 * the system is not set to that energy mode while the block is not being
\r
209 * Every SLEEP_SleepBlockBegin() increases the corresponding counter and
\r
210 * every SLEEP_SleepBlockEnd() decreases it.
\r
213 * SLEEP_SleepBlockBegin(sleepEM2); // do not allow EM2 or higher
\r
214 * // do some stuff that requires EM1 at least, like ADC sampling
\r
215 * SLEEP_SleepBlockEnd(sleepEM2); // remove restriction for EM2\endcode
\r
218 * Be aware that there is limit of maximum blocks nesting to 255.
\r
221 * Energy mode to begin to block. Possible values:
\r
222 * @li sleepEM1 - Begin to block the system from being set to EM1 (and EM2..4).
\r
223 * @li sleepEM2 - Begin to block the system from being set to EM2 (and EM3/EM4).
\r
224 * @li sleepEM3 - Begin to block the system from being set to EM3 (and EM4).
\r
225 ******************************************************************************/
\r
226 void SLEEP_SleepBlockBegin(SLEEP_EnergyMode_t eMode);
\r
229 /***************************************************************************//**
\r
231 * End sleep block in the requested energy mode.
\r
234 * Release restriction for entering certain energy mode. Every call of this
\r
235 * function reduce blocking counter by 1. Once the counter for specific energy
\r
236 * mode is 0 and all counters for lower energy modes are 0 as well, using
\r
237 * particular energy mode is allowed.
\r
238 * Every SLEEP_SleepBlockBegin() increases the corresponding counter and
\r
239 * every SLEEP_SleepBlockEnd() decreases it.
\r
242 * // at start all energy modes are allowed
\r
243 * SLEEP_SleepBlockBegin(sleepEM2); // EM2, EM3, EM4 are blocked
\r
244 * SLEEP_SleepBlockBegin(sleepEM1); // EM1, EM2, EM3, EM4 are blocked
\r
245 * SLEEP_SleepBlockBegin(sleepEM1); // EM1, EM2, EM3, EM4 are blocked
\r
246 * SLEEP_SleepBlockEnd(sleepEM2); // still EM1, EM2, EM3, EM4 are blocked
\r
247 * SLEEP_SleepBlockEnd(sleepEM1); // still EM1, EM2, EM3, EM4 are blocked
\r
248 * SLEEP_SleepBlockEnd(sleepEM1); // all energy modes are allowed now\endcode
\r
251 * Energy mode to end to block. Possible values:
\r
252 * @li sleepEM1 - End to block the system from being set to EM1 (and EM2..4).
\r
253 * @li sleepEM2 - End to block the system from being set to EM2 (and EM3/EM4).
\r
254 * @li sleepEM3 - End to block the system from being set to EM3 (and EM4).
\r
255 ******************************************************************************/
\r
256 void SLEEP_SleepBlockEnd(SLEEP_EnergyMode_t eMode);
\r
259 /** @} (end addtogroup SLEEP) */
\r
260 /** @} (end addtogroup EM_Drivers) */
\r
265 #endif /* __SLEEP_H */
\r