Making use of STM32F407VET6's Truly Random Number Generator in Arduino IDE

Post here first, or if you can't find a relevant section!
_Northstrix_
Posts: 13
Joined: Sun Sep 03, 2023 4:11 am

Making use of STM32F407VET6's Truly Random Number Generator in Arduino IDE

Post by _Northstrix_ »

Hello, Everyone.
Is there a way to make use of the STM32F407VET6's truly random number generator in the Arduino IDE?
dannyf
Posts: 446
Joined: Sat Jul 04, 2020 7:46 pm

Re: Making use of STM32F407VET6's Truly Random Number Generator in Arduino IDE

Post 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.
_Northstrix_
Posts: 13
Joined: Sun Sep 03, 2023 4:11 am

Re: Making use of STM32F407VET6's Truly Random Number Generator in Arduino IDE

Post 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.
ag123
Posts: 1653
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Making use of STM32F407VET6's Truly Random Number Generator in Arduino IDE

Post by ag123 »

have you reviewed RM0009?
https://www.st.com/resource/en/referenc ... ronics.pdf
those are necessary references
_Northstrix_
Posts: 13
Joined: Sun Sep 03, 2023 4:11 am

Re: Making use of STM32F407VET6's Truly Random Number Generator in Arduino IDE

Post 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.
User avatar
fpiSTM
Posts: 1723
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Making use of STM32F407VET6's Truly Random Number Generator in Arduino IDE

Post 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:

Code: Select all

#define HAL_RNG_MODULE_ENABLED
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);
}

dannyf
Posts: 446
Joined: Sat Jul 04, 2020 7:46 pm

Re: Making use of STM32F407VET6's Truly Random Number Generator in Arduino IDE

Post 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.
_Northstrix_
Posts: 13
Joined: Sun Sep 03, 2023 4:11 am

Re: Making use of STM32F407VET6's Truly Random Number Generator in Arduino IDE

Post 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:

Code: Select all

#define HAL_RNG_MODULE_ENABLED
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.
Image
Image
User avatar
fpiSTM
Posts: 1723
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Making use of STM32F407VET6's Truly Random Number Generator in Arduino IDE

Post 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:

Code: Select all

#define HAL_RNG_MODULE_ENABLED
This means you have to create file with arduino ide
Post Reply

Return to “General discussion”