25
25
#define MBED_POWER_MGMT_H
26
26
27
27
#include "hal/sleep_api.h"
28
- #include "mbed_toolchain.h"
29
28
#include "hal/ticker_api.h"
29
+ #include "platform/mbed_toolchain.h"
30
+ #include "platform/mbed_critical.h"
30
31
#include <stdbool.h>
31
32
32
33
#ifdef __cplusplus
@@ -170,6 +171,33 @@ bool sleep_manager_can_deep_sleep_test_check(void);
170
171
*/
171
172
void sleep_manager_sleep_auto (void );
172
173
174
+ /** Applications may implement this to hook any calls to sleep or deepsleep
175
+ *
176
+ * This hook gives the application a chance to run additional code when the
177
+ * system would normally waste cycles waiting for an external event.
178
+ *
179
+ * @note This function is explicitly _not_ called when we are in an interrupt
180
+ * or critical section, as this would violate the expectations of both critical
181
+ * sections and sleep hooks. In normal usage interrupts should not be calling
182
+ * sleep.
183
+ *
184
+ * Example blinks an LED when the application sleeps:
185
+ * @code
186
+ * DigitalOut led1(LED1);
187
+ *
188
+ * void mbed_override_sleep_hook(void) {
189
+ * led1 = !led1;
190
+ * }
191
+ *
192
+ * int main() {
193
+ * while (true) {
194
+ * ThisThread::sleep_for(1000);
195
+ * }
196
+ * }
197
+ * @endcode
198
+ */
199
+ void mbed_override_sleep_hook (void );
200
+
173
201
/** Send the microcontroller to sleep
174
202
*
175
203
* @note This function can be a noop if not implemented by the platform.
@@ -193,6 +221,9 @@ void sleep_manager_sleep_auto(void);
193
221
*/
194
222
static inline void sleep (void )
195
223
{
224
+ if (core_util_are_interrupts_enabled ()) {
225
+ mbed_override_sleep_hook ();
226
+ }
196
227
#if DEVICE_SLEEP
197
228
#if (MBED_CONF_RTOS_PRESENT == 0 ) || (DEVICE_SYSTICK_CLK_OFF_DURING_SLEEP == 0 ) || defined(MBED_TICKLESS )
198
229
sleep_manager_sleep_auto ();
@@ -223,6 +254,9 @@ static inline void sleep(void)
223
254
MBED_DEPRECATED_SINCE ("mbed-os-5.6" , "One entry point for an application, use sleep()" )
224
255
static inline void deepsleep (void )
225
256
{
257
+ if (core_util_are_interrupts_enabled ()) {
258
+ mbed_override_sleep_hook ();
259
+ }
226
260
#if DEVICE_SLEEP
227
261
sleep_manager_sleep_auto ();
228
262
#endif /* DEVICE_SLEEP */
0 commit comments