mbed_power_mgmt.h 7.9 KB

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