From 849749f86f731d5b6e74e1cf01dbc5abafa6c84c Mon Sep 17 00:00:00 2001 From: Laurent MEUNIER Date: Thu, 21 Sep 2017 10:42:27 +0200 Subject: [PATCH] STM32: RNG: Ensure that no more than 1 instance is used There is only 1 RNG HW IP and we do not support more than one driver user at a time, so let's ensure this is the case and raise an error if needed. --- targets/TARGET_STM/trng_api.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/targets/TARGET_STM/trng_api.c b/targets/TARGET_STM/trng_api.c index 212bd318d9..f77f2b3151 100644 --- a/targets/TARGET_STM/trng_api.c +++ b/targets/TARGET_STM/trng_api.c @@ -23,11 +23,20 @@ #include #include "cmsis.h" #include "trng_api.h" +#include "mbed_error.h" +#include "mbed_critical.h" +static uint8_t users = 0; void trng_init(trng_t *obj) { uint32_t dummy; + + /* We're only supporting a single user of RNG */ + if (core_util_atomic_incr_u8(&users, 1) > 1 ) { + error("Only 1 RNG instance supported\r\n"); + } + #if defined(TARGET_STM32L4) RCC_PeriphCLKInitTypeDef PeriphClkInitStruct; @@ -57,6 +66,8 @@ void trng_free(trng_t *obj) HAL_RNG_DeInit(&obj->handle); /* RNG Peripheral clock disable - assume we're the only users of RNG */ __HAL_RCC_RNG_CLK_DISABLE(); + + users = 0; } int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length)