20
20
#define MBED_SLEEP_H
21
21
22
22
#include "sleep_api.h"
23
+ #include "mbed_toolchain.h"
24
+ #include <stdbool.h>
23
25
24
26
#ifdef __cplusplus
25
27
extern "C" {
26
28
#endif
27
29
30
+ /** Sleep manager API
31
+ * The sleep manager provides API to automatically select sleep mode.
32
+ *
33
+ * There are two sleep modes:
34
+ * - sleep
35
+ * - deepsleep
36
+ *
37
+ * Use locking/unlocking deepsleep for drivers that depend on features that
38
+ * are not allowed (=disabled) during the deepsleep. For instance, high frequency
39
+ * clocks.
40
+ *
41
+ * Example:
42
+ * @code
43
+ *
44
+ * void driver::handler()
45
+ * {
46
+ * if (_sensor.get_event()) {
47
+ * // any event - we are finished, unlock the deepsleep
48
+ * sleep_manager_unlock_deep_sleep();
49
+ * _callback();
50
+ * }
51
+ * }
52
+ *
53
+ * int driver::measure(event_t event, callback_t& callback)
54
+ * {
55
+ * _callback = callback;
56
+ * sleep_manager_lock_deep_sleep();
57
+ * // start async transaction, we are waiting for an event
58
+ * return _sensor.start(event, callback);
59
+ * }
60
+ * @endcode
61
+ */
62
+
63
+ /** Lock the deep sleep mode
64
+ *
65
+ * This locks the automatic deep mode selection.
66
+ * sleep_manager_sleep_auto() will ignore deepsleep mode if
67
+ * this function is invoked at least once (the internal counter is non-zero)
68
+ *
69
+ * Use this locking mechanism for interrupt driven API that are
70
+ * running in the background and deepsleep could affect their functionality
71
+ *
72
+ * The lock is a counter, can be locked up to USHRT_MAX
73
+ * This function is IRQ and thread safe
74
+ */
75
+ void sleep_manager_lock_deep_sleep (void );
76
+
77
+ /** Unlock the deep sleep mode
78
+ *
79
+ * Use unlocking in pair with sleep_manager_lock_deep_sleep().
80
+ *
81
+ * The lock is a counter, should be equally unlocked as locked
82
+ * This function is IRQ and thread safe
83
+ */
84
+ void sleep_manager_unlock_deep_sleep (void );
85
+
86
+ /** Get the status of deep sleep allowance for a target
87
+ *
88
+ * @return true if a target can go to deepsleep, false otherwise
89
+ */
90
+ bool sleep_manager_can_deep_sleep (void );
91
+
92
+ /** Enter auto selected sleep mode. It chooses the sleep or deeepsleep modes based
93
+ * on the deepsleep locking counter
94
+ *
95
+ * This function is IRQ and thread safe
96
+ *
97
+ * @note
98
+ * If MBED_DEBUG is defined, only hal_sleep is allowed. This ensures the debugger
99
+ * to be active for debug modes.
100
+ *
101
+ */
102
+ void sleep_manager_sleep_auto (void );
103
+
28
104
/** Send the microcontroller to sleep
29
105
*
30
106
* @note This function can be a noop if not implemented by the platform.
@@ -46,11 +122,9 @@ extern "C" {
46
122
__INLINE static void sleep (void )
47
123
{
48
124
#if !(defined(FEATURE_UVISOR ) && defined(TARGET_UVISOR_SUPPORTED ))
49
- #ifndef MBED_DEBUG
50
125
#if DEVICE_SLEEP
51
- hal_sleep ();
126
+ sleep_manager_sleep_auto ();
52
127
#endif /* DEVICE_SLEEP */
53
- #endif /* MBED_DEBUG */
54
128
#endif /* !(defined(FEATURE_UVISOR) && defined(TARGET_UVISOR_SUPPORTED)) */
55
129
}
56
130
@@ -60,7 +134,7 @@ __INLINE static void sleep(void)
60
134
* @note This function will be a noop in debug mode (debug build profile when MBED_DEBUG is defined)
61
135
* @note This function will be a noop while uVisor is in use.
62
136
*
63
- * This processor is setup ready for deep sleep, and sent to sleep using __WFI() . This mode
137
+ * This processor is setup ready for deep sleep, and sent to sleep. This mode
64
138
* has the same sleep features as sleep plus it powers down peripherals and clocks. All state
65
139
* is still maintained.
66
140
*
@@ -71,14 +145,14 @@ __INLINE static void sleep(void)
71
145
* Flash re-programming and the USB serial port will remain active, but the mbed program will no longer be
72
146
* able to access the LocalFileSystem
73
147
*/
148
+
149
+ MBED_DEPRECATED_SINCE ("mbed-os-5.6" , "One entry point for an application, use sleep()" )
74
150
__INLINE static void deepsleep (void )
75
151
{
76
152
#if !(defined(FEATURE_UVISOR ) && defined(TARGET_UVISOR_SUPPORTED ))
77
- #ifndef MBED_DEBUG
78
153
#if DEVICE_SLEEP
79
- hal_deepsleep ();
154
+ sleep_manager_sleep_auto ();
80
155
#endif /* DEVICE_SLEEP */
81
- #endif /* MBED_DEBUG */
82
156
#endif /* !(defined(FEATURE_UVISOR) && defined(TARGET_UVISOR_SUPPORTED)) */
83
157
}
84
158
0 commit comments