pm_impl.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. * If profiling is enabled, this data type will be used to store microsecond
  47. * timestamps.
  48. */
  49. typedef int64_t pm_time_t;
  50. /**
  51. * See \ref esp_pm_impl_switch_mode
  52. */
  53. typedef enum {
  54. MODE_LOCK,
  55. MODE_UNLOCK
  56. } pm_mode_switch_t;
  57. /**
  58. * @brief Switch between power modes when lock is taken or released
  59. * @param mode pm_mode_t corresponding to the lock being taken or released,
  60. * as returned by \ref esp_pm_impl_get_mode
  61. * @param lock_or_unlock
  62. * - MODE_LOCK: lock was taken. Implementation needs to make sure
  63. * that the constraints of the lock are met by switching to the
  64. * given 'mode' or any of the higher power ones.
  65. * - MODE_UNLOCK: lock was released. If all the locks for given
  66. * mode are released, and no locks for higher power modes are
  67. * taken, implementation can switch to one of lower power modes.
  68. * @param now timestamp when the lock was taken or released. Passed as
  69. * a minor optimization, so that the implementation does not need to
  70. * call pm_get_time again.
  71. */
  72. void esp_pm_impl_switch_mode(pm_mode_t mode, pm_mode_switch_t lock_or_unlock, pm_time_t now);
  73. /**
  74. * @brief Call once at startup to initialize pm implementation
  75. */
  76. void esp_pm_impl_init(void);
  77. /**
  78. * @brief Hook function for the idle task
  79. * Must be called from the IDLE task on each CPU before entering waiti state.
  80. */
  81. void esp_pm_impl_idle_hook(void);
  82. /**
  83. * @brief Hook function for the interrupt dispatcher
  84. * Must be called soon after entering the ISR
  85. */
  86. void esp_pm_impl_isr_hook(void);
  87. /**
  88. * @brief Dump the information about time spent in each of the pm modes.
  89. *
  90. * Prints three columns:
  91. * mode name, total time in mode (in microseconds), percentage of time in mode
  92. *
  93. * @param out stream to dump the information to
  94. */
  95. void esp_pm_impl_dump_stats(FILE* out);
  96. /**
  97. * @brief Hook function implementing `waiti` instruction, should be invoked from idle task context
  98. */
  99. void esp_pm_impl_waiti(void);
  100. #if CONFIG_IDF_TARGET_ESP32S2
  101. /**
  102. * @brief Callback function type for peripherals to skip light sleep.
  103. *
  104. */
  105. typedef bool (* skip_light_sleep_cb_t)(void);
  106. /**
  107. * @brief Register peripherals skip light sleep callback
  108. *
  109. * This function allows you to register a callback that gets the result
  110. * that if light sleep should be skipped by peripherals.
  111. * @param cb function to get the result
  112. * @return
  113. * - ESP_OK on success
  114. * - ESP_ERR_NO_MEM if no more callback slots are available
  115. */
  116. esp_err_t esp_pm_register_skip_light_sleep_callback(skip_light_sleep_cb_t cb);
  117. /**
  118. * @brief Unregisterperipherals skip light sleep callback
  119. *
  120. * This function allows you to unregister a callback which was previously
  121. * registered using esp_register_skip_light_sleep_callback.
  122. * @param cb function to get the result
  123. * @return
  124. * - ESP_OK on success
  125. * - ESP_ERR_INVALID_STATE if the given callback hasn't been registered before
  126. */
  127. esp_err_t esp_pm_unregister_skip_light_sleep_callback(skip_light_sleep_cb_t cb);
  128. #endif
  129. #ifdef CONFIG_PM_PROFILING
  130. #define WITH_PROFILING
  131. #endif
  132. #ifdef WITH_PROFILING
  133. static inline pm_time_t IRAM_ATTR pm_get_time(void)
  134. {
  135. return esp_timer_get_time();
  136. }
  137. #endif // WITH_PROFILING
  138. #ifdef __cplusplus
  139. }
  140. #endif