summaryrefslogtreecommitdiff
path: root/ports
diff options
context:
space:
mode:
authorLucian Copeland <hierophect@gmail.com>2020-08-26 18:47:19 -0400
committerLucian Copeland <hierophect@gmail.com>2020-08-26 18:47:19 -0400
commitc22934574146c4468374a10916d2746f0400a8a8 (patch)
tree98de4c37ae6cc41fce9aa8aee94c1b46593c5abd /ports
parent05bde255f7a00dd9b41d2e4200eda652aeec28f2 (diff)
improve efficiency of stm32 random gen
Diffstat (limited to 'ports')
-rw-r--r--ports/stm/common-hal/os/__init__.c13
1 files changed, 9 insertions, 4 deletions
diff --git a/ports/stm/common-hal/os/__init__.c b/ports/stm/common-hal/os/__init__.c
index 13871a46e..8da7243a3 100644
--- a/ports/stm/common-hal/os/__init__.c
+++ b/ports/stm/common-hal/os/__init__.c
@@ -72,15 +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"));
//Assign bytes
- for (uint i = 0; i < length; i++) {
- uint32_t temp;
+ uint32_t i = 0;
+ while (i < length) {
+ uint32_t new_random;
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) {
+ if (HAL_RNG_GenerateRandomNumber(&handle, &new_random) != HAL_OK) {
mp_raise_ValueError(translate("Random number generation error"));
}
- buffer[i] = (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