ESPHome 2026.5.0b1
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#ifdef USE_ZIGBEE
31 // zboss thread uses a lot of CPU cycles during startup
32 wdt_config.window.max = 10000;
33#else
34 wdt_config.window.max = 2000;
35#endif
36 wdt_channel_id = wdt_install_timeout(WDT, &wdt_config);
37 if (wdt_channel_id >= 0) {
38 uint8_t options = 0;
39#ifdef USE_DEBUG
40 options |= WDT_OPT_PAUSE_HALTED_BY_DBG;
41#endif
42#ifdef USE_DEEP_SLEEP
43 options |= WDT_OPT_PAUSE_IN_SLEEP;
44#endif
45 wdt_setup(WDT, options);
46 }
47 }
48#endif
49 // feed watchdog early. Otherwise OTA may rollback.
51}
52
54#ifdef CONFIG_WATCHDOG
55 if (wdt_channel_id >= 0) {
56 wdt_feed(WDT, wdt_channel_id);
57 }
58#endif
59}
60
61void arch_restart() { sys_reboot(SYS_REBOOT_COLD); }
62
63} // namespace esphome
64
65#endif // USE_ZEPHYR
uint8_t options
void arch_init()
Definition hal.cpp:47
void arch_feed_wdt()
Definition hal.cpp:53
void arch_restart()
Definition hal.cpp:39