rtc.h 9.2 KB

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