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