diff --git a/esphome/core/helpers.cpp b/esphome/core/helpers.cpp index 79e706cf0d..48ef406a81 100644 --- a/esphome/core/helpers.cpp +++ b/esphome/core/helpers.cpp @@ -23,6 +23,7 @@ #include #elif defined(USE_RP2040) && defined(USE_WIFI) #include +#include #endif #ifdef USE_ESP32_IGNORE_EFUSE_MAC_CRC @@ -94,7 +95,12 @@ uint32_t random_uint32() { #elif defined(USE_ESP8266) return os_random(); #elif defined(USE_RP2040) - return ((uint32_t) rand()) << 16 + ((uint32_t) rand()); + uint32_t result = 0; + for (uint8_t i = 0; i < 32; i++) { + result <<= 1; + result |= rosc_hw->randombit; + } + return result; #else #error "No random source available for this configuration." #endif @@ -107,7 +113,15 @@ bool random_bytes(uint8_t *data, size_t len) { #elif defined(USE_ESP8266) return os_get_random(data, len) == 0; #elif defined(USE_RP2040) - return false; + while (len-- != 0) { + uint8_t result = 0; + for (uint8_t i = 0; i < 8; i++) { + result <<= 1; + result |= rosc_hw->randombit; + } + *data++ = result; + } + return true; #else #error "No random source available for this configuration." #endif