Skip to content

Commit 92ef812

Browse files
LMESTM0xc0170
authored andcommitted
STM32WB: Add TRNG HW Semaphore
Because TRNG is a shared resource between the 2 STM32WB cores, SW needs to acquire HW Semaphore before using the resource.
1 parent e6cecda commit 92ef812

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

targets/TARGET_STM/trng_api.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
#include "trng_api.h"
2626
#include "mbed_error.h"
2727
#include "mbed_critical.h"
28+
#if defined (TARGET_STM32WB)
29+
/* Family specific include for WB with HW semaphores */
30+
#include "hw.h"
31+
#include "hw_conf.h"
32+
#endif
2833

2934
static uint8_t users = 0;
3035

@@ -63,18 +68,36 @@ void trng_init(trng_t *obj)
6368
obj->handle.State = HAL_RNG_STATE_RESET;
6469
obj->handle.Lock = HAL_UNLOCKED;
6570

71+
#if defined(CFG_HW_RNG_SEMID)
72+
/* In case RNG is a shared ressource, get the HW semaphore first */
73+
while( LL_HSEM_1StepLock( HSEM, CFG_HW_RNG_SEMID ) );
74+
#endif
6675
HAL_RNG_Init(&obj->handle);
6776

6877
/* first random number generated after setting the RNGEN bit should not be used */
6978
HAL_RNG_GenerateRandomNumber(&obj->handle, &dummy);
79+
80+
#if defined(CFG_HW_RNG_SEMID)
81+
LL_HSEM_ReleaseLock( HSEM, CFG_HW_RNG_SEMID, 0 );
82+
#endif
7083
}
7184

7285
void trng_free(trng_t *obj)
7386
{
87+
#if defined(CFG_HW_RNG_SEMID)
88+
/* In case RNG is a shared ressource, get the HW semaphore first */
89+
while( LL_HSEM_1StepLock( HSEM, CFG_HW_RNG_SEMID ) );
90+
#endif
7491
/*Disable the RNG peripheral */
7592
HAL_RNG_DeInit(&obj->handle);
93+
94+
#if defined(CFG_HW_RNG_SEMID)
95+
/* In case RNG is a shared ressource, get the HW semaphore first */
96+
LL_HSEM_ReleaseLock( HSEM, CFG_HW_RNG_SEMID, 0 );
97+
#else
7698
/* RNG Peripheral clock disable - assume we're the only users of RNG */
7799
__HAL_RCC_RNG_CLK_DISABLE();
100+
#endif
78101

79102
users = 0;
80103
}
@@ -85,6 +108,11 @@ int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_l
85108
volatile uint8_t random[4];
86109
*output_length = 0;
87110

111+
#if defined(CFG_HW_RNG_SEMID)
112+
/* In case RNG is a shared ressource, get the HW semaphore first */
113+
while( LL_HSEM_1StepLock( HSEM, CFG_HW_RNG_SEMID ) );
114+
#endif
115+
88116
/* Get Random byte */
89117
while ((*output_length < length) && (ret == 0)) {
90118
if (HAL_RNG_GenerateRandomNumber(&obj->handle, (uint32_t *)random) != HAL_OK) {
@@ -103,6 +131,11 @@ int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_l
103131
ret = -1;
104132
}
105133

134+
#if defined(CFG_HW_RNG_SEMID)
135+
/* In case RNG is a shared ressource, get the HW semaphore first */
136+
LL_HSEM_ReleaseLock( HSEM, CFG_HW_RNG_SEMID, 0 );
137+
#endif
138+
106139
return (ret);
107140
}
108141

0 commit comments

Comments
 (0)