ESPHome 2026.3.3
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(); }
62uint8_t progmem_read_byte(const uint8_t *addr) { return *addr; }
63const char *progmem_read_ptr(const char *const *addr) { return *addr; }
64uint16_t progmem_read_uint16(const uint16_t *addr) { return *addr; }
65
67 auto *mutex = new k_mutex();
68 this->handle_ = mutex;
69 k_mutex_init(mutex);
70}
71Mutex::~Mutex() { delete static_cast<k_mutex *>(this->handle_); }
72void Mutex::lock() { k_mutex_lock(static_cast<k_mutex *>(this->handle_), K_FOREVER); }
73bool Mutex::try_lock() { return k_mutex_lock(static_cast<k_mutex *>(this->handle_), K_NO_WAIT) == 0; }
74void Mutex::unlock() { k_mutex_unlock(static_cast<k_mutex *>(this->handle_)); }
75
76IRAM_ATTR InterruptLock::InterruptLock() { state_ = irq_lock(); }
77IRAM_ATTR InterruptLock::~InterruptLock() { irq_unlock(state_); }
78
79// Zephyr LwIPLock is defined inline as a no-op in helpers.h
80
81uint32_t random_uint32() { return rand(); } // NOLINT(cert-msc30-c, cert-msc50-cpp)
82bool random_bytes(uint8_t *data, size_t len) {
83 sys_rand_get(data, len);
84 return true;
85}
86
87#ifdef USE_NRF52
88void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parameter)
89 mac[0] = ((NRF_FICR->DEVICEADDR[1] & 0xFFFF) >> 8) | 0xC0;
90 mac[1] = NRF_FICR->DEVICEADDR[1] & 0xFFFF;
91 mac[2] = NRF_FICR->DEVICEADDR[0] >> 24;
92 mac[3] = NRF_FICR->DEVICEADDR[0] >> 16;
93 mac[4] = NRF_FICR->DEVICEADDR[0] >> 8;
94 mac[5] = NRF_FICR->DEVICEADDR[0];
95}
96#endif
97} // namespace esphome
98
99void setup();
100void loop();
101
102int main() {
103 setup();
104 while (true) {
105 loop();
107 }
108 return 0;
109}
110
111#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:77
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:59
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:39
const char * progmem_read_ptr(const char *const *addr)
Definition core.cpp:57
std::string size_t len
Definition helpers.h:892
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:60
uint64_t HOT millis_64()
Definition core.cpp:27
uint32_t IRAM_ATTR HOT micros()
Definition core.cpp:29
uint32_t random_uint32()
Return a random 32-bit unsigned integer.
Definition helpers.cpp:17
void HOT arch_feed_wdt()
Definition core.cpp:54
uint16_t progmem_read_uint16(const uint16_t *addr)
Definition core.cpp:58
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 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
uint8_t progmem_read_byte(const uint8_t *addr)
Definition core.cpp:56
static void uint32_t