Page 1 of 2
Making use of STM32F407VET6's Truly Random Number Generator in Arduino IDE
Posted: Sun Sep 03, 2023 4:22 am
by _Northstrix_
Hello, Everyone.
Is there a way to make use of the STM32F407VET6's truly random number generator in the Arduino IDE?
Re: Making use of STM32F407VET6's Truly Random Number Generator in Arduino IDE
Posted: Sun Sep 03, 2023 7:16 pm
by dannyf
those peripherals are often fairly simple. Here is what I used for a STM32G030 (programmed as a G031):
https://dannyelectronics.wordpress.com/ ... stm32g041/
the rng piece is here:
Code: Select all
//true rng
//initialize the rng
void rngInit(void) {
RCC->AHBENR |= RCC_AHBENR_RNGEN; //ENABLE rng
//configure the clock
RCC->CCIPR = (RCC->CCIPR &~RCC_CCIPR_RNGSEL) | (1<<RCC_CCIPR_RNGSEL_Pos); //select the clock. 0->no clock, 1->HSI16, 2->sysclk, 3->pllclk
RCC->CCIPR = (RCC->CCIPR &~RCC_CCIPR_RNGDIV) | (0<<RCC_CCIPR_RNGDIV_Pos); //set the divider. 0->1:1, 1->2:1, 2->4:1, 3->8:1
RNG->CR = 0; //default value
RNG->SR = 0; //default value
RNG->CR|= RNG_CR_RNGEN; //ENABLE rng
}
you will need to check for the data ready flag beore reading the rng.
yours must be roughly the same.
Re: Making use of STM32F407VET6's Truly Random Number Generator in Arduino IDE
Posted: Mon Sep 04, 2023 2:51 am
by _Northstrix_
Thank you for your response.
But I guess these boards use different registers or something, so it won't even initialize.
And since I don't really understand how to work stuff like that, I guess, I'll just have to use the Arduino RNG again.
Re: Making use of STM32F407VET6's Truly Random Number Generator in Arduino IDE
Posted: Mon Sep 04, 2023 2:54 am
by _Northstrix_
Re: Making use of STM32F407VET6's Truly Random Number Generator in Arduino IDE
Posted: Mon Sep 04, 2023 4:38 am
by ag123
have you reviewed RM0009?
https://www.st.com/resource/en/referenc ... ronics.pdf
those are necessary references
Re: Making use of STM32F407VET6's Truly Random Number Generator in Arduino IDE
Posted: Mon Sep 04, 2023 8:34 am
by _Northstrix_
Ok, thank you.
That document gave me a very vague understanding of how the RNG works (in theory).
But still, I don't know what to do with it in practice.
Re: Making use of STM32F407VET6's Truly Random Number Generator in Arduino IDE
Posted: Mon Sep 04, 2023 1:24 pm
by fpiSTM
You can also use HAL directly:
First HAL RNG have to be enabled, to do this, simply add a file named "hal_conf_extra.h" and add this:
Then the code:
Code: Select all
RNG_HandleTypeDef hrng;
/**
* @brief RNG MSP Initialization
* This function configures the hardware resources used in this example
* @param hrng: RNG handle pointer
* @retval None
*/
extern "C" void HAL_RNG_MspInit(RNG_HandleTypeDef* hrng) {
/* Peripheral clock enable */
__HAL_RCC_RNG_CLK_ENABLE();
}
/**
* @brief RNG MSP De-Initialization
* This function freeze the hardware resources used in this example
* @param hrng: RNG handle pointer
* @retval None
*/
extern "C" void HAL_RNG_MspDeInit(RNG_HandleTypeDef* hrng) {
if (hrng->Instance == RNG) {
/* Peripheral clock disable */
__HAL_RCC_RNG_CLK_DISABLE();
/* Enable RNG reset state */
__HAL_RCC_RNG_FORCE_RESET();
/* Release RNG from reset state */
__HAL_RCC_RNG_RELEASE_RESET();
}
}
void setup() {
Serial.begin(9600);
hrng.Instance = RNG;
if (HAL_RNG_Init(&hrng) != HAL_OK) {
Error_Handler();
}
}
void loop() {
uint32_t aRandom32bit;
if (HAL_RNG_GenerateRandomNumber(&hrng, &aRandom32bit) != HAL_OK) {
/* Random number generation error */
Error_Handler();
} else {
Serial.println(aRandom32bit);
}
delay(1000);
}
Re: Making use of STM32F407VET6's Truly Random Number Generator in Arduino IDE
Posted: Mon Sep 04, 2023 1:27 pm
by dannyf
And since I don't really understand how to work stuff like that, I guess, I'll just have to use the Arduino RNG again.
the code I gave was for the G0 series. For your chip, you will have to read the datasheet / reference manul and the header file to see which bits will need to be set / reset for this to work. But the general approach is the same.
Re: Making use of STM32F407VET6's Truly Random Number Generator in Arduino IDE
Posted: Tue Sep 05, 2023 3:21 am
by _Northstrix_
fpiSTM wrote: Mon Sep 04, 2023 1:24 pm
You can also use HAL directly:
First HAL RNG have to be enabled, to do this, simply add a file named "hal_conf_extra.h" and add this:
Then the code:
Code: Select all
RNG_HandleTypeDef hrng;
/**
* @brief RNG MSP Initialization
* This function configures the hardware resources used in this example
* @param hrng: RNG handle pointer
* @retval None
*/
extern "C" void HAL_RNG_MspInit(RNG_HandleTypeDef* hrng) {
/* Peripheral clock enable */
__HAL_RCC_RNG_CLK_ENABLE();
}
/**
* @brief RNG MSP De-Initialization
* This function freeze the hardware resources used in this example
* @param hrng: RNG handle pointer
* @retval None
*/
extern "C" void HAL_RNG_MspDeInit(RNG_HandleTypeDef* hrng) {
if (hrng->Instance == RNG) {
/* Peripheral clock disable */
__HAL_RCC_RNG_CLK_DISABLE();
/* Enable RNG reset state */
__HAL_RCC_RNG_FORCE_RESET();
/* Release RNG from reset state */
__HAL_RCC_RNG_RELEASE_RESET();
}
}
void setup() {
Serial.begin(9600);
hrng.Instance = RNG;
if (HAL_RNG_Init(&hrng) != HAL_OK) {
Error_Handler();
}
}
void loop() {
uint32_t aRandom32bit;
if (HAL_RNG_GenerateRandomNumber(&hrng, &aRandom32bit) != HAL_OK) {
/* Random number generation error */
Error_Handler();
} else {
Serial.println(aRandom32bit);
}
delay(1000);
}
Thank you so much for that code. I have an issue with it though. It doesn't compile. And I don't know what to do with it.
I literally have zero experience with HAL. I only programmed the STM32 in Arduino IDE as if it was an Arduino.
Please, tell me what I'm doing wrong.

Re: Making use of STM32F407VET6's Truly Random Number Generator in Arduino IDE
Posted: Tue Sep 05, 2023 4:23 am
by fpiSTM
Read carefully my post.
First HAL RNG have to be enabled, to do this, simply add a file named "hal_conf_extra.h" and add this:
This means you have to create file with arduino ide