pm_impl.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. /**
  26. * This is an enum of possible power modes supported by the implementation
  27. */
  28. typedef enum {
  29. PM_MODE_LIGHT_SLEEP,//!< Light sleep
  30. PM_MODE_APB_MIN, //!< Idle (no CPU frequency or APB frequency locks)
  31. PM_MODE_APB_MAX, //!< Maximum APB frequency mode
  32. PM_MODE_CPU_MAX, //!< Maximum CPU frequency mode
  33. PM_MODE_COUNT //!< Number of items
  34. } pm_mode_t;
  35. /**
  36. * @brief Get the mode corresponding to a certain lock
  37. * @param type lock type
  38. * @param arg argument value for this lock (passed to esp_pm_lock_create)
  39. * @return lowest power consumption mode which meets the constraints of the lock
  40. */
  41. pm_mode_t esp_pm_impl_get_mode(esp_pm_lock_type_t type, int arg);
  42. /**
  43. * If profiling is enabled, this data type will be used to store microsecond
  44. * timestamps.
  45. */
  46. typedef int64_t pm_time_t;
  47. /**
  48. * See \ref esp_pm_impl_switch_mode
  49. */
  50. typedef enum {
  51. MODE_LOCK,
  52. MODE_UNLOCK
  53. } pm_mode_switch_t;
  54. /**
  55. * @brief Switch between power modes when lock is taken or released
  56. * @param mode pm_mode_t corresponding to the lock being taken or released,
  57. * as returned by \ref esp_pm_impl_get_mode
  58. * @param lock_or_unlock
  59. * - MODE_LOCK: lock was taken. Implementation needs to make sure
  60. * that the constraints of the lock are met by switching to the
  61. * given 'mode' or any of the higher power ones.
  62. * - MODE_UNLOCK: lock was released. If all the locks for given
  63. * mode are released, and no locks for higher power modes are
  64. * taken, implementation can switch to one of lower power modes.
  65. * @param now timestamp when the lock was taken or released. Passed as
  66. * a minor optimization, so that the implementation does not need to
  67. * call pm_get_time again.
  68. */
  69. void esp_pm_impl_switch_mode(pm_mode_t mode, pm_mode_switch_t lock_or_unlock, pm_time_t now);
  70. /**
  71. * @brief Call once at startup to initialize pm implementation
  72. */
  73. void esp_pm_impl_init();
  74. /**
  75. * @brief Hook function for the idle task
  76. * Must be called from the IDLE task on each CPU before entering waiti state.
  77. */
  78. void esp_pm_impl_idle_hook();
  79. /**
  80. * @brief Hook function for the interrupt dispatcher
  81. * Must be called soon after entering the ISR
  82. */
  83. void esp_pm_impl_isr_hook();
  84. /**
  85. * @brief Dump the information about time spent in each of the pm modes.
  86. *
  87. * Prints three columns:
  88. * mode name, total time in mode (in microseconds), percentage of time in mode
  89. *
  90. * @param out stream to dump the information to
  91. */
  92. void esp_pm_impl_dump_stats(FILE* out);
  93. /**
  94. * @brief Hook function implementing `waiti` instruction, should be invoked from idle task context
  95. */
  96. void esp_pm_impl_waiti();
  97. #ifdef CONFIG_PM_PROFILING
  98. #define WITH_PROFILING
  99. #endif
  100. #ifdef WITH_PROFILING
  101. static inline pm_time_t IRAM_ATTR pm_get_time()
  102. {
  103. return esp_timer_get_time();
  104. }
  105. #endif // WITH_PROFILING