From f298ea406e4a60062ebc607222fda147375b4407 Mon Sep 17 00:00:00 2001 From: bugobliterator Date: Wed, 6 Oct 2021 12:00:38 +0530 Subject: [PATCH] HAL_ChibiOS: enable RNG only for H7 based boards off for the rest by default --- libraries/AP_HAL_ChibiOS/Util.cpp | 4 ++-- libraries/AP_HAL_ChibiOS/hwdef/common/board.c | 2 +- libraries/AP_HAL_ChibiOS/hwdef/common/stm32_util.c | 4 ++-- libraries/AP_HAL_ChibiOS/hwdef/common/stm32_util.h | 2 +- libraries/AP_HAL_ChibiOS/hwdef/scripts/chibios_hwdef.py | 7 +++++++ 5 files changed, 13 insertions(+), 6 deletions(-) diff --git a/libraries/AP_HAL_ChibiOS/Util.cpp b/libraries/AP_HAL_ChibiOS/Util.cpp index e07b0cbf1b..2b3845d704 100644 --- a/libraries/AP_HAL_ChibiOS/Util.cpp +++ b/libraries/AP_HAL_ChibiOS/Util.cpp @@ -610,7 +610,7 @@ void Util::uart_info(ExpandingString &str) */ bool Util::get_random_vals(uint8_t* data, size_t size) { -#ifdef RNG +#if HAL_USE_HW_RNG && defined(RNG) size_t true_random_vals = stm32_rand_generate_nonblocking(data, size); if (true_random_vals == size) { return true; @@ -638,7 +638,7 @@ bool Util::get_random_vals(uint8_t* data, size_t size) */ bool Util::get_true_random_vals(uint8_t* data, size_t size, uint32_t timeout_us) { -#ifdef RNG +#if HAL_USE_HW_RNG && defined(RNG) if (stm32_rand_generate_blocking(data, size, timeout_us)) { return true; } else { diff --git a/libraries/AP_HAL_ChibiOS/hwdef/common/board.c b/libraries/AP_HAL_ChibiOS/hwdef/common/board.c index 41c5513573..c35969a0d6 100644 --- a/libraries/AP_HAL_ChibiOS/hwdef/common/board.c +++ b/libraries/AP_HAL_ChibiOS/hwdef/common/board.c @@ -255,7 +255,7 @@ void __late_init(void) { /* * Initialize RNG */ -#ifdef RNG +#if HAL_USE_HW_RNG && defined(RNG) rccEnableAHB2(RCC_AHB2ENR_RNGEN, 0); RNG->CR |= RNG_CR_IE; RNG->CR |= RNG_CR_RNGEN; diff --git a/libraries/AP_HAL_ChibiOS/hwdef/common/stm32_util.c b/libraries/AP_HAL_ChibiOS/hwdef/common/stm32_util.c index c6fd964ba8..2682206237 100644 --- a/libraries/AP_HAL_ChibiOS/hwdef/common/stm32_util.c +++ b/libraries/AP_HAL_ChibiOS/hwdef/common/stm32_util.c @@ -480,7 +480,7 @@ uint32_t stack_free(void *stack_base) } #endif -#ifdef RNG +#if HAL_USE_HW_RNG && defined(RNG) static bool stm32_rand_generate(uint32_t *val) { uint32_t error_bits = 0; @@ -548,4 +548,4 @@ unsigned int stm32_rand_generate_nonblocking(unsigned char* output, unsigned int return i; } -#endif // #ifdef RNG +#endif // #if HAL_USE_HW_RNG && defined(RNG) diff --git a/libraries/AP_HAL_ChibiOS/hwdef/common/stm32_util.h b/libraries/AP_HAL_ChibiOS/hwdef/common/stm32_util.h index 1936f08b63..eaf5417670 100644 --- a/libraries/AP_HAL_ChibiOS/hwdef/common/stm32_util.h +++ b/libraries/AP_HAL_ChibiOS/hwdef/common/stm32_util.h @@ -132,7 +132,7 @@ uint32_t stack_free(void *stack_base); * Generates a block of random values, returns total values generated * if nonblocking, for blocking returns if successful or not */ -#ifdef RNG +#if HAL_USE_HW_RNG && defined(RNG) bool stm32_rand_generate_blocking(unsigned char* output, unsigned int sz, uint32_t timeout_us); unsigned int stm32_rand_generate_nonblocking(unsigned char* output, unsigned int sz); #endif diff --git a/libraries/AP_HAL_ChibiOS/hwdef/scripts/chibios_hwdef.py b/libraries/AP_HAL_ChibiOS/hwdef/scripts/chibios_hwdef.py index 76490cf191..deccc30986 100644 --- a/libraries/AP_HAL_ChibiOS/hwdef/scripts/chibios_hwdef.py +++ b/libraries/AP_HAL_ChibiOS/hwdef/scripts/chibios_hwdef.py @@ -737,6 +737,13 @@ def write_mcu_config(f): for d in defines.keys(): v = defines[d] f.write("#ifndef %s\n#define %s %s\n#endif\n" % (d, d, v)) + else: + defines = {} + # enable RNG for all H7 chips + if mcu_series.startswith("STM32H7") and 'HAL_USE_HW_RNG' not in defines.keys(): + f.write("#define HAL_USE_HW_RNG TRUE\n") + elif 'HAL_USE_HW_RNG' not in defines.keys(): + f.write("#define HAL_USE_HW_RNG FALSE\n") if get_config('PROCESS_STACK', required=False): env_vars['PROCESS_STACK'] = get_config('PROCESS_STACK')