The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Committer:
AnnaBridge
Date:
Thu Nov 08 11:45:42 2018 +0000
Revision:
171:3a7713b1edbc
Parent:
170:e95d10626187
Child:
172:65be27845400
mbed library. Release version 164

Who changed what in which revision?

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