rtc.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdbool.h>
  8. #include <stdint.h>
  9. #include "esp_assert.h"
  10. #include "soc/soc.h"
  11. #include "soc/lp_aon_reg.h"
  12. #include "soc/reset_reasons.h"
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. /** \defgroup rtc_apis, rtc registers and memory related apis
  17. * @brief rtc apis
  18. */
  19. /** @addtogroup rtc_apis
  20. * @{
  21. */
  22. /**************************************************************************************
  23. * Note: *
  24. * Some Rtc memory and registers are used, in ROM or in internal library. *
  25. * Please do not use reserved or used rtc memory or registers. *
  26. * *
  27. *************************************************************************************
  28. * LP Memory & Store Register usage
  29. *************************************************************************************
  30. * rtc memory addr type size usage
  31. * 0x3f421000(0x50000000) Slow SIZE_CP Co-Processor code/Reset Entry
  32. * 0x3f421000+SIZE_CP Slow 8192-SIZE_CP
  33. *
  34. * 0x3ff80000(0x40070000) Fast 8192 deep sleep entry code
  35. *
  36. *************************************************************************************
  37. * RTC store registers usage
  38. * LP_AON_STORE0_REG Reserved
  39. * LP_AON_STORE1_REG RTC_SLOW_CLK calibration value
  40. * LP_AON_STORE2_REG Boot time, low word
  41. * LP_AON_STORE3_REG Boot time, high word
  42. * LP_AON_STORE4_REG External XTAL frequency
  43. * LP_AON_STORE5_REG FAST_RTC_MEMORY_LENGTH
  44. * LP_AON_STORE6_REG FAST_RTC_MEMORY_ENTRY
  45. * LP_AON_STORE7_REG FAST_RTC_MEMORY_CRC
  46. * LP_AON_STORE8_REG Store light sleep wake stub addr
  47. * LP_AON_STORE9_REG Store the sleep mode at bit[0] (0:light sleep 1:deep sleep)
  48. *************************************************************************************
  49. */
  50. #define RTC_SLOW_CLK_CAL_REG LP_AON_STORE1_REG
  51. #define RTC_BOOT_TIME_LOW_REG LP_AON_STORE2_REG
  52. #define RTC_BOOT_TIME_HIGH_REG LP_AON_STORE3_REG
  53. #define RTC_XTAL_FREQ_REG LP_AON_STORE4_REG
  54. #define RTC_ENTRY_LENGTH_REG LP_AON_STORE5_REG
  55. #define RTC_ENTRY_ADDR_REG LP_AON_STORE6_REG
  56. #define RTC_RESET_CAUSE_REG LP_AON_STORE6_REG
  57. #define RTC_MEMORY_CRC_REG LP_AON_STORE7_REG
  58. #define LIGHT_SLEEP_WAKE_STUB_ADDR_REG LP_AON_STORE8_REG
  59. #define SLEEP_MODE_REG LP_AON_STORE9_REG
  60. #define RTC_DISABLE_ROM_LOG ((1 << 0) | (1 << 16)) //!< Disable logging from the ROM code.
  61. typedef enum {
  62. AWAKE = 0, //<CPU ON
  63. LIGHT_SLEEP = BIT0, //CPU waiti, PLL ON. We don't need explicitly set this mode.
  64. DEEP_SLEEP = BIT1 //CPU OFF, PLL OFF, only specific timer could wake up
  65. } SLEEP_MODE;
  66. typedef enum {
  67. NO_MEAN = 0,
  68. POWERON_RESET = 1, /**<1, Vbat power on reset*/
  69. RTC_SW_SYS_RESET = 3, /**<3, Software reset digital core (hp system)*/
  70. DEEPSLEEP_RESET = 5, /**<5, Deep Sleep reset digital core (hp system)*/
  71. SDIO_RESET = 6, /**<6, Reset by SLC module, reset digital core (hp system)*/
  72. TG0WDT_SYS_RESET = 7, /**<7, Timer Group0 Watch dog reset digital core (hp system)*/
  73. TG1WDT_SYS_RESET = 8, /**<8, Timer Group1 Watch dog reset digital core (hp system)*/
  74. RTCWDT_SYS_RESET = 9, /**<9, RTC Watch dog Reset digital core (hp system)*/
  75. TG0WDT_CPU_RESET = 11, /**<11, Time Group0 reset CPU*/
  76. RTC_SW_CPU_RESET = 12, /**<12, Software reset CPU*/
  77. RTCWDT_CPU_RESET = 13, /**<13, RTC Watch dog Reset CPU*/
  78. RTCWDT_BROWN_OUT_RESET = 15, /**<15, Reset when the vdd voltage is not stable*/
  79. RTCWDT_RTC_RESET = 16, /**<16, RTC Watch dog reset digital core and rtc module*/
  80. TG1WDT_CPU_RESET = 17, /**<17, Time Group1 reset CPU*/
  81. SUPER_WDT_RESET = 18, /**<18, super watchdog reset digital core and rtc module*/
  82. EFUSE_RESET = 20, /**<20, efuse reset digital core (hp system)*/
  83. USB_UART_CHIP_RESET = 21, /**<21, usb uart reset digital core (hp system)*/
  84. USB_JTAG_CHIP_RESET = 22, /**<22, usb jtag reset digital core (hp system)*/
  85. JTAG_RESET = 24, /**<24, jtag reset CPU*/
  86. } RESET_REASON;
  87. // Check if the reset reason defined in ROM is compatible with soc/reset_reasons.h
  88. ESP_STATIC_ASSERT((soc_reset_reason_t)POWERON_RESET == RESET_REASON_CHIP_POWER_ON, "POWERON_RESET != RESET_REASON_CHIP_POWER_ON");
  89. ESP_STATIC_ASSERT((soc_reset_reason_t)RTC_SW_SYS_RESET == RESET_REASON_CORE_SW, "RTC_SW_SYS_RESET != RESET_REASON_CORE_SW");
  90. ESP_STATIC_ASSERT((soc_reset_reason_t)DEEPSLEEP_RESET == RESET_REASON_CORE_DEEP_SLEEP, "DEEPSLEEP_RESET != RESET_REASON_CORE_DEEP_SLEEP");
  91. ESP_STATIC_ASSERT((soc_reset_reason_t)SDIO_RESET == RESET_REASON_CORE_SDIO, "SDIO_RESET != RESET_REASON_CORE_SDIO");
  92. ESP_STATIC_ASSERT((soc_reset_reason_t)TG0WDT_SYS_RESET == RESET_REASON_CORE_MWDT0, "TG0WDT_SYS_RESET != RESET_REASON_CORE_MWDT0");
  93. ESP_STATIC_ASSERT((soc_reset_reason_t)TG1WDT_SYS_RESET == RESET_REASON_CORE_MWDT1, "TG1WDT_SYS_RESET != RESET_REASON_CORE_MWDT1");
  94. ESP_STATIC_ASSERT((soc_reset_reason_t)RTCWDT_SYS_RESET == RESET_REASON_CORE_RTC_WDT, "RTCWDT_SYS_RESET != RESET_REASON_CORE_RTC_WDT");
  95. ESP_STATIC_ASSERT((soc_reset_reason_t)TG0WDT_CPU_RESET == RESET_REASON_CPU0_MWDT0, "TG0WDT_CPU_RESET != RESET_REASON_CPU0_MWDT0");
  96. ESP_STATIC_ASSERT((soc_reset_reason_t)RTC_SW_CPU_RESET == RESET_REASON_CPU0_SW, "RTC_SW_CPU_RESET != RESET_REASON_CPU0_SW");
  97. ESP_STATIC_ASSERT((soc_reset_reason_t)RTCWDT_CPU_RESET == RESET_REASON_CPU0_RTC_WDT, "RTCWDT_CPU_RESET != RESET_REASON_CPU0_RTC_WDT");
  98. ESP_STATIC_ASSERT((soc_reset_reason_t)RTCWDT_BROWN_OUT_RESET == RESET_REASON_SYS_BROWN_OUT, "RTCWDT_BROWN_OUT_RESET != RESET_REASON_SYS_BROWN_OUT");
  99. ESP_STATIC_ASSERT((soc_reset_reason_t)RTCWDT_RTC_RESET == RESET_REASON_SYS_RTC_WDT, "RTCWDT_RTC_RESET != RESET_REASON_SYS_RTC_WDT");
  100. ESP_STATIC_ASSERT((soc_reset_reason_t)TG1WDT_CPU_RESET == RESET_REASON_CPU0_MWDT1, "TG1WDT_CPU_RESET != RESET_REASON_CPU0_MWDT1");
  101. ESP_STATIC_ASSERT((soc_reset_reason_t)SUPER_WDT_RESET == RESET_REASON_SYS_SUPER_WDT, "SUPER_WDT_RESET != RESET_REASON_SYS_SUPER_WDT");
  102. ESP_STATIC_ASSERT((soc_reset_reason_t)EFUSE_RESET == RESET_REASON_CORE_EFUSE_CRC, "EFUSE_RESET != RESET_REASON_CORE_EFUSE_CRC");
  103. ESP_STATIC_ASSERT((soc_reset_reason_t)USB_UART_CHIP_RESET == RESET_REASON_CORE_USB_UART, "USB_UART_CHIP_RESET != RESET_REASON_CORE_USB_UART");
  104. ESP_STATIC_ASSERT((soc_reset_reason_t)USB_JTAG_CHIP_RESET == RESET_REASON_CORE_USB_JTAG, "USB_JTAG_CHIP_RESET != RESET_REASON_CORE_USB_JTAG");
  105. ESP_STATIC_ASSERT((soc_reset_reason_t)JTAG_RESET == RESET_REASON_CPU0_JTAG, "JTAG_RESET != RESET_REASON_CPU0_JTAG");
  106. typedef enum {
  107. NO_SLEEP = 0,
  108. EXT_EVENT0_TRIG = BIT0,
  109. EXT_EVENT1_TRIG = BIT1,
  110. GPIO_TRIG = BIT2,
  111. TIMER_EXPIRE = BIT3,
  112. SDIO_TRIG = BIT4,
  113. MAC_TRIG = BIT5,
  114. UART0_TRIG = BIT6,
  115. UART1_TRIG = BIT7,
  116. TOUCH_TRIG = BIT8,
  117. SAR_TRIG = BIT9,
  118. BT_TRIG = BIT10,
  119. RISCV_TRIG = BIT11,
  120. XTAL_DEAD_TRIG = BIT12,
  121. RISCV_TRAP_TRIG = BIT13,
  122. USB_TRIG = BIT14
  123. } WAKEUP_REASON;
  124. typedef enum {
  125. DISEN_WAKEUP = NO_SLEEP,
  126. EXT_EVENT0_TRIG_EN = EXT_EVENT0_TRIG,
  127. EXT_EVENT1_TRIG_EN = EXT_EVENT1_TRIG,
  128. GPIO_TRIG_EN = GPIO_TRIG,
  129. TIMER_EXPIRE_EN = TIMER_EXPIRE,
  130. SDIO_TRIG_EN = SDIO_TRIG,
  131. MAC_TRIG_EN = MAC_TRIG,
  132. UART0_TRIG_EN = UART0_TRIG,
  133. UART1_TRIG_EN = UART1_TRIG,
  134. TOUCH_TRIG_EN = TOUCH_TRIG,
  135. SAR_TRIG_EN = SAR_TRIG,
  136. BT_TRIG_EN = BT_TRIG,
  137. RISCV_TRIG_EN = RISCV_TRIG,
  138. XTAL_DEAD_TRIG_EN = XTAL_DEAD_TRIG,
  139. RISCV_TRAP_TRIG_EN = RISCV_TRAP_TRIG,
  140. USB_TRIG_EN = USB_TRIG
  141. } WAKEUP_ENABLE;
  142. /**
  143. * @brief Get the reset reason for CPU.
  144. *
  145. * @param int cpu_no : CPU no.
  146. *
  147. * @return RESET_REASON
  148. */
  149. RESET_REASON rtc_get_reset_reason(int cpu_no);
  150. /**
  151. * @brief Get the wakeup cause for CPU.
  152. *
  153. * @param int cpu_no : CPU no.
  154. *
  155. * @return WAKEUP_REASON
  156. */
  157. WAKEUP_REASON rtc_get_wakeup_cause(void);
  158. typedef void (* esp_rom_wake_func_t)(void);
  159. /**
  160. * @brief Read stored RTC wake function address
  161. *
  162. * Returns pointer to wake address if a value is set in RTC registers, and stored length & CRC all valid.
  163. * valid means that both stored stub length and stored wake function address are four-byte aligned non-zero values
  164. * and the crc check passes
  165. *
  166. * @param None
  167. *
  168. * @return esp_rom_wake_func_t : Returns pointer to wake address if a value is set in RTC registers
  169. */
  170. esp_rom_wake_func_t esp_rom_get_rtc_wake_addr(void);
  171. /**
  172. * @brief Store new RTC wake function address
  173. *
  174. * Set a new RTC wake address function. If a non-NULL function pointer is set then the function
  175. * memory is calculated and stored also.
  176. *
  177. * @param entry_addr Address of function. should be 4-bytes aligned otherwise it will not start from the stub after wake from deepsleep,
  178. * if NULL length will be ignored and all registers are cleared to 0.
  179. *
  180. * @param length length of function in RTC fast memory. should be less than RTC Fast memory size and aligned to 4-bytes.
  181. * otherwise all registers are cleared to 0.
  182. *
  183. * @return None
  184. */
  185. void esp_rom_set_rtc_wake_addr(esp_rom_wake_func_t entry_addr, size_t length);
  186. /**
  187. * @brief Suppress ROM log by setting specific RTC control register.
  188. * @note This is not a permanent disable of ROM logging since the RTC register can not retain after chip reset.
  189. *
  190. * @param None
  191. *
  192. * @return None
  193. */
  194. static inline void rtc_suppress_rom_log(void)
  195. {
  196. /* To disable logging in the ROM, only the least significant bit of the register is used,
  197. * but since this register is also used to store the frequency of the main crystal (RTC_XTAL_FREQ_REG),
  198. * you need to write to this register in the same format.
  199. * Namely, the upper 16 bits and lower should be the same.
  200. */
  201. REG_SET_BIT(LP_AON_STORE4_REG, RTC_DISABLE_ROM_LOG);
  202. }
  203. /**
  204. * @brief Software Reset digital core.
  205. *
  206. * It is not recommended to use this function in esp-idf, use
  207. * esp_restart() instead.
  208. *
  209. * @param None
  210. *
  211. * @return None
  212. */
  213. void software_reset(void);
  214. /**
  215. * @brief Software Reset digital core.
  216. *
  217. * It is not recommended to use this function in esp-idf, use
  218. * esp_restart() instead.
  219. *
  220. * @param int cpu_no : The CPU to reset, 0 for PRO CPU, 1 for APP CPU.
  221. *
  222. * @return None
  223. */
  224. void software_reset_cpu(int cpu_no);
  225. /**
  226. * @}
  227. */
  228. #ifdef __cplusplus
  229. }
  230. #endif