esp_pm.h 6.3 KB

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