ESPHome 2026.9.1
Loading...
Searching...
No Matches
hal.cpp
Go to the documentation of this file.
1#ifdef USE_ZEPHYR
2
4#include "esphome/core/hal.h"
5
6#include <zephyr/drivers/watchdog.h>
7#include <zephyr/sys/reboot.h>
8
9// Empty zephyr namespace block to satisfy ci-custom's lint_namespace check.
10// HAL functions live in namespace esphome (root) — they are not part of the
11// zephyr component's API.
12namespace esphome::zephyr {} // namespace esphome::zephyr
13
14namespace esphome {
15
16#ifdef CONFIG_WATCHDOG
17static int wdt_channel_id = -1; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
18static const device *const WDT = DEVICE_DT_GET(DT_ALIAS(watchdog0));
19#endif
20
21// yield(), delay(), micros(), millis(), millis_64(), delayMicroseconds(),
22// arch_get_cpu_cycle_count(), arch_get_cpu_freq_hz() inlined in
23// components/zephyr/hal.h.
24
25void arch_init() {
26#ifdef CONFIG_WATCHDOG
27 if (device_is_ready(WDT)) {
28 static wdt_timeout_cfg wdt_config{};
29 wdt_config.flags = WDT_FLAG_RESET_SOC;
30#ifndef USE_BOOTLOADER_MCUBOOT
31 // Adafruit bootloader doesn't feed the WDT while
32 // erasing flash during a firmware update, so a shorter timeout can break the update.
33 wdt_config.window.max = 30000;
34#elif defined(USE_ZIGBEE)
35 // zboss thread uses a lot of CPU cycles during startup
36 wdt_config.window.max = 10000;
37#else
38 wdt_config.window.max = 2000;
39#endif
40 wdt_channel_id = wdt_install_timeout(WDT, &wdt_config);
41 if (wdt_channel_id >= 0) {
42 uint8_t options = 0;
43#ifdef USE_DEBUG
44 options |= WDT_OPT_PAUSE_HALTED_BY_DBG;
45#endif
46#ifdef USE_DEEP_SLEEP
47 options |= WDT_OPT_PAUSE_IN_SLEEP;
48#endif
49 wdt_setup(WDT, options);
50 }
51 }
52#endif
53 // feed watchdog early. Otherwise OTA may rollback.
55}
56
58#ifdef CONFIG_WATCHDOG
59 if (wdt_channel_id >= 0) {
60 wdt_feed(WDT, wdt_channel_id);
61 }
62#endif
63}
64
65void arch_restart() { sys_reboot(SYS_REBOOT_COLD); }
66
67} // namespace esphome
68
69#endif // USE_ZEPHYR
uint8_t options
void arch_init()
Definition hal.cpp:44
void arch_feed_wdt()
Definition hal.cpp:57
void arch_restart()
Definition hal.cpp:36