summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ports/stm32/Makefile1
-rw-r--r--ports/stm32/main.c4
-rw-r--r--ports/stm32/rng.c47
-rw-r--r--ports/stm32/rng.h3
4 files changed, 21 insertions, 34 deletions
diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile
index 65962bea6..55ccafe76 100644
--- a/ports/stm32/Makefile
+++ b/ports/stm32/Makefile
@@ -265,7 +265,6 @@ SRC_HAL = $(addprefix $(HAL_DIR)/Src/stm32$(MCU_SERIES)xx_,\
hal_pwr_ex.c \
hal_rcc.c \
hal_rcc_ex.c \
- hal_rng.c \
hal_rtc.c \
hal_rtc_ex.c \
hal_sd.c \
diff --git a/ports/stm32/main.c b/ports/stm32/main.c
index 352c09bcd..60615736d 100644
--- a/ports/stm32/main.c
+++ b/ports/stm32/main.c
@@ -552,10 +552,6 @@ soft_reset:
can_init0();
#endif
-#if MICROPY_HW_ENABLE_RNG
- rng_init0();
-#endif
-
#if MICROPY_HW_ENABLE_HW_I2C
i2c_init0();
#endif
diff --git a/ports/stm32/rng.c b/ports/stm32/rng.c
index c0c5e9aeb..85dcc1410 100644
--- a/ports/stm32/rng.c
+++ b/ports/stm32/rng.c
@@ -3,7 +3,7 @@
*
* The MIT License (MIT)
*
- * Copyright (c) 2013, 2014 Damien P. George
+ * Copyright (c) 2013-2018 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -24,44 +24,35 @@
* THE SOFTWARE.
*/
-#include <string.h>
-
-#include "py/obj.h"
#include "rng.h"
#if MICROPY_HW_ENABLE_RNG
-/// \moduleref pyb
-
-STATIC RNG_HandleTypeDef RNGHandle = {.Instance = NULL};
-
-void rng_init0(void) {
- // reset the RNG handle
- memset(&RNGHandle, 0, sizeof(RNG_HandleTypeDef));
- RNGHandle.Instance = RNG;
-}
-
-void rng_init(void) {
- __RNG_CLK_ENABLE();
- HAL_RNG_Init(&RNGHandle);
-}
+#define RNG_TIMEOUT_MS (10)
uint32_t rng_get(void) {
- if (RNGHandle.State == HAL_RNG_STATE_RESET) {
- rng_init();
+ // Enable the RNG peripheral if it's not already enabled
+ if (!(RNG->CR & RNG_CR_RNGEN)) {
+ __HAL_RCC_RNG_CLK_ENABLE();
+ RNG->CR |= RNG_CR_RNGEN;
}
- return HAL_RNG_GetRandomNumber(&RNGHandle);
-}
-/// \function rng()
-/// Return a 30-bit hardware generated random number.
-STATIC mp_obj_t pyb_rng_get(void) {
- if (RNGHandle.State == HAL_RNG_STATE_RESET) {
- rng_init();
+ // Wait for a new random number to be ready, takes on the order of 10us
+ uint32_t start = HAL_GetTick();
+ while (!(RNG->SR & RNG_SR_DRDY)) {
+ if (HAL_GetTick() - start >= RNG_TIMEOUT_MS) {
+ return 0;
+ }
}
- return mp_obj_new_int(HAL_RNG_GetRandomNumber(&RNGHandle) >> 2);
+
+ // Get and return the new random number
+ return RNG->DR;
}
+// Return a 30-bit hardware generated random number.
+STATIC mp_obj_t pyb_rng_get(void) {
+ return mp_obj_new_int(rng_get() >> 2);
+}
MP_DEFINE_CONST_FUN_OBJ_0(pyb_rng_get_obj, pyb_rng_get);
#endif // MICROPY_HW_ENABLE_RNG
diff --git a/ports/stm32/rng.h b/ports/stm32/rng.h
index 43e49fe72..1478b7d3f 100644
--- a/ports/stm32/rng.h
+++ b/ports/stm32/rng.h
@@ -26,7 +26,8 @@
#ifndef MICROPY_INCLUDED_STMHAL_RNG_H
#define MICROPY_INCLUDED_STMHAL_RNG_H
-void rng_init0(void);
+#include "py/obj.h"
+
uint32_t rng_get(void);
MP_DECLARE_CONST_FUN_OBJ_0(pyb_rng_get_obj);