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