esp_pm.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * SPDX-FileCopyrightText: 2016-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdint.h>
  8. #include <stdbool.h>
  9. #include "esp_err.h"
  10. #include "sdkconfig.h"
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. /**
  15. * @brief Power management config
  16. *
  17. * Pass a pointer to this structure as an argument to esp_pm_configure function.
  18. */
  19. typedef struct {
  20. int max_freq_mhz; /*!< Maximum CPU frequency, in MHz */
  21. int min_freq_mhz; /*!< Minimum CPU frequency to use when no locks are taken, in MHz */
  22. bool light_sleep_enable; /*!< Enter light sleep when no locks are taken */
  23. } esp_pm_config_t;
  24. /**
  25. * backward compatibility
  26. * newer chips no longer require this typedef
  27. */
  28. typedef esp_pm_config_t esp_pm_config_esp32_t __attribute__((deprecated("please use esp_pm_config_t instead")));
  29. typedef esp_pm_config_t esp_pm_config_esp32s2_t __attribute__((deprecated("please use esp_pm_config_t instead")));
  30. typedef esp_pm_config_t esp_pm_config_esp32s3_t __attribute__((deprecated("please use esp_pm_config_t instead")));
  31. typedef esp_pm_config_t esp_pm_config_esp32c3_t __attribute__((deprecated("please use esp_pm_config_t instead")));
  32. typedef esp_pm_config_t esp_pm_config_esp32c2_t __attribute__((deprecated("please use esp_pm_config_t instead")));
  33. typedef esp_pm_config_t esp_pm_config_esp32c6_t __attribute__((deprecated("please use esp_pm_config_t instead")));
  34. /**
  35. * @brief Power management constraints
  36. */
  37. typedef enum {
  38. /**
  39. * Require CPU frequency to be at the maximum value set via esp_pm_configure.
  40. * Argument is unused and should be set to 0.
  41. */
  42. ESP_PM_CPU_FREQ_MAX,
  43. /**
  44. * Require APB frequency to be at the maximum value supported by the chip.
  45. * Argument is unused and should be set to 0.
  46. */
  47. ESP_PM_APB_FREQ_MAX,
  48. /**
  49. * Prevent the system from going into light sleep.
  50. * Argument is unused and should be set to 0.
  51. */
  52. ESP_PM_NO_LIGHT_SLEEP,
  53. } esp_pm_lock_type_t;
  54. /**
  55. * @brief Set implementation-specific power management configuration
  56. * @param config pointer to implementation-specific configuration structure (e.g. esp_pm_config_esp32)
  57. * @return
  58. * - ESP_OK on success
  59. * - ESP_ERR_INVALID_ARG if the configuration values are not correct
  60. * - ESP_ERR_NOT_SUPPORTED if certain combination of values is not supported,
  61. * or if CONFIG_PM_ENABLE is not enabled in sdkconfig
  62. */
  63. esp_err_t esp_pm_configure(const void* config);
  64. /**
  65. * @brief Get implementation-specific power management configuration
  66. * @param config pointer to implementation-specific configuration structure (e.g. esp_pm_config_esp32)
  67. * @return
  68. * - ESP_OK on success
  69. * - ESP_ERR_INVALID_ARG if the pointer is null
  70. */
  71. esp_err_t esp_pm_get_configuration(void* config);
  72. /**
  73. * @brief Opaque handle to the power management lock
  74. */
  75. typedef struct esp_pm_lock* esp_pm_lock_handle_t;
  76. /**
  77. * @brief Initialize a lock handle for certain power management parameter
  78. *
  79. * When lock is created, initially it is not taken.
  80. * Call esp_pm_lock_acquire to take the lock.
  81. *
  82. * This function must not be called from an ISR.
  83. *
  84. * @param lock_type Power management constraint which the lock should control
  85. * @param arg argument, value depends on lock_type, see esp_pm_lock_type_t
  86. * @param name arbitrary string identifying the lock (e.g. "wifi" or "spi").
  87. * Used by the esp_pm_dump_locks function to list existing locks.
  88. * May be set to NULL. If not set to NULL, must point to a string which is valid
  89. * for the lifetime of the lock.
  90. * @param[out] out_handle handle returned from this function. Use this handle when calling
  91. * esp_pm_lock_delete, esp_pm_lock_acquire, esp_pm_lock_release.
  92. * Must not be NULL.
  93. * @return
  94. * - ESP_OK on success
  95. * - ESP_ERR_NO_MEM if the lock structure can not be allocated
  96. * - ESP_ERR_INVALID_ARG if out_handle is NULL or type argument is not valid
  97. * - ESP_ERR_NOT_SUPPORTED if CONFIG_PM_ENABLE is not enabled in sdkconfig
  98. */
  99. esp_err_t esp_pm_lock_create(esp_pm_lock_type_t lock_type, int arg,
  100. const char* name, esp_pm_lock_handle_t* out_handle);
  101. /**
  102. * @brief Take a power management lock
  103. *
  104. * Once the lock is taken, power management algorithm will not switch to the
  105. * mode specified in a call to esp_pm_lock_create, or any of the lower power
  106. * modes (higher numeric values of 'mode').
  107. *
  108. * The lock is recursive, in the sense that if esp_pm_lock_acquire is called
  109. * a number of times, esp_pm_lock_release has to be called the same number of
  110. * times in order to release the lock.
  111. *
  112. * This function may be called from an ISR.
  113. *
  114. * This function is not thread-safe w.r.t. calls to other esp_pm_lock_*
  115. * functions for the same handle.
  116. *
  117. * @param handle handle obtained from esp_pm_lock_create function
  118. * @return
  119. * - ESP_OK on success
  120. * - ESP_ERR_INVALID_ARG if the handle is invalid
  121. * - ESP_ERR_NOT_SUPPORTED if CONFIG_PM_ENABLE is not enabled in sdkconfig
  122. */
  123. esp_err_t esp_pm_lock_acquire(esp_pm_lock_handle_t handle);
  124. /**
  125. * @brief Release the lock taken using esp_pm_lock_acquire.
  126. *
  127. * Call to this functions removes power management restrictions placed when
  128. * taking the lock.
  129. *
  130. * Locks are recursive, so if esp_pm_lock_acquire is called a number of times,
  131. * esp_pm_lock_release has to be called the same number of times in order to
  132. * actually release the lock.
  133. *
  134. * This function may be called from an ISR.
  135. *
  136. * This function is not thread-safe w.r.t. calls to other esp_pm_lock_*
  137. * functions for the same handle.
  138. *
  139. * @param handle handle obtained from esp_pm_lock_create function
  140. * @return
  141. * - ESP_OK on success
  142. * - ESP_ERR_INVALID_ARG if the handle is invalid
  143. * - ESP_ERR_INVALID_STATE if lock is not acquired
  144. * - ESP_ERR_NOT_SUPPORTED if CONFIG_PM_ENABLE is not enabled in sdkconfig
  145. */
  146. esp_err_t esp_pm_lock_release(esp_pm_lock_handle_t handle);
  147. /**
  148. * @brief Delete a lock created using esp_pm_lock
  149. *
  150. * The lock must be released before calling this function.
  151. *
  152. * This function must not be called from an ISR.
  153. *
  154. * @param handle handle obtained from esp_pm_lock_create function
  155. * @return
  156. * - ESP_OK on success
  157. * - ESP_ERR_INVALID_ARG if the handle argument is NULL
  158. * - ESP_ERR_INVALID_STATE if the lock is still acquired
  159. * - ESP_ERR_NOT_SUPPORTED if CONFIG_PM_ENABLE is not enabled in sdkconfig
  160. */
  161. esp_err_t esp_pm_lock_delete(esp_pm_lock_handle_t handle);
  162. /**
  163. * Dump the list of all locks to stderr
  164. *
  165. * This function dumps debugging information about locks created using
  166. * esp_pm_lock_create to an output stream.
  167. *
  168. * This function must not be called from an ISR. If esp_pm_lock_acquire/release
  169. * are called while this function is running, inconsistent results may be
  170. * reported.
  171. *
  172. * @param stream stream to print information to; use stdout or stderr to print
  173. * to the console; use fmemopen/open_memstream to print to a
  174. * string buffer.
  175. * @return
  176. * - ESP_OK on success
  177. * - ESP_ERR_NOT_SUPPORTED if CONFIG_PM_ENABLE is not enabled in sdkconfig
  178. */
  179. esp_err_t esp_pm_dump_locks(FILE* stream);
  180. #if CONFIG_PM_LIGHT_SLEEP_CALLBACKS
  181. /**
  182. * @brief Function prototype for light sleep callback functions (if CONFIG_FREERTOS_USE_TICKLESS_IDLE)
  183. *
  184. * @param sleep_time_us supplied by the power management framework.
  185. * For entry callback, sleep_time_us indicates the expected sleep time in us
  186. * For exit callback, sleep_time_us indicates the actual sleep time in us
  187. * @param arg is the user provided argument while registering callbacks
  188. *
  189. * @return
  190. * - ESP_OK allow entry light sleep mode
  191. */
  192. typedef esp_err_t (*esp_pm_light_sleep_cb_t)(int64_t sleep_time_us, void *arg);
  193. typedef struct {
  194. /**
  195. * Callback function defined by internal developers.
  196. */
  197. esp_pm_light_sleep_cb_t enter_cb;
  198. esp_pm_light_sleep_cb_t exit_cb;
  199. /**
  200. * Input parameters of callback function defined by internal developers.
  201. */
  202. void *enter_cb_user_arg;
  203. void *exit_cb_user_arg;
  204. /**
  205. * Execution priority of callback function defined by internal developers.
  206. * The smaller the priority, the earlier it executes when call esp_sleep_execute_event_callbacks.
  207. * If functions have the same priority, the function registered first will be executed first.
  208. */
  209. uint32_t enter_cb_prior;
  210. uint32_t exit_cb_prior;
  211. } esp_pm_sleep_cbs_register_config_t;
  212. /**
  213. * @brief Register entry or exit callbacks for light sleep (if CONFIG_FREERTOS_USE_TICKLESS_IDLE)
  214. * @param cbs_conf Config struct containing entry or exit callbacks function and corresponding argument
  215. * @return
  216. * - ESP_OK on success
  217. * - ESP_ERR_INVALID_ARG if the input parameter enter_cb and exit_cb in cbs_conf are NULL
  218. * - ESP_ERR_NO_MEM if the remaining memory is insufficient to support malloc
  219. * - ESP_FAIL if register the same function repeatedly
  220. *
  221. * @note These callback functions are called from IDLE task context hence they cannot call any blocking functions
  222. */
  223. esp_err_t esp_pm_light_sleep_register_cbs(esp_pm_sleep_cbs_register_config_t *cbs_conf);
  224. /**
  225. * @brief Unregister entry or exit callbacks for light sleep (if CONFIG_FREERTOS_USE_TICKLESS_IDLE)
  226. * @param cbs_conf Config struct containing entry or exit callbacks function and corresponding argument
  227. * @return
  228. * - ESP_OK on success
  229. * - ESP_ERR_INVALID_ARG if the input parameter enter_cb and exit_cb in cbs_conf are NULL
  230. */
  231. esp_err_t esp_pm_light_sleep_unregister_cbs(esp_pm_sleep_cbs_register_config_t *cbs_conf);
  232. #endif
  233. #ifdef __cplusplus
  234. }
  235. #endif