esp_pm.h 6.3 KB

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