ESPHome 2026.11.0b6
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/random/random.h>
5#include "esphome/core/hal.h"
8
9namespace esphome {
10
11// HAL functions live in hal.cpp.
12
14 auto *mutex = new k_mutex();
15 this->handle_ = mutex;
16 k_mutex_init(mutex);
17}
18Mutex::~Mutex() { delete static_cast<k_mutex *>(this->handle_); }
19void Mutex::lock() { k_mutex_lock(static_cast<k_mutex *>(this->handle_), K_FOREVER); }
20bool Mutex::try_lock() { return k_mutex_lock(static_cast<k_mutex *>(this->handle_), K_NO_WAIT) == 0; }
21void Mutex::unlock() { k_mutex_unlock(static_cast<k_mutex *>(this->handle_)); }
22
24#ifdef USE_ZEPHYR_DWT_MICROS
25 micros(); // Unlocked, so it catches up any sleep before timestamps are taken under the lock
26#endif
27 state_ = irq_lock();
28}
29IRAM_ATTR InterruptLock::~InterruptLock() { irq_unlock(state_); }
30
31// Zephyr LwIPLock is defined inline as a no-op in helpers.h
32
33bool random_bytes(uint8_t *data, size_t len) {
34 sys_rand_get(data, len);
35 return true;
36}
37
38#ifdef USE_NRF52
39void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parameter)
40 mac[0] = ((NRF_FICR->DEVICEADDR[1] & 0xFFFF) >> 8) | 0xC0;
41 mac[1] = NRF_FICR->DEVICEADDR[1] & 0xFFFF;
42 mac[2] = NRF_FICR->DEVICEADDR[0] >> 24;
43 mac[3] = NRF_FICR->DEVICEADDR[0] >> 16;
44 mac[4] = NRF_FICR->DEVICEADDR[0] >> 8;
45 mac[5] = NRF_FICR->DEVICEADDR[0];
46}
47#endif
48} // namespace esphome
49
50void setup();
51void loop();
52
53int main() {
54 setup();
55 while (true) {
56 loop();
57 }
58 return 0;
59}
60
61#endif
ESPHOME_ALWAYS_INLINE InterruptLock()
Definition helpers.h:2255
ESPHOME_ALWAYS_INLINE ~InterruptLock()
Definition helpers.h:2256
~Mutex()=default
Definition helpers.cpp:40
void unlock()
Definition helpers.h:2194
Mutex()=default
Definition helpers.cpp:39
bool try_lock()
Definition helpers.h:2193
void setup()
void loop()
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
const void size_t len
Definition hal.h:68
uint32_t IRAM_ATTR HOT micros()
Definition hal.cpp:43
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:71
int main()
Definition core.cpp:53