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