File tree Expand file tree Collapse file tree 3 files changed +24
-8
lines changed
Expand file tree Collapse file tree 3 files changed +24
-8
lines changed Original file line number Diff line number Diff line change @@ -458,8 +458,7 @@ int main(void) {
458458#endif
459459
460460#if MICROPY_HW_ENABLE_RNG
461- // RNG
462- rng_init ();
461+ rng_init0 ();
463462#endif
464463
465464#if MICROPY_HW_ENABLE_TIMER
Original file line number Diff line number Diff line change 1+ #include <string.h>
2+
13#include "stm32f4xx_hal.h"
24
35#include "misc.h"
68#include "obj.h"
79#include "rng.h"
810
9- STATIC RNG_HandleTypeDef RngHandle ;
11+ #if MICROPY_HW_ENABLE_RNG
12+
13+ STATIC RNG_HandleTypeDef RNGHandle = {.Instance = NULL };
14+
15+ void rng_init0 (void ) {
16+ // reset the RNG handle
17+ memset (& RNGHandle , 0 , sizeof (RNG_HandleTypeDef ));
18+ RNGHandle .Instance = RNG ;
19+ }
1020
1121void rng_init (void ) {
1222 __RNG_CLK_ENABLE ();
13- RngHandle .Instance = RNG ;
14- HAL_RNG_Init (& RngHandle );
23+ HAL_RNG_Init (& RNGHandle );
1524}
1625
1726uint32_t rng_get (void ) {
18- return HAL_RNG_GetRandomNumber (& RngHandle );
27+ if (RNGHandle .State == HAL_RNG_STATE_RESET ) {
28+ rng_init ();
29+ }
30+ return HAL_RNG_GetRandomNumber (& RNGHandle );
1931}
2032
2133STATIC mp_obj_t pyb_rng_get (void ) {
22- return mp_obj_new_int (HAL_RNG_GetRandomNumber (& RngHandle ) >> 2 );
34+ if (RNGHandle .State == HAL_RNG_STATE_RESET ) {
35+ rng_init ();
36+ }
37+ return mp_obj_new_int (HAL_RNG_GetRandomNumber (& RNGHandle ) >> 2 );
2338}
2439
2540MP_DEFINE_CONST_FUN_OBJ_0 (pyb_rng_get_obj , pyb_rng_get );
41+
42+ #endif // MICROPY_HW_ENABLE_RNG
Original file line number Diff line number Diff line change 1- void rng_init (void );
1+ void rng_init0 (void );
22uint32_t rng_get (void );
33
44MP_DECLARE_CONST_FUN_OBJ (pyb_rng_get_obj );
You can’t perform that action at this time.
0 commit comments