stm32/rng: Simplify RNG implementation by accessing raw peripheral regs.
It saves code size and RAM, and is more efficient to execute.
This commit is contained in:
parent
762db9ad2f
commit
57d2ac1300
|
@ -265,7 +265,6 @@ SRC_HAL = $(addprefix $(HAL_DIR)/Src/stm32$(MCU_SERIES)xx_,\
|
||||||
hal_pwr_ex.c \
|
hal_pwr_ex.c \
|
||||||
hal_rcc.c \
|
hal_rcc.c \
|
||||||
hal_rcc_ex.c \
|
hal_rcc_ex.c \
|
||||||
hal_rng.c \
|
|
||||||
hal_rtc.c \
|
hal_rtc.c \
|
||||||
hal_rtc_ex.c \
|
hal_rtc_ex.c \
|
||||||
hal_sd.c \
|
hal_sd.c \
|
||||||
|
|
|
@ -552,10 +552,6 @@ soft_reset:
|
||||||
can_init0();
|
can_init0();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if MICROPY_HW_ENABLE_RNG
|
|
||||||
rng_init0();
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if MICROPY_HW_ENABLE_HW_I2C
|
#if MICROPY_HW_ENABLE_HW_I2C
|
||||||
i2c_init0();
|
i2c_init0();
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
*
|
*
|
||||||
* The MIT License (MIT)
|
* 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
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
@ -24,44 +24,35 @@
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include "py/obj.h"
|
|
||||||
#include "rng.h"
|
#include "rng.h"
|
||||||
|
|
||||||
#if MICROPY_HW_ENABLE_RNG
|
#if MICROPY_HW_ENABLE_RNG
|
||||||
|
|
||||||
/// \moduleref pyb
|
#define RNG_TIMEOUT_MS (10)
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t rng_get(void) {
|
uint32_t rng_get(void) {
|
||||||
if (RNGHandle.State == HAL_RNG_STATE_RESET) {
|
// Enable the RNG peripheral if it's not already enabled
|
||||||
rng_init();
|
if (!(RNG->CR & RNG_CR_RNGEN)) {
|
||||||
|
__HAL_RCC_RNG_CLK_ENABLE();
|
||||||
|
RNG->CR |= RNG_CR_RNGEN;
|
||||||
}
|
}
|
||||||
return HAL_RNG_GetRandomNumber(&RNGHandle);
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get and return the new random number
|
||||||
|
return RNG->DR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// \function rng()
|
// Return a 30-bit hardware generated random number.
|
||||||
/// Return a 30-bit hardware generated random number.
|
|
||||||
STATIC mp_obj_t pyb_rng_get(void) {
|
STATIC mp_obj_t pyb_rng_get(void) {
|
||||||
if (RNGHandle.State == HAL_RNG_STATE_RESET) {
|
return mp_obj_new_int(rng_get() >> 2);
|
||||||
rng_init();
|
|
||||||
}
|
|
||||||
return mp_obj_new_int(HAL_RNG_GetRandomNumber(&RNGHandle) >> 2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MP_DEFINE_CONST_FUN_OBJ_0(pyb_rng_get_obj, pyb_rng_get);
|
MP_DEFINE_CONST_FUN_OBJ_0(pyb_rng_get_obj, pyb_rng_get);
|
||||||
|
|
||||||
#endif // MICROPY_HW_ENABLE_RNG
|
#endif // MICROPY_HW_ENABLE_RNG
|
||||||
|
|
|
@ -26,7 +26,8 @@
|
||||||
#ifndef MICROPY_INCLUDED_STMHAL_RNG_H
|
#ifndef MICROPY_INCLUDED_STMHAL_RNG_H
|
||||||
#define MICROPY_INCLUDED_STMHAL_RNG_H
|
#define MICROPY_INCLUDED_STMHAL_RNG_H
|
||||||
|
|
||||||
void rng_init0(void);
|
#include "py/obj.h"
|
||||||
|
|
||||||
uint32_t rng_get(void);
|
uint32_t rng_get(void);
|
||||||
|
|
||||||
MP_DECLARE_CONST_FUN_OBJ_0(pyb_rng_get_obj);
|
MP_DECLARE_CONST_FUN_OBJ_0(pyb_rng_get_obj);
|
||||||
|
|
Loading…
Reference in New Issue