[374] | 1 | /** \addtogroup platform */
|
---|
| 2 | /** @{*/
|
---|
| 3 | /**
|
---|
| 4 | * \defgroup platform_power_mgmt Power management functions
|
---|
| 5 | * @{
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | /* mbed Microcontroller Library
|
---|
| 9 | * Copyright (c) 2006-2018 ARM Limited
|
---|
| 10 | *
|
---|
| 11 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
| 12 | * you may not use this file except in compliance with the License.
|
---|
| 13 | * You may obtain a copy of the License at
|
---|
| 14 | *
|
---|
| 15 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
| 16 | *
|
---|
| 17 | * Unless required by applicable law or agreed to in writing, software
|
---|
| 18 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
| 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
| 20 | * See the License for the specific language governing permissions and
|
---|
| 21 | * limitations under the License.
|
---|
| 22 | */
|
---|
| 23 | #ifndef MBED_POWER_MGMT_H
|
---|
| 24 | #define MBED_POWER_MGMT_H
|
---|
| 25 |
|
---|
| 26 | #include "hal/sleep_api.h"
|
---|
| 27 | #include "platform/mbed_toolchain.h"
|
---|
| 28 | #include "hal/ticker_api.h"
|
---|
| 29 | #include <stdbool.h>
|
---|
| 30 |
|
---|
| 31 | #ifdef __cplusplus
|
---|
| 32 | extern "C" {
|
---|
| 33 | #endif
|
---|
| 34 |
|
---|
| 35 | /** Sleep manager API
|
---|
| 36 | * The sleep manager provides API to automatically select sleep mode.
|
---|
| 37 | *
|
---|
| 38 | * There are two sleep modes:
|
---|
| 39 | * - sleep
|
---|
| 40 | * - deepsleep
|
---|
| 41 | *
|
---|
| 42 | * Use locking/unlocking deepsleep for drivers that depend on features that
|
---|
| 43 | * are not allowed (=disabled) during the deepsleep. For instance, high frequency
|
---|
| 44 | * clocks.
|
---|
| 45 | *
|
---|
| 46 | * Example:
|
---|
| 47 | * @code
|
---|
| 48 | *
|
---|
| 49 | * void driver::handler()
|
---|
| 50 | * {
|
---|
| 51 | * if (_sensor.get_event()) {
|
---|
| 52 | * // any event - we are finished, unlock the deepsleep
|
---|
| 53 | * sleep_manager_unlock_deep_sleep();
|
---|
| 54 | * _callback();
|
---|
| 55 | * }
|
---|
| 56 | * }
|
---|
| 57 | *
|
---|
| 58 | * int driver::measure(event_t event, callback_t& callback)
|
---|
| 59 | * {
|
---|
| 60 | * _callback = callback;
|
---|
| 61 | * sleep_manager_lock_deep_sleep();
|
---|
| 62 | * // start async transaction, we are waiting for an event
|
---|
| 63 | * return _sensor.start(event, callback);
|
---|
| 64 | * }
|
---|
| 65 | * @endcode
|
---|
| 66 | */
|
---|
| 67 | #ifdef MBED_SLEEP_TRACING_ENABLED
|
---|
| 68 |
|
---|
| 69 | void sleep_tracker_lock(const char *const filename, int line);
|
---|
| 70 | void sleep_tracker_unlock(const char *const filename, int line);
|
---|
| 71 |
|
---|
| 72 | #define sleep_manager_lock_deep_sleep() \
|
---|
| 73 | do \
|
---|
| 74 | { \
|
---|
| 75 | sleep_manager_lock_deep_sleep_internal(); \
|
---|
| 76 | sleep_tracker_lock(MBED_FILENAME, __LINE__); \
|
---|
| 77 | } while (0);
|
---|
| 78 |
|
---|
| 79 | #define sleep_manager_unlock_deep_sleep() \
|
---|
| 80 | do \
|
---|
| 81 | { \
|
---|
| 82 | sleep_manager_unlock_deep_sleep_internal(); \
|
---|
| 83 | sleep_tracker_unlock(MBED_FILENAME, __LINE__); \
|
---|
| 84 | } while (0);
|
---|
| 85 |
|
---|
| 86 | #else
|
---|
| 87 |
|
---|
| 88 | #define sleep_manager_lock_deep_sleep() \
|
---|
| 89 | sleep_manager_lock_deep_sleep_internal()
|
---|
| 90 |
|
---|
| 91 | #define sleep_manager_unlock_deep_sleep() \
|
---|
| 92 | sleep_manager_unlock_deep_sleep_internal()
|
---|
| 93 |
|
---|
| 94 | #endif // MBED_SLEEP_TRACING_ENABLED
|
---|
| 95 |
|
---|
| 96 | /** Lock the deep sleep mode
|
---|
| 97 | *
|
---|
| 98 | * This locks the automatic deep mode selection.
|
---|
| 99 | * sleep_manager_sleep_auto() will ignore deepsleep mode if
|
---|
| 100 | * this function is invoked at least once (the internal counter is non-zero)
|
---|
| 101 | *
|
---|
| 102 | * Use this locking mechanism for interrupt driven API that are
|
---|
| 103 | * running in the background and deepsleep could affect their functionality
|
---|
| 104 | *
|
---|
| 105 | * The lock is a counter, can be locked up to USHRT_MAX
|
---|
| 106 | * This function is IRQ and thread safe
|
---|
| 107 | */
|
---|
| 108 | void sleep_manager_lock_deep_sleep_internal(void);
|
---|
| 109 |
|
---|
| 110 | /** Unlock the deep sleep mode
|
---|
| 111 | *
|
---|
| 112 | * Use unlocking in pair with sleep_manager_lock_deep_sleep().
|
---|
| 113 | *
|
---|
| 114 | * The lock is a counter, should be equally unlocked as locked
|
---|
| 115 | * This function is IRQ and thread safe
|
---|
| 116 | */
|
---|
| 117 | void sleep_manager_unlock_deep_sleep_internal(void);
|
---|
| 118 |
|
---|
| 119 | /** Get the status of deep sleep allowance for a target
|
---|
| 120 | *
|
---|
| 121 | * @return true if a target can go to deepsleep, false otherwise
|
---|
| 122 | */
|
---|
| 123 | bool sleep_manager_can_deep_sleep(void);
|
---|
| 124 |
|
---|
| 125 | /** Check if the target can deep sleep within a period of time
|
---|
| 126 | *
|
---|
| 127 | * This function in intended for use in testing. The amount
|
---|
| 128 | * of time this functions waits for deeps sleep to be available
|
---|
| 129 | * is currently 2ms. This may change in the future depending
|
---|
| 130 | * on testing requirements.
|
---|
| 131 | *
|
---|
| 132 | * @return true if a target can go to deepsleep, false otherwise
|
---|
| 133 | */
|
---|
| 134 | bool sleep_manager_can_deep_sleep_test_check(void);
|
---|
| 135 |
|
---|
| 136 | /** Enter auto selected sleep mode. It chooses the sleep or deeepsleep modes based
|
---|
| 137 | * on the deepsleep locking counter
|
---|
| 138 | *
|
---|
| 139 | * This function is IRQ and thread safe
|
---|
| 140 | *
|
---|
| 141 | * @note
|
---|
| 142 | * If MBED_DEBUG is defined, only hal_sleep is allowed. This ensures the debugger
|
---|
| 143 | * to be active for debug modes.
|
---|
| 144 | *
|
---|
| 145 | */
|
---|
| 146 | void sleep_manager_sleep_auto(void);
|
---|
| 147 |
|
---|
| 148 | /** Send the microcontroller to sleep
|
---|
| 149 | *
|
---|
| 150 | * @note This function can be a noop if not implemented by the platform.
|
---|
| 151 | * @note This function will be a noop in debug mode (debug build profile when MBED_DEBUG is defined).
|
---|
| 152 | * @note This function will be a noop if the following conditions are met:
|
---|
| 153 | * - The RTOS is present
|
---|
| 154 | * - The processor turn off the Systick clock during sleep
|
---|
| 155 | * - The target does not implement tickless mode
|
---|
| 156 | *
|
---|
| 157 | * The processor is setup ready for sleep, and sent to sleep using __WFI(). In this mode, the
|
---|
| 158 | * system clock to the core is stopped until a reset or an interrupt occurs. This eliminates
|
---|
| 159 | * dynamic power used by the processor, memory systems and buses. The processor, peripheral and
|
---|
| 160 | * memory state are maintained, and the peripherals continue to work and can generate interrupts.
|
---|
| 161 | *
|
---|
| 162 | * The processor can be woken up by any internal peripheral interrupt or external pin interrupt.
|
---|
| 163 | *
|
---|
| 164 | * @note
|
---|
| 165 | * The mbed interface semihosting is disconnected as part of going to sleep, and can not be restored.
|
---|
| 166 | * Flash re-programming and the USB serial port will remain active, but the mbed program will no longer be
|
---|
| 167 | * able to access the LocalFileSystem
|
---|
| 168 | */
|
---|
| 169 | static inline void mcu_sleep(void)
|
---|
| 170 | {
|
---|
| 171 | #if DEVICE_SLEEP
|
---|
| 172 | #if (MBED_CONF_RTOS_PRESENT == 0) || (DEVICE_STCLK_OFF_DURING_SLEEP == 0) || defined(MBED_TICKLESS)
|
---|
| 173 | sleep_manager_sleep_auto();
|
---|
| 174 | #endif /* (MBED_CONF_RTOS_PRESENT == 0) || (DEVICE_STCLK_OFF_DURING_SLEEP == 0) || defined(MBED_TICKLESS) */
|
---|
| 175 | #endif /* DEVICE_SLEEP */
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | /** Send the microcontroller to deep sleep
|
---|
| 179 | *
|
---|
| 180 | * @deprecated
|
---|
| 181 | * Do not use this function. Applications should use sleep() API which puts the system in deepsleep mode if supported.
|
---|
| 182 | *
|
---|
| 183 | * @note This function can be a noop if not implemented by the platform.
|
---|
| 184 | * @note This function will be a noop in debug mode (debug build profile when MBED_DEBUG is defined)
|
---|
| 185 | *
|
---|
| 186 | * This processor is setup ready for deep sleep, and sent to sleep. This mode
|
---|
| 187 | * has the same sleep features as sleep plus it powers down peripherals and clocks. All state
|
---|
| 188 | * is still maintained.
|
---|
| 189 | *
|
---|
| 190 | * The processor can only be woken up by an external interrupt on a pin or a watchdog timer.
|
---|
| 191 | *
|
---|
| 192 | * @note
|
---|
| 193 | * The mbed interface semihosting is disconnected as part of going to sleep, and can not be restored.
|
---|
| 194 | * Flash re-programming and the USB serial port will remain active, but the mbed program will no longer be
|
---|
| 195 | * able to access the LocalFileSystem
|
---|
| 196 | */
|
---|
| 197 |
|
---|
| 198 | MBED_DEPRECATED_SINCE("mbed-os-5.6", "One entry point for an application, use sleep()")
|
---|
| 199 | static inline void deepsleep(void)
|
---|
| 200 | {
|
---|
| 201 | #if DEVICE_SLEEP
|
---|
| 202 | sleep_manager_sleep_auto();
|
---|
| 203 | #endif /* DEVICE_SLEEP */
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | /** Resets the processor and most of the sub-system
|
---|
| 207 | *
|
---|
| 208 | * @note Does not affect the debug sub-system
|
---|
| 209 | */
|
---|
| 210 | static inline void system_reset(void)
|
---|
| 211 | {
|
---|
| 212 | NVIC_SystemReset();
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | /** Provides the time spent in sleep mode since boot.
|
---|
| 216 | *
|
---|
| 217 | * @return Time spent in sleep
|
---|
| 218 | * @note Works only if platform supports LP ticker.
|
---|
| 219 | */
|
---|
| 220 | us_timestamp_t mbed_time_sleep(void);
|
---|
| 221 |
|
---|
| 222 | /** Provides the time spent in deep sleep mode since boot.
|
---|
| 223 | *
|
---|
| 224 | * @return Time spent in deep sleep
|
---|
| 225 | * @note Works only if platform supports LP ticker.
|
---|
| 226 | */
|
---|
| 227 | us_timestamp_t mbed_time_deepsleep(void);
|
---|
| 228 |
|
---|
| 229 | /** Provides the time spent in idle mode since boot.
|
---|
| 230 | *
|
---|
| 231 | * @return Idle thread time.
|
---|
| 232 | * @note Works only if platform supports LP ticker.
|
---|
| 233 | */
|
---|
| 234 | us_timestamp_t mbed_time_idle(void);
|
---|
| 235 |
|
---|
| 236 | /** Provides the time since the system is up i.e. boot.
|
---|
| 237 | *
|
---|
| 238 | * @return System uptime.
|
---|
| 239 | * @note Works only if platform supports LP ticker.
|
---|
| 240 | */
|
---|
| 241 | us_timestamp_t mbed_uptime(void);
|
---|
| 242 |
|
---|
| 243 | #ifdef __cplusplus
|
---|
| 244 | }
|
---|
| 245 | #endif
|
---|
| 246 |
|
---|
| 247 | #endif
|
---|
| 248 |
|
---|
| 249 | /** @}*/
|
---|
| 250 | /** @}*/
|
---|