Skip to content

Commit 849749f

Browse files
committed
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.
1 parent 14343c4 commit 849749f

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

targets/TARGET_STM/trng_api.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,20 @@
2323
#include <stdlib.h>
2424
#include "cmsis.h"
2525
#include "trng_api.h"
26+
#include "mbed_error.h"
27+
#include "mbed_critical.h"
2628

29+
static uint8_t users = 0;
2730

2831
void trng_init(trng_t *obj)
2932
{
3033
uint32_t dummy;
34+
35+
/* We're only supporting a single user of RNG */
36+
if (core_util_atomic_incr_u8(&users, 1) > 1 ) {
37+
error("Only 1 RNG instance supported\r\n");
38+
}
39+
3140
#if defined(TARGET_STM32L4)
3241
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
3342

@@ -57,6 +66,8 @@ void trng_free(trng_t *obj)
5766
HAL_RNG_DeInit(&obj->handle);
5867
/* RNG Peripheral clock disable - assume we're the only users of RNG */
5968
__HAL_RCC_RNG_CLK_DISABLE();
69+
70+
users = 0;
6071
}
6172

6273
int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length)

0 commit comments

Comments
 (0)