esp_pmu.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdbool.h>
  8. #include <stddef.h>
  9. #include <stdint.h>
  10. #include <stdlib.h>
  11. #include "soc/soc_caps.h"
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. #if SOC_PMU_SUPPORTED
  16. #include "hal/pmu_hal.h"
  17. #include "pmu_param.h"
  18. #define RTC_SLEEP_PD_DIG PMU_SLEEP_PD_TOP //!< Deep sleep (power down digital domain, includes all power domains
  19. // except CPU, Modem, LP peripheral, AON,VDDSDIO, MEM and clock power domains)
  20. #define RTC_SLEEP_PD_RTC_PERIPH PMU_SLEEP_PD_LP_PERIPH //!< Power down RTC peripherals
  21. #define RTC_SLEEP_PD_VDDSDIO PMU_SLEEP_PD_VDDSDIO //!< Power down VDDSDIO regulator
  22. #define RTC_SLEEP_PD_CPU PMU_SLEEP_PD_CPU //!< Power down CPU when in lightsleep, but not restart
  23. #define RTC_SLEEP_PD_DIG_PERIPH PMU_SLEEP_PD_HP_PERIPH //!< Power down DIG peripherals
  24. #define RTC_SLEEP_PD_INT_8M PMU_SLEEP_PD_RC_FAST //!< Power down Internal 20M oscillator
  25. #define RTC_SLEEP_PD_XTAL PMU_SLEEP_PD_XTAL //!< Power down main XTAL
  26. #define RTC_SLEEP_PD_MODEM PMU_SLEEP_PD_MODEM //!< Power down modem(include wifi, ble and 15.4)
  27. //These flags are not power domains, but will affect some sleep parameters
  28. #define RTC_SLEEP_DIG_USE_8M BIT(16)
  29. #define RTC_SLEEP_USE_ADC_TESEN_MONITOR BIT(17)
  30. #define RTC_SLEEP_NO_ULTRA_LOW BIT(18) //!< Avoid using ultra low power in deep sleep, in which RTCIO cannot be used as input, and RTCMEM can't work under high temperature
  31. #if SOC_PM_SUPPORT_EXT0_WAKEUP
  32. #define RTC_EXT0_TRIG_EN PMU_EXT0_WAKEUP_EN //!< EXT0 wakeup
  33. #else
  34. #define RTC_EXT0_TRIG_EN 0
  35. #endif
  36. #if SOC_PM_SUPPORT_EXT1_WAKEUP
  37. #define RTC_EXT1_TRIG_EN PMU_EXT1_WAKEUP_EN //!< EXT1 wakeup
  38. #else
  39. #define RTC_EXT1_TRIG_EN 0
  40. #endif
  41. #define RTC_GPIO_TRIG_EN PMU_GPIO_WAKEUP_EN //!< GPIO wakeup
  42. #if SOC_LP_TIMER_SUPPORTED
  43. #define RTC_TIMER_TRIG_EN PMU_LP_TIMER_WAKEUP_EN //!< Timer wakeup
  44. #else
  45. #define RTC_TIMER_TRIG_EN 0
  46. #endif
  47. #if SOC_WIFI_SUPPORTED
  48. #define RTC_WIFI_TRIG_EN PMU_WIFI_SOC_WAKEUP_EN //!< WIFI wakeup (light sleep only)
  49. #else
  50. #define RTC_WIFI_TRIG_EN 0
  51. #endif
  52. #if SOC_UART_SUPPORT_WAKEUP_INT
  53. #define RTC_UART0_TRIG_EN PMU_UART0_WAKEUP_EN //!< UART0 wakeup (light sleep only)
  54. #define RTC_UART1_TRIG_EN PMU_UART1_WAKEUP_EN //!< UART1 wakeup (light sleep only)
  55. #else
  56. #define RTC_UART0_TRIG_EN 0
  57. #define RTC_UART1_TRIG_EN 0
  58. #endif
  59. #if SOC_BT_SUPPORTED
  60. #define RTC_BT_TRIG_EN PMU_BLE_SOC_WAKEUP_EN //!< BT wakeup (light sleep only)
  61. #else
  62. #define RTC_BT_TRIG_EN 0
  63. #endif
  64. #define RTC_USB_TRIG_EN PMU_USB_WAKEUP_EN
  65. #if SOC_LP_CORE_SUPPORTED
  66. #define RTC_LP_CORE_TRIG_EN PMU_LP_CORE_WAKEUP_EN //!< LP core wakeup
  67. #else
  68. #define RTC_LP_CORE_TRIG_EN 0
  69. #endif //SOC_LP_CORE_SUPPORTED
  70. #define RTC_XTAL32K_DEAD_TRIG_EN 0 // TODO
  71. #define RTC_BROWNOUT_DET_TRIG_EN 0 // TODO
  72. /**
  73. * RTC_SLEEP_REJECT_MASK records sleep reject sources supported by chip
  74. */
  75. #define RTC_SLEEP_REJECT_MASK (RTC_EXT0_TRIG_EN | \
  76. RTC_EXT1_TRIG_EN | \
  77. RTC_GPIO_TRIG_EN | \
  78. RTC_TIMER_TRIG_EN | \
  79. RTC_WIFI_TRIG_EN | \
  80. RTC_UART0_TRIG_EN | \
  81. RTC_UART1_TRIG_EN | \
  82. RTC_BT_TRIG_EN | \
  83. RTC_LP_CORE_TRIG_EN | \
  84. RTC_XTAL32K_DEAD_TRIG_EN | \
  85. RTC_USB_TRIG_EN | \
  86. RTC_BROWNOUT_DET_TRIG_EN)
  87. #if SOC_PM_SUPPORT_EXT0_WAKEUP
  88. #define PMU_EXT0_WAKEUP_EN BIT(0)
  89. #endif
  90. #if SOC_PM_SUPPORT_EXT1_WAKEUP
  91. #define PMU_EXT1_WAKEUP_EN BIT(1)
  92. #endif
  93. #define PMU_GPIO_WAKEUP_EN BIT(2)
  94. #define PMU_WIFI_BEACON_WAKEUP_EN BIT(3)
  95. #define PMU_LP_TIMER_WAKEUP_EN BIT(4)
  96. #define PMU_WIFI_SOC_WAKEUP_EN BIT(5)
  97. #define PMU_UART0_WAKEUP_EN BIT(6)
  98. #define PMU_UART1_WAKEUP_EN BIT(7)
  99. #define PMU_SDIO_WAKEUP_EN BIT(8)
  100. #define PMU_BLE_SOC_WAKEUP_EN BIT(10)
  101. #if SOC_LP_CORE_SUPPORTED
  102. #define PMU_LP_CORE_WAKEUP_EN BIT(11)
  103. #endif //SOC_LP_CORE_SUPPORTED
  104. #define PMU_USB_WAKEUP_EN BIT(14)
  105. #define PMU_SLEEP_PD_TOP BIT(0)
  106. #define PMU_SLEEP_PD_VDDSDIO BIT(1)
  107. #define PMU_SLEEP_PD_MODEM BIT(2)
  108. #define PMU_SLEEP_PD_HP_PERIPH BIT(3)
  109. #define PMU_SLEEP_PD_CPU BIT(4)
  110. #if SOC_PM_SUPPORT_HP_AON_PD
  111. #define PMU_SLEEP_PD_HP_AON BIT(5)
  112. #endif
  113. #define PMU_SLEEP_PD_MEM_G0 BIT(6)
  114. #define PMU_SLEEP_PD_MEM_G1 BIT(7)
  115. #define PMU_SLEEP_PD_MEM_G2 BIT(8)
  116. #define PMU_SLEEP_PD_MEM_G3 BIT(9)
  117. #define PMU_SLEEP_PD_MEM (PMU_SLEEP_PD_MEM_G0|PMU_SLEEP_PD_MEM_G1|PMU_SLEEP_PD_MEM_G2|PMU_SLEEP_PD_MEM_G3)
  118. #define PMU_SLEEP_PD_XTAL BIT(10)
  119. #define PMU_SLEEP_PD_RC_FAST BIT(11)
  120. #define PMU_SLEEP_PD_XTAL32K BIT(12)
  121. #define PMU_SLEEP_PD_RC32K BIT(13)
  122. #define PMU_SLEEP_PD_LP_PERIPH BIT(14)
  123. typedef struct {
  124. pmu_hal_context_t *hal;
  125. void *mc;
  126. } pmu_context_t;
  127. pmu_context_t * PMU_instance(void);
  128. typedef enum pmu_hp_sysclk_src {
  129. PMU_HP_SYSCLK_XTAL = 0,
  130. PMU_HP_SYSCLK_PLL,
  131. PMU_HP_SYSCLK_FOSC
  132. } pmu_hp_sysclk_src_t;
  133. typedef enum pmu_sleep_protect_mode {
  134. PMU_SLEEP_PROTECT_HP_SLEEP = 0,
  135. PMU_SLEEP_PROTECT_XTAL,
  136. PMU_SLEEP_PROTECT_HP_LP_SLEEP,
  137. PMU_SLEEP_PROTECT_DISABLE
  138. } pmu_sleep_protect_mode_t;
  139. typedef enum pmu_sleep_regdma_entry {
  140. PMU_SLEEP_REGDMA_ENTRY_0 = 0,
  141. PMU_SLEEP_REGDMA_ENTRY_1,
  142. PMU_SLEEP_REGDMA_ENTRY_2,
  143. PMU_SLEEP_REGDMA_ENTRY_3,
  144. PMU_SLEEP_REGDMA_ENTRY_MAX
  145. } pmu_sleep_regdma_entry_t;
  146. /**
  147. * @brief PMU ICG modem code of HP system
  148. */
  149. typedef enum {
  150. PMU_HP_ICG_MODEM_CODE_SLEEP = 0,
  151. PMU_HP_ICG_MODEM_CODE_MODEM = 1,
  152. PMU_HP_ICG_MODEM_CODE_ACTIVE = 2,
  153. } pmu_hp_icg_modem_mode_t;
  154. /**
  155. * @brief Enable_regdma_backup.
  156. */
  157. void pmu_sleep_enable_regdma_backup(void);
  158. /**
  159. * @brief Disable_regdma_backup.
  160. */
  161. void pmu_sleep_disable_regdma_backup(void);
  162. /**
  163. * @brief Get sleep PLL enable status
  164. *
  165. * @return true if PLL is enabled by PMU in modem state
  166. */
  167. bool pmu_sleep_pll_already_enabled(void);
  168. /**
  169. * @brief Calculate the hardware time overhead during sleep to compensate for sleep time
  170. *
  171. * @param pd_flags flags indicates the power domain that will be powered down
  172. * @param slowclk_period re-calibrated slow clock period
  173. * @param fastclk_period re-calibrated fast clock period
  174. *
  175. * @return hardware time overhead in us
  176. */
  177. uint32_t pmu_sleep_calculate_hw_wait_time(uint32_t pd_flags, uint32_t slowclk_period, uint32_t fastclk_period);
  178. /**
  179. * @brief Get default sleep configuration
  180. * @param config pmu_sleep_config instance
  181. * @param pd_flags flags indicates the power domain that will be powered down
  182. * @param adjustment total software and hardware time overhead
  183. * @param slowclk_period re-calibrated slow clock period in microseconds,
  184. * Q13.19 fixed point format
  185. * @param fastclk_period re-calibrated fast clock period in microseconds,
  186. * Q13.19 fixed point format
  187. * @param dslp configuration for deep sleep mode
  188. * @return hardware time overhead in us
  189. */
  190. const pmu_sleep_config_t* pmu_sleep_config_default(pmu_sleep_config_t *config, uint32_t pd_flags, uint32_t adjustment, uint32_t slowclk_period, uint32_t fastclk_period, bool dslp);
  191. /**
  192. * @brief Prepare the chip to enter sleep mode
  193. *
  194. * This function configures various power/analog parameters and lp/lp system configuration
  195. * used in sleep state machines
  196. *
  197. * This function does not actually enter sleep mode; this is done using
  198. * pmu_sleep_start function. Software may do some other actions between
  199. * pmu_sleep_init and pmu_sleep_start, such as set wakeup timer and configure
  200. * wakeup sources.
  201. *
  202. * @param config sleep mode configuration
  203. *
  204. * @param dslp is initialize for deep sleep mode
  205. */
  206. void pmu_sleep_init(const pmu_sleep_config_t *config, bool dslp);
  207. /**
  208. * @brief Enter deep or light sleep mode
  209. *
  210. * This function enters the sleep mode previously configured using pmu_sleep_init
  211. * function. Before entering sleep, software should configure wake up sources
  212. * appropriately (set up GPIO wakeup registers, timer wakeup registers,
  213. * and so on).
  214. *
  215. * If deep sleep mode was configured using pmu_sleep_init, and sleep is not
  216. * rejected by hardware (based on reject_opt flags), this function never returns.
  217. * When the chip wakes up from deep sleep, CPU is reset and execution starts
  218. * from ROM bootloader.
  219. *
  220. * If light sleep mode was configured using pmu_sleep_init, this function
  221. * returns on wakeup, or if sleep is rejected by hardware.
  222. *
  223. * @param wakeup_opt bit mask wake up reasons to enable (RTC_xxx_TRIG_EN flags
  224. * combined with OR)
  225. * @param reject_opt bit mask of sleep reject reasons, used to
  226. * prevent wakeup source set before the sleep request)
  227. * @param lslp_mem_inf_fpu If non-zero then the low power config is restored
  228. * immediately on wake. Recommended for light sleep,
  229. * has no effect if the system goes into deep sleep.
  230. *
  231. * @return non-zero if sleep was rejected by hardware
  232. */
  233. uint32_t pmu_sleep_start(uint32_t wakeup_opt, uint32_t reject_opt, uint32_t lslp_mem_inf_fpu, bool dslp);
  234. /**
  235. * @brief Finish sleep process settings and get sleep reject status
  236. * @return return sleep reject status
  237. */
  238. bool pmu_sleep_finish(void);
  239. /**
  240. * @brief Initialize PMU related power/clock/digital parameters and functions
  241. */
  242. void pmu_init(void);
  243. /**
  244. * @brief Enable or disable system clock in PMU HP sleep state
  245. *
  246. * This API only used for fix BLE 40 MHz low power clock source issue
  247. *
  248. * @param enable true to enable, false to disable
  249. */
  250. void pmu_sleep_enable_hp_sleep_sysclk(bool enable);
  251. #endif //#if SOC_PMU_SUPPORTED
  252. #ifdef __cplusplus
  253. }
  254. #endif