ESPHome 2026.5.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/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
23IRAM_ATTR InterruptLock::InterruptLock() { state_ = irq_lock(); }
24IRAM_ATTR InterruptLock::~InterruptLock() { irq_unlock(state_); }
25
26// Zephyr LwIPLock is defined inline as a no-op in helpers.h
27
28bool random_bytes(uint8_t *data, size_t len) {
29 sys_rand_get(data, len);
30 return true;
31}
32
33#ifdef USE_NRF52
34void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parameter)
35 mac[0] = ((NRF_FICR->DEVICEADDR[1] & 0xFFFF) >> 8) | 0xC0;
36 mac[1] = NRF_FICR->DEVICEADDR[1] & 0xFFFF;
37 mac[2] = NRF_FICR->DEVICEADDR[0] >> 24;
38 mac[3] = NRF_FICR->DEVICEADDR[0] >> 16;
39 mac[4] = NRF_FICR->DEVICEADDR[0] >> 8;
40 mac[5] = NRF_FICR->DEVICEADDR[0];
41}
42#endif
43} // namespace esphome
44
45void setup();
46void loop();
47
48int main() {
49 setup();
50 while (true) {
51 loop();
52 }
53 return 0;
54}
55
56#endif
~Mutex()=default
Definition helpers.cpp:36
void unlock()
Definition helpers.h:1895
Mutex()=default
Definition helpers.cpp:35
bool try_lock()
Definition helpers.h:1894
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
std::string size_t len
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
int main()
Definition core.cpp:48