From a8a92b4c11885a4a9c9a36d855d6d20b464bd905 Mon Sep 17 00:00:00 2001 From: Guillaume Revaillot Date: Tue, 5 Feb 2019 17:22:55 +0100 Subject: [PATCH] rng: fix clock error handling, based on RM. According to L4/L0/G0 RM, in case of clock error, interrupt flag must be cleared, and CECS flag should be cleared as soon as clock meets requirement. Reviewed-on: https://github.com/libopencm3/libopencm3/pull/1062 --- lib/stm32/common/rng_common_v1.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/stm32/common/rng_common_v1.c b/lib/stm32/common/rng_common_v1.c index e35fc46a..20c4ff1c 100644 --- a/lib/stm32/common/rng_common_v1.c +++ b/lib/stm32/common/rng_common_v1.c @@ -87,6 +87,9 @@ bool rng_get_random(uint32_t *rand_nr) * Get a random number and block until it works. * Unless you have a clock problem, this should always return "promptly" * If you have a clock problem, you will wait here forever! + * Check device RM for clock requirements (usually fRNGCLK > fHCLK/16 or + * fRNGCLK > fHCLK/32 + * @returns a random 32bit number */ uint32_t rng_get_random_blocking(void) @@ -104,6 +107,10 @@ uint32_t rng_get_random_blocking(void) RNG_CR |= RNG_CR_RNGEN; } + if (RNG_SR & RNG_SR_CEIS) { + RNG_SR = RNG_SR & ~RNG_SR_CEIS; + } + done = rng_get_random(&rv); } while (!done);