ESPHome 2026.2.1
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 k_ticks_to_ms_floor32(k_uptime_ticks()); }
20uint32_t micros() { return k_ticks_to_us_floor32(k_uptime_ticks()); }
21void delayMicroseconds(uint32_t us) { ::k_usleep(us); }
22void delay(uint32_t ms) { ::k_msleep(ms); }
23
24void arch_init() {
25#ifdef CONFIG_WATCHDOG
26 if (device_is_ready(WDT)) {
27 static wdt_timeout_cfg wdt_config{};
28 wdt_config.flags = WDT_FLAG_RESET_SOC;
29#ifdef USE_ZIGBEE
30 // zboss thread use a lot of cpu cycles during start
31 wdt_config.window.max = 10000;
32#else
33 wdt_config.window.max = 2000;
34#endif
35 wdt_channel_id = wdt_install_timeout(WDT, &wdt_config);
36 if (wdt_channel_id >= 0) {
37 uint8_t options = 0;
38#ifdef USE_DEBUG
39 options |= WDT_OPT_PAUSE_HALTED_BY_DBG;
40#endif
41#ifdef USE_DEEP_SLEEP
42 options |= WDT_OPT_PAUSE_IN_SLEEP;
43#endif
44 wdt_setup(WDT, options);
45 }
46 }
47#endif
48}
49
50void arch_feed_wdt() {
51#ifdef CONFIG_WATCHDOG
52 if (wdt_channel_id >= 0) {
53 wdt_feed(WDT, wdt_channel_id);
54 }
55#endif
56}
57
58void arch_restart() { sys_reboot(SYS_REBOOT_COLD); }
59uint32_t arch_get_cpu_cycle_count() { return k_cycle_get_32(); }
60uint32_t arch_get_cpu_freq_hz() { return sys_clock_hw_cycles_per_sec(); }
61uint8_t progmem_read_byte(const uint8_t *addr) { return *addr; }
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 doesn't support lwIP core locking, so this is a no-op
79
80uint32_t random_uint32() { return rand(); } // NOLINT(cert-msc30-c, cert-msc50-cpp)
81bool random_bytes(uint8_t *data, size_t len) {
82 sys_rand_get(data, len);
83 return true;
84}
85
86#ifdef USE_NRF52
87void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parameter)
88 mac[0] = ((NRF_FICR->DEVICEADDR[1] & 0xFFFF) >> 8) | 0xC0;
89 mac[1] = NRF_FICR->DEVICEADDR[1] & 0xFFFF;
90 mac[2] = NRF_FICR->DEVICEADDR[0] >> 24;
91 mac[3] = NRF_FICR->DEVICEADDR[0] >> 16;
92 mac[4] = NRF_FICR->DEVICEADDR[0] >> 8;
93 mac[5] = NRF_FICR->DEVICEADDR[0];
94}
95#endif
96} // namespace esphome
97
98void setup();
99void loop();
100
101int main() {
102 setup();
103 while (true) {
104 loop();
106 }
107 return 0;
108}
109
110#endif
void unlock()
Definition helpers.cpp:27
bool try_lock()
Definition helpers.cpp:26
uint8_t options
void setup()
void loop()
int main()
Definition core.cpp:69
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:50
bool random_bytes(uint8_t *data, size_t len)
Generate len number of random bytes.
Definition helpers.cpp:18
void arch_init()
Definition core.cpp:37
std::string size_t len
Definition helpers.h:692
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition core.cpp:28
void IRAM_ATTR HOT yield()
Definition core.cpp:24
uint32_t arch_get_cpu_freq_hz()
Definition core.cpp:51
uint32_t IRAM_ATTR HOT micros()
Definition core.cpp:27
uint32_t random_uint32()
Return a random 32-bit unsigned integer.
Definition helpers.cpp:17
void IRAM_ATTR HOT arch_feed_wdt()
Definition core.cpp:47
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:73
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:26
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:25
void arch_restart()
Definition core.cpp:29
uint8_t progmem_read_byte(const uint8_t *addr)
Definition core.cpp:49