pm_impl.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. /**
  16. * @file esp_private/pm_impl.h
  17. *
  18. * This header file defines interface between PM lock functions (pm_locks.c)
  19. * and the chip-specific power management (DFS/light sleep) implementation.
  20. */
  21. #include "soc/rtc.h"
  22. #include "esp_pm.h"
  23. #include "esp_timer.h"
  24. #include "sdkconfig.h"
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. /**
  29. * This is an enum of possible power modes supported by the implementation
  30. */
  31. typedef enum {
  32. PM_MODE_LIGHT_SLEEP,//!< Light sleep
  33. PM_MODE_APB_MIN, //!< Idle (no CPU frequency or APB frequency locks)
  34. PM_MODE_APB_MAX, //!< Maximum APB frequency mode
  35. PM_MODE_CPU_MAX, //!< Maximum CPU frequency mode
  36. PM_MODE_COUNT //!< Number of items
  37. } pm_mode_t;
  38. /**
  39. * @brief Get the mode corresponding to a certain lock
  40. * @param type lock type
  41. * @param arg argument value for this lock (passed to esp_pm_lock_create)
  42. * @return lowest power consumption mode which meets the constraints of the lock
  43. */
  44. pm_mode_t esp_pm_impl_get_mode(esp_pm_lock_type_t type, int arg);
  45. /**
  46. * @brief Get CPU clock frequency by power mode
  47. * @param mode power mode
  48. * @return CPU clock frequency
  49. */
  50. int esp_pm_impl_get_cpu_freq(pm_mode_t mode);
  51. /**
  52. * If profiling is enabled, this data type will be used to store microsecond
  53. * timestamps.
  54. */
  55. typedef int64_t pm_time_t;
  56. /**
  57. * See \ref esp_pm_impl_switch_mode
  58. */
  59. typedef enum {
  60. MODE_LOCK,
  61. MODE_UNLOCK
  62. } pm_mode_switch_t;
  63. /**
  64. * @brief Switch between power modes when lock is taken or released
  65. * @param mode pm_mode_t corresponding to the lock being taken or released,
  66. * as returned by \ref esp_pm_impl_get_mode
  67. * @param lock_or_unlock
  68. * - MODE_LOCK: lock was taken. Implementation needs to make sure
  69. * that the constraints of the lock are met by switching to the
  70. * given 'mode' or any of the higher power ones.
  71. * - MODE_UNLOCK: lock was released. If all the locks for given
  72. * mode are released, and no locks for higher power modes are
  73. * taken, implementation can switch to one of lower power modes.
  74. * @param now timestamp when the lock was taken or released. Passed as
  75. * a minor optimization, so that the implementation does not need to
  76. * call pm_get_time again.
  77. */
  78. void esp_pm_impl_switch_mode(pm_mode_t mode, pm_mode_switch_t lock_or_unlock, pm_time_t now);
  79. /**
  80. * @brief Call once at startup to initialize pm implementation
  81. */
  82. void esp_pm_impl_init(void);
  83. /**
  84. * @brief Hook function for the idle task
  85. * Must be called from the IDLE task on each CPU before entering waiti state.
  86. */
  87. void esp_pm_impl_idle_hook(void);
  88. /**
  89. * @brief Hook function for the interrupt dispatcher
  90. * Must be called soon after entering the ISR
  91. */
  92. void esp_pm_impl_isr_hook(void);
  93. /**
  94. * @brief Dump the information about time spent in each of the pm modes.
  95. *
  96. * Prints three columns:
  97. * mode name, total time in mode (in microseconds), percentage of time in mode
  98. *
  99. * @param out stream to dump the information to
  100. */
  101. void esp_pm_impl_dump_stats(FILE* out);
  102. /**
  103. * @brief Hook function implementing `waiti` instruction, should be invoked from idle task context
  104. */
  105. void esp_pm_impl_waiti(void);
  106. /**
  107. * @brief Callback function type for peripherals to skip light sleep.
  108. *
  109. */
  110. typedef bool (* skip_light_sleep_cb_t)(void);
  111. /**
  112. * @brief Register peripherals skip light sleep callback
  113. *
  114. * This function allows you to register a callback that gets the result
  115. * that if light sleep should be skipped by peripherals.
  116. * @param cb function to get the result
  117. * @return
  118. * - ESP_OK on success
  119. * - ESP_ERR_NO_MEM if no more callback slots are available
  120. */
  121. esp_err_t esp_pm_register_skip_light_sleep_callback(skip_light_sleep_cb_t cb);
  122. /**
  123. * @brief Unregisterperipherals skip light sleep callback
  124. *
  125. * This function allows you to unregister a callback which was previously
  126. * registered using esp_register_skip_light_sleep_callback.
  127. * @param cb function to get the result
  128. * @return
  129. * - ESP_OK on success
  130. * - ESP_ERR_INVALID_STATE if the given callback hasn't been registered before
  131. */
  132. esp_err_t esp_pm_unregister_skip_light_sleep_callback(skip_light_sleep_cb_t cb);
  133. /**
  134. * @brief Callback function type for peripherals to know light sleep wakeup overhead.
  135. *
  136. */
  137. typedef void (* inform_out_light_sleep_overhead_cb_t)(uint32_t);
  138. /**
  139. * @brief Register informing peripherals light sleep wakeup overhead time callback
  140. *
  141. * This function allows you to register a callback that informs the peripherals of
  142. * the wakeup overhead time of light sleep.
  143. * @param cb function to inform time
  144. * @return
  145. * - ESP_OK on success
  146. * - ESP_ERR_NO_MEM if no more callback slots are available
  147. */
  148. esp_err_t esp_pm_register_inform_out_light_sleep_overhead_callback(inform_out_light_sleep_overhead_cb_t cb);
  149. /**
  150. * @brief Unregister informing peripherals light sleep wakeup overhead time callback
  151. *
  152. * This function allows you to unregister a callback that informs the peripherals of
  153. * the wakeup overhead time of light sleep.
  154. * @param cb function to inform time
  155. * @return
  156. * - ESP_OK on success
  157. * - ESP_ERR_INVALID_STATE if the given callback hasn't been registered before
  158. */
  159. esp_err_t esp_pm_unregister_inform_out_light_sleep_overhead_callback(inform_out_light_sleep_overhead_cb_t cb);
  160. /**
  161. * @brief Callback function type for peripherals to know light sleep default parameters
  162. */
  163. typedef void (* update_light_sleep_default_params_config_cb_t)(int, int);
  164. /**
  165. * @brief Register peripherals light sleep default parameters configure callback
  166. *
  167. * This function allows you to register a callback that configure the peripherals
  168. * of default parameters of light sleep
  169. * @param cb function to update default parameters
  170. */
  171. void esp_pm_register_light_sleep_default_params_config_callback(update_light_sleep_default_params_config_cb_t cb);
  172. /**
  173. * @brief Unregister peripherals light sleep default parameters configure Callback
  174. *
  175. * This function allows you to unregister a callback that configure the peripherals
  176. * of default parameters of light sleep
  177. */
  178. void esp_pm_unregister_light_sleep_default_params_config_callback(void);
  179. #ifdef CONFIG_PM_PROFILING
  180. #define WITH_PROFILING
  181. #endif
  182. #ifdef WITH_PROFILING
  183. static inline pm_time_t IRAM_ATTR pm_get_time(void)
  184. {
  185. return esp_timer_get_time();
  186. }
  187. #endif // WITH_PROFILING
  188. #ifdef __cplusplus
  189. }
  190. #endif