esp_pm.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * SPDX-FileCopyrightText: 2016-2022 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_esp32h4_t __attribute__((deprecated("please use esp_pm_config_t instead")));
  33. typedef esp_pm_config_t esp_pm_config_esp32c2_t __attribute__((deprecated("please use esp_pm_config_t instead")));
  34. typedef esp_pm_config_t esp_pm_config_esp32c6_t __attribute__((deprecated("please use esp_pm_config_t instead")));
  35. /**
  36. * @brief Power management constraints
  37. */
  38. typedef enum {
  39. /**
  40. * Require CPU frequency to be at the maximum value set via esp_pm_configure.
  41. * Argument is unused and should be set to 0.
  42. */
  43. ESP_PM_CPU_FREQ_MAX,
  44. /**
  45. * Require APB frequency to be at the maximum value supported by the chip.
  46. * Argument is unused and should be set to 0.
  47. */
  48. ESP_PM_APB_FREQ_MAX,
  49. /**
  50. * Prevent the system from going into light sleep.
  51. * Argument is unused and should be set to 0.
  52. */
  53. ESP_PM_NO_LIGHT_SLEEP,
  54. } esp_pm_lock_type_t;
  55. /**
  56. * @brief Set implementation-specific power management configuration
  57. * @param config pointer to implementation-specific configuration structure (e.g. esp_pm_config_esp32)
  58. * @return
  59. * - ESP_OK on success
  60. * - ESP_ERR_INVALID_ARG if the configuration values are not correct
  61. * - ESP_ERR_NOT_SUPPORTED if certain combination of values is not supported,
  62. * or if CONFIG_PM_ENABLE is not enabled in sdkconfig
  63. */
  64. esp_err_t esp_pm_configure(const void* config);
  65. /**
  66. * @brief Get implementation-specific power management configuration
  67. * @param config pointer to implementation-specific configuration structure (e.g. esp_pm_config_esp32)
  68. * @return
  69. * - ESP_OK on success
  70. * - ESP_ERR_INVALID_ARG if the pointer is null
  71. */
  72. esp_err_t esp_pm_get_configuration(void* config);
  73. /**
  74. * @brief Opaque handle to the power management lock
  75. */
  76. typedef struct esp_pm_lock* esp_pm_lock_handle_t;
  77. /**
  78. * @brief Initialize a lock handle for certain power management parameter
  79. *
  80. * When lock is created, initially it is not taken.
  81. * Call esp_pm_lock_acquire to take the lock.
  82. *
  83. * This function must not be called from an ISR.
  84. *
  85. * @param lock_type Power management constraint which the lock should control
  86. * @param arg argument, value depends on lock_type, see esp_pm_lock_type_t
  87. * @param name arbitrary string identifying the lock (e.g. "wifi" or "spi").
  88. * Used by the esp_pm_dump_locks function to list existing locks.
  89. * May be set to NULL. If not set to NULL, must point to a string which is valid
  90. * for the lifetime of the lock.
  91. * @param[out] out_handle handle returned from this function. Use this handle when calling
  92. * esp_pm_lock_delete, esp_pm_lock_acquire, esp_pm_lock_release.
  93. * Must not be NULL.
  94. * @return
  95. * - ESP_OK on success
  96. * - ESP_ERR_NO_MEM if the lock structure can not be allocated
  97. * - ESP_ERR_INVALID_ARG if out_handle is NULL or type argument is not valid
  98. * - ESP_ERR_NOT_SUPPORTED if CONFIG_PM_ENABLE is not enabled in sdkconfig
  99. */
  100. esp_err_t esp_pm_lock_create(esp_pm_lock_type_t lock_type, int arg,
  101. const char* name, esp_pm_lock_handle_t* out_handle);
  102. /**
  103. * @brief Take a power management lock
  104. *
  105. * Once the lock is taken, power management algorithm will not switch to the
  106. * mode specified in a call to esp_pm_lock_create, or any of the lower power
  107. * modes (higher numeric values of 'mode').
  108. *
  109. * The lock is recursive, in the sense that if esp_pm_lock_acquire is called
  110. * a number of times, esp_pm_lock_release has to be called the same number of
  111. * times in order to release the lock.
  112. *
  113. * This function may be called from an ISR.
  114. *
  115. * This function is not thread-safe w.r.t. calls to other esp_pm_lock_*
  116. * functions for the same handle.
  117. *
  118. * @param handle handle obtained from esp_pm_lock_create function
  119. * @return
  120. * - ESP_OK on success
  121. * - ESP_ERR_INVALID_ARG if the handle is invalid
  122. * - ESP_ERR_NOT_SUPPORTED if CONFIG_PM_ENABLE is not enabled in sdkconfig
  123. */
  124. esp_err_t esp_pm_lock_acquire(esp_pm_lock_handle_t handle);
  125. /**
  126. * @brief Release the lock taken using esp_pm_lock_acquire.
  127. *
  128. * Call to this functions removes power management restrictions placed when
  129. * taking the lock.
  130. *
  131. * Locks are recursive, so if esp_pm_lock_acquire is called a number of times,
  132. * esp_pm_lock_release has to be called the same number of times in order to
  133. * actually release the lock.
  134. *
  135. * This function may be called from an ISR.
  136. *
  137. * This function is not thread-safe w.r.t. calls to other esp_pm_lock_*
  138. * functions for the same handle.
  139. *
  140. * @param handle handle obtained from esp_pm_lock_create function
  141. * @return
  142. * - ESP_OK on success
  143. * - ESP_ERR_INVALID_ARG if the handle is invalid
  144. * - ESP_ERR_INVALID_STATE if lock is not acquired
  145. * - ESP_ERR_NOT_SUPPORTED if CONFIG_PM_ENABLE is not enabled in sdkconfig
  146. */
  147. esp_err_t esp_pm_lock_release(esp_pm_lock_handle_t handle);
  148. /**
  149. * @brief Delete a lock created using esp_pm_lock
  150. *
  151. * The lock must be released before calling this function.
  152. *
  153. * This function must not be called from an ISR.
  154. *
  155. * @param handle handle obtained from esp_pm_lock_create function
  156. * @return
  157. * - ESP_OK on success
  158. * - ESP_ERR_INVALID_ARG if the handle argument is NULL
  159. * - ESP_ERR_INVALID_STATE if the lock is still acquired
  160. * - ESP_ERR_NOT_SUPPORTED if CONFIG_PM_ENABLE is not enabled in sdkconfig
  161. */
  162. esp_err_t esp_pm_lock_delete(esp_pm_lock_handle_t handle);
  163. /**
  164. * Dump the list of all locks to stderr
  165. *
  166. * This function dumps debugging information about locks created using
  167. * esp_pm_lock_create to an output stream.
  168. *
  169. * This function must not be called from an ISR. If esp_pm_lock_acquire/release
  170. * are called while this function is running, inconsistent results may be
  171. * reported.
  172. *
  173. * @param stream stream to print information to; use stdout or stderr to print
  174. * to the console; use fmemopen/open_memstream to print to a
  175. * string buffer.
  176. * @return
  177. * - ESP_OK on success
  178. * - ESP_ERR_NOT_SUPPORTED if CONFIG_PM_ENABLE is not enabled in sdkconfig
  179. */
  180. esp_err_t esp_pm_dump_locks(FILE* stream);
  181. #ifdef __cplusplus
  182. }
  183. #endif