From 60717b074e6e8dba28e1fb60237926f8f0f78b85 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Tue, 25 Oct 2022 17:59:10 +1300 Subject: [PATCH] Implement different random for rp2040 (#3939) --- esphome/core/helpers.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) 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