Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lypinator 0:bb348c97df44 1 /** \addtogroup platform */
lypinator 0:bb348c97df44 2 /** @{*/
lypinator 0:bb348c97df44 3 /**
lypinator 0:bb348c97df44 4 * \defgroup platform_power_mgmt Power management functions
lypinator 0:bb348c97df44 5 * @{
lypinator 0:bb348c97df44 6 */
lypinator 0:bb348c97df44 7
lypinator 0:bb348c97df44 8 /* mbed Microcontroller Library
lypinator 0:bb348c97df44 9 * Copyright (c) 2006-2018 ARM Limited
lypinator 0:bb348c97df44 10 *
lypinator 0:bb348c97df44 11 * Licensed under the Apache License, Version 2.0 (the "License");
lypinator 0:bb348c97df44 12 * you may not use this file except in compliance with the License.
lypinator 0:bb348c97df44 13 * You may obtain a copy of the License at
lypinator 0:bb348c97df44 14 *
lypinator 0:bb348c97df44 15 * http://www.apache.org/licenses/LICENSE-2.0
lypinator 0:bb348c97df44 16 *
lypinator 0:bb348c97df44 17 * Unless required by applicable law or agreed to in writing, software
lypinator 0:bb348c97df44 18 * distributed under the License is distributed on an "AS IS" BASIS,
lypinator 0:bb348c97df44 19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
lypinator 0:bb348c97df44 20 * See the License for the specific language governing permissions and
lypinator 0:bb348c97df44 21 * limitations under the License.
lypinator 0:bb348c97df44 22 */
lypinator 0:bb348c97df44 23 #ifndef MBED_POWER_MGMT_H
lypinator 0:bb348c97df44 24 #define MBED_POWER_MGMT_H
lypinator 0:bb348c97df44 25
lypinator 0:bb348c97df44 26 #include "sleep_api.h"
lypinator 0:bb348c97df44 27 #include "mbed_toolchain.h"
lypinator 0:bb348c97df44 28 #include "hal/ticker_api.h"
lypinator 0:bb348c97df44 29 #include <stdbool.h>
lypinator 0:bb348c97df44 30
lypinator 0:bb348c97df44 31 #ifdef __cplusplus
lypinator 0:bb348c97df44 32 extern "C" {
lypinator 0:bb348c97df44 33 #endif
lypinator 0:bb348c97df44 34
lypinator 0:bb348c97df44 35 /** Sleep manager API
lypinator 0:bb348c97df44 36 * The sleep manager provides API to automatically select sleep mode.
lypinator 0:bb348c97df44 37 *
lypinator 0:bb348c97df44 38 * There are two sleep modes:
lypinator 0:bb348c97df44 39 * - sleep
lypinator 0:bb348c97df44 40 * - deepsleep
lypinator 0:bb348c97df44 41 *
lypinator 0:bb348c97df44 42 * Use locking/unlocking deepsleep for drivers that depend on features that
lypinator 0:bb348c97df44 43 * are not allowed (=disabled) during the deepsleep. For instance, high frequency
lypinator 0:bb348c97df44 44 * clocks.
lypinator 0:bb348c97df44 45 *
lypinator 0:bb348c97df44 46 * Example:
lypinator 0:bb348c97df44 47 * @code
lypinator 0:bb348c97df44 48 *
lypinator 0:bb348c97df44 49 * void driver::handler()
lypinator 0:bb348c97df44 50 * {
lypinator 0:bb348c97df44 51 * if (_sensor.get_event()) {
lypinator 0:bb348c97df44 52 * // any event - we are finished, unlock the deepsleep
lypinator 0:bb348c97df44 53 * sleep_manager_unlock_deep_sleep();
lypinator 0:bb348c97df44 54 * _callback();
lypinator 0:bb348c97df44 55 * }
lypinator 0:bb348c97df44 56 * }
lypinator 0:bb348c97df44 57 *
lypinator 0:bb348c97df44 58 * int driver::measure(event_t event, callback_t& callback)
lypinator 0:bb348c97df44 59 * {
lypinator 0:bb348c97df44 60 * _callback = callback;
lypinator 0:bb348c97df44 61 * sleep_manager_lock_deep_sleep();
lypinator 0:bb348c97df44 62 * // start async transaction, we are waiting for an event
lypinator 0:bb348c97df44 63 * return _sensor.start(event, callback);
lypinator 0:bb348c97df44 64 * }
lypinator 0:bb348c97df44 65 * @endcode
lypinator 0:bb348c97df44 66 */
lypinator 0:bb348c97df44 67 #ifdef MBED_SLEEP_TRACING_ENABLED
lypinator 0:bb348c97df44 68
lypinator 0:bb348c97df44 69 void sleep_tracker_lock(const char *const filename, int line);
lypinator 0:bb348c97df44 70 void sleep_tracker_unlock(const char *const filename, int line);
lypinator 0:bb348c97df44 71
lypinator 0:bb348c97df44 72 #define sleep_manager_lock_deep_sleep() \
lypinator 0:bb348c97df44 73 do \
lypinator 0:bb348c97df44 74 { \
lypinator 0:bb348c97df44 75 sleep_manager_lock_deep_sleep_internal(); \
lypinator 0:bb348c97df44 76 sleep_tracker_lock(MBED_FILENAME, __LINE__); \
lypinator 0:bb348c97df44 77 } while (0);
lypinator 0:bb348c97df44 78
lypinator 0:bb348c97df44 79 #define sleep_manager_unlock_deep_sleep() \
lypinator 0:bb348c97df44 80 do \
lypinator 0:bb348c97df44 81 { \
lypinator 0:bb348c97df44 82 sleep_manager_unlock_deep_sleep_internal(); \
lypinator 0:bb348c97df44 83 sleep_tracker_unlock(MBED_FILENAME, __LINE__); \
lypinator 0:bb348c97df44 84 } while (0);
lypinator 0:bb348c97df44 85
lypinator 0:bb348c97df44 86 #else
lypinator 0:bb348c97df44 87
lypinator 0:bb348c97df44 88 #define sleep_manager_lock_deep_sleep() \
lypinator 0:bb348c97df44 89 sleep_manager_lock_deep_sleep_internal()
lypinator 0:bb348c97df44 90
lypinator 0:bb348c97df44 91 #define sleep_manager_unlock_deep_sleep() \
lypinator 0:bb348c97df44 92 sleep_manager_unlock_deep_sleep_internal()
lypinator 0:bb348c97df44 93
lypinator 0:bb348c97df44 94 #endif // MBED_SLEEP_TRACING_ENABLED
lypinator 0:bb348c97df44 95
lypinator 0:bb348c97df44 96 /** Lock the deep sleep mode
lypinator 0:bb348c97df44 97 *
lypinator 0:bb348c97df44 98 * This locks the automatic deep mode selection.
lypinator 0:bb348c97df44 99 * sleep_manager_sleep_auto() will ignore deepsleep mode if
lypinator 0:bb348c97df44 100 * this function is invoked at least once (the internal counter is non-zero)
lypinator 0:bb348c97df44 101 *
lypinator 0:bb348c97df44 102 * Use this locking mechanism for interrupt driven API that are
lypinator 0:bb348c97df44 103 * running in the background and deepsleep could affect their functionality
lypinator 0:bb348c97df44 104 *
lypinator 0:bb348c97df44 105 * The lock is a counter, can be locked up to USHRT_MAX
lypinator 0:bb348c97df44 106 * This function is IRQ and thread safe
lypinator 0:bb348c97df44 107 */
lypinator 0:bb348c97df44 108 void sleep_manager_lock_deep_sleep_internal(void);
lypinator 0:bb348c97df44 109
lypinator 0:bb348c97df44 110 /** Unlock the deep sleep mode
lypinator 0:bb348c97df44 111 *
lypinator 0:bb348c97df44 112 * Use unlocking in pair with sleep_manager_lock_deep_sleep().
lypinator 0:bb348c97df44 113 *
lypinator 0:bb348c97df44 114 * The lock is a counter, should be equally unlocked as locked
lypinator 0:bb348c97df44 115 * This function is IRQ and thread safe
lypinator 0:bb348c97df44 116 */
lypinator 0:bb348c97df44 117 void sleep_manager_unlock_deep_sleep_internal(void);
lypinator 0:bb348c97df44 118
lypinator 0:bb348c97df44 119 /** Get the status of deep sleep allowance for a target
lypinator 0:bb348c97df44 120 *
lypinator 0:bb348c97df44 121 * @return true if a target can go to deepsleep, false otherwise
lypinator 0:bb348c97df44 122 */
lypinator 0:bb348c97df44 123 bool sleep_manager_can_deep_sleep(void);
lypinator 0:bb348c97df44 124
lypinator 0:bb348c97df44 125 /** Enter auto selected sleep mode. It chooses the sleep or deeepsleep modes based
lypinator 0:bb348c97df44 126 * on the deepsleep locking counter
lypinator 0:bb348c97df44 127 *
lypinator 0:bb348c97df44 128 * This function is IRQ and thread safe
lypinator 0:bb348c97df44 129 *
lypinator 0:bb348c97df44 130 * @note
lypinator 0:bb348c97df44 131 * If MBED_DEBUG is defined, only hal_sleep is allowed. This ensures the debugger
lypinator 0:bb348c97df44 132 * to be active for debug modes.
lypinator 0:bb348c97df44 133 *
lypinator 0:bb348c97df44 134 */
lypinator 0:bb348c97df44 135 void sleep_manager_sleep_auto(void);
lypinator 0:bb348c97df44 136
lypinator 0:bb348c97df44 137 /** Send the microcontroller to sleep
lypinator 0:bb348c97df44 138 *
lypinator 0:bb348c97df44 139 * @note This function can be a noop if not implemented by the platform.
lypinator 0:bb348c97df44 140 * @note This function will be a noop in debug mode (debug build profile when MBED_DEBUG is defined).
lypinator 0:bb348c97df44 141 * @note This function will be a noop while uVisor is in use.
lypinator 0:bb348c97df44 142 * @note This function will be a noop if the following conditions are met:
lypinator 0:bb348c97df44 143 * - The RTOS is present
lypinator 0:bb348c97df44 144 * - The processor turn off the Systick clock during sleep
lypinator 0:bb348c97df44 145 * - The target does not implement tickless mode
lypinator 0:bb348c97df44 146 *
lypinator 0:bb348c97df44 147 * The processor is setup ready for sleep, and sent to sleep using __WFI(). In this mode, the
lypinator 0:bb348c97df44 148 * system clock to the core is stopped until a reset or an interrupt occurs. This eliminates
lypinator 0:bb348c97df44 149 * dynamic power used by the processor, memory systems and buses. The processor, peripheral and
lypinator 0:bb348c97df44 150 * memory state are maintained, and the peripherals continue to work and can generate interrupts.
lypinator 0:bb348c97df44 151 *
lypinator 0:bb348c97df44 152 * The processor can be woken up by any internal peripheral interrupt or external pin interrupt.
lypinator 0:bb348c97df44 153 *
lypinator 0:bb348c97df44 154 * @note
lypinator 0:bb348c97df44 155 * The mbed interface semihosting is disconnected as part of going to sleep, and can not be restored.
lypinator 0:bb348c97df44 156 * Flash re-programming and the USB serial port will remain active, but the mbed program will no longer be
lypinator 0:bb348c97df44 157 * able to access the LocalFileSystem
lypinator 0:bb348c97df44 158 */
lypinator 0:bb348c97df44 159 static inline void sleep(void)
lypinator 0:bb348c97df44 160 {
lypinator 0:bb348c97df44 161 #if !(defined(FEATURE_UVISOR) && defined(TARGET_UVISOR_SUPPORTED))
lypinator 0:bb348c97df44 162 #if DEVICE_SLEEP
lypinator 0:bb348c97df44 163 #if (MBED_CONF_RTOS_PRESENT == 0) || (DEVICE_STCLK_OFF_DURING_SLEEP == 0) || defined(MBED_TICKLESS)
lypinator 0:bb348c97df44 164 sleep_manager_sleep_auto();
lypinator 0:bb348c97df44 165 #endif /* (MBED_CONF_RTOS_PRESENT == 0) || (DEVICE_STCLK_OFF_DURING_SLEEP == 0) || defined(MBED_TICKLESS) */
lypinator 0:bb348c97df44 166 #endif /* DEVICE_SLEEP */
lypinator 0:bb348c97df44 167 #endif /* !(defined(FEATURE_UVISOR) && defined(TARGET_UVISOR_SUPPORTED)) */
lypinator 0:bb348c97df44 168 }
lypinator 0:bb348c97df44 169
lypinator 0:bb348c97df44 170 /** Send the microcontroller to deep sleep
lypinator 0:bb348c97df44 171 *
lypinator 0:bb348c97df44 172 * @deprecated
lypinator 0:bb348c97df44 173 * Do not use this function. Applications should use sleep() API which puts the system in deepsleep mode if supported.
lypinator 0:bb348c97df44 174 *
lypinator 0:bb348c97df44 175 * @note This function can be a noop if not implemented by the platform.
lypinator 0:bb348c97df44 176 * @note This function will be a noop in debug mode (debug build profile when MBED_DEBUG is defined)
lypinator 0:bb348c97df44 177 * @note This function will be a noop while uVisor is in use.
lypinator 0:bb348c97df44 178 *
lypinator 0:bb348c97df44 179 * This processor is setup ready for deep sleep, and sent to sleep. This mode
lypinator 0:bb348c97df44 180 * has the same sleep features as sleep plus it powers down peripherals and clocks. All state
lypinator 0:bb348c97df44 181 * is still maintained.
lypinator 0:bb348c97df44 182 *
lypinator 0:bb348c97df44 183 * The processor can only be woken up by an external interrupt on a pin or a watchdog timer.
lypinator 0:bb348c97df44 184 *
lypinator 0:bb348c97df44 185 * @note
lypinator 0:bb348c97df44 186 * The mbed interface semihosting is disconnected as part of going to sleep, and can not be restored.
lypinator 0:bb348c97df44 187 * Flash re-programming and the USB serial port will remain active, but the mbed program will no longer be
lypinator 0:bb348c97df44 188 * able to access the LocalFileSystem
lypinator 0:bb348c97df44 189 */
lypinator 0:bb348c97df44 190
lypinator 0:bb348c97df44 191 MBED_DEPRECATED_SINCE("mbed-os-5.6", "One entry point for an application, use sleep()")
lypinator 0:bb348c97df44 192 static inline void deepsleep(void)
lypinator 0:bb348c97df44 193 {
lypinator 0:bb348c97df44 194 #if !(defined(FEATURE_UVISOR) && defined(TARGET_UVISOR_SUPPORTED))
lypinator 0:bb348c97df44 195 #if DEVICE_SLEEP
lypinator 0:bb348c97df44 196 sleep_manager_sleep_auto();
lypinator 0:bb348c97df44 197 #endif /* DEVICE_SLEEP */
lypinator 0:bb348c97df44 198 #endif /* !(defined(FEATURE_UVISOR) && defined(TARGET_UVISOR_SUPPORTED)) */
lypinator 0:bb348c97df44 199 }
lypinator 0:bb348c97df44 200
lypinator 0:bb348c97df44 201 /** Resets the processor and most of the sub-system
lypinator 0:bb348c97df44 202 *
lypinator 0:bb348c97df44 203 * @note Does not affect the debug sub-system
lypinator 0:bb348c97df44 204 */
lypinator 0:bb348c97df44 205 static inline void system_reset(void)
lypinator 0:bb348c97df44 206 {
lypinator 0:bb348c97df44 207 NVIC_SystemReset();
lypinator 0:bb348c97df44 208 }
lypinator 0:bb348c97df44 209
lypinator 0:bb348c97df44 210 /** Provides the time spent in sleep mode since boot.
lypinator 0:bb348c97df44 211 *
lypinator 0:bb348c97df44 212 * @return Time spent in sleep
lypinator 0:bb348c97df44 213 * @note Works only if platform supports LP ticker.
lypinator 0:bb348c97df44 214 */
lypinator 0:bb348c97df44 215 us_timestamp_t mbed_time_sleep(void);
lypinator 0:bb348c97df44 216
lypinator 0:bb348c97df44 217 /** Provides the time spent in deep sleep mode since boot.
lypinator 0:bb348c97df44 218 *
lypinator 0:bb348c97df44 219 * @return Time spent in deep sleep
lypinator 0:bb348c97df44 220 * @note Works only if platform supports LP ticker.
lypinator 0:bb348c97df44 221 */
lypinator 0:bb348c97df44 222 us_timestamp_t mbed_time_deepsleep(void);
lypinator 0:bb348c97df44 223
lypinator 0:bb348c97df44 224 /** Provides the time spent in idle mode since boot.
lypinator 0:bb348c97df44 225 *
lypinator 0:bb348c97df44 226 * @return Idle thread time.
lypinator 0:bb348c97df44 227 * @note Works only if platform supports LP ticker.
lypinator 0:bb348c97df44 228 */
lypinator 0:bb348c97df44 229 us_timestamp_t mbed_time_idle(void);
lypinator 0:bb348c97df44 230
lypinator 0:bb348c97df44 231 /** Provides the time since the system is up i.e. boot.
lypinator 0:bb348c97df44 232 *
lypinator 0:bb348c97df44 233 * @return System uptime.
lypinator 0:bb348c97df44 234 * @note Works only if platform supports LP ticker.
lypinator 0:bb348c97df44 235 */
lypinator 0:bb348c97df44 236 us_timestamp_t mbed_uptime(void);
lypinator 0:bb348c97df44 237
lypinator 0:bb348c97df44 238 #ifdef __cplusplus
lypinator 0:bb348c97df44 239 }
lypinator 0:bb348c97df44 240 #endif
lypinator 0:bb348c97df44 241
lypinator 0:bb348c97df44 242 #endif
lypinator 0:bb348c97df44 243
lypinator 0:bb348c97df44 244 /** @}*/
lypinator 0:bb348c97df44 245 /** @}*/