6#include <zephyr/drivers/watchdog.h>
7#include <zephyr/sys/reboot.h>
19static int wdt_channel_id = -1;
20static const device *
const WDT = DEVICE_DT_GET(DT_ALIAS(watchdog0));
26#if CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC == 1000000 || defined(USE_ZEPHYR_DWT_MICROS)
29 SystemCoreClockUpdate();
34#if CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC == 1000000
39#elif defined(USE_ZEPHYR_DWT_MICROS)
47#ifdef USE_ZEPHYR_DWT_MICROS
63static constexpr uint32_t CYCLES_PER_US = 64;
66static constexpr int32_t MAX_LEAD_US = 1000000;
69 const unsigned int key = irq_lock();
71 const uint32_t elapsed_us = (cycles - micros_state.last_cycles) / CYCLES_PER_US;
72 micros_state.last_cycles += elapsed_us * CYCLES_PER_US;
75 const uint32_t kernel_us = k_ticks_to_us_floor32(k_uptime_ticks());
77 const int32_t tick_us = k_ticks_to_us_ceil32(1) + 1;
78 const int32_t ahead =
static_cast<int32_t
>(
value - kernel_us);
79 const bool unlocked = arch_irq_unlocked(
key);
80 if ((ahead < -tick_us || ahead > MAX_LEAD_US) && (unlocked || elapsed_us == 0)) {
83 micros_state.last_cycles = cycles;
84 }
else if (ahead >= tick_us && ahead <= MAX_LEAD_US && unlocked) {
87 const uint32_t cap = kernel_us + tick_us - 1;
88 value =
static_cast<int32_t
>(cap - micros_state.value) > 0 ? cap : micros_state.
value;
90 micros_state.value =
value;
98#if CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC == 1000000
100#elif defined(USE_ZEPHYR_DWT_MICROS)
101 CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
102 DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
104#ifdef CONFIG_WATCHDOG
105 if (device_is_ready(WDT)) {
106 static wdt_timeout_cfg wdt_config{};
107 wdt_config.flags = WDT_FLAG_RESET_SOC;
108#ifndef USE_BOOTLOADER_MCUBOOT
111 wdt_config.window.max = 30000;
112#elif defined(USE_ZIGBEE)
114 wdt_config.window.max = 10000;
116 wdt_config.window.max = 2000;
118 wdt_channel_id = wdt_install_timeout(WDT, &wdt_config);
119 if (wdt_channel_id >= 0) {
121 uint8_t
options = WDT_OPT_PAUSE_HALTED_BY_DBG;
123 options |= WDT_OPT_PAUSE_IN_SLEEP;
134#ifdef CONFIG_WATCHDOG
135 if (wdt_channel_id >= 0) {
136 wdt_feed(WDT, wdt_channel_id);
uint32_t arch_get_cpu_cycle_count()
pthread_t & main_loop_thread()
uint32_t arch_get_cpu_freq_hz()
uint32_t IRAM_ATTR HOT micros()