25
25
#include "trng_api.h"
26
26
#include "mbed_error.h"
27
27
#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
28
33
29
34
static uint8_t users = 0 ;
30
35
@@ -63,18 +68,36 @@ void trng_init(trng_t *obj)
63
68
obj -> handle .State = HAL_RNG_STATE_RESET ;
64
69
obj -> handle .Lock = HAL_UNLOCKED ;
65
70
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
66
75
HAL_RNG_Init (& obj -> handle );
67
76
68
77
/* first random number generated after setting the RNGEN bit should not be used */
69
78
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
70
83
}
71
84
72
85
void trng_free (trng_t * obj )
73
86
{
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
74
91
/*Disable the RNG peripheral */
75
92
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
76
98
/* RNG Peripheral clock disable - assume we're the only users of RNG */
77
99
__HAL_RCC_RNG_CLK_DISABLE ();
100
+ #endif
78
101
79
102
users = 0 ;
80
103
}
@@ -85,6 +108,11 @@ int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_l
85
108
volatile uint8_t random [4 ];
86
109
* output_length = 0 ;
87
110
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
+
88
116
/* Get Random byte */
89
117
while ((* output_length < length ) && (ret == 0 )) {
90
118
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
103
131
ret = -1 ;
104
132
}
105
133
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
+
106
139
return (ret );
107
140
}
108
141
0 commit comments