task_wdt.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. #include <stdint.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <stdbool.h>
  19. #include "sdkconfig.h"
  20. #include "freertos/FreeRTOSConfig.h"
  21. #include "freertos/FreeRTOS.h"
  22. #include "freertos/task.h"
  23. #include "freertos/queue.h"
  24. #include "freertos/semphr.h"
  25. #include <esp_types.h>
  26. #include "esp_err.h"
  27. #include "esp_intr_alloc.h"
  28. #include "esp_attr.h"
  29. #include "esp_debug_helpers.h"
  30. #include "esp_freertos_hooks.h"
  31. #include "soc/timer_periph.h"
  32. #include "esp_log.h"
  33. #include "driver/timer.h"
  34. #include "driver/periph_ctrl.h"
  35. #include "esp_task_wdt.h"
  36. #include "esp_private/system_internal.h"
  37. #include "esp_private/crosscore_int.h"
  38. static const char *TAG = "task_wdt";
  39. //Assertion macro where, if 'cond' is false, will exit the critical section and return 'ret'
  40. #define ASSERT_EXIT_CRIT_RETURN(cond, ret) ({ \
  41. if(!(cond)){ \
  42. portEXIT_CRITICAL(&twdt_spinlock); \
  43. return ret; \
  44. } \
  45. })
  46. //Empty define used in ASSERT_EXIT_CRIT_RETURN macro when returning in void
  47. #define VOID_RETURN
  48. //Structure used for each subscribed task
  49. typedef struct twdt_task_t twdt_task_t;
  50. struct twdt_task_t {
  51. TaskHandle_t task_handle;
  52. bool has_reset;
  53. twdt_task_t *next;
  54. };
  55. //Structure used to hold run time configuration of the TWDT
  56. typedef struct twdt_config_t twdt_config_t;
  57. struct twdt_config_t {
  58. twdt_task_t *list; //Linked list of subscribed tasks
  59. uint32_t timeout; //Timeout period of TWDT
  60. bool panic; //Flag to trigger panic when TWDT times out
  61. intr_handle_t intr_handle;
  62. };
  63. static twdt_config_t *twdt_config = NULL;
  64. static portMUX_TYPE twdt_spinlock = portMUX_INITIALIZER_UNLOCKED;
  65. /*
  66. * Idle hook callback for Idle Tasks to reset the TWDT. This callback will only
  67. * be registered to the Idle Hook of a particular core when the corresponding
  68. * Idle Task subscribes to the TWDT.
  69. */
  70. static bool idle_hook_cb(void)
  71. {
  72. esp_task_wdt_reset();
  73. return true;
  74. }
  75. /*
  76. * Internal function that looks for the target task in the TWDT task list.
  77. * Returns the list item if found and returns null if not found. Also checks if
  78. * all the other tasks have reset. Should be called within critical.
  79. */
  80. static twdt_task_t *find_task_in_twdt_list(TaskHandle_t handle, bool *all_reset)
  81. {
  82. twdt_task_t *target = NULL;
  83. *all_reset = true;
  84. for(twdt_task_t *task = twdt_config->list; task != NULL; task = task->next){
  85. if(task->task_handle == handle){
  86. target = task; //Get pointer to target task list member
  87. }else{
  88. if(task->has_reset == false){ //If a task has yet to reset
  89. *all_reset = false;
  90. }
  91. }
  92. }
  93. return target;
  94. }
  95. /*
  96. * Resets the hardware timer and has_reset flags of each task on the list.
  97. * Called within critical
  98. */
  99. static void reset_hw_timer()
  100. {
  101. //All tasks have reset; time to reset the hardware timer.
  102. TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE;
  103. TIMERG0.wdt_feed=1;
  104. TIMERG0.wdt_wprotect=0;
  105. //Clear all has_reset flags in list
  106. for (twdt_task_t *task = twdt_config->list; task != NULL; task = task->next){
  107. task->has_reset=false;
  108. }
  109. }
  110. /*
  111. * This function is called by task_wdt_isr function (ISR for when TWDT times out).
  112. * It can be redefined in user code to handle twdt events.
  113. * Note: It has the same limitations as the interrupt function.
  114. * Do not use ESP_LOGI functions inside.
  115. */
  116. void __attribute__((weak)) esp_task_wdt_isr_user_handler(void)
  117. {
  118. }
  119. /*
  120. * ISR for when TWDT times out. Checks for which tasks have not reset. Also
  121. * triggers panic if configured to do so
  122. */
  123. static void task_wdt_isr(void *arg)
  124. {
  125. portENTER_CRITICAL_ISR(&twdt_spinlock);
  126. twdt_task_t *twdttask;
  127. const char *cpu;
  128. //Reset hardware timer so that 2nd stage timeout is not reached (will trigger system reset)
  129. TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE;
  130. TIMERG0.wdt_feed=1;
  131. TIMERG0.wdt_wprotect=0;
  132. //Acknowledge interrupt
  133. TIMERG0.int_clr_timers.wdt=1;
  134. //We are taking a spinlock while doing I/O (ESP_EARLY_LOGE) here. Normally, that is a pretty
  135. //bad thing, possibly (temporarily) hanging up the 2nd core and stopping FreeRTOS. In this case,
  136. //something bad already happened and reporting this is considered more important
  137. //than the badness caused by a spinlock here.
  138. //Return immediately if no tasks have been added to task list
  139. ASSERT_EXIT_CRIT_RETURN((twdt_config->list != NULL), VOID_RETURN);
  140. //Watchdog got triggered because at least one task did not reset in time.
  141. ESP_EARLY_LOGE(TAG, "Task watchdog got triggered. The following tasks did not reset the watchdog in time:");
  142. for (twdttask=twdt_config->list; twdttask!=NULL; twdttask=twdttask->next) {
  143. if (!twdttask->has_reset) {
  144. cpu=xTaskGetAffinity(twdttask->task_handle)==0?DRAM_STR("CPU 0"):DRAM_STR("CPU 1");
  145. if (xTaskGetAffinity(twdttask->task_handle)==tskNO_AFFINITY) cpu=DRAM_STR("CPU 0/1");
  146. ESP_EARLY_LOGE(TAG, " - %s (%s)", pcTaskGetTaskName(twdttask->task_handle), cpu);
  147. }
  148. }
  149. ESP_EARLY_LOGE(TAG, "%s", DRAM_STR("Tasks currently running:"));
  150. for (int x=0; x<portNUM_PROCESSORS; x++) {
  151. ESP_EARLY_LOGE(TAG, "CPU %d: %s", x, pcTaskGetTaskName(xTaskGetCurrentTaskHandleForCPU(x)));
  152. }
  153. esp_task_wdt_isr_user_handler();
  154. if (twdt_config->panic){ //Trigger Panic if configured to do so
  155. ESP_EARLY_LOGE(TAG, "Aborting.");
  156. portEXIT_CRITICAL_ISR(&twdt_spinlock);
  157. esp_reset_reason_set_hint(ESP_RST_TASK_WDT);
  158. abort();
  159. } else {
  160. int current_core = xPortGetCoreID();
  161. //Print backtrace of current core
  162. ESP_EARLY_LOGE(TAG, "Print CPU %d (current core) backtrace", current_core);
  163. esp_backtrace_print(100);
  164. #if !CONFIG_FREERTOS_UNICORE
  165. //Print backtrace of other core
  166. ESP_EARLY_LOGE(TAG, "Print CPU %d backtrace", !current_core);
  167. esp_crosscore_int_send_print_backtrace(!current_core);
  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. TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE; //Disable write protection
  193. TIMERG0.wdt_config0.sys_reset_length=7; //3.2uS
  194. TIMERG0.wdt_config0.cpu_reset_length=7; //3.2uS
  195. TIMERG0.wdt_config0.level_int_en=1;
  196. TIMERG0.wdt_config0.stg0=TIMG_WDT_STG_SEL_INT; //1st stage timeout: interrupt
  197. TIMERG0.wdt_config0.stg1=TIMG_WDT_STG_SEL_RESET_SYSTEM; //2nd stage timeout: reset system
  198. TIMERG0.wdt_config1.clk_prescale=80*500; //Prescaler: wdt counts in ticks of 0.5mS
  199. TIMERG0.wdt_config2=twdt_config->timeout*2000; //Set timeout before interrupt
  200. TIMERG0.wdt_config3=twdt_config->timeout*4000; //Set timeout before reset
  201. TIMERG0.wdt_config0.en=1;
  202. TIMERG0.wdt_feed=1;
  203. TIMERG0.wdt_wprotect=0; //Enable write protection
  204. }else{ //twdt_config previously initialized
  205. //Reconfigure task wdt
  206. twdt_config->panic = panic;
  207. twdt_config->timeout = timeout;
  208. //Reconfigure hardware timer
  209. TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE; //Disable write protection
  210. TIMERG0.wdt_config0.en=0; //Disable timer
  211. TIMERG0.wdt_config2=twdt_config->timeout*2000; //Set timeout before interrupt
  212. TIMERG0.wdt_config3=twdt_config->timeout*4000; //Set timeout before reset
  213. TIMERG0.wdt_config0.en=1; //Renable timer
  214. TIMERG0.wdt_feed=1; //Reset timer
  215. TIMERG0.wdt_wprotect=0; //Enable write protection
  216. }
  217. portEXIT_CRITICAL(&twdt_spinlock);
  218. return ESP_OK;
  219. }
  220. esp_err_t esp_task_wdt_deinit()
  221. {
  222. portENTER_CRITICAL(&twdt_spinlock);
  223. //TWDT must already be initialized
  224. ASSERT_EXIT_CRIT_RETURN((twdt_config != NULL), ESP_ERR_NOT_FOUND);
  225. //Task list must be empty
  226. ASSERT_EXIT_CRIT_RETURN((twdt_config->list == NULL), ESP_ERR_INVALID_STATE);
  227. //Disable hardware timer
  228. TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE; //Disable write protection
  229. TIMERG0.wdt_config0.en=0; //Disable timer
  230. TIMERG0.wdt_wprotect=0; //Enable write protection
  231. ESP_ERROR_CHECK(esp_intr_free(twdt_config->intr_handle)); //Unregister interrupt
  232. free(twdt_config); //Free twdt_config
  233. twdt_config = NULL;
  234. portEXIT_CRITICAL(&twdt_spinlock);
  235. return ESP_OK;
  236. }
  237. esp_err_t esp_task_wdt_add(TaskHandle_t handle)
  238. {
  239. portENTER_CRITICAL(&twdt_spinlock);
  240. //TWDT must already be initialized
  241. ASSERT_EXIT_CRIT_RETURN((twdt_config != NULL), ESP_ERR_INVALID_STATE);
  242. twdt_task_t *target_task;
  243. bool all_reset;
  244. if (handle == NULL){ //Get handle of current task if none is provided
  245. handle = xTaskGetCurrentTaskHandle();
  246. }
  247. //Check if tasks exists in task list, and if all other tasks have reset
  248. target_task = find_task_in_twdt_list(handle, &all_reset);
  249. //task cannot be already subscribed
  250. ASSERT_EXIT_CRIT_RETURN((target_task == NULL), ESP_ERR_INVALID_ARG);
  251. //Add target task to TWDT task list
  252. target_task = calloc(1,sizeof(twdt_task_t));
  253. ASSERT_EXIT_CRIT_RETURN((target_task != NULL), ESP_ERR_NO_MEM);
  254. target_task->task_handle = handle;
  255. target_task->has_reset = true;
  256. target_task->next = NULL;
  257. if (twdt_config->list == NULL) { //Adding to empty list
  258. twdt_config->list = target_task;
  259. } else { //Adding to tail of list
  260. twdt_task_t *task;
  261. for (task = twdt_config->list; task->next != NULL; task = task->next){
  262. ; //point task to current tail of TWDT task list
  263. }
  264. task->next = target_task;
  265. }
  266. //If idle task, register the idle hook callback to appropriate core
  267. for(int i = 0; i < portNUM_PROCESSORS; i++){
  268. if(handle == xTaskGetIdleTaskHandleForCPU(i)){
  269. ESP_ERROR_CHECK(esp_register_freertos_idle_hook_for_cpu(idle_hook_cb, i));
  270. break;
  271. }
  272. }
  273. if(all_reset){ //Reset hardware timer if all other tasks in list have reset in
  274. reset_hw_timer();
  275. }
  276. portEXIT_CRITICAL(&twdt_spinlock); //Nested critical if Legacy
  277. return ESP_OK;
  278. }
  279. esp_err_t esp_task_wdt_reset()
  280. {
  281. portENTER_CRITICAL(&twdt_spinlock);
  282. //TWDT must already be initialized
  283. ASSERT_EXIT_CRIT_RETURN((twdt_config != NULL), ESP_ERR_INVALID_STATE);
  284. TaskHandle_t handle = xTaskGetCurrentTaskHandle();
  285. twdt_task_t *target_task;
  286. bool all_reset;
  287. //Check if task exists in task list, and if all other tasks have reset
  288. target_task = find_task_in_twdt_list(handle, &all_reset);
  289. //Return error if trying to reset task that is not on the task list
  290. ASSERT_EXIT_CRIT_RETURN((target_task != NULL), ESP_ERR_NOT_FOUND);
  291. target_task->has_reset = true; //Reset the task if it's on the task list
  292. if(all_reset){ //Reset if all other tasks in list have reset in
  293. reset_hw_timer();
  294. }
  295. portEXIT_CRITICAL(&twdt_spinlock);
  296. return ESP_OK;
  297. }
  298. esp_err_t esp_task_wdt_delete(TaskHandle_t handle)
  299. {
  300. if(handle == NULL){
  301. handle = xTaskGetCurrentTaskHandle();
  302. }
  303. portENTER_CRITICAL(&twdt_spinlock);
  304. //Return error if twdt has not been initialized
  305. ASSERT_EXIT_CRIT_RETURN((twdt_config != NULL), ESP_ERR_NOT_FOUND);
  306. twdt_task_t *target_task;
  307. bool all_reset;
  308. target_task = find_task_in_twdt_list(handle, &all_reset);
  309. //Task doesn't exist on list. Return error
  310. ASSERT_EXIT_CRIT_RETURN((target_task != NULL), ESP_ERR_INVALID_ARG);
  311. if(target_task == twdt_config->list){ //target_task is head of list. Delete
  312. twdt_config->list = target_task->next;
  313. free(target_task);
  314. }else{ //target_task not head of list. Delete
  315. twdt_task_t *prev;
  316. for (prev = twdt_config->list; prev->next != target_task; prev = prev->next){
  317. ; //point prev to task preceding target_task
  318. }
  319. prev->next = target_task->next;
  320. free(target_task);
  321. }
  322. //If idle task, deregister idle hook callback form appropriate core
  323. for(int i = 0; i < portNUM_PROCESSORS; i++){
  324. if(handle == xTaskGetIdleTaskHandleForCPU(i)){
  325. esp_deregister_freertos_idle_hook_for_cpu(idle_hook_cb, i);
  326. break;
  327. }
  328. }
  329. if(all_reset){ //Reset hardware timer if all remaining tasks have reset
  330. reset_hw_timer();
  331. }
  332. portEXIT_CRITICAL(&twdt_spinlock);
  333. return ESP_OK;
  334. }
  335. esp_err_t esp_task_wdt_status(TaskHandle_t handle)
  336. {
  337. if(handle == NULL){
  338. handle = xTaskGetCurrentTaskHandle();
  339. }
  340. portENTER_CRITICAL(&twdt_spinlock);
  341. //Return if TWDT is not initialized
  342. ASSERT_EXIT_CRIT_RETURN((twdt_config != NULL), ESP_ERR_INVALID_STATE);
  343. twdt_task_t *task;
  344. for(task = twdt_config->list; task!=NULL; task=task->next){
  345. //Return ESP_OK if task is found
  346. ASSERT_EXIT_CRIT_RETURN((task->task_handle != handle), ESP_OK);
  347. }
  348. //Task could not be found
  349. portEXIT_CRITICAL(&twdt_spinlock);
  350. return ESP_ERR_NOT_FOUND;
  351. }