Skip to content

Add random to ESP32-S2, fix it on STM32 #3324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion ports/esp32s2/common-hal/os/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
1 change: 0 additions & 1 deletion ports/esp32s2/mpconfigport.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 1 addition & 2 deletions ports/stm/common-hal/os/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down