ESPHome 2025.7.1
Loading...
Searching...
No Matches
helpers.cpp
Go to the documentation of this file.
3
4#ifdef USE_RP2040
5
6#include "esphome/core/hal.h"
7
8#if defined(USE_WIFI)
9#include <WiFi.h>
10#endif
11#include <hardware/structs/rosc.h>
12#include <hardware/sync.h>
13
14namespace esphome {
15
17 uint32_t result = 0;
18 for (uint8_t i = 0; i < 32; i++) {
19 result <<= 1;
20 result |= rosc_hw->randombit;
21 }
22 return result;
23}
24
25bool random_bytes(uint8_t *data, size_t len) {
26 while (len-- != 0) {
27 uint8_t result = 0;
28 for (uint8_t i = 0; i < 8; i++) {
29 result <<= 1;
30 result |= rosc_hw->randombit;
31 }
32 *data++ = result;
33 }
34 return true;
35}
36
37// RP2040 doesn't have mutexes, but that shouldn't be an issue as it's single-core and non-preemptive OS.
38Mutex::Mutex() {}
40void Mutex::lock() {}
41bool Mutex::try_lock() { return true; }
42void Mutex::unlock() {}
43
44IRAM_ATTR InterruptLock::InterruptLock() { state_ = save_and_disable_interrupts(); }
45IRAM_ATTR InterruptLock::~InterruptLock() { restore_interrupts(state_); }
46
47// RP2040 doesn't support lwIP core locking, so this is a no-op
50
51void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parameter)
52#ifdef USE_WIFI
53 WiFi.macAddress(mac);
54#endif
55}
56
57} // namespace esphome
58
59#endif // USE_RP2040
void unlock()
Definition helpers.cpp:27
bool try_lock()
Definition helpers.cpp:26
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
bool random_bytes(uint8_t *data, size_t len)
Generate len number of random bytes.
Definition helpers.cpp:18
std::string size_t len
Definition helpers.h:229
uint32_t random_uint32()
Return a random 32-bit unsigned integer.
Definition helpers.cpp:17
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