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