Complete project attachment, replace \STM32Cube_FW_H5_V1.2.0\Projects\NUCLEO-H533RE\Applications\ThreadX\Tx_Thread_Creation
This article continues to experience RNG random number generation. Random numbers can be used for AES to generate IV, etc.
2. Hardware Driver
Add driver file
stm32h5xx_hal_rng.c
Uncomment in stm32h5xx_hal_conf.h
#define HAL_RNG_MODULE_ENABLED
MSP Initialize RNG
/**
* @brief RNG MSP Initialization
* This function configures the hardware resources used in this example
* @param hrng: RNG handle pointer
* @retval None
*/
void HAL_RNG_MspInit(RNG_HandleTypeDef* hrng)
{
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
if(hrng->Instance==RNG)
{
/* USER CODE BEGIN RNG_MspInit 0 */
/* USER CODE END RNG_MspInit 0 */
/** Initializes the peripherals clock
*/
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RNG;
PeriphClkInitStruct.RngClockSelection = RCC_RNGCLKSOURCE_HSI48;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
{
Error_Handler();
}
/* Peripheral clock enable */
__HAL_RCC_RNG_CLK_ENABLE();
/* RNG interrupt Init */
HAL_NVIC_SetPriority(RNG_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(RNG_IRQn);
/* USER CODE BEGIN RNG_MspInit 1 */
/* USER CODE END RNG_MspInit 1 */
}
}
/**
* @brief RNG MSP De-Initialization
* This function freeze the hardware resources used in this example
* @param hrng: RNG handle pointer
* @retval None
*/
void HAL_RNG_MspDeInit(RNG_HandleTypeDef* hrng)
{
if(hrng->Instance==RNG)
{
/* USER CODE BEGIN RNG_MspDeInit 0 */
/* USER CODE END RNG_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_RNG_CLK_DISABLE();
/* RNG interrupt DeInit */
HAL_NVIC_DisableIRQ(RNG_IRQn);
/* USER CODE BEGIN RNG_MspDeInit 1 */
/* USER CODE END RNG_MspDeInit 1 */
}
}
RNG_HandleTypeDef hrng;
RNG Initialization
hrng.Instance = RNG;
hrng.Init.ClockErrorDetection = RNG_CED_ENABLE;
if (HAL_RNG_Init(&hrng) != HAL_OK)
{
Error_Handler();
}
Interrupt handling
extern RNG_HandleTypeDef hrng;
/**
* @brief This function handles RNG global interrupt.
*/
void RNG_IRQHandler(void)
{
/* USER CODE BEGIN RNG_IRQn 0 */
/* USER CODE END RNG_IRQn 0 */
HAL_RNG_IRQHandler(&hrng);
/* USER CODE BEGIN RNG_IRQn 1 */
/* USER CODE END RNG_IRQn 1 */
}
Generate random numbers
HAL_RNG_GenerateRandomNumber
3. Test
We add commands to generate random numbers and then send them to the PC. The PC uses a visualization tool to draw a curve to see if the curve is random.
shell_func.c
Declare the implementation function
static void rng_func(uint8_t* param);
New commands for calculation
{ (uint8_t*)"rng", rng_func, (uint8_t*)"rng testtimes"}, "},
The implementation is as follows
static void rng_func(uint8_t* param)
{
uint32_t test_times;
if(sscanf((const char*)param,"%*s %d",&test_times) == 1)
{
hrng.Instance = RNG;
hrng.Init.ClockErrorDetection = RNG_CED_ENABLE;
if (HAL_RNG_Init(&hrng) != HAL_OK)
{
}
for(uint32_t i=0; i<test_times; i++)
{
uint32_t randnum;
HAL_RNG_GenerateRandomNumber(&hrng,&randomnum);
tx_thread_sleep(10);
xprintf("/*%d*/\r\n",randnum);
}
if(HAL_RNG_DeInit(&hrng) != HAL_OK)
{
}
}
}
The test is as follows
Input rng 1000
Check the curve as follows
Using a random number generator to generate random numbers makes the encryption result different each time, increasing the difficulty of cracking. This is the most commonly used method. This article experiences the random number generator of STM32H5, and the code is very simple.
Note that the HSI48 clock must be enabled.