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