esp_pm.h 6.4 KB

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