task_wdt.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdint.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <stdbool.h>
  11. #include "sdkconfig.h"
  12. #include "freertos/FreeRTOS.h"
  13. #include "freertos/task.h"
  14. #include "freertos/queue.h"
  15. #include "freertos/semphr.h"
  16. #include <esp_types.h>
  17. #include "esp_err.h"
  18. #include "esp_intr_alloc.h"
  19. #include "esp_attr.h"
  20. #include "esp_debug_helpers.h"
  21. #include "esp_freertos_hooks.h"
  22. #include "soc/timer_periph.h"
  23. #include "esp_log.h"
  24. #include "driver/timer.h"
  25. #include "driver/periph_ctrl.h"
  26. #include "esp_task_wdt.h"
  27. #include "esp_private/system_internal.h"
  28. #include "esp_private/crosscore_int.h"
  29. #include "hal/timer_types.h"
  30. #include "hal/wdt_hal.h"
  31. static const char *TAG = "task_wdt";
  32. //Assertion macro where, if 'cond' is false, will exit the critical section and return 'ret'
  33. #define ASSERT_EXIT_CRIT_RETURN(cond, ret) ({ \
  34. if(!(cond)){ \
  35. portEXIT_CRITICAL(&twdt_spinlock); \
  36. return ret; \
  37. } \
  38. })
  39. //Empty define used in ASSERT_EXIT_CRIT_RETURN macro when returning in void
  40. #define VOID_RETURN
  41. //HAL related variables and constants
  42. #define TWDT_INSTANCE WDT_MWDT0
  43. #define TWDT_TICKS_PER_US MWDT0_TICKS_PER_US
  44. #define TWDT_PRESCALER MWDT0_TICK_PRESCALER //Tick period of 500us if WDT source clock is 80MHz
  45. static wdt_hal_context_t twdt_context;
  46. //Structure used for each subscribed task
  47. typedef struct twdt_task_t twdt_task_t;
  48. struct twdt_task_t {
  49. TaskHandle_t task_handle;
  50. bool has_reset;
  51. twdt_task_t *next;
  52. };
  53. //Structure used to hold run time configuration of the TWDT
  54. typedef struct twdt_config_t twdt_config_t;
  55. struct twdt_config_t {
  56. twdt_task_t *list; //Linked list of subscribed tasks
  57. uint32_t timeout; //Timeout period of TWDT
  58. bool panic; //Flag to trigger panic when TWDT times out
  59. intr_handle_t intr_handle;
  60. };
  61. static twdt_config_t *twdt_config = NULL;
  62. static portMUX_TYPE twdt_spinlock = portMUX_INITIALIZER_UNLOCKED;
  63. /*
  64. * Idle hook callback for Idle Tasks to reset the TWDT. This callback will only
  65. * be registered to the Idle Hook of a particular core when the corresponding
  66. * Idle Task subscribes to the TWDT.
  67. */
  68. static bool idle_hook_cb(void)
  69. {
  70. esp_task_wdt_reset();
  71. return true;
  72. }
  73. /*
  74. * Internal function that looks for the target task in the TWDT task list.
  75. * Returns the list item if found and returns null if not found. Also checks if
  76. * all the other tasks have reset. Should be called within critical.
  77. */
  78. static twdt_task_t *find_task_in_twdt_list(TaskHandle_t handle, bool *all_reset)
  79. {
  80. twdt_task_t *target = NULL;
  81. *all_reset = true;
  82. for(twdt_task_t *task = twdt_config->list; task != NULL; task = task->next){
  83. if(task->task_handle == handle){
  84. target = task; //Get pointer to target task list member
  85. }else{
  86. if(task->has_reset == false){ //If a task has yet to reset
  87. *all_reset = false;
  88. }
  89. }
  90. }
  91. return target;
  92. }
  93. /*
  94. * Resets the hardware timer and has_reset flags of each task on the list.
  95. * Called within critical
  96. */
  97. static void reset_hw_timer(void)
  98. {
  99. //All tasks have reset; time to reset the hardware timer.
  100. wdt_hal_write_protect_disable(&twdt_context);
  101. wdt_hal_feed(&twdt_context);
  102. wdt_hal_write_protect_enable(&twdt_context);
  103. //Clear all has_reset flags in list
  104. for (twdt_task_t *task = twdt_config->list; task != NULL; task = task->next){
  105. task->has_reset=false;
  106. }
  107. }
  108. /*
  109. * This function is called by task_wdt_isr function (ISR for when TWDT times out).
  110. * It can be redefined in user code to handle twdt events.
  111. * Note: It has the same limitations as the interrupt function.
  112. * Do not use ESP_LOGI functions inside.
  113. */
  114. void __attribute__((weak)) esp_task_wdt_isr_user_handler(void)
  115. {
  116. }
  117. /*
  118. * ISR for when TWDT times out. Checks for which tasks have not reset. Also
  119. * triggers panic if configured to do so
  120. */
  121. static void task_wdt_isr(void *arg)
  122. {
  123. portENTER_CRITICAL_ISR(&twdt_spinlock);
  124. twdt_task_t *twdttask;
  125. const char *cpu;
  126. //Reset hardware timer so that 2nd stage timeout is not reached (will trigger system reset)
  127. wdt_hal_write_protect_disable(&twdt_context);
  128. wdt_hal_handle_intr(&twdt_context); //Feeds WDT and clears acknowledges interrupt
  129. wdt_hal_write_protect_enable(&twdt_context);
  130. //We are taking a spinlock while doing I/O (ESP_EARLY_LOGE) here. Normally, that is a pretty
  131. //bad thing, possibly (temporarily) hanging up the 2nd core and stopping FreeRTOS. In this case,
  132. //something bad already happened and reporting this is considered more important
  133. //than the badness caused by a spinlock here.
  134. //Return immediately if no tasks have been added to task list
  135. ASSERT_EXIT_CRIT_RETURN((twdt_config->list != NULL), VOID_RETURN);
  136. //Watchdog got triggered because at least one task did not reset in time.
  137. ESP_EARLY_LOGE(TAG, "Task watchdog got triggered. The following tasks did not reset the watchdog in time:");
  138. for (twdttask=twdt_config->list; twdttask!=NULL; twdttask=twdttask->next) {
  139. if (!twdttask->has_reset) {
  140. cpu=xTaskGetAffinity(twdttask->task_handle)==0?DRAM_STR("CPU 0"):DRAM_STR("CPU 1");
  141. if (xTaskGetAffinity(twdttask->task_handle)==tskNO_AFFINITY) {
  142. cpu=DRAM_STR("CPU 0/1");
  143. }
  144. ESP_EARLY_LOGE(TAG, " - %s (%s)", pcTaskGetTaskName(twdttask->task_handle), cpu);
  145. }
  146. }
  147. ESP_EARLY_LOGE(TAG, "%s", DRAM_STR("Tasks currently running:"));
  148. for (int x=0; x<portNUM_PROCESSORS; x++) {
  149. ESP_EARLY_LOGE(TAG, "CPU %d: %s", x, pcTaskGetTaskName(xTaskGetCurrentTaskHandleForCPU(x)));
  150. }
  151. esp_task_wdt_isr_user_handler();
  152. if (twdt_config->panic){ //Trigger Panic if configured to do so
  153. ESP_EARLY_LOGE(TAG, "Aborting.");
  154. portEXIT_CRITICAL_ISR(&twdt_spinlock);
  155. esp_reset_reason_set_hint(ESP_RST_TASK_WDT);
  156. abort();
  157. } else {
  158. #if !CONFIG_IDF_TARGET_ESP32C3 && !CONFIG_IDF_TARGET_ESP32H2 // TODO: ESP32-C3 IDF-2986
  159. int current_core = xPortGetCoreID();
  160. //Print backtrace of current core
  161. ESP_EARLY_LOGE(TAG, "Print CPU %d (current core) backtrace", current_core);
  162. esp_backtrace_print(100);
  163. #if !CONFIG_FREERTOS_UNICORE
  164. //Print backtrace of other core
  165. ESP_EARLY_LOGE(TAG, "Print CPU %d backtrace", !current_core);
  166. esp_crosscore_int_send_print_backtrace(!current_core);
  167. #endif
  168. #endif
  169. }
  170. portEXIT_CRITICAL_ISR(&twdt_spinlock);
  171. }
  172. /*
  173. * Initializes the TWDT by allocating memory for the config data
  174. * structure, obtaining the idle task handles/registering idle hooks, and
  175. * setting the hardware timer registers. If reconfiguring, it will just modify
  176. * wdt_config and reset the hardware timer.
  177. */
  178. esp_err_t esp_task_wdt_init(uint32_t timeout, bool panic)
  179. {
  180. portENTER_CRITICAL(&twdt_spinlock);
  181. if(twdt_config == NULL){ //TWDT not initialized yet
  182. //Allocate memory for wdt_config
  183. twdt_config = calloc(1, sizeof(twdt_config_t));
  184. ASSERT_EXIT_CRIT_RETURN((twdt_config != NULL), ESP_ERR_NO_MEM);
  185. twdt_config->list = NULL;
  186. twdt_config->timeout = timeout;
  187. twdt_config->panic = panic;
  188. //Register Interrupt and ISR
  189. ESP_ERROR_CHECK(esp_intr_alloc(ETS_TG0_WDT_LEVEL_INTR_SOURCE, 0, task_wdt_isr, NULL, &twdt_config->intr_handle));
  190. //Configure hardware timer
  191. periph_module_enable(PERIPH_TIMG0_MODULE);
  192. wdt_hal_init(&twdt_context, TWDT_INSTANCE, TWDT_PRESCALER, true);
  193. wdt_hal_write_protect_disable(&twdt_context);
  194. //Configure 1st stage timeout and behavior
  195. wdt_hal_config_stage(&twdt_context, WDT_STAGE0, twdt_config->timeout * (1000000 / TWDT_TICKS_PER_US), WDT_STAGE_ACTION_INT);
  196. //Configure 2nd stage timeout and behavior
  197. wdt_hal_config_stage(&twdt_context, WDT_STAGE1, twdt_config->timeout * (2 * 1000000 / TWDT_TICKS_PER_US), WDT_STAGE_ACTION_RESET_SYSTEM);
  198. //Enable the WDT
  199. wdt_hal_enable(&twdt_context);
  200. wdt_hal_write_protect_enable(&twdt_context);
  201. } else { //twdt_config previously initialized
  202. //Reconfigure task wdt
  203. twdt_config->panic = panic;
  204. twdt_config->timeout = timeout;
  205. //Reconfigure hardware timer
  206. wdt_hal_write_protect_disable(&twdt_context);
  207. wdt_hal_disable(&twdt_context);
  208. wdt_hal_config_stage(&twdt_context, WDT_STAGE0, twdt_config->timeout * (1000 * 1000 / TWDT_TICKS_PER_US), WDT_STAGE_ACTION_INT);
  209. wdt_hal_config_stage(&twdt_context, WDT_STAGE1, twdt_config->timeout * (2 * 1000 * 1000 / TWDT_TICKS_PER_US), WDT_STAGE_ACTION_RESET_SYSTEM);
  210. wdt_hal_enable(&twdt_context);
  211. wdt_hal_write_protect_enable(&twdt_context);
  212. }
  213. portEXIT_CRITICAL(&twdt_spinlock);
  214. return ESP_OK;
  215. }
  216. esp_err_t esp_task_wdt_deinit(void)
  217. {
  218. portENTER_CRITICAL(&twdt_spinlock);
  219. //TWDT must already be initialized
  220. ASSERT_EXIT_CRIT_RETURN((twdt_config != NULL), ESP_ERR_NOT_FOUND);
  221. //Task list must be empty
  222. ASSERT_EXIT_CRIT_RETURN((twdt_config->list == NULL), ESP_ERR_INVALID_STATE);
  223. //Disable hardware timer
  224. wdt_hal_deinit(&twdt_context);
  225. ESP_ERROR_CHECK(esp_intr_free(twdt_config->intr_handle)); //Unregister interrupt
  226. free(twdt_config); //Free twdt_config
  227. twdt_config = NULL;
  228. portEXIT_CRITICAL(&twdt_spinlock);
  229. return ESP_OK;
  230. }
  231. esp_err_t esp_task_wdt_add(TaskHandle_t handle)
  232. {
  233. portENTER_CRITICAL(&twdt_spinlock);
  234. //TWDT must already be initialized
  235. ASSERT_EXIT_CRIT_RETURN((twdt_config != NULL), ESP_ERR_INVALID_STATE);
  236. twdt_task_t *target_task;
  237. bool all_reset;
  238. if (handle == NULL){ //Get handle of current task if none is provided
  239. handle = xTaskGetCurrentTaskHandle();
  240. }
  241. //Check if tasks exists in task list, and if all other tasks have reset
  242. target_task = find_task_in_twdt_list(handle, &all_reset);
  243. //task cannot be already subscribed
  244. ASSERT_EXIT_CRIT_RETURN((target_task == NULL), ESP_ERR_INVALID_ARG);
  245. //Add target task to TWDT task list
  246. target_task = calloc(1,sizeof(twdt_task_t));
  247. ASSERT_EXIT_CRIT_RETURN((target_task != NULL), ESP_ERR_NO_MEM);
  248. target_task->task_handle = handle;
  249. target_task->has_reset = true;
  250. target_task->next = NULL;
  251. if (twdt_config->list == NULL) { //Adding to empty list
  252. twdt_config->list = target_task;
  253. } else { //Adding to tail of list
  254. twdt_task_t *task;
  255. for (task = twdt_config->list; task->next != NULL; task = task->next){
  256. ; //point task to current tail of TWDT task list
  257. }
  258. task->next = target_task;
  259. }
  260. //If idle task, register the idle hook callback to appropriate core
  261. for(int i = 0; i < portNUM_PROCESSORS; i++){
  262. if(handle == xTaskGetIdleTaskHandleForCPU(i)){
  263. ESP_ERROR_CHECK(esp_register_freertos_idle_hook_for_cpu(idle_hook_cb, i));
  264. break;
  265. }
  266. }
  267. if(all_reset){ //Reset hardware timer if all other tasks in list have reset in
  268. reset_hw_timer();
  269. }
  270. portEXIT_CRITICAL(&twdt_spinlock); //Nested critical if Legacy
  271. return ESP_OK;
  272. }
  273. esp_err_t esp_task_wdt_reset(void)
  274. {
  275. portENTER_CRITICAL(&twdt_spinlock);
  276. //TWDT must already be initialized
  277. ASSERT_EXIT_CRIT_RETURN((twdt_config != NULL), ESP_ERR_INVALID_STATE);
  278. TaskHandle_t handle = xTaskGetCurrentTaskHandle();
  279. twdt_task_t *target_task;
  280. bool all_reset;
  281. //Check if task exists in task list, and if all other tasks have reset
  282. target_task = find_task_in_twdt_list(handle, &all_reset);
  283. //Return error if trying to reset task that is not on the task list
  284. ASSERT_EXIT_CRIT_RETURN((target_task != NULL), ESP_ERR_NOT_FOUND);
  285. target_task->has_reset = true; //Reset the task if it's on the task list
  286. if(all_reset){ //Reset if all other tasks in list have reset in
  287. reset_hw_timer();
  288. }
  289. portEXIT_CRITICAL(&twdt_spinlock);
  290. return ESP_OK;
  291. }
  292. esp_err_t esp_task_wdt_delete(TaskHandle_t handle)
  293. {
  294. if(handle == NULL){
  295. handle = xTaskGetCurrentTaskHandle();
  296. }
  297. portENTER_CRITICAL(&twdt_spinlock);
  298. //Return error if twdt has not been initialized
  299. ASSERT_EXIT_CRIT_RETURN((twdt_config != NULL), ESP_ERR_NOT_FOUND);
  300. twdt_task_t *target_task;
  301. bool all_reset;
  302. target_task = find_task_in_twdt_list(handle, &all_reset);
  303. //Task doesn't exist on list. Return error
  304. ASSERT_EXIT_CRIT_RETURN((target_task != NULL), ESP_ERR_INVALID_ARG);
  305. if(target_task == twdt_config->list){ //target_task is head of list. Delete
  306. twdt_config->list = target_task->next;
  307. free(target_task);
  308. }else{ //target_task not head of list. Delete
  309. twdt_task_t *prev;
  310. for (prev = twdt_config->list; prev->next != target_task; prev = prev->next){
  311. ; //point prev to task preceding target_task
  312. }
  313. prev->next = target_task->next;
  314. free(target_task);
  315. }
  316. //If idle task, deregister idle hook callback form appropriate core
  317. for(int i = 0; i < portNUM_PROCESSORS; i++){
  318. if(handle == xTaskGetIdleTaskHandleForCPU(i)){
  319. esp_deregister_freertos_idle_hook_for_cpu(idle_hook_cb, i);
  320. break;
  321. }
  322. }
  323. if(all_reset){ //Reset hardware timer if all remaining tasks have reset
  324. reset_hw_timer();
  325. }
  326. portEXIT_CRITICAL(&twdt_spinlock);
  327. return ESP_OK;
  328. }
  329. esp_err_t esp_task_wdt_status(TaskHandle_t handle)
  330. {
  331. if(handle == NULL){
  332. handle = xTaskGetCurrentTaskHandle();
  333. }
  334. portENTER_CRITICAL(&twdt_spinlock);
  335. //Return if TWDT is not initialized
  336. ASSERT_EXIT_CRIT_RETURN((twdt_config != NULL), ESP_ERR_INVALID_STATE);
  337. twdt_task_t *task;
  338. for(task = twdt_config->list; task!=NULL; task=task->next){
  339. //Return ESP_OK if task is found
  340. ASSERT_EXIT_CRIT_RETURN((task->task_handle != handle), ESP_OK);
  341. }
  342. //Task could not be found
  343. portEXIT_CRITICAL(&twdt_spinlock);
  344. return ESP_ERR_NOT_FOUND;
  345. }