Making use of STM32F407VET6's Truly Random Number Generator in Arduino IDE
-
- Posts: 13
- Joined: Sun Sep 03, 2023 4:11 am
Making use of STM32F407VET6's Truly Random Number Generator in Arduino IDE
Hello, Everyone.
Is there a way to make use of the STM32F407VET6's truly random number generator in the Arduino IDE?
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
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:
you will need to check for the data ready flag beore reading the rng.
yours must be roughly the same.
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
}
yours must be roughly the same.
-
- Posts: 13
- Joined: Sun Sep 03, 2023 4:11 am
Re: Making use of STM32F407VET6's Truly Random Number Generator in Arduino IDE
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.
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.
-
- Posts: 13
- Joined: Sun Sep 03, 2023 4:11 am
Re: Making use of STM32F407VET6's Truly Random Number Generator in Arduino IDE
have you reviewed RM0009?
https://www.st.com/resource/en/referenc ... ronics.pdf
those are necessary references
https://www.st.com/resource/en/referenc ... ronics.pdf
those are necessary references
-
- Posts: 13
- Joined: Sun Sep 03, 2023 4:11 am
Re: Making use of STM32F407VET6's Truly Random Number Generator in Arduino IDE
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.
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
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:
First HAL RNG have to be enabled, to do this, simply add a file named "hal_conf_extra.h" and add this:
Code: Select all
#define HAL_RNG_MODULE_ENABLED
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
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.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.
-
- Posts: 13
- Joined: Sun Sep 03, 2023 4:11 am
Re: Making use of STM32F407VET6's Truly Random Number Generator in Arduino IDE
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.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
#define HAL_RNG_MODULE_ENABLED
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); }
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
Read carefully my post.
This means you have to create file with arduino ideFirst HAL RNG have to be enabled, to do this, simply add a file named "hal_conf_extra.h" and add this:
Code: Select all
#define HAL_RNG_MODULE_ENABLED