task_wdt.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <stdint.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <stdbool.h>
  18. #include "sdkconfig.h"
  19. #include "freertos/FreeRTOS.h"
  20. #include "freertos/task.h"
  21. #include "freertos/queue.h"
  22. #include "freertos/semphr.h"
  23. #include <esp_types.h>
  24. #include "esp_err.h"
  25. #include "esp_intr.h"
  26. #include "esp_intr_alloc.h"
  27. #include "esp_attr.h"
  28. #include "esp_freertos_hooks.h"
  29. #include "soc/timer_group_struct.h"
  30. #include "soc/timer_group_reg.h"
  31. #include "esp_log.h"
  32. #include "driver/timer.h"
  33. #include "esp_task_wdt.h"
  34. #if CONFIG_TASK_WDT
  35. static const char* TAG = "task_wdt";
  36. typedef struct wdt_task_t wdt_task_t;
  37. struct wdt_task_t {
  38. TaskHandle_t task_handle;
  39. bool fed_watchdog;
  40. wdt_task_t *next;
  41. };
  42. static wdt_task_t *wdt_task_list=NULL;
  43. static portMUX_TYPE taskwdt_spinlock = portMUX_INITIALIZER_UNLOCKED;
  44. static void task_wdt_isr(void *arg) {
  45. wdt_task_t *wdttask;
  46. const char *cpu;
  47. //Feed the watchdog so we do not reset
  48. TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE;
  49. TIMERG0.wdt_feed=1;
  50. TIMERG0.wdt_wprotect=0;
  51. //Ack interrupt
  52. TIMERG0.int_clr_timers.wdt=1;
  53. //We are taking a spinlock while doing I/O (ets_printf) here. Normally, that is a pretty
  54. //bad thing, possibly (temporarily) hanging up the 2nd core and stopping FreeRTOS. In this case,
  55. //something bad already happened and reporting this is considered more important
  56. //than the badness caused by a spinlock here.
  57. portENTER_CRITICAL(&taskwdt_spinlock);
  58. if (!wdt_task_list) {
  59. //No task on list. Maybe none registered yet.
  60. portEXIT_CRITICAL(&taskwdt_spinlock);
  61. return;
  62. }
  63. //Watchdog got triggered because at least one task did not report in.
  64. ets_printf("Task watchdog got triggered. The following tasks did not feed the watchdog in time:\n");
  65. for (wdttask=wdt_task_list; wdttask!=NULL; wdttask=wdttask->next) {
  66. if (!wdttask->fed_watchdog) {
  67. cpu=xTaskGetAffinity(wdttask->task_handle)==0?DRAM_STR("CPU 0"):DRAM_STR("CPU 1");
  68. if (xTaskGetAffinity(wdttask->task_handle)==tskNO_AFFINITY) cpu=DRAM_STR("CPU 0/1");
  69. ets_printf(" - %s (%s)\n", pcTaskGetTaskName(wdttask->task_handle), cpu);
  70. }
  71. }
  72. ets_printf(DRAM_STR("Tasks currently running:\n"));
  73. for (int x=0; x<portNUM_PROCESSORS; x++) {
  74. ets_printf("CPU %d: %s\n", x, pcTaskGetTaskName(xTaskGetCurrentTaskHandleForCPU(x)));
  75. }
  76. #if CONFIG_TASK_WDT_PANIC
  77. ets_printf("Aborting.\n");
  78. abort();
  79. #endif
  80. portEXIT_CRITICAL(&taskwdt_spinlock);
  81. }
  82. void esp_task_wdt_feed() {
  83. wdt_task_t *wdttask=wdt_task_list;
  84. bool found_task=false, do_feed_wdt=true;
  85. TaskHandle_t handle=xTaskGetCurrentTaskHandle();
  86. portENTER_CRITICAL(&taskwdt_spinlock);
  87. //Walk the linked list of wdt tasks to find this one, as well as see if we need to feed
  88. //the real watchdog timer.
  89. for (wdttask=wdt_task_list; wdttask!=NULL; wdttask=wdttask->next) {
  90. //See if we are at the current task.
  91. if (wdttask->task_handle == handle) {
  92. wdttask->fed_watchdog=true;
  93. found_task=true;
  94. }
  95. //If even one task in the list doesn't have the do_feed_wdt var set, we do not feed the watchdog.
  96. if (!wdttask->fed_watchdog) do_feed_wdt=false;
  97. }
  98. if (!found_task) {
  99. //This is the first time the task calls the task_wdt_feed function. Create a new entry for it in
  100. //the linked list.
  101. wdt_task_t *newtask=malloc(sizeof(wdt_task_t));
  102. memset(newtask, 0, sizeof(wdt_task_t));
  103. newtask->task_handle=handle;
  104. newtask->fed_watchdog=true;
  105. if (wdt_task_list == NULL) {
  106. wdt_task_list=newtask;
  107. } else {
  108. for (wdttask=wdt_task_list; wdttask->next!=NULL; wdttask=wdttask->next) ;
  109. wdttask->next=newtask;
  110. }
  111. }
  112. if (do_feed_wdt) {
  113. //All tasks have checked in; time to feed the hw watchdog.
  114. TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE;
  115. TIMERG0.wdt_feed=1;
  116. TIMERG0.wdt_wprotect=0;
  117. //Reset fed_watchdog status
  118. for (wdttask=wdt_task_list; wdttask->next!=NULL; wdttask=wdttask->next) wdttask->fed_watchdog=false;
  119. }
  120. portEXIT_CRITICAL(&taskwdt_spinlock);
  121. }
  122. void esp_task_wdt_delete() {
  123. TaskHandle_t handle=xTaskGetCurrentTaskHandle();
  124. wdt_task_t *wdttask=wdt_task_list;
  125. portENTER_CRITICAL(&taskwdt_spinlock);
  126. //Wdt task list can't be empty
  127. if (!wdt_task_list) {
  128. ESP_LOGE(TAG, "task_wdt_delete: No tasks in list?");
  129. portEXIT_CRITICAL(&taskwdt_spinlock);
  130. return;
  131. }
  132. if (handle==wdt_task_list) {
  133. //Current task is first on list.
  134. wdt_task_list=wdt_task_list->next;
  135. free(wdttask);
  136. } else {
  137. //Find current task in list
  138. if (wdt_task_list->task_handle==handle) {
  139. //Task is the very first one.
  140. wdt_task_t *freeme=wdt_task_list;
  141. wdt_task_list=wdt_task_list->next;
  142. free(freeme);
  143. portEXIT_CRITICAL(&taskwdt_spinlock);
  144. return;
  145. }
  146. while (wdttask->next!=NULL && wdttask->next->task_handle!=handle) wdttask=wdttask->next;
  147. if (!wdttask->next) {
  148. ESP_LOGE(TAG, "task_wdt_delete: Task never called task_wdt_feed!");
  149. portEXIT_CRITICAL(&taskwdt_spinlock);
  150. return;
  151. }
  152. wdt_task_t *freeme=wdttask->next;
  153. wdttask->next=wdttask->next->next;
  154. free(freeme);
  155. }
  156. portEXIT_CRITICAL(&taskwdt_spinlock);
  157. }
  158. #if CONFIG_TASK_WDT_CHECK_IDLE_TASK
  159. static bool idle_hook(void) {
  160. #if !CONFIG_TASK_WDT_CHECK_IDLE_TASK_CPU1
  161. if (xPortGetCoreID()!=0) return true;
  162. #endif
  163. esp_task_wdt_feed();
  164. return true;
  165. }
  166. #endif
  167. void esp_task_wdt_init() {
  168. TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE;
  169. TIMERG0.wdt_config0.sys_reset_length=7; //3.2uS
  170. TIMERG0.wdt_config0.cpu_reset_length=7; //3.2uS
  171. TIMERG0.wdt_config0.level_int_en=1;
  172. TIMERG0.wdt_config0.stg0=TIMG_WDT_STG_SEL_INT; //1st stage timeout: interrupt
  173. TIMERG0.wdt_config0.stg1=TIMG_WDT_STG_SEL_RESET_SYSTEM; //2nd stage timeout: reset system
  174. TIMERG0.wdt_config1.clk_prescale=80*500; //Prescaler: wdt counts in ticks of 0.5mS
  175. TIMERG0.wdt_config2=CONFIG_TASK_WDT_TIMEOUT_S*2000; //Set timeout before interrupt
  176. TIMERG0.wdt_config3=CONFIG_TASK_WDT_TIMEOUT_S*4000; //Set timeout before reset
  177. TIMERG0.wdt_config0.en=1;
  178. TIMERG0.wdt_feed=1;
  179. TIMERG0.wdt_wprotect=0;
  180. #if CONFIG_TASK_WDT_CHECK_IDLE_TASK
  181. esp_register_freertos_idle_hook(idle_hook);
  182. #endif
  183. esp_intr_alloc(ETS_TG0_WDT_LEVEL_INTR_SOURCE, 0, task_wdt_isr, NULL, NULL);
  184. }
  185. #endif