esp_pm.h 6.3 KB

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