Implement different random for rp2040 (#3939)

This commit is contained in:
Jesse Hills 2022-10-25 17:59:10 +13:00 committed by GitHub
parent c3fba97b4c
commit 60717b074e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -23,6 +23,7 @@
#include <freertos/portmacro.h>
#elif defined(USE_RP2040) && defined(USE_WIFI)
#include <WiFi.h>
#include <hardware/structs/rosc.h>
#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