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