rtc_wdt.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * SPDX-FileCopyrightText: 2018-2021 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. #ifdef __cplusplus
  41. extern "C"
  42. {
  43. #endif
  44. /// List of stage of rtc watchdog. WDT has 4 stage.
  45. typedef enum {
  46. RTC_WDT_STAGE0 = 0, /*!< Stage 0 */
  47. RTC_WDT_STAGE1 = 1, /*!< Stage 1 */
  48. RTC_WDT_STAGE2 = 2, /*!< Stage 2 */
  49. RTC_WDT_STAGE3 = 3 /*!< Stage 3 */
  50. } rtc_wdt_stage_t;
  51. /// List of action. When the time of stage expires this action will be triggered.
  52. typedef enum {
  53. RTC_WDT_STAGE_ACTION_OFF = RTC_WDT_STG_SEL_OFF, /*!< Disabled. This stage will have no effects on the system. */
  54. RTC_WDT_STAGE_ACTION_INTERRUPT = RTC_WDT_STG_SEL_INT, /*!< Trigger an interrupt. When the stage expires an interrupt is triggered. */
  55. RTC_WDT_STAGE_ACTION_RESET_CPU = RTC_WDT_STG_SEL_RESET_CPU, /*!< Reset a CPU core. */
  56. 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. */
  57. RTC_WDT_STAGE_ACTION_RESET_RTC = RTC_WDT_STG_SEL_RESET_RTC /*!< Reset the main system and the RTC. */
  58. } rtc_wdt_stage_action_t;
  59. /// Type of reset signal
  60. typedef enum {
  61. RTC_WDT_SYS_RESET_SIG = 0, /*!< System reset signal length selection */
  62. RTC_WDT_CPU_RESET_SIG = 1 /*!< CPU reset signal length selection */
  63. } rtc_wdt_reset_sig_t;
  64. /// Length of reset signal
  65. typedef enum {
  66. RTC_WDT_LENGTH_100ns = 0, /*!< 100 ns */
  67. RTC_WDT_LENGTH_200ns = 1, /*!< 200 ns */
  68. RTC_WDT_LENGTH_300ns = 2, /*!< 300 ns */
  69. RTC_WDT_LENGTH_400ns = 3, /*!< 400 ns */
  70. RTC_WDT_LENGTH_500ns = 4, /*!< 500 ns */
  71. RTC_WDT_LENGTH_800ns = 5, /*!< 800 ns */
  72. RTC_WDT_LENGTH_1_6us = 6, /*!< 1.6 us */
  73. RTC_WDT_LENGTH_3_2us = 7 /*!< 3.2 us */
  74. } rtc_wdt_length_sig_t;
  75. /**
  76. * @brief Get status of protect of rtc_wdt.
  77. *
  78. * @return
  79. * - True if the protect of RTC_WDT is set
  80. */
  81. bool rtc_wdt_get_protect_status(void);
  82. /**
  83. * @brief Set protect of rtc_wdt.
  84. */
  85. void rtc_wdt_protect_on(void);
  86. /**
  87. * @brief Reset protect of rtc_wdt.
  88. */
  89. void rtc_wdt_protect_off(void);
  90. /**
  91. * @brief Enable rtc_wdt.
  92. */
  93. void rtc_wdt_enable(void);
  94. /**
  95. * @brief Enable the flash boot protection procedure for WDT.
  96. *
  97. * Do not recommend to use it in the app.
  98. * This function was added to be compatibility with the old bootloaders.
  99. * This mode is disabled in bootloader or using rtc_wdt_disable() function.
  100. */
  101. void rtc_wdt_flashboot_mode_enable(void);
  102. /**
  103. * @brief Disable rtc_wdt.
  104. */
  105. void rtc_wdt_disable(void);
  106. /**
  107. * @brief Reset counter rtc_wdt.
  108. *
  109. * It returns to stage 0 and its expiry counter restarts from 0.
  110. */
  111. void rtc_wdt_feed(void);
  112. /**
  113. * @brief Set time for required stage.
  114. *
  115. * @param[in] stage Stage of rtc_wdt.
  116. * @param[in] timeout_ms Timeout for this stage.
  117. *
  118. * @return
  119. * - ESP_OK In case of success
  120. * - ESP_ERR_INVALID_ARG If stage has invalid value
  121. */
  122. esp_err_t rtc_wdt_set_time(rtc_wdt_stage_t stage, unsigned int timeout_ms);
  123. /**
  124. * @brief Get the timeout set for the required stage.
  125. *
  126. * @param[in] stage Stage of rtc_wdt.
  127. * @param[out] timeout_ms Timeout set for this stage. (not elapsed time).
  128. *
  129. * @return
  130. * - ESP_OK In case of success
  131. * - ESP_ERR_INVALID_ARG If stage has invalid value
  132. */
  133. esp_err_t rtc_wdt_get_timeout(rtc_wdt_stage_t stage, unsigned int* timeout_ms);
  134. /**
  135. * @brief Set an action for required stage.
  136. *
  137. * @param[in] stage Stage of rtc_wdt.
  138. * @param[in] stage_sel Action for this stage. When the time of stage expires this action will be triggered.
  139. *
  140. * @return
  141. * - ESP_OK In case of success
  142. * - ESP_ERR_INVALID_ARG If stage or stage_sel have invalid value
  143. */
  144. esp_err_t rtc_wdt_set_stage(rtc_wdt_stage_t stage, rtc_wdt_stage_action_t stage_sel);
  145. /**
  146. * @brief Set a length of reset signal.
  147. *
  148. * @param[in] reset_src Type of reset signal.
  149. * @param[in] reset_signal_length A length of reset signal.
  150. *
  151. * @return
  152. * - ESP_OK In case of success
  153. * - ESP_ERR_INVALID_ARG If reset_src or reset_signal_length have invalid value
  154. */
  155. 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);
  156. /**
  157. * @brief Return true if rtc_wdt is enabled.
  158. *
  159. * @return
  160. * - True rtc_wdt is enabled
  161. */
  162. bool rtc_wdt_is_on(void);
  163. #ifdef __cplusplus
  164. }
  165. #endif