ESPHome 2026.11.0-dev
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 main_loop_thread() = k_current_get();
27#ifdef CONFIG_WATCHDOG
28 if (device_is_ready(WDT)) {
29 static wdt_timeout_cfg wdt_config{};
30 wdt_config.flags = WDT_FLAG_RESET_SOC;
31#ifndef USE_BOOTLOADER_MCUBOOT
32 // Adafruit bootloader doesn't feed the WDT while
33 // erasing flash during a firmware update, so a shorter timeout can break the update.
34 wdt_config.window.max = 30000;
35#elif defined(USE_ZIGBEE)
36 // zboss thread uses a lot of CPU cycles during startup
37 wdt_config.window.max = 10000;
38#else
39 wdt_config.window.max = 2000;
40#endif
41 wdt_channel_id = wdt_install_timeout(WDT, &wdt_config);
42 if (wdt_channel_id >= 0) {
43 uint8_t options = 0;
44#ifdef USE_DEBUG
45 options |= WDT_OPT_PAUSE_HALTED_BY_DBG;
46#endif
47#ifdef USE_DEEP_SLEEP
48 options |= WDT_OPT_PAUSE_IN_SLEEP;
49#endif
50 wdt_setup(WDT, options);
51 }
52 }
53#endif
54 // feed watchdog early. Otherwise OTA may rollback.
56}
57
59#ifdef CONFIG_WATCHDOG
60 if (wdt_channel_id >= 0) {
61 wdt_feed(WDT, wdt_channel_id);
62 }
63#endif
64}
65
66void arch_restart() { sys_reboot(SYS_REBOOT_COLD); }
67
68} // namespace esphome
69
70#endif // USE_ZEPHYR
uint8_t options
void arch_init()
Definition hal.cpp:44
pthread_t & main_loop_thread()
Definition hal.h:20
void arch_feed_wdt()
Definition hal.cpp:58
void arch_restart()
Definition hal.cpp:36