6#include <zephyr/drivers/watchdog.h>
7#include <zephyr/sys/reboot.h>
9#ifdef USE_ZEPHYR_DWT_MICROS
10#include <cmsis_core.h>
21static int wdt_channel_id = -1;
22static const device *
const WDT = DEVICE_DT_GET(DT_ALIAS(watchdog0));
29#ifdef USE_ZEPHYR_DWT_MICROS
45static constexpr uint32_t CYCLES_PER_US = 64;
48static constexpr int32_t MAX_LEAD_US = 1000000;
51 const unsigned int key = irq_lock();
53 const uint32_t elapsed_us = (cycles - micros_state.last_cycles) / CYCLES_PER_US;
54 micros_state.last_cycles += elapsed_us * CYCLES_PER_US;
57 const uint32_t kernel_us = k_ticks_to_us_floor32(k_uptime_ticks());
59 const int32_t tick_us = k_ticks_to_us_ceil32(1) + 1;
60 const int32_t ahead =
static_cast<int32_t
>(
value - kernel_us);
61 const bool unlocked = arch_irq_unlocked(
key);
62 if ((ahead < -tick_us || ahead > MAX_LEAD_US) && (unlocked || elapsed_us == 0)) {
65 micros_state.last_cycles = cycles;
66 }
else if (ahead >= tick_us && ahead <= MAX_LEAD_US && unlocked) {
69 const uint32_t cap = kernel_us + tick_us - 1;
70 value =
static_cast<int32_t
>(cap - micros_state.value) > 0 ? cap : micros_state.
value;
72 micros_state.value =
value;
80#ifdef USE_ZEPHYR_DWT_MICROS
81 CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
82 DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
85 if (device_is_ready(WDT)) {
86 static wdt_timeout_cfg wdt_config{};
87 wdt_config.flags = WDT_FLAG_RESET_SOC;
88#ifndef USE_BOOTLOADER_MCUBOOT
91 wdt_config.window.max = 30000;
92#elif defined(USE_ZIGBEE)
94 wdt_config.window.max = 10000;
96 wdt_config.window.max = 2000;
98 wdt_channel_id = wdt_install_timeout(WDT, &wdt_config);
99 if (wdt_channel_id >= 0) {
102 options |= WDT_OPT_PAUSE_HALTED_BY_DBG;
105 options |= WDT_OPT_PAUSE_IN_SLEEP;
116#ifdef CONFIG_WATCHDOG
117 if (wdt_channel_id >= 0) {
118 wdt_feed(WDT, wdt_channel_id);
pthread_t & main_loop_thread()
uint32_t IRAM_ATTR HOT micros()