From 57d2ac130016cf8500423171cb630ec5b2f09b3a Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 2 Feb 2018 18:22:57 +1100 Subject: [PATCH] stm32/rng: Simplify RNG implementation by accessing raw peripheral regs. It saves code size and RAM, and is more efficient to execute. --- ports/stm32/Makefile | 1 - ports/stm32/main.c | 4 ---- ports/stm32/rng.c | 47 ++++++++++++++++++-------------------------- ports/stm32/rng.h | 3 ++- 4 files changed, 21 insertions(+), 34 deletions(-) diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile index 65962bea67..55ccafe766 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 352c09bcd4..60615736d3 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 c0c5e9aeb5..85dcc14109 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 - -#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); + + // 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) { - if (RNGHandle.State == HAL_RNG_STATE_RESET) { - rng_init(); - } - return mp_obj_new_int(HAL_RNG_GetRandomNumber(&RNGHandle) >> 2); + 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 43e49fe72e..1478b7d3f2 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);