inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

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