1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include "sdkconfig.h"
23 #include "freertos/FreeRTOS.h"
24 #include "freertos/task.h"
25 #include "freertos/queue.h"
26 #include "freertos/semphr.h"
27 #include <esp_types.h>
30 #include "esp_intr_alloc.h"
32 #include "esp_freertos_hooks.h"
33 #include "soc/timer_group_struct.h"
34 #include "soc/timer_group_reg.h"
36 #include "driver/timer.h"
38 #include "esp_task_wdt.h"
42 static const char* TAG = "task_wdt";
44 typedef struct wdt_task_t wdt_task_t;
46 TaskHandle_t task_handle;
51 static wdt_task_t *wdt_task_list=NULL;
52 static portMUX_TYPE taskwdt_spinlock = portMUX_INITIALIZER_UNLOCKED;
55 static void task_wdt_isr(void *arg) {
58 //Feed the watchdog so we do not reset
59 TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE;
61 TIMERG0.wdt_wprotect=0;
63 TIMERG0.int_clr_timers.wdt=1;
64 //We are taking a spinlock while doing I/O (ets_printf) here. Normally, that is a pretty
65 //bad thing, possibly (temporarily) hanging up the 2nd core and stopping FreeRTOS. In this case,
66 //something bad already happened and reporting this is considered more important
67 //than the badness caused by a spinlock here.
68 portENTER_CRITICAL(&taskwdt_spinlock);
70 //No task on list. Maybe none registered yet.
71 portEXIT_CRITICAL(&taskwdt_spinlock);
74 //Watchdog got triggered because at least one task did not report in.
75 ets_printf("Task watchdog got triggered. The following tasks did not feed the watchdog in time:\n");
76 for (wdttask=wdt_task_list; wdttask!=NULL; wdttask=wdttask->next) {
77 if (!wdttask->fed_watchdog) {
78 cpu=xTaskGetAffinity(wdttask->task_handle)==0?DRAM_STR("CPU 0"):DRAM_STR("CPU 1");
79 if (xTaskGetAffinity(wdttask->task_handle)==tskNO_AFFINITY) cpu=DRAM_STR("CPU 0/1");
80 ets_printf(" - %s (%s)\n", pcTaskGetTaskName(wdttask->task_handle), cpu);
83 ets_printf(DRAM_STR("Tasks currently running:\n"));
84 for (int x=0; x<portNUM_PROCESSORS; x++) {
85 ets_printf("CPU %d: %s\n", x, pcTaskGetTaskName(xTaskGetCurrentTaskHandleForCPU(x)));
88 #if CONFIG_TASK_WDT_PANIC
89 ets_printf("Aborting.\n");
92 portEXIT_CRITICAL(&taskwdt_spinlock);
96 void esp_task_wdt_feed() {
97 wdt_task_t *wdttask=wdt_task_list;
98 bool found_task=false, do_feed_wdt=true;
99 TaskHandle_t handle=xTaskGetCurrentTaskHandle();
100 portENTER_CRITICAL(&taskwdt_spinlock);
102 //Walk the linked list of wdt tasks to find this one, as well as see if we need to feed
103 //the real watchdog timer.
104 for (wdttask=wdt_task_list; wdttask!=NULL; wdttask=wdttask->next) {
105 //See if we are at the current task.
106 if (wdttask->task_handle == handle) {
107 wdttask->fed_watchdog=true;
110 //If even one task in the list doesn't have the do_feed_wdt var set, we do not feed the watchdog.
111 if (!wdttask->fed_watchdog) do_feed_wdt=false;
115 //This is the first time the task calls the task_wdt_feed function. Create a new entry for it in
117 wdt_task_t *newtask=malloc(sizeof(wdt_task_t));
118 memset(newtask, 0, sizeof(wdt_task_t));
119 newtask->task_handle=handle;
120 newtask->fed_watchdog=true;
121 if (wdt_task_list == NULL) {
122 wdt_task_list=newtask;
124 for (wdttask=wdt_task_list; wdttask->next!=NULL; wdttask=wdttask->next) ;
125 wdttask->next=newtask;
129 //All tasks have checked in; time to feed the hw watchdog.
130 TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE;
132 TIMERG0.wdt_wprotect=0;
133 //Reset fed_watchdog status
134 for (wdttask=wdt_task_list; wdttask!=NULL; wdttask=wdttask->next) wdttask->fed_watchdog=false;
136 portEXIT_CRITICAL(&taskwdt_spinlock);
139 void esp_task_wdt_delete() {
140 TaskHandle_t handle=xTaskGetCurrentTaskHandle();
141 wdt_task_t *wdttask=wdt_task_list;
142 portENTER_CRITICAL(&taskwdt_spinlock);
144 //Wdt task list can't be empty
145 if (!wdt_task_list) {
146 ESP_LOGE(TAG, "task_wdt_delete: No tasks in list?");
147 portEXIT_CRITICAL(&taskwdt_spinlock);
150 if (handle==wdt_task_list) {
151 //Current task is first on list.
152 wdt_task_list=wdt_task_list->next;
155 //Find current task in list
156 if (wdt_task_list->task_handle==handle) {
157 //Task is the very first one.
158 wdt_task_t *freeme=wdt_task_list;
159 wdt_task_list=wdt_task_list->next;
161 portEXIT_CRITICAL(&taskwdt_spinlock);
164 while (wdttask->next!=NULL && wdttask->next->task_handle!=handle) wdttask=wdttask->next;
165 if (!wdttask->next) {
166 ESP_LOGE(TAG, "task_wdt_delete: Task never called task_wdt_feed!");
167 portEXIT_CRITICAL(&taskwdt_spinlock);
170 wdt_task_t *freeme=wdttask->next;
171 wdttask->next=wdttask->next->next;
174 portEXIT_CRITICAL(&taskwdt_spinlock);
178 #if CONFIG_TASK_WDT_CHECK_IDLE_TASK
179 static bool idle_hook(void) {
180 #if !CONFIG_TASK_WDT_CHECK_IDLE_TASK_CPU1
181 if (xPortGetCoreID()!=0) return true;
189 void esp_task_wdt_init() {
190 TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE;
191 TIMERG0.wdt_config0.sys_reset_length=7; //3.2uS
192 TIMERG0.wdt_config0.cpu_reset_length=7; //3.2uS
193 TIMERG0.wdt_config0.level_int_en=1;
194 TIMERG0.wdt_config0.stg0=TIMG_WDT_STG_SEL_INT; //1st stage timeout: interrupt
195 TIMERG0.wdt_config0.stg1=TIMG_WDT_STG_SEL_RESET_SYSTEM; //2nd stage timeout: reset system
196 TIMERG0.wdt_config1.clk_prescale=80*500; //Prescaler: wdt counts in ticks of 0.5mS
197 TIMERG0.wdt_config2=CONFIG_TASK_WDT_TIMEOUT_S*2000; //Set timeout before interrupt
198 TIMERG0.wdt_config3=CONFIG_TASK_WDT_TIMEOUT_S*4000; //Set timeout before reset
199 TIMERG0.wdt_config0.en=1;
201 TIMERG0.wdt_wprotect=0;
202 #if CONFIG_TASK_WDT_CHECK_IDLE_TASK
203 esp_register_freertos_idle_hook(idle_hook);
205 ESP_ERROR_CHECK( esp_intr_alloc(ETS_TG0_WDT_LEVEL_INTR_SOURCE, 0, task_wdt_isr, NULL, NULL) );