esp_pm.h 6.7 KB

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