ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
core.cpp
Go to the documentation of this file.
1#ifdef USE_ZEPHYR
2
3#include <zephyr/kernel.h>
4#include <zephyr/drivers/watchdog.h>
5#include <zephyr/sys/reboot.h>
6#include <zephyr/random/random.h>
7#include "esphome/core/hal.h"
10
11namespace esphome {
12
13#ifdef CONFIG_WATCHDOG
14static int wdt_channel_id = -1; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
15static const device *const WDT = DEVICE_DT_GET(DT_ALIAS(watchdog0));
16#endif
17
18void yield() { ::k_yield(); }
19uint32_t millis() { return static_cast<uint32_t>(millis_64()); }
20uint64_t millis_64() { return static_cast<uint64_t>(k_uptime_get()); }
21uint32_t micros() { return k_ticks_to_us_floor32(k_uptime_ticks()); }
22void delayMicroseconds(uint32_t us) { ::k_usleep(us); }
23void delay(uint32_t ms) { ::k_msleep(ms); }
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 use a lot of cpu cycles during start
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}
50
51void arch_feed_wdt() {
52#ifdef CONFIG_WATCHDOG
53 if (wdt_channel_id >= 0) {
54 wdt_feed(WDT, wdt_channel_id);
55 }
56#endif
57}
58
59void arch_restart() { sys_reboot(SYS_REBOOT_COLD); }
60uint32_t arch_get_cpu_cycle_count() { return k_cycle_get_32(); }
61uint32_t arch_get_cpu_freq_hz() { return sys_clock_hw_cycles_per_sec(); }
62
64 auto *mutex = new k_mutex();
65 this->handle_ = mutex;
66 k_mutex_init(mutex);
67}
68Mutex::~Mutex() { delete static_cast<k_mutex *>(this->handle_); }
69void Mutex::lock() { k_mutex_lock(static_cast<k_mutex *>(this->handle_), K_FOREVER); }
70bool Mutex::try_lock() { return k_mutex_lock(static_cast<k_mutex *>(this->handle_), K_NO_WAIT) == 0; }
71void Mutex::unlock() { k_mutex_unlock(static_cast<k_mutex *>(this->handle_)); }
72
73IRAM_ATTR InterruptLock::InterruptLock() { state_ = irq_lock(); }
74IRAM_ATTR InterruptLock::~InterruptLock() { irq_unlock(state_); }
75
76// Zephyr LwIPLock is defined inline as a no-op in helpers.h
77
78bool random_bytes(uint8_t *data, size_t len) {
79 sys_rand_get(data, len);
80 return true;
81}
82
83#ifdef USE_NRF52
84void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parameter)
85 mac[0] = ((NRF_FICR->DEVICEADDR[1] & 0xFFFF) >> 8) | 0xC0;
86 mac[1] = NRF_FICR->DEVICEADDR[1] & 0xFFFF;
87 mac[2] = NRF_FICR->DEVICEADDR[0] >> 24;
88 mac[3] = NRF_FICR->DEVICEADDR[0] >> 16;
89 mac[4] = NRF_FICR->DEVICEADDR[0] >> 8;
90 mac[5] = NRF_FICR->DEVICEADDR[0];
91}
92#endif
93} // namespace esphome
94
95void setup();
96void loop();
97
98int main() {
99 setup();
100 while (true) {
101 loop();
102 }
103 return 0;
104}
105
106#endif
~Mutex()=default
Definition helpers.cpp:36
void unlock()
Definition helpers.h:2047
Mutex()=default
Definition helpers.cpp:35
bool try_lock()
Definition helpers.h:2046
uint8_t options
void setup()
int main()
Definition core.cpp:80
void loop()
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint32_t arch_get_cpu_cycle_count()
Definition core.cpp:56
bool random_bytes(uint8_t *data, size_t len)
Generate len random bytes using the platform's secure RNG (hardware RNG or OS CSPRNG).
Definition helpers.cpp:20
void arch_init()
Definition core.cpp:39
std::string size_t len
Definition helpers.h:1045
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition core.cpp:30
void HOT yield()
Definition core.cpp:25
uint32_t arch_get_cpu_freq_hz()
Definition core.cpp:57
uint64_t HOT millis_64()
Definition core.cpp:27
uint32_t IRAM_ATTR HOT micros()
Definition core.cpp:29
void HOT arch_feed_wdt()
Definition core.cpp:54
void get_mac_address_raw(uint8_t *mac)
Get the device MAC address as raw bytes, written into the provided byte array (6 bytes).
Definition helpers.cpp:74
void HOT delay(uint32_t ms)
Definition core.cpp:28
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:26
void arch_restart()
Definition core.cpp:31
static void uint32_t