Skip to content

Commit aeacaa8

Browse files
authored
Merge pull request #13219 from jeromecoutant/PR_WB_RNG
STM32WB RNG: enable use from both M4 and M0+ cores
2 parents fe2fb48 + c8737c5 commit aeacaa8

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

targets/TARGET_STM/TARGET_STM32WB/STM32Cube_FW/hw_conf.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
* Semaphores
2929
* THIS SHALL NO BE CHANGED AS THESE SEMAPHORES ARE USED AS WELL ON THE CM0+
3030
*****************************************************************************/
31+
/* Index of the semaphore used to update HSI48 oscillator configuration */
32+
#define CFG_HW_HSI48_SEMID 5
33+
3134
/* Index of the semaphore used to manage the entry Stop Mode procedure */
3235
#define CFG_HW_ENTRY_STOP_MODE_SEMID 4
3336

targets/TARGET_STM/TARGET_STM32WB/TARGET_STM32WB55xx/TARGET_NUCLEO_WB55RG/system_clock.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,17 @@ void SetSysClock(void)
6767

6868
__HAL_RCC_HSEM_CLK_ENABLE();
6969

70+
/* This prevents the CPU2 (M0+) to configure RCC */
7071
while (LL_HSEM_1StepLock(HSEM, CFG_HW_RCC_SEMID));
7172

7273
Config_HSE();
7374

7475
__HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);
7576
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
7677

78+
/* This prevents the CPU2 (M0+) to disable the HSI48 oscillator */
79+
while (LL_HSEM_1StepLock(HSEM, CFG_HW_HSI48_SEMID));
80+
7781
/* Initializes the CPU, AHB and APB busses clocks */
7882
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_HSI48 | RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_LSE;
7983
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
@@ -101,8 +105,8 @@ void SetSysClock(void)
101105
error("HAL_RCC_ClockConfig error\n");
102106
}
103107

104-
/** Initializes the peripherals clocks
105-
*/
108+
/* Initializes the peripherals clocks */
109+
/* RNG needs to be configured like in M0 core, i.e. with HSI48 */
106110
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_SMPS | RCC_PERIPHCLK_RFWAKEUP | RCC_PERIPHCLK_RNG | RCC_PERIPHCLK_USB;
107111
PeriphClkInitStruct.UsbClockSelection = RCC_USBCLKSOURCE_HSI48;
108112
PeriphClkInitStruct.RngClockSelection = RCC_RNGCLKSOURCE_HSI48;

targets/TARGET_STM/TARGET_STM32WB/wb_sleep.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ extern void restore_timer_ctx(void);
2424
extern int serial_is_tx_ongoing(void);
2525
extern void PWR_EnterStopMode(void);
2626
extern void PWR_ExitStopMode(void);
27+
extern void SetSysClock(void);
2728

2829

2930
extern int mbed_sdk_inited;
@@ -58,6 +59,9 @@ void hal_deepsleep(void)
5859
PWR_EnterStopMode();
5960
PWR_ExitStopMode();
6061

62+
/* Force complete clock reconfiguration */
63+
SetSysClock();
64+
6165
restore_timer_ctx();
6266

6367
/* us_ticker context restored, allow HAL_GetTick() to read the us_ticker

targets/TARGET_STM/trng_api.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,22 @@
2525
#include "trng_api.h"
2626
#include "mbed_error.h"
2727
#include "mbed_atomic.h"
28+
2829
#if defined (TARGET_STM32WB)
2930
/* Family specific include for WB with HW semaphores */
3031
#include "hw.h"
3132
#include "hw_conf.h"
3233
#endif
3334

34-
static uint8_t users = 0;
3535

3636
void trng_init(trng_t *obj)
3737
{
3838
uint32_t dummy;
3939

40-
/* We're only supporting a single user of RNG */
41-
if (core_util_atomic_incr_u8(&users, 1) > 1) {
42-
error("Only 1 RNG instance supported\r\n");
43-
}
44-
4540
#if defined(RCC_PERIPHCLK_RNG) /* STM32L4 / STM32H7 / STM32WB */
4641

4742
#if defined(TARGET_STM32WB)
48-
/* No need to reconfigure RngClockSelection as RNG is already clocked by M0 */
43+
/* No need to configure RngClockSelection as already done in SetSysClock */
4944

5045
#elif defined(TARGET_STM32H7)
5146
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
@@ -92,6 +87,7 @@ void trng_init(trng_t *obj)
9287
#else
9388
#error("RNG clock not configured");
9489
#endif
90+
9591
#endif /* defined(RCC_PERIPHCLK_RNG) */
9692

9793
/* RNG Peripheral clock enable */
@@ -109,6 +105,7 @@ void trng_init(trng_t *obj)
109105
/* In case RNG is a shared ressource, get the HW semaphore first */
110106
while (LL_HSEM_1StepLock(HSEM, CFG_HW_RNG_SEMID));
111107
#endif
108+
112109
if (HAL_RNG_Init(&obj->handle) != HAL_OK) {
113110
error("trng_init: HAL_RNG_Init\n");
114111
}
@@ -123,6 +120,7 @@ void trng_init(trng_t *obj)
123120
#endif
124121
}
125122

123+
126124
void trng_free(trng_t *obj)
127125
{
128126
#if defined(CFG_HW_RNG_SEMID)
@@ -139,10 +137,9 @@ void trng_free(trng_t *obj)
139137
/* RNG Peripheral clock disable - assume we're the only users of RNG */
140138
__HAL_RCC_RNG_CLK_DISABLE();
141139
#endif
142-
143-
users = 0;
144140
}
145141

142+
146143
int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length)
147144
{
148145
int ret = 0;
@@ -154,6 +151,11 @@ int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_l
154151
while (LL_HSEM_1StepLock(HSEM, CFG_HW_RNG_SEMID));
155152
#endif
156153

154+
#if defined(TARGET_STM32WB)
155+
/* M0+ could have disabled RNG */
156+
__HAL_RNG_ENABLE(&obj->handle);
157+
#endif // TARGET_STM32WB
158+
157159
/* Get Random byte */
158160
while ((*output_length < length) && (ret == 0)) {
159161
if (HAL_RNG_GenerateRandomNumber(&obj->handle, (uint32_t *)random) != HAL_OK) {

0 commit comments

Comments
 (0)