pm_impl.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * SPDX-FileCopyrightText: 2016-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. /**
  8. * @file esp_private/pm_impl.h
  9. *
  10. * This header file defines interface between PM lock functions (pm_locks.c)
  11. * and the chip-specific power management (DFS/light sleep) implementation.
  12. */
  13. #include "soc/rtc.h"
  14. #include "esp_pm.h"
  15. #include "esp_timer.h"
  16. #include "sdkconfig.h"
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. /**
  21. * This is an enum of possible power modes supported by the implementation
  22. */
  23. typedef enum {
  24. PM_MODE_LIGHT_SLEEP,//!< Light sleep
  25. PM_MODE_APB_MIN, //!< Idle (no CPU frequency or APB frequency locks)
  26. PM_MODE_APB_MAX, //!< Maximum APB frequency mode
  27. PM_MODE_CPU_MAX, //!< Maximum CPU frequency mode
  28. PM_MODE_COUNT //!< Number of items
  29. } pm_mode_t;
  30. /**
  31. * @brief Get the mode corresponding to a certain lock
  32. * @param type lock type
  33. * @param arg argument value for this lock (passed to esp_pm_lock_create)
  34. * @return lowest power consumption mode which meets the constraints of the lock
  35. */
  36. pm_mode_t esp_pm_impl_get_mode(esp_pm_lock_type_t type, int arg);
  37. /**
  38. * @brief Get CPU clock frequency by power mode
  39. * @param mode power mode
  40. * @return CPU clock frequency
  41. */
  42. int esp_pm_impl_get_cpu_freq(pm_mode_t mode);
  43. /**
  44. * If profiling is enabled, this data type will be used to store microsecond
  45. * timestamps.
  46. */
  47. typedef int64_t pm_time_t;
  48. /**
  49. * See \ref esp_pm_impl_switch_mode
  50. */
  51. typedef enum {
  52. MODE_LOCK,
  53. MODE_UNLOCK
  54. } pm_mode_switch_t;
  55. /**
  56. * @brief Switch between power modes when lock is taken or released
  57. * @param mode pm_mode_t corresponding to the lock being taken or released,
  58. * as returned by \ref esp_pm_impl_get_mode
  59. * @param lock_or_unlock
  60. * - MODE_LOCK: lock was taken. Implementation needs to make sure
  61. * that the constraints of the lock are met by switching to the
  62. * given 'mode' or any of the higher power ones.
  63. * - MODE_UNLOCK: lock was released. If all the locks for given
  64. * mode are released, and no locks for higher power modes are
  65. * taken, implementation can switch to one of lower power modes.
  66. * @param now timestamp when the lock was taken or released. Passed as
  67. * a minor optimization, so that the implementation does not need to
  68. * call pm_get_time again.
  69. */
  70. void esp_pm_impl_switch_mode(pm_mode_t mode, pm_mode_switch_t lock_or_unlock, pm_time_t now);
  71. /**
  72. * @brief Call once at startup to initialize pm implementation
  73. */
  74. void esp_pm_impl_init(void);
  75. /**
  76. * @brief Hook function for the idle task
  77. * Must be called from the IDLE task on each CPU before entering waiti state.
  78. */
  79. void esp_pm_impl_idle_hook(void);
  80. /**
  81. * @brief Hook function for the interrupt dispatcher
  82. * Must be called soon after entering the ISR
  83. */
  84. void esp_pm_impl_isr_hook(void);
  85. /**
  86. * @brief Dump the information about time spent in each of the pm modes.
  87. *
  88. * Prints three columns:
  89. * mode name, total time in mode (in microseconds), percentage of time in mode
  90. *
  91. * @param out stream to dump the information to
  92. */
  93. void esp_pm_impl_dump_stats(FILE* out);
  94. /**
  95. * @brief Hook function implementing `waiti` instruction, should be invoked from idle task context
  96. */
  97. void esp_pm_impl_waiti(void);
  98. /**
  99. * @brief Callback function type for peripherals to skip light sleep.
  100. *
  101. */
  102. typedef bool (* skip_light_sleep_cb_t)(void);
  103. /**
  104. * @brief Register peripherals skip light sleep callback
  105. *
  106. * This function allows you to register a callback that gets the result
  107. * that if light sleep should be skipped by peripherals.
  108. * @param cb function to get the result
  109. * @return
  110. * - ESP_OK on success
  111. * - ESP_ERR_NO_MEM if no more callback slots are available
  112. */
  113. esp_err_t esp_pm_register_skip_light_sleep_callback(skip_light_sleep_cb_t cb);
  114. /**
  115. * @brief Unregisterperipherals skip light sleep callback
  116. *
  117. * This function allows you to unregister a callback which was previously
  118. * registered using esp_register_skip_light_sleep_callback.
  119. * @param cb function to get the result
  120. * @return
  121. * - ESP_OK on success
  122. * - ESP_ERR_INVALID_STATE if the given callback hasn't been registered before
  123. */
  124. esp_err_t esp_pm_unregister_skip_light_sleep_callback(skip_light_sleep_cb_t cb);
  125. #ifdef CONFIG_PM_PROFILING
  126. #define WITH_PROFILING
  127. #endif
  128. #ifdef WITH_PROFILING
  129. static inline pm_time_t IRAM_ATTR pm_get_time(void)
  130. {
  131. return esp_timer_get_time();
  132. }
  133. #endif // WITH_PROFILING
  134. #ifdef __cplusplus
  135. }
  136. #endif