Page 1 of 1

How to generate truly random numbers on STM32F401CCU6 in Arduino IDE?

Posted: Fri Nov 10, 2023 8:58 am
by _Northstrix_
Hello, Everyone.
I'm working on a project that requires the STM32F401CCU6 to generate truly random numbers.
Is there a way to do it in the Arduino IDE? And if there is, please give me a code that does it.
I tried to use the code from the STM32F407VET6 for that purpose, but it won't even compile.

Re: How to generate truly random numbers on STM32F401CCU6 in Arduino IDE?

Posted: Sat Nov 11, 2023 8:24 pm
by GonzoG
Probably the same as for F407:
viewtopic.php?p=12993#p12993

Re: How to generate truly random numbers on STM32F401CCU6 in Arduino IDE?

Posted: Sun Nov 12, 2023 4:40 am
by _Northstrix_
I've tried that.

And I got the following error:

Arduino: 1.8.19 (Windows 10), Board: "Generic STM32F4 series, Generic F401CC, STM32CubeProgrammer (DFU), Enabled (generic 'Serial'), CDC (generic 'Serial' supersede U(S)ART), Low/Full Speed, Smallest (-Os default), Newlib Nano (default)"





















STM32F407VET6_RNG:26:1: error: 'RNG_HandleTypeDef' does not name a type; did you mean 'RTC_HandleTypeDef'?

26 | RNG_HandleTypeDef hrng;

| ^~~~~~~~~~~~~~~~~

| RTC_HandleTypeDef

STM32F407VET6_RNG:34:34: error: variable or field 'HAL_RNG_MspInit' declared void

34 | extern "C" void HAL_RNG_MspInit(RNG_HandleTypeDef* hrng) {

| ^~~~~~~~~~~~~~~~~

STM32F407VET6_RNG:34:34: error: 'RNG_HandleTypeDef' was not declared in this scope; did you mean 'RTC_HandleTypeDef'?

34 | extern "C" void HAL_RNG_MspInit(RNG_HandleTypeDef* hrng) {

| ^~~~~~~~~~~~~~~~~

| RTC_HandleTypeDef

STM32F407VET6_RNG:34:53: error: 'hrng' was not declared in this scope

34 | extern "C" void HAL_RNG_MspInit(RNG_HandleTypeDef* hrng) {

| ^~~~

STM32F407VET6_RNG:45:36: error: variable or field 'HAL_RNG_MspDeInit' declared void

45 | extern "C" void HAL_RNG_MspDeInit(RNG_HandleTypeDef* hrng) {

| ^~~~~~~~~~~~~~~~~

STM32F407VET6_RNG:45:36: error: 'RNG_HandleTypeDef' was not declared in this scope; did you mean 'RTC_HandleTypeDef'?

45 | extern "C" void HAL_RNG_MspDeInit(RNG_HandleTypeDef* hrng) {

| ^~~~~~~~~~~~~~~~~

| RTC_HandleTypeDef

STM32F407VET6_RNG:45:55: error: 'hrng' was not declared in this scope

45 | extern "C" void HAL_RNG_MspDeInit(RNG_HandleTypeDef* hrng) {

| ^~~~

STM32F407VET6_RNG:34:33: error: variable or field 'HAL_RNG_MspInit' declared void

34 | extern "C" void HAL_RNG_MspInit(RNG_HandleTypeDef* hrng) {

| ^~~~~~~~~~~~~~~~~

STM32F407VET6_RNG:34:33: error: 'RNG_HandleTypeDef' was not declared in this scope; did you mean 'RTC_HandleTypeDef'?

34 | extern "C" void HAL_RNG_MspInit(RNG_HandleTypeDef* hrng) {

| ^~~~~~~~~~~~~~~~~

| RTC_HandleTypeDef

STM32F407VET6_RNG:34:52: error: 'hrng' was not declared in this scope

34 | extern "C" void HAL_RNG_MspInit(RNG_HandleTypeDef* hrng) {

| ^~~~

STM32F407VET6_RNG:45:35: error: variable or field 'HAL_RNG_MspDeInit' declared void

45 | extern "C" void HAL_RNG_MspDeInit(RNG_HandleTypeDef* hrng) {

| ^~~~~~~~~~~~~~~~~

STM32F407VET6_RNG:45:35: error: 'RNG_HandleTypeDef' was not declared in this scope; did you mean 'RTC_HandleTypeDef'?

45 | extern "C" void HAL_RNG_MspDeInit(RNG_HandleTypeDef* hrng) {

| ^~~~~~~~~~~~~~~~~

| RTC_HandleTypeDef

STM32F407VET6_RNG:45:54: error: 'hrng' was not declared in this scope

45 | extern "C" void HAL_RNG_MspDeInit(RNG_HandleTypeDef* hrng) {

| ^~~~

C:\STM32F407VET6_RNG\STM32F407VET6_RNG.ino: In function 'void setup()':

STM32F407VET6_RNG:60:3: error: 'hrng' was not declared in this scope

60 | hrng.Instance = RNG;

| ^~~~

STM32F407VET6_RNG:60:19: error: 'RNG' was not declared in this scope

60 | hrng.Instance = RNG;

| ^~~

STM32F407VET6_RNG:61:7: error: 'HAL_RNG_Init' was not declared in this scope; did you mean 'HAL_RTC_Init'?

61 | if (HAL_RNG_Init(&hrng) != HAL_OK) {

| ^~~~~~~~~~~~

| HAL_RTC_Init

C:\STM32F407VET6_RNG\STM32F407VET6_RNG.ino: In function 'void loop()':

STM32F407VET6_RNG:68:37: error: 'hrng' was not declared in this scope

68 | if (HAL_RNG_GenerateRandomNumber(&hrng, &aRandom32bit) != HAL_OK) {

| ^~~~

STM32F407VET6_RNG:68:7: error: 'HAL_RNG_GenerateRandomNumber' was not declared in this scope

68 | if (HAL_RNG_GenerateRandomNumber(&hrng, &aRandom32bit) != HAL_OK) {

| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~

exit status 1

'RNG_HandleTypeDef' does not name a type; did you mean 'RTC_HandleTypeDef'?



This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Re: How to generate truly random numbers on STM32F401CCU6 in Arduino IDE?

Posted: Tue Nov 14, 2023 8:49 am
by ag123
rm0368 stm32f401 reference manual
https://www.st.com/resource/en/referenc ... ronics.pdf

stm32f401 apparently don't have hardware RNG and crypto
hence, your options are PRNG
https://en.wikipedia.org/wiki/List_of_r ... rs_(PRNGs)
f4xx has an FPU hence, you can even use those PRNG that is based on floating point values

to seed that, you can try things like to run the RTC and seed that with RTC time each time the user manually resets.
Or place a wire like at least 10s of cm long and read the analog pin, it may pick up random radio waves and perhaps you can use a random radio wave value to seed the PRNG

Re: How to generate truly random numbers on STM32F401CCU6 in Arduino IDE?

Posted: Wed Nov 15, 2023 9:39 am
by _Northstrix_
Got it. Thank you.