Add random to ESP32-S2, fix it on STM32

This commit is contained in:
Lucian Copeland 2020-08-25 14:00:29 -04:00
parent 2529c0aa83
commit 05bde255f7
3 changed files with 14 additions and 4 deletions

View File

@ -30,6 +30,8 @@
#include "py/objtuple.h"
#include "py/qstr.h"
#include "esp_system.h"
STATIC const qstr os_uname_info_fields[] = {
MP_QSTR_sysname, MP_QSTR_nodename,
MP_QSTR_release, MP_QSTR_version, MP_QSTR_machine
@ -57,5 +59,15 @@ mp_obj_t common_hal_os_uname(void) {
}
bool common_hal_os_urandom(uint8_t* buffer, uint32_t length) {
return false;
uint32_t i = 0;
while (i < length) {
uint32_t new_random = esp_random();
for (int j = 0; j < 4 && i < length; j++) {
buffer[i] = new_random & 0xff;
i++;
new_random >>= 8;
}
}
return true;
}

View File

@ -25,7 +25,6 @@ CIRCUITPY_COUNTIO = 0
# These modules are implemented in shared-module/ - they can be included in
# any port once their prerequisites in common-hal are complete.
CIRCUITPY_RANDOM = 0 # Requires OS
CIRCUITPY_USB_MIDI = 0 # Requires USB
CIRCUITPY_ULAB = 0 # No requirements, but takes extra flash

View File

@ -77,11 +77,10 @@ bool common_hal_os_urandom(uint8_t *buffer, uint32_t length) {
uint32_t start = HAL_GetTick();
//the HAL function has a timeout, but it isn't long enough, and isn't adjustable
while(!(__HAL_RNG_GET_FLAG(&handle,RNG_FLAG_DRDY)) && ((HAL_GetTick() - start) < RNG_TIMEOUT));
//
if (HAL_RNG_GenerateRandomNumber(&handle, &temp) != HAL_OK) {
mp_raise_ValueError(translate("Random number generation error"));
}
*buffer = (uint8_t)temp;
buffer[i] = (uint8_t)temp;
}
//shut down the peripheral