10000 stmhal: Only init RNG if it's used. · errordeveloper/micropython@13c19c5 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit 13c19c5

Browse files
committed
stmhal: Only init RNG if it's used.
1 parent 0a6e9f5 commit 13c19c5

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

stmhal/main.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff 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

stmhal/rng.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include <string.h>
2+
13
#include "stm32f4xx_hal.h"
24

35
#include "misc.h"
@@ -6,20 +8,35 @@
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

1121
void rng_init(void) {
1222
__RNG_CLK_ENABLE();
13-
RngHandle.Instance = RNG;
14-
HAL_RNG_Init(&RngHandle);
23+
HAL_RNG_Init(&RNGHandle);
1524
}
1625

1726
uint32_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

2133
STATIC 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

2540
MP_DEFINE_CONST_FUN_OBJ_0(pyb_rng_get_obj, pyb_rng_get);
41+
42+
#endif // MICROPY_HW_ENABLE_RNG

stmhal/rng.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
void rng_init(void);
1+
void rng_init0(void);
22
uint32_t rng_get(void);
33

44
MP_DECLARE_CONST_FUN_OBJ(pyb_rng_get_obj);

0 commit comments

Comments
 (0)
0