1 // Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 /*******************************************************************************
16  * NOTICE
17  * The hal is not public api, don't use in application code.
18  * See readme.md in hal/include/hal/readme.md
19  ******************************************************************************/
20 
21 #pragma once
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 #include <stdbool.h>
28 #include "soc/soc_caps.h"
29 #include "hal/wdt_types.h"
30 #include "hal/mwdt_ll.h"
31 #include "hal/rwdt_ll.h"
32 
33 /**
34  * Context that should be maintained by both the driver and the HAL
35  */
36 typedef struct {
37     wdt_inst_t inst;                /**< Which WDT instance this HAL context is using (i.e. MWDT0, MWDT1, RWDT)*/
38     union {
39         timg_dev_t *mwdt_dev;       /**< Starting address of the MWDT */
40         rtc_cntl_dev_t *rwdt_dev;   /**< Starting address of the RWDT*/
41     };
42 } wdt_hal_context_t;
43 
44 /* ---------------------------- Init and Config ----------------------------- */
45 
46 /**
47  * @brief Initialize one of the WDTs associated HAL context
48  *
49  * This function initializes one of the WDTs (MWDT0, MWDT1, or RWDT) hardware by
50  * doing the following:
51  * - Disables the WDT and all of its stages
52  * - Sets some registers with default values
53  * - Sets the WDTs source clock prescaler (not applicable to RWDT)
54  * - Optionally enables the level interrupt
55  *
56  * The HAL context is initialized by storing the type (i.e. MWDT or RWDT) of
57  * this WDT instance, and a pointer to the associated registers.
58  *
59  * @param hal Context of HAL layer
60  * @param wdt_inst Which WDT instance to initialize (MWDT0, MWDT1, or RWDT)
61  * @param prescaler MWDT source clock prescaler. Unused for RWDT
62  * @param enable_intr True to enable level interrupt. False to disable
63  *
64  * @note Although the WDTs on the ESP32 have an edge interrupt, this HAL does
65  *       not utilize it and will always disables it.
66  * @note RWDT does not have a prescaler. Its tick rate is equal to the
67  *       frequency of its source clock (RTC slow clock).
68  */
69 void wdt_hal_init(wdt_hal_context_t *hal, wdt_inst_t wdt_inst, uint32_t prescaler, bool enable_intr);
70 
71 /**
72  * @brief Deinitialize a WDT and its HAL context
73  *
74  * This function deinitializes a WDT by feeding then disabling it. The WDT's
75  * interrupt is also cleared and disabled. The HAL context is cleared.
76  *
77  * @param hal Context of HAL layer
78  */
79 void wdt_hal_deinit(wdt_hal_context_t *hal);
80 
81 /**
82  * @brief Configure a particular stage of a WDT
83  *
84  * @param hal Context of HAL layer
85  * @param stage Stage to configure (0 to 3)
86  * @param timeout Number of WDT ticks for the stage to time out
87  * @param behavior What action to take when the stage times out. Note that only
88  *                 the RWDT supports the RTC reset action.
89  *
90  * @note This function can only be called when the WDT is unlocked. Call
91  *       wdt_hal_write_protect_disable() first.
92  */
93 void wdt_hal_config_stage(wdt_hal_context_t *hal, wdt_stage_t stage, uint32_t timeout, wdt_stage_action_t behavior);
94 
95 /* -------------------------------- Runtime --------------------------------- */
96 
97 /**
98  * @brief Disable write protection of the WDT registers
99  *
100  * @param hal Context of HAL layer
101  */
102 void wdt_hal_write_protect_disable(wdt_hal_context_t *hal);
103 
104 /**
105  * @brief Enable write protection of the WDT registers
106  *
107  * @param hal Context of HAL layer
108  */
109 void wdt_hal_write_protect_enable(wdt_hal_context_t *hal);
110 
111 /**
112  * @brief Enable the WDT
113  *
114  * The WDT will start counting when enabled. This function also feeds the WDT
115  * before enabling it.
116  *
117  * @param hal Context of HAL layer
118  *
119  * @note This function can only be called when the WDT is unlocked. Call
120  *       wdt_hal_write_protect_disable() first.
121  */
122 void wdt_hal_enable(wdt_hal_context_t *hal);
123 
124 /**
125  * @brief Disable the WDT
126  *
127  * @param hal Context of HAL layer
128  *
129  * @note This function can only be called when the WDT is unlocked. Call
130  *       wdt_hal_write_protect_disable() first.
131  */
132 void wdt_hal_disable(wdt_hal_context_t *hal);
133 
134 /**
135  * @brief Handle WDT interrupt
136  *
137  * Clears the interrupt status bit and feeds the WDT
138  *
139  * @param hal Context of HAL layer
140  *
141  * @note This function can only be called when the WDT is unlocked. Call
142  *       wdt_hal_write_protect_disable() first.
143  */
144 void wdt_hal_handle_intr(wdt_hal_context_t *hal);
145 
146 /**
147  * @brief Feed the WDT
148  *
149  * Feeding the WDT will reset the internal count and current stage.
150  *
151  * @param hal Context of HAL layer
152  *
153  * @note This function can only be called when the WDT is unlocked. Call
154  *       wdt_hal_write_protect_disable() first.
155  */
156 void wdt_hal_feed(wdt_hal_context_t *hal);
157 
158 /**
159  * @brief Enable/Disable the WDT flash boot mode
160  *
161  * @param hal Context of HAL layer
162  * @param enable True to enable flash boot mode, false to disable.
163  *
164  * @note Flash boot mode can trigger a time out even if the WDT is disabled.
165  * @note This function can only be called when the WDT is unlocked. Call
166  *       wdt_hal_write_protect_disable() first.
167  */
168 void wdt_hal_set_flashboot_en(wdt_hal_context_t *hal, bool enable);
169 
170 /**
171  * @brief Check if the WDT is enabled
172  *
173  * @param hal Context of HAL layer
174  * @return True if enabled, false otherwise
175  */
176 bool wdt_hal_is_enabled(wdt_hal_context_t *hal);
177 
178 #ifdef __cplusplus
179 }
180 #endif
181