rtc.h 8.9 KB

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