001

Committer:
ganlikun
Date:
Sun Jun 12 14:02:44 2022 +0000
Revision:
0:13413ea9a877
00

Who changed what in which revision?

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