Merge pull request #3324 from hierophect/esp32-random

Add random to ESP32-S2, fix it on STM32
This commit is contained in:
Dan Halbert 2020-08-27 13:22:05 -04:00 committed by GitHub
commit 350e88d4b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 8 deletions

View File

@ -30,6 +30,8 @@
#include "py/objtuple.h" #include "py/objtuple.h"
#include "py/qstr.h" #include "py/qstr.h"
#include "esp_system.h"
STATIC const qstr os_uname_info_fields[] = { STATIC const qstr os_uname_info_fields[] = {
MP_QSTR_sysname, MP_QSTR_nodename, MP_QSTR_sysname, MP_QSTR_nodename,
MP_QSTR_release, MP_QSTR_version, MP_QSTR_machine 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) { 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,8 +25,6 @@ CIRCUITPY_COUNTIO = 0
# These modules are implemented in shared-module/ - they can be included in # These modules are implemented in shared-module/ - they can be included in
# any port once their prerequisites in common-hal are complete. # any port once their prerequisites in common-hal are complete.
# Requires OS
CIRCUITPY_RANDOM = 0
# Requires USB # Requires USB
CIRCUITPY_USB_MIDI = 0 CIRCUITPY_USB_MIDI = 0
# Too large for the partition table! # Too large for the partition table!

View File

@ -72,16 +72,20 @@ bool common_hal_os_urandom(uint8_t *buffer, uint32_t length) {
if (HAL_RNG_Init(&handle) != HAL_OK) mp_raise_ValueError(translate("RNG Init Error")); if (HAL_RNG_Init(&handle) != HAL_OK) mp_raise_ValueError(translate("RNG Init Error"));
//Assign bytes //Assign bytes
for (uint i = 0; i < length; i++) { uint32_t i = 0;
uint32_t temp; while (i < length) {
uint32_t new_random;
uint32_t start = HAL_GetTick(); uint32_t start = HAL_GetTick();
//the HAL function has a timeout, but it isn't long enough, and isn't adjustable //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)); while(!(__HAL_RNG_GET_FLAG(&handle,RNG_FLAG_DRDY)) && ((HAL_GetTick() - start) < RNG_TIMEOUT));
// if (HAL_RNG_GenerateRandomNumber(&handle, &new_random) != HAL_OK) {
if (HAL_RNG_GenerateRandomNumber(&handle, &temp) != HAL_OK) {
mp_raise_ValueError(translate("Random number generation error")); mp_raise_ValueError(translate("Random number generation error"));
} }
*buffer = (uint8_t)temp; for (int j = 0; j < 4 && i < length; j++) {
buffer[i] = new_random & 0xff;
i++;
new_random >>= 8;
}
} }
//shut down the peripheral //shut down the peripheral