diff options
| author | Lucian Copeland <hierophect@gmail.com> | 2020-08-25 14:00:29 -0400 |
|---|---|---|
| committer | Lucian Copeland <hierophect@gmail.com> | 2020-08-25 14:00:29 -0400 |
| commit | 05bde255f7a00dd9b41d2e4200eda652aeec28f2 (patch) | |
| tree | 13f95d88338ac211cece5ad21d981f51a354c1ed | |
| parent | 2529c0aa836d120a9653e3cfd7e7cea0d743c0b7 (diff) | |
Add random to ESP32-S2, fix it on STM32
| -rw-r--r-- | ports/esp32s2/common-hal/os/__init__.c | 14 | ||||
| -rw-r--r-- | ports/esp32s2/mpconfigport.mk | 1 | ||||
| -rw-r--r-- | ports/stm/common-hal/os/__init__.c | 3 |
3 files changed, 14 insertions, 4 deletions
diff --git a/ports/esp32s2/common-hal/os/__init__.c b/ports/esp32s2/common-hal/os/__init__.c index 17bda75ad..4d6a6a2bf 100644 --- a/ports/esp32s2/common-hal/os/__init__.c +++ b/ports/esp32s2/common-hal/os/__init__.c @@ -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; } diff --git a/ports/esp32s2/mpconfigport.mk b/ports/esp32s2/mpconfigport.mk index a7873aa46..13c0d66fb 100644 --- a/ports/esp32s2/mpconfigport.mk +++ b/ports/esp32s2/mpconfigport.mk @@ -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 diff --git a/ports/stm/common-hal/os/__init__.c b/ports/stm/common-hal/os/__init__.c index 84deb8175..13871a46e 100644 --- a/ports/stm/common-hal/os/__init__.c +++ b/ports/stm/common-hal/os/__init__.c @@ -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 |
