Код: #include <stdio.h> #include <stdlib.h> #include "stm32wlxx_hal_def.h" #include "stm32wlxx_hal_rng.h" #include "stm32wlxx_hal_rcc.h"
void rng_init(void) { __HAL_RCC_RNG_CLK_ENABLE();
#if 0 RCC_PeriphCLKInitTypeDef PeriphClkInitStruct; //Select PLLQ output as RNG clock source PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RNG; PeriphClkInitStruct.RngClockSelection = RCC_RNGCLKSOURCE_PLL; HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct); #endif
RNG_HandleTypeDef h = {0}; h.Instance = RNG;
HAL_StatusTypeDef res = HAL_RNG_DeInit(&h); res = HAL_RNG_Init(&h); // <--------------- timeout if (HAL_OK == res) { uint32_t r; res = HAL_RNG_GenerateRandomNumber(&h, &r); // discard the first result if (HAL_OK == res) { res = HAL_RNG_GenerateRandomNumber(&h, &r); if (HAL_OK == res) { printf("RND: %lu\n", r); srand(r); } else { printf("[ERROR] HAL_RNG_GenerateRandomNumber(1) %d, %d\n", (int)res, (int)h.ErrorCode); } } else { printf("[ERROR] HAL_RNG_GenerateRandomNumber(2) %d, %d\n", (int)res, (int)h.ErrorCode); } } else { printf("[ERROR] HAL_RNG_Init( %d, %d )\n", (int)res, (int)h.ErrorCode); // <--------------- timeout }
HAL_RNG_DeInit(&h); __HAL_RCC_RNG_CLK_DISABLE(); }
|