Skip to content

Commit a4c0586

Browse files
Merge pull request #6560 from dgarske/stm32_nuttx
Support for using the Nuttx STM RNG register definitions
2 parents 052fba8 + 53c30f4 commit a4c0586

1 file changed

Lines changed: 23 additions & 6 deletions

File tree

wolfcrypt/src/random.c

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2949,7 +2949,22 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
29492949

29502950
return 0;
29512951
}
2952-
#elif defined(WOLFSSL_STM32F427_RNG) || defined(WOLFSSL_STM32_RNG_NOLIB)
2952+
#elif defined(WOLFSSL_STM32F427_RNG) || defined(WOLFSSL_STM32_RNG_NOLIB) \
2953+
|| defined(STM32_NUTTX_RNG)
2954+
2955+
#ifdef STM32_NUTTX_RNG
2956+
#include "hardware/stm32_rng.h"
2957+
/* Set CONFIG_STM32U5_RNG in NuttX to enable the RCC */
2958+
#define WC_RNG_CR *((volatile uint32_t*)(STM32_RNG_CR))
2959+
#define WC_RNG_SR *((volatile uint32_t*)(STM32_RNG_SR))
2960+
#define WC_RNG_DR *((volatile uint32_t*)(STM32_RNG_DR))
2961+
#else
2962+
/* Comes from "stm32xxxx_hal.h" */
2963+
#define WC_RNG_CR RNG->CR
2964+
#define WC_RNG_SR RNG->SR
2965+
#define WC_RNG_DR RNG->DR
2966+
#endif
2967+
29532968

29542969
/* Generate a RNG seed using the hardware RNG on the STM32F427
29552970
* directly, following steps outlined in STM32F4 Reference
@@ -2965,29 +2980,31 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
29652980
return ret;
29662981
}
29672982

2983+
#ifndef STM32_NUTTX_RNG
29682984
/* enable RNG peripheral clock */
29692985
RCC->AHB2ENR |= RCC_AHB2ENR_RNGEN;
2986+
#endif
29702987

29712988
/* enable RNG interrupt, set IE bit in RNG->CR register */
2972-
RNG->CR |= RNG_CR_IE;
2989+
WC_RNG_CR |= RNG_CR_IE;
29732990

29742991
/* enable RNG, set RNGEN bit in RNG->CR. Activates RNG,
29752992
* RNG_LFSR, and error detector */
2976-
RNG->CR |= RNG_CR_RNGEN;
2993+
WC_RNG_CR |= RNG_CR_RNGEN;
29772994

29782995
/* verify no errors, make sure SEIS and CEIS bits are 0
29792996
* in RNG->SR register */
2980-
if (RNG->SR & (RNG_SR_SECS | RNG_SR_CECS)) {
2997+
if (WC_RNG_SR & (RNG_SR_SECS | RNG_SR_CECS)) {
29812998
wolfSSL_CryptHwMutexUnLock();
29822999
return RNG_FAILURE_E;
29833000
}
29843001

29853002
for (i = 0; i < sz; i++) {
29863003
/* wait until RNG number is ready */
2987-
while ((RNG->SR & RNG_SR_DRDY) == 0) { }
3004+
while ((WC_RNG_SR & RNG_SR_DRDY) == 0) { }
29883005

29893006
/* get value */
2990-
output[i] = RNG->DR;
3007+
output[i] = WC_RNG_DR;
29913008
}
29923009

29933010
wolfSSL_CryptHwMutexUnLock();

0 commit comments

Comments
 (0)