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