int_wdt.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright 2015-2020 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 <stdbool.h>
  18. #include "sdkconfig.h"
  19. #include "freertos/FreeRTOS.h"
  20. #include "freertos/task.h"
  21. #include "esp_types.h"
  22. #include "esp_err.h"
  23. #include "esp_intr_alloc.h"
  24. #include "esp_attr.h"
  25. #include "esp_freertos_hooks.h"
  26. #include "soc/timer_periph.h"
  27. #include "driver/timer.h"
  28. #include "driver/periph_ctrl.h"
  29. #include "esp_int_wdt.h"
  30. #include "esp_private/system_internal.h"
  31. #include "hal/cpu_hal.h"
  32. #include "hal/timer_types.h"
  33. #include "hal/wdt_hal.h"
  34. #include "hal/interrupt_controller_hal.h"
  35. #if CONFIG_ESP_INT_WDT
  36. #define WDT_INT_NUM ETS_T1_WDT_INUM
  37. #define IWDT_INSTANCE WDT_MWDT1
  38. #define IWDT_PRESCALER MWDT1_TICK_PRESCALER //Tick period of 500us if WDT source clock is 80MHz
  39. #define IWDT_TICKS_PER_US MWDT1_TICKS_PER_US
  40. #define IWDT_INITIAL_TIMEOUT_S 5
  41. static wdt_hal_context_t iwdt_context;
  42. #if CONFIG_ESP32_ECO3_CACHE_LOCK_FIX
  43. /*
  44. * This parameter is used to indicate the response time of Interrupt watchdog to
  45. * identify the live lock.
  46. */
  47. #define IWDT_LIVELOCK_TIMEOUT_MS (20)
  48. extern uint32_t _lx_intr_livelock_counter, _lx_intr_livelock_max;
  49. #endif
  50. //Take care: the tick hook can also be called before esp_int_wdt_init() is called.
  51. #if CONFIG_ESP_INT_WDT_CHECK_CPU1
  52. //Not static; the ISR assembly checks this.
  53. bool int_wdt_app_cpu_ticked = false;
  54. static void IRAM_ATTR tick_hook(void)
  55. {
  56. if (cpu_hal_get_core_id() != 0) {
  57. int_wdt_app_cpu_ticked = true;
  58. } else {
  59. //Only feed wdt if app cpu also ticked.
  60. if (int_wdt_app_cpu_ticked) {
  61. //Todo: Check if there's a way to avoid reconfiguring the stages on each feed.
  62. wdt_hal_write_protect_disable(&iwdt_context);
  63. //Reconfigure stage timeouts
  64. #if CONFIG_ESP32_ECO3_CACHE_LOCK_FIX
  65. _lx_intr_livelock_counter = 0;
  66. wdt_hal_config_stage(&iwdt_context, WDT_STAGE0,
  67. CONFIG_ESP_INT_WDT_TIMEOUT_MS * 1000 / IWDT_TICKS_PER_US / (_lx_intr_livelock_max + 1), WDT_STAGE_ACTION_INT); //Set timeout before interrupt
  68. #else
  69. wdt_hal_config_stage(&iwdt_context, WDT_STAGE0, CONFIG_ESP_INT_WDT_TIMEOUT_MS * 1000 / IWDT_TICKS_PER_US, WDT_STAGE_ACTION_INT); //Set timeout before interrupt
  70. #endif
  71. wdt_hal_config_stage(&iwdt_context, WDT_STAGE1, 2 * CONFIG_ESP_INT_WDT_TIMEOUT_MS * 1000 / IWDT_TICKS_PER_US, WDT_STAGE_ACTION_RESET_SYSTEM); //Set timeout before reset
  72. wdt_hal_feed(&iwdt_context);
  73. wdt_hal_write_protect_enable(&iwdt_context);
  74. int_wdt_app_cpu_ticked = false;
  75. }
  76. }
  77. }
  78. #else
  79. static void IRAM_ATTR tick_hook(void)
  80. {
  81. #if !CONFIG_FREERTOS_UNICORE
  82. if (cpu_hal_get_core_id() != 0) {
  83. return;
  84. }
  85. #endif
  86. //Todo: Check if there's a way to avoid reconfiguring the stages on each feed.
  87. wdt_hal_write_protect_disable(&iwdt_context);
  88. //Reconfigure stage timeouts
  89. wdt_hal_config_stage(&iwdt_context, WDT_STAGE0, CONFIG_ESP_INT_WDT_TIMEOUT_MS * 1000 / IWDT_TICKS_PER_US, WDT_STAGE_ACTION_INT); //Set timeout before interrupt
  90. wdt_hal_config_stage(&iwdt_context, WDT_STAGE1, 2 * CONFIG_ESP_INT_WDT_TIMEOUT_MS * 1000 / IWDT_TICKS_PER_US, WDT_STAGE_ACTION_RESET_SYSTEM); //Set timeout before reset
  91. wdt_hal_feed(&iwdt_context);
  92. wdt_hal_write_protect_enable(&iwdt_context);
  93. }
  94. #endif
  95. void esp_int_wdt_init(void)
  96. {
  97. periph_module_enable(PERIPH_TIMG1_MODULE);
  98. //The timer configs initially are set to 5 seconds, to make sure the CPU can start up. The tick hook sets
  99. //it to their actual value.
  100. wdt_hal_init(&iwdt_context, IWDT_INSTANCE, IWDT_PRESCALER, true);
  101. wdt_hal_write_protect_disable(&iwdt_context);
  102. //1st stage timeout: interrupt
  103. wdt_hal_config_stage(&iwdt_context, WDT_STAGE0, IWDT_INITIAL_TIMEOUT_S * 1000000 / IWDT_TICKS_PER_US, WDT_STAGE_ACTION_INT);
  104. //2nd stage timeout: reset system
  105. wdt_hal_config_stage(&iwdt_context, WDT_STAGE1, IWDT_INITIAL_TIMEOUT_S * 1000000 / IWDT_TICKS_PER_US, WDT_STAGE_ACTION_RESET_SYSTEM);
  106. //Enable WDT
  107. wdt_hal_enable(&iwdt_context);
  108. wdt_hal_write_protect_enable(&iwdt_context);
  109. #if (CONFIG_ESP32_ECO3_CACHE_LOCK_FIX && CONFIG_BTDM_CTRL_HLI)
  110. #define APB_DCRSET (0x200c)
  111. #define APB_ITCTRL (0x3f00)
  112. #define ERI_ADDR(APB) (0x100000 + (APB))
  113. #define _SYM2STR(x) # x
  114. #define SYM2STR(x) _SYM2STR(x)
  115. uint32_t eriadrs, scratch = 0, immediate = 0;
  116. if (soc_has_cache_lock_bug()) {
  117. if (xPortGetCoreID() != CONFIG_BTDM_CTRL_PINNED_TO_CORE) {
  118. __asm__ __volatile__ (
  119. /* Enable Xtensa Debug Module Integration Mode */
  120. "movi %[ERI], " SYM2STR(ERI_ADDR(APB_ITCTRL)) "\n"
  121. "rer %[REG], %[ERI]\n"
  122. "movi %[IMM], 1\n"
  123. "or %[REG], %[IMM], %[REG]\n"
  124. "wer %[REG], %[ERI]\n"
  125. /* Enable Xtensa Debug Module BreakIn signal */
  126. "movi %[ERI], " SYM2STR(ERI_ADDR(APB_DCRSET)) "\n"
  127. "rer %[REG], %[ERI]\n"
  128. "movi %[IMM], 0x10000\n"
  129. "or %[REG], %[IMM], %[REG]\n"
  130. "wer %[REG], %[ERI]\n"
  131. : [ERI] "=r" (eriadrs), [REG] "+r" (scratch), [IMM] "+r" (immediate)
  132. );
  133. }
  134. }
  135. #endif
  136. }
  137. void esp_int_wdt_cpu_init(void)
  138. {
  139. assert((CONFIG_ESP_INT_WDT_TIMEOUT_MS >= (portTICK_PERIOD_MS << 1)) && "Interrupt watchdog timeout needs to meet twice the RTOS tick period!");
  140. esp_register_freertos_tick_hook_for_cpu(tick_hook, cpu_hal_get_core_id());
  141. ESP_INTR_DISABLE(WDT_INT_NUM);
  142. intr_matrix_set(cpu_hal_get_core_id(), ETS_TG1_WDT_LEVEL_INTR_SOURCE, WDT_INT_NUM);
  143. /* Set the type and priority to watch dog interrupts */
  144. #if SOC_CPU_HAS_FLEXIBLE_INTC
  145. interrupt_controller_hal_set_int_type(WDT_INT_NUM, INTR_TYPE_LEVEL);
  146. interrupt_controller_hal_set_int_level(WDT_INT_NUM, SOC_INTERRUPT_LEVEL_MEDIUM);
  147. #endif
  148. #if CONFIG_ESP32_ECO3_CACHE_LOCK_FIX
  149. /*
  150. * This is a workaround for issue 3.15 in "ESP32 ECO and workarounds for
  151. * Bugs" document.
  152. */
  153. _lx_intr_livelock_counter = 0;
  154. if (soc_has_cache_lock_bug()) {
  155. assert((portTICK_PERIOD_MS << 1) <= IWDT_LIVELOCK_TIMEOUT_MS);
  156. assert(CONFIG_ESP_INT_WDT_TIMEOUT_MS >= (IWDT_LIVELOCK_TIMEOUT_MS * 3));
  157. _lx_intr_livelock_max = CONFIG_ESP_INT_WDT_TIMEOUT_MS / IWDT_LIVELOCK_TIMEOUT_MS - 1;
  158. }
  159. #endif
  160. // We do not register a handler for the watchdog interrupt because:
  161. // 1. Interrupt level 4 on Xtensa architecture is not servicable from C
  162. // 2. Instead, we set the entry of watchdog interrupt to the panic handler, see riscv/vector.S and xtensa_vectors.S
  163. ESP_INTR_ENABLE(WDT_INT_NUM);
  164. }
  165. #endif