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