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 #include <string.h>
16 #include <stdbool.h>
17 #include "hal/wdt_types.h"
18 #include "hal/wdt_hal.h"
19
20 /* ---------------------------- Init and Config ----------------------------- */
21
wdt_hal_init(wdt_hal_context_t * hal,wdt_inst_t wdt_inst,uint32_t prescaler,bool enable_intr)22 void wdt_hal_init(wdt_hal_context_t *hal, wdt_inst_t wdt_inst, uint32_t prescaler, bool enable_intr)
23 {
24 //Initialize HAL context
25 memset(hal, 0, sizeof(wdt_hal_context_t));
26 if (wdt_inst == WDT_MWDT0) {
27 hal->mwdt_dev = &TIMERG0;
28 } else if (wdt_inst == WDT_MWDT1) {
29 hal->mwdt_dev = &TIMERG1;
30 } else {
31 hal->rwdt_dev = &RTCCNTL;
32 }
33 hal->inst = wdt_inst;
34
35 if (hal->inst == WDT_RWDT) {
36 //Unlock RTC WDT
37 rwdt_ll_write_protect_disable(hal->rwdt_dev);
38 //Disable RTC WDT, all stages, and all interrupts.
39 rwdt_ll_disable(hal->rwdt_dev);
40 rwdt_ll_disable_stage(hal->rwdt_dev, WDT_STAGE0);
41 rwdt_ll_disable_stage(hal->rwdt_dev, WDT_STAGE1);
42 rwdt_ll_disable_stage(hal->rwdt_dev, WDT_STAGE2);
43 rwdt_ll_disable_stage(hal->rwdt_dev, WDT_STAGE3);
44 #ifdef CONFIG_IDF_TARGET_ESP32
45 //Enable or disable level interrupt. Edge interrupt is always disabled.
46 rwdt_ll_set_edge_intr(hal->rwdt_dev, false);
47 rwdt_ll_set_level_intr(hal->rwdt_dev, enable_intr);
48 #else //CONFIG_IDF_TARGET_ESP32S2BETA
49 //Enable or disable chip reset on timeout, and length of chip reset signal
50 rwdt_ll_set_chip_reset_width(hal->rwdt_dev, 0);
51 rwdt_ll_set_chip_reset_en(hal->rwdt_dev, false);
52 #endif
53 rwdt_ll_clear_intr_status(hal->rwdt_dev);
54 rwdt_ll_set_intr_enable(hal->rwdt_dev, enable_intr);
55 //Set default values
56 rwdt_ll_set_appcpu_reset_en(hal->rwdt_dev, true);
57 rwdt_ll_set_procpu_reset_en(hal->rwdt_dev, true);
58 rwdt_ll_set_pause_in_sleep_en(hal->rwdt_dev, true);
59 rwdt_ll_set_cpu_reset_length(hal->rwdt_dev, WDT_RESET_SIG_LENGTH_3_2us);
60 rwdt_ll_set_sys_reset_length(hal->rwdt_dev, WDT_RESET_SIG_LENGTH_3_2us);
61 //Lock RTC WDT
62 rwdt_ll_write_protect_enable(hal->rwdt_dev);
63 } else {
64 //Unlock WDT
65 mwdt_ll_write_protect_disable(hal->mwdt_dev);
66 //Disable WDT and stages.
67 mwdt_ll_disable(hal->mwdt_dev);
68 mwdt_ll_disable_stage(hal->mwdt_dev, 0);
69 mwdt_ll_disable_stage(hal->mwdt_dev, 1);
70 mwdt_ll_disable_stage(hal->mwdt_dev, 2);
71 mwdt_ll_disable_stage(hal->mwdt_dev, 3);
72 #if !CONFIG_IDF_TARGET_ESP32C3 && !CONFIG_IDF_TARGET_ESP32H2
73 //Enable or disable level interrupt. Edge interrupt is always disabled.
74 mwdt_ll_set_edge_intr(hal->mwdt_dev, false);
75 mwdt_ll_set_level_intr(hal->mwdt_dev, enable_intr);
76 #endif
77 mwdt_ll_clear_intr_status(hal->mwdt_dev);
78 mwdt_ll_set_intr_enable(hal->mwdt_dev, enable_intr);
79 //Set default values
80 mwdt_ll_set_cpu_reset_length(hal->mwdt_dev, WDT_RESET_SIG_LENGTH_3_2us);
81 mwdt_ll_set_sys_reset_length(hal->mwdt_dev, WDT_RESET_SIG_LENGTH_3_2us);
82 //Set tick period
83 mwdt_ll_set_prescaler(hal->mwdt_dev, prescaler);
84 //Lock WDT
85 mwdt_ll_write_protect_enable(hal->mwdt_dev);
86 }
87 }
88
wdt_hal_deinit(wdt_hal_context_t * hal)89 void wdt_hal_deinit(wdt_hal_context_t *hal)
90 {
91 if (hal->inst == WDT_RWDT) {
92 //Unlock WDT
93 rwdt_ll_write_protect_disable(hal->rwdt_dev);
94 //Disable WDT and clear any interrupts
95 rwdt_ll_feed(hal->rwdt_dev);
96 rwdt_ll_disable(hal->rwdt_dev);
97 rwdt_ll_clear_intr_status(hal->rwdt_dev);
98 rwdt_ll_set_intr_enable(hal->rwdt_dev, false);
99 //Lock WDT
100 rwdt_ll_write_protect_enable(hal->rwdt_dev);
101 } else {
102 //Unlock WDT
103 mwdt_ll_write_protect_disable(hal->mwdt_dev);
104 //Disable WDT and clear/disable any interrupts
105 mwdt_ll_feed(hal->mwdt_dev);
106 mwdt_ll_disable(hal->mwdt_dev);
107 mwdt_ll_clear_intr_status(hal->mwdt_dev);
108 mwdt_ll_set_intr_enable(hal->mwdt_dev, false);
109 //Lock WDT
110 mwdt_ll_write_protect_enable(hal->mwdt_dev);
111 }
112 //Deinit HAL context
113 hal->mwdt_dev = NULL;
114 }
115
wdt_hal_config_stage(wdt_hal_context_t * hal,wdt_stage_t stage,uint32_t timeout_ticks,wdt_stage_action_t behavior)116 void wdt_hal_config_stage(wdt_hal_context_t *hal, wdt_stage_t stage, uint32_t timeout_ticks, wdt_stage_action_t behavior)
117 {
118 if (hal->inst == WDT_RWDT) {
119 rwdt_ll_config_stage(hal->rwdt_dev, stage, timeout_ticks, behavior);
120 } else {
121 mwdt_ll_config_stage(hal->mwdt_dev, stage, timeout_ticks, behavior);
122 }
123 }
124
125 /* -------------------------------- Runtime --------------------------------- */
126
wdt_hal_write_protect_disable(wdt_hal_context_t * hal)127 void wdt_hal_write_protect_disable(wdt_hal_context_t *hal)
128 {
129 if (hal->inst == WDT_RWDT) {
130 rwdt_ll_write_protect_disable(hal->rwdt_dev);
131 } else {
132 mwdt_ll_write_protect_disable(hal->mwdt_dev);
133 }
134 }
135
wdt_hal_write_protect_enable(wdt_hal_context_t * hal)136 void wdt_hal_write_protect_enable(wdt_hal_context_t *hal)
137 {
138 if (hal->inst == WDT_RWDT) {
139 rwdt_ll_write_protect_enable(hal->rwdt_dev);
140 } else {
141 mwdt_ll_write_protect_enable(hal->mwdt_dev);
142 }
143 }
144
wdt_hal_enable(wdt_hal_context_t * hal)145 void wdt_hal_enable(wdt_hal_context_t *hal)
146 {
147 if (hal->inst == WDT_RWDT) {
148 rwdt_ll_feed(hal->rwdt_dev);
149 rwdt_ll_enable(hal->rwdt_dev);
150 } else {
151 mwdt_ll_feed(hal->mwdt_dev);
152 mwdt_ll_enable(hal->mwdt_dev);
153 }
154 }
155
wdt_hal_disable(wdt_hal_context_t * hal)156 void wdt_hal_disable(wdt_hal_context_t *hal)
157 {
158 if (hal->inst == WDT_RWDT) {
159 rwdt_ll_disable(hal->rwdt_dev);
160 } else {
161 mwdt_ll_disable(hal->mwdt_dev);
162 }
163 }
164
wdt_hal_handle_intr(wdt_hal_context_t * hal)165 void wdt_hal_handle_intr(wdt_hal_context_t *hal)
166 {
167 if (hal->inst == WDT_RWDT) {
168 rwdt_ll_feed(hal->rwdt_dev);
169 rwdt_ll_clear_intr_status(hal->rwdt_dev);
170 } else {
171 mwdt_ll_feed(hal->mwdt_dev);
172 mwdt_ll_clear_intr_status(hal->mwdt_dev);
173 }
174 }
175
wdt_hal_feed(wdt_hal_context_t * hal)176 void wdt_hal_feed(wdt_hal_context_t *hal)
177 {
178 if (hal->inst == WDT_RWDT) {
179 rwdt_ll_feed(hal->rwdt_dev);
180 } else {
181 mwdt_ll_feed(hal->mwdt_dev);
182 }
183 }
184
wdt_hal_set_flashboot_en(wdt_hal_context_t * hal,bool enable)185 void wdt_hal_set_flashboot_en(wdt_hal_context_t *hal, bool enable)
186 {
187 if (hal->inst == WDT_RWDT) {
188 rwdt_ll_set_flashboot_en(hal->rwdt_dev, enable);
189 } else {
190 mwdt_ll_set_flashboot_en(hal->mwdt_dev, enable);
191 }
192 }
193
wdt_hal_is_enabled(wdt_hal_context_t * hal)194 bool wdt_hal_is_enabled(wdt_hal_context_t *hal)
195 {
196 if (hal->inst == WDT_RWDT) {
197 return rwdt_ll_check_if_enabled(hal->rwdt_dev);
198 } else {
199 return mwdt_ll_check_if_enabled(hal->mwdt_dev);
200 }
201 }
202