rtc_wdt.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * SPDX-FileCopyrightText: 2018-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /* Recommendation of using API RTC_WDT.
  7. 1) Setting and enabling rtc_wdt:
  8. @code
  9. rtc_wdt_protect_off();
  10. rtc_wdt_disable();
  11. rtc_wdt_set_length_of_reset_signal(RTC_WDT_SYS_RESET_SIG, RTC_WDT_LENGTH_3_2us);
  12. rtc_wdt_set_stage(RTC_WDT_STAGE0, RTC_WDT_STAGE_ACTION_RESET_SYSTEM); //RTC_WDT_STAGE_ACTION_RESET_SYSTEM or RTC_WDT_STAGE_ACTION_RESET_RTC
  13. rtc_wdt_set_time(RTC_WDT_STAGE0, 7000); // timeout rtd_wdt 7000ms.
  14. rtc_wdt_enable();
  15. rtc_wdt_protect_on();
  16. @endcode
  17. * If you use this option RTC_WDT_STAGE_ACTION_RESET_SYSTEM then after reset you can see these messages.
  18. They can help to understand where the CPUs were when the WDT was triggered.
  19. W (30) boot: PRO CPU has been reset by WDT.
  20. W (30) boot: WDT reset info: PRO CPU PC=0x400xxxxx
  21. ... function where it happened
  22. W (31) boot: WDT reset info: APP CPU PC=0x400xxxxx
  23. ... function where it happened
  24. * If you use this option RTC_WDT_STAGE_ACTION_RESET_RTC then you will see message (rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT))
  25. without description where were CPUs when it happened.
  26. 2) Reset counter of rtc_wdt:
  27. @code
  28. rtc_wdt_feed();
  29. @endcode
  30. 3) Disable rtc_wdt:
  31. @code
  32. rtc_wdt_disable();
  33. @endcode
  34. */
  35. #pragma once
  36. #include <stdint.h>
  37. #include <stdbool.h>
  38. #include "soc/rtc_periph.h"
  39. #include "esp_err.h"
  40. #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
  41. #ifdef __cplusplus
  42. extern "C"
  43. {
  44. #endif
  45. /// List of stage of rtc watchdog. WDT has 4 stage.
  46. typedef enum {
  47. RTC_WDT_STAGE0 = 0, /*!< Stage 0 */
  48. RTC_WDT_STAGE1 = 1, /*!< Stage 1 */
  49. RTC_WDT_STAGE2 = 2, /*!< Stage 2 */
  50. RTC_WDT_STAGE3 = 3 /*!< Stage 3 */
  51. } rtc_wdt_stage_t;
  52. /// List of action. When the time of stage expires this action will be triggered.
  53. typedef enum {
  54. RTC_WDT_STAGE_ACTION_OFF = RTC_WDT_STG_SEL_OFF, /*!< Disabled. This stage will have no effects on the system. */
  55. RTC_WDT_STAGE_ACTION_INTERRUPT = RTC_WDT_STG_SEL_INT, /*!< Trigger an interrupt. When the stage expires an interrupt is triggered. */
  56. RTC_WDT_STAGE_ACTION_RESET_CPU = RTC_WDT_STG_SEL_RESET_CPU, /*!< Reset a CPU core. */
  57. RTC_WDT_STAGE_ACTION_RESET_SYSTEM = RTC_WDT_STG_SEL_RESET_SYSTEM, /*!< Reset the main system includes the CPU and all peripherals. The RTC is an exception to this, and it will not be reset. */
  58. RTC_WDT_STAGE_ACTION_RESET_RTC = RTC_WDT_STG_SEL_RESET_RTC /*!< Reset the main system and the RTC. */
  59. } rtc_wdt_stage_action_t;
  60. /// Type of reset signal
  61. typedef enum {
  62. RTC_WDT_SYS_RESET_SIG = 0, /*!< System reset signal length selection */
  63. RTC_WDT_CPU_RESET_SIG = 1 /*!< CPU reset signal length selection */
  64. } rtc_wdt_reset_sig_t;
  65. /// Length of reset signal
  66. typedef enum {
  67. RTC_WDT_LENGTH_100ns = 0, /*!< 100 ns */
  68. RTC_WDT_LENGTH_200ns = 1, /*!< 200 ns */
  69. RTC_WDT_LENGTH_300ns = 2, /*!< 300 ns */
  70. RTC_WDT_LENGTH_400ns = 3, /*!< 400 ns */
  71. RTC_WDT_LENGTH_500ns = 4, /*!< 500 ns */
  72. RTC_WDT_LENGTH_800ns = 5, /*!< 800 ns */
  73. RTC_WDT_LENGTH_1_6us = 6, /*!< 1.6 us */
  74. RTC_WDT_LENGTH_3_2us = 7 /*!< 3.2 us */
  75. } rtc_wdt_length_sig_t;
  76. /**
  77. * @brief Get status of protect of rtc_wdt.
  78. *
  79. * @return
  80. * - True if the protect of RTC_WDT is set
  81. */
  82. bool rtc_wdt_get_protect_status(void);
  83. /**
  84. * @brief Set protect of rtc_wdt.
  85. */
  86. void rtc_wdt_protect_on(void);
  87. /**
  88. * @brief Reset protect of rtc_wdt.
  89. */
  90. void rtc_wdt_protect_off(void);
  91. /**
  92. * @brief Enable rtc_wdt.
  93. */
  94. void rtc_wdt_enable(void);
  95. /**
  96. * @brief Enable the flash boot protection procedure for WDT.
  97. *
  98. * Do not recommend to use it in the app.
  99. * This function was added to be compatibility with the old bootloaders.
  100. * This mode is disabled in bootloader or using rtc_wdt_disable() function.
  101. */
  102. void rtc_wdt_flashboot_mode_enable(void);
  103. /**
  104. * @brief Disable rtc_wdt.
  105. */
  106. void rtc_wdt_disable(void);
  107. /**
  108. * @brief Reset counter rtc_wdt.
  109. *
  110. * It returns to stage 0 and its expiry counter restarts from 0.
  111. */
  112. void rtc_wdt_feed(void);
  113. /**
  114. * @brief Set time for required stage.
  115. *
  116. * @param[in] stage Stage of rtc_wdt.
  117. * @param[in] timeout_ms Timeout for this stage.
  118. *
  119. * @return
  120. * - ESP_OK In case of success
  121. * - ESP_ERR_INVALID_ARG If stage has invalid value
  122. */
  123. esp_err_t rtc_wdt_set_time(rtc_wdt_stage_t stage, unsigned int timeout_ms);
  124. /**
  125. * @brief Get the timeout set for the required stage.
  126. *
  127. * @param[in] stage Stage of rtc_wdt.
  128. * @param[out] timeout_ms Timeout set for this stage. (not elapsed time).
  129. *
  130. * @return
  131. * - ESP_OK In case of success
  132. * - ESP_ERR_INVALID_ARG If stage has invalid value
  133. */
  134. esp_err_t rtc_wdt_get_timeout(rtc_wdt_stage_t stage, unsigned int* timeout_ms);
  135. /**
  136. * @brief Set an action for required stage.
  137. *
  138. * @param[in] stage Stage of rtc_wdt.
  139. * @param[in] stage_sel Action for this stage. When the time of stage expires this action will be triggered.
  140. *
  141. * @return
  142. * - ESP_OK In case of success
  143. * - ESP_ERR_INVALID_ARG If stage or stage_sel have invalid value
  144. */
  145. esp_err_t rtc_wdt_set_stage(rtc_wdt_stage_t stage, rtc_wdt_stage_action_t stage_sel);
  146. /**
  147. * @brief Set a length of reset signal.
  148. *
  149. * @param[in] reset_src Type of reset signal.
  150. * @param[in] reset_signal_length A length of reset signal.
  151. *
  152. * @return
  153. * - ESP_OK In case of success
  154. * - ESP_ERR_INVALID_ARG If reset_src or reset_signal_length have invalid value
  155. */
  156. esp_err_t rtc_wdt_set_length_of_reset_signal(rtc_wdt_reset_sig_t reset_src, rtc_wdt_length_sig_t reset_signal_length);
  157. /**
  158. * @brief Return true if rtc_wdt is enabled.
  159. *
  160. * @return
  161. * - True rtc_wdt is enabled
  162. */
  163. bool rtc_wdt_is_on(void);
  164. #ifdef __cplusplus
  165. }
  166. #endif
  167. #endif // CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2