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