]> granicus.if.org Git - esp-idf/blob - components/esp32/task_wdt.c
Merge branch 'feature/i2s_built_in_adc' into 'master'
[esp-idf] / components / esp32 / task_wdt.c
1 // Copyright 2015-2016 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
17 #include <stdint.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <stdbool.h>
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>
28 #include "esp_err.h"
29 #include "esp_intr.h"
30 #include "esp_intr_alloc.h"
31 #include "esp_attr.h"
32 #include "esp_freertos_hooks.h"
33 #include "soc/timer_group_struct.h"
34 #include "soc/timer_group_reg.h"
35 #include "esp_log.h"
36 #include "driver/timer.h"
37
38 #include "esp_task_wdt.h"
39
40 #if CONFIG_TASK_WDT
41
42 static const char* TAG = "task_wdt";
43
44 typedef struct wdt_task_t wdt_task_t;
45 struct wdt_task_t {
46     TaskHandle_t task_handle;
47     bool fed_watchdog;
48     wdt_task_t *next;
49 };
50
51 static wdt_task_t *wdt_task_list=NULL;
52 static portMUX_TYPE taskwdt_spinlock = portMUX_INITIALIZER_UNLOCKED;
53
54
55 static void task_wdt_isr(void *arg) {
56     wdt_task_t *wdttask;
57     const char *cpu;
58     //Feed the watchdog so we do not reset
59     TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE;
60     TIMERG0.wdt_feed=1;
61     TIMERG0.wdt_wprotect=0;
62     //Ack interrupt
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);
69     if (!wdt_task_list) {
70         //No task on list. Maybe none registered yet.
71         portEXIT_CRITICAL(&taskwdt_spinlock);
72         return;
73     }
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);
81         }
82     }
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)));
86     }
87
88 #if CONFIG_TASK_WDT_PANIC
89     ets_printf("Aborting.\n");
90     abort();
91 #endif
92     portEXIT_CRITICAL(&taskwdt_spinlock);
93 }
94
95
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);
101
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;
108             found_task=true;
109         }
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;
112     }
113     
114     if (!found_task) {
115         //This is the first time the task calls the task_wdt_feed function. Create a new entry for it in
116         //the linked list.
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;
123         } else {
124             for (wdttask=wdt_task_list; wdttask->next!=NULL; wdttask=wdttask->next) ;
125             wdttask->next=newtask;
126         }
127     }
128     if (do_feed_wdt) {
129         //All tasks have checked in; time to feed the hw watchdog.
130         TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE;
131         TIMERG0.wdt_feed=1;
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;
135     }
136     portEXIT_CRITICAL(&taskwdt_spinlock);
137 }
138
139 void esp_task_wdt_delete() {
140     TaskHandle_t handle=xTaskGetCurrentTaskHandle();
141     wdt_task_t *wdttask=wdt_task_list;
142     portENTER_CRITICAL(&taskwdt_spinlock);
143
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);
148         return;
149     }
150     if (handle==wdt_task_list) {
151         //Current task is first on list.
152         wdt_task_list=wdt_task_list->next;
153         free(wdttask);
154     } else {
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;
160             free(freeme);
161             portEXIT_CRITICAL(&taskwdt_spinlock);
162             return;
163         }
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);
168             return;
169         }
170         wdt_task_t *freeme=wdttask->next;
171         wdttask->next=wdttask->next->next;
172         free(freeme);
173     }
174     portEXIT_CRITICAL(&taskwdt_spinlock);
175 }
176
177
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;
182 #endif
183     esp_task_wdt_feed();
184     return true;
185 }
186 #endif
187
188
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;
200     TIMERG0.wdt_feed=1;
201     TIMERG0.wdt_wprotect=0;
202 #if CONFIG_TASK_WDT_CHECK_IDLE_TASK
203     esp_register_freertos_idle_hook(idle_hook);
204 #endif
205     ESP_ERROR_CHECK( esp_intr_alloc(ETS_TG0_WDT_LEVEL_INTR_SOURCE, 0, task_wdt_isr, NULL, NULL) );
206 }
207
208
209 #endif