ESPHome 2025.8.0b1
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/rand32.h>
7#include "esphome/core/hal.h"
9
10namespace esphome {
11
12static int wdt_channel_id = -1; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
13static const device *const WDT = DEVICE_DT_GET(DT_ALIAS(watchdog0));
14
15void yield() { ::k_yield(); }
16uint32_t millis() { return k_ticks_to_ms_floor32(k_uptime_ticks()); }
17uint32_t micros() { return k_ticks_to_us_floor32(k_uptime_ticks()); }
18void delayMicroseconds(uint32_t us) { ::k_usleep(us); }
19void delay(uint32_t ms) { ::k_msleep(ms); }
20
21void arch_init() {
22 if (device_is_ready(WDT)) {
23 static wdt_timeout_cfg wdt_config{};
24 wdt_config.flags = WDT_FLAG_RESET_SOC;
25 wdt_config.window.max = 2000;
26 wdt_channel_id = wdt_install_timeout(WDT, &wdt_config);
27 if (wdt_channel_id >= 0) {
28 wdt_setup(WDT, WDT_OPT_PAUSE_HALTED_BY_DBG | WDT_OPT_PAUSE_IN_SLEEP);
29 }
30 }
31}
32
33void arch_feed_wdt() {
34 if (wdt_channel_id >= 0) {
35 wdt_feed(WDT, wdt_channel_id);
36 }
37}
38
39void arch_restart() { sys_reboot(SYS_REBOOT_COLD); }
40uint32_t arch_get_cpu_cycle_count() { return k_cycle_get_32(); }
41uint32_t arch_get_cpu_freq_hz() { return sys_clock_hw_cycles_per_sec(); }
42uint8_t progmem_read_byte(const uint8_t *addr) { return *addr; }
43
45 auto *mutex = new k_mutex();
46 this->handle_ = mutex;
47 k_mutex_init(mutex);
48}
49Mutex::~Mutex() { delete static_cast<k_mutex *>(this->handle_); }
50void Mutex::lock() { k_mutex_lock(static_cast<k_mutex *>(this->handle_), K_FOREVER); }
51bool Mutex::try_lock() { return k_mutex_lock(static_cast<k_mutex *>(this->handle_), K_NO_WAIT) == 0; }
52void Mutex::unlock() { k_mutex_unlock(static_cast<k_mutex *>(this->handle_)); }
53
54IRAM_ATTR InterruptLock::InterruptLock() { state_ = irq_lock(); }
55IRAM_ATTR InterruptLock::~InterruptLock() { irq_unlock(state_); }
56
57// Zephyr doesn't support lwIP core locking, so this is a no-op
60
61uint32_t random_uint32() { return rand(); } // NOLINT(cert-msc30-c, cert-msc50-cpp)
62bool random_bytes(uint8_t *data, size_t len) {
63 sys_rand_get(data, len);
64 return true;
65}
66
67void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parameter)
68 mac[0] = ((NRF_FICR->DEVICEADDR[1] & 0xFFFF) >> 8) | 0xC0;
69 mac[1] = NRF_FICR->DEVICEADDR[1] & 0xFFFF;
70 mac[2] = NRF_FICR->DEVICEADDR[0] >> 24;
71 mac[3] = NRF_FICR->DEVICEADDR[0] >> 16;
72 mac[4] = NRF_FICR->DEVICEADDR[0] >> 8;
73 mac[5] = NRF_FICR->DEVICEADDR[0];
74}
75
76} // namespace esphome
77
78void setup();
79void loop();
80
81int main() {
82 setup();
83 while (true) {
84 loop();
86 }
87 return 0;
88}
89
90#endif
void unlock()
Definition helpers.cpp:27
bool try_lock()
Definition helpers.cpp:26
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: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:40
std::string size_t len
Definition helpers.h:279
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition core.cpp:31
void IRAM_ATTR HOT yield()
Definition core.cpp:27
uint32_t arch_get_cpu_freq_hz()
Definition core.cpp:60
uint32_t IRAM_ATTR HOT micros()
Definition core.cpp:30
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:56
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:29
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:28
void arch_restart()
Definition core.cpp:32
uint8_t progmem_read_byte(const uint8_t *addr)
Definition core.cpp:58