训练营PLSR题目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

868 lines
25 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_rng.c
  4. * @author MCD Application Team
  5. * @brief RNG HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Random Number Generator (RNG) peripheral:
  8. * + Initialization and configuration functions
  9. * + Peripheral Control functions
  10. * + Peripheral State functions
  11. *
  12. ******************************************************************************
  13. * @attention
  14. *
  15. * Copyright (c) 2016 STMicroelectronics.
  16. * All rights reserved.
  17. *
  18. * This software is licensed under terms that can be found in the LICENSE file
  19. * in the root directory of this software component.
  20. * If no LICENSE file comes with this software, it is provided AS-IS.
  21. *
  22. ******************************************************************************
  23. @verbatim
  24. ==============================================================================
  25. ##### How to use this driver #####
  26. ==============================================================================
  27. [..]
  28. The RNG HAL driver can be used as follows:
  29. (#) Enable the RNG controller clock using __HAL_RCC_RNG_CLK_ENABLE() macro
  30. in HAL_RNG_MspInit().
  31. (#) Activate the RNG peripheral using HAL_RNG_Init() function.
  32. (#) Wait until the 32 bit Random Number Generator contains a valid
  33. random data using (polling/interrupt) mode.
  34. (#) Get the 32 bit random number using HAL_RNG_GenerateRandomNumber() function.
  35. ##### Callback registration #####
  36. ==================================
  37. [..]
  38. The compilation define USE_HAL_RNG_REGISTER_CALLBACKS when set to 1
  39. allows the user to configure dynamically the driver callbacks.
  40. [..]
  41. Use Function HAL_RNG_RegisterCallback() to register a user callback.
  42. Function HAL_RNG_RegisterCallback() allows to register following callbacks:
  43. (+) ErrorCallback : RNG Error Callback.
  44. (+) MspInitCallback : RNG MspInit.
  45. (+) MspDeInitCallback : RNG MspDeInit.
  46. This function takes as parameters the HAL peripheral handle, the Callback ID
  47. and a pointer to the user callback function.
  48. [..]
  49. Use function HAL_RNG_UnRegisterCallback() to reset a callback to the default
  50. weak (surcharged) function.
  51. HAL_RNG_UnRegisterCallback() takes as parameters the HAL peripheral handle,
  52. and the Callback ID.
  53. This function allows to reset following callbacks:
  54. (+) ErrorCallback : RNG Error Callback.
  55. (+) MspInitCallback : RNG MspInit.
  56. (+) MspDeInitCallback : RNG MspDeInit.
  57. [..]
  58. For specific callback ReadyDataCallback, use dedicated register callbacks:
  59. respectively HAL_RNG_RegisterReadyDataCallback() , HAL_RNG_UnRegisterReadyDataCallback().
  60. [..]
  61. By default, after the HAL_RNG_Init() and when the state is HAL_RNG_STATE_RESET
  62. all callbacks are set to the corresponding weak (surcharged) functions:
  63. example HAL_RNG_ErrorCallback().
  64. Exception done for MspInit and MspDeInit functions that are respectively
  65. reset to the legacy weak (surcharged) functions in the HAL_RNG_Init()
  66. and HAL_RNG_DeInit() only when these callbacks are null (not registered beforehand).
  67. If not, MspInit or MspDeInit are not null, the HAL_RNG_Init() and HAL_RNG_DeInit()
  68. keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
  69. [..]
  70. Callbacks can be registered/unregistered in HAL_RNG_STATE_READY state only.
  71. Exception done MspInit/MspDeInit that can be registered/unregistered
  72. in HAL_RNG_STATE_READY or HAL_RNG_STATE_RESET state, thus registered (user)
  73. MspInit/DeInit callbacks can be used during the Init/DeInit.
  74. In that case first register the MspInit/MspDeInit user callbacks
  75. using HAL_RNG_RegisterCallback() before calling HAL_RNG_DeInit()
  76. or HAL_RNG_Init() function.
  77. [..]
  78. When The compilation define USE_HAL_RNG_REGISTER_CALLBACKS is set to 0 or
  79. not defined, the callback registration feature is not available
  80. and weak (surcharged) callbacks are used.
  81. @endverbatim
  82. ******************************************************************************
  83. */
  84. /* Includes ------------------------------------------------------------------*/
  85. #include "stm32f4xx_hal.h"
  86. /** @addtogroup STM32F4xx_HAL_Driver
  87. * @{
  88. */
  89. #if defined (RNG)
  90. /** @addtogroup RNG
  91. * @brief RNG HAL module driver.
  92. * @{
  93. */
  94. #ifdef HAL_RNG_MODULE_ENABLED
  95. /* Private types -------------------------------------------------------------*/
  96. /* Private defines -----------------------------------------------------------*/
  97. /* Private variables ---------------------------------------------------------*/
  98. /* Private constants ---------------------------------------------------------*/
  99. /** @defgroup RNG_Private_Constants RNG Private Constants
  100. * @{
  101. */
  102. #define RNG_TIMEOUT_VALUE 2U
  103. /**
  104. * @}
  105. */
  106. /* Private macros ------------------------------------------------------------*/
  107. /* Private functions prototypes ----------------------------------------------*/
  108. /* Private functions ---------------------------------------------------------*/
  109. /* Exported functions --------------------------------------------------------*/
  110. /** @addtogroup RNG_Exported_Functions
  111. * @{
  112. */
  113. /** @addtogroup RNG_Exported_Functions_Group1
  114. * @brief Initialization and configuration functions
  115. *
  116. @verbatim
  117. ===============================================================================
  118. ##### Initialization and configuration functions #####
  119. ===============================================================================
  120. [..] This section provides functions allowing to:
  121. (+) Initialize the RNG according to the specified parameters
  122. in the RNG_InitTypeDef and create the associated handle
  123. (+) DeInitialize the RNG peripheral
  124. (+) Initialize the RNG MSP
  125. (+) DeInitialize RNG MSP
  126. @endverbatim
  127. * @{
  128. */
  129. /**
  130. * @brief Initializes the RNG peripheral and creates the associated handle.
  131. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  132. * the configuration information for RNG.
  133. * @retval HAL status
  134. */
  135. HAL_StatusTypeDef HAL_RNG_Init(RNG_HandleTypeDef *hrng)
  136. {
  137. /* Check the RNG handle allocation */
  138. if (hrng == NULL)
  139. {
  140. return HAL_ERROR;
  141. }
  142. /* Check the parameters */
  143. assert_param(IS_RNG_ALL_INSTANCE(hrng->Instance));
  144. #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
  145. if (hrng->State == HAL_RNG_STATE_RESET)
  146. {
  147. /* Allocate lock resource and initialize it */
  148. hrng->Lock = HAL_UNLOCKED;
  149. hrng->ReadyDataCallback = HAL_RNG_ReadyDataCallback; /* Legacy weak ReadyDataCallback */
  150. hrng->ErrorCallback = HAL_RNG_ErrorCallback; /* Legacy weak ErrorCallback */
  151. if (hrng->MspInitCallback == NULL)
  152. {
  153. hrng->MspInitCallback = HAL_RNG_MspInit; /* Legacy weak MspInit */
  154. }
  155. /* Init the low level hardware */
  156. hrng->MspInitCallback(hrng);
  157. }
  158. #else
  159. if (hrng->State == HAL_RNG_STATE_RESET)
  160. {
  161. /* Allocate lock resource and initialize it */
  162. hrng->Lock = HAL_UNLOCKED;
  163. /* Init the low level hardware */
  164. HAL_RNG_MspInit(hrng);
  165. }
  166. #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
  167. /* Change RNG peripheral state */
  168. hrng->State = HAL_RNG_STATE_BUSY;
  169. /* Enable the RNG Peripheral */
  170. __HAL_RNG_ENABLE(hrng);
  171. /* Initialize the RNG state */
  172. hrng->State = HAL_RNG_STATE_READY;
  173. /* Initialise the error code */
  174. hrng->ErrorCode = HAL_RNG_ERROR_NONE;
  175. /* Return function status */
  176. return HAL_OK;
  177. }
  178. /**
  179. * @brief DeInitializes the RNG peripheral.
  180. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  181. * the configuration information for RNG.
  182. * @retval HAL status
  183. */
  184. HAL_StatusTypeDef HAL_RNG_DeInit(RNG_HandleTypeDef *hrng)
  185. {
  186. /* Check the RNG handle allocation */
  187. if (hrng == NULL)
  188. {
  189. return HAL_ERROR;
  190. }
  191. /* Disable the RNG Peripheral */
  192. CLEAR_BIT(hrng->Instance->CR, RNG_CR_IE | RNG_CR_RNGEN);
  193. /* Clear RNG interrupt status flags */
  194. CLEAR_BIT(hrng->Instance->SR, RNG_SR_CEIS | RNG_SR_SEIS);
  195. #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
  196. if (hrng->MspDeInitCallback == NULL)
  197. {
  198. hrng->MspDeInitCallback = HAL_RNG_MspDeInit; /* Legacy weak MspDeInit */
  199. }
  200. /* DeInit the low level hardware */
  201. hrng->MspDeInitCallback(hrng);
  202. #else
  203. /* DeInit the low level hardware */
  204. HAL_RNG_MspDeInit(hrng);
  205. #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
  206. /* Update the RNG state */
  207. hrng->State = HAL_RNG_STATE_RESET;
  208. /* Initialise the error code */
  209. hrng->ErrorCode = HAL_RNG_ERROR_NONE;
  210. /* Release Lock */
  211. __HAL_UNLOCK(hrng);
  212. /* Return the function status */
  213. return HAL_OK;
  214. }
  215. /**
  216. * @brief Initializes the RNG MSP.
  217. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  218. * the configuration information for RNG.
  219. * @retval None
  220. */
  221. __weak void HAL_RNG_MspInit(RNG_HandleTypeDef *hrng)
  222. {
  223. /* Prevent unused argument(s) compilation warning */
  224. UNUSED(hrng);
  225. /* NOTE : This function should not be modified. When the callback is needed,
  226. function HAL_RNG_MspInit must be implemented in the user file.
  227. */
  228. }
  229. /**
  230. * @brief DeInitializes the RNG MSP.
  231. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  232. * the configuration information for RNG.
  233. * @retval None
  234. */
  235. __weak void HAL_RNG_MspDeInit(RNG_HandleTypeDef *hrng)
  236. {
  237. /* Prevent unused argument(s) compilation warning */
  238. UNUSED(hrng);
  239. /* NOTE : This function should not be modified. When the callback is needed,
  240. function HAL_RNG_MspDeInit must be implemented in the user file.
  241. */
  242. }
  243. #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
  244. /**
  245. * @brief Register a User RNG Callback
  246. * To be used instead of the weak predefined callback
  247. * @param hrng RNG handle
  248. * @param CallbackID ID of the callback to be registered
  249. * This parameter can be one of the following values:
  250. * @arg @ref HAL_RNG_ERROR_CB_ID Error callback ID
  251. * @arg @ref HAL_RNG_MSPINIT_CB_ID MspInit callback ID
  252. * @arg @ref HAL_RNG_MSPDEINIT_CB_ID MspDeInit callback ID
  253. * @param pCallback pointer to the Callback function
  254. * @retval HAL status
  255. */
  256. HAL_StatusTypeDef HAL_RNG_RegisterCallback(RNG_HandleTypeDef *hrng, HAL_RNG_CallbackIDTypeDef CallbackID,
  257. pRNG_CallbackTypeDef pCallback)
  258. {
  259. HAL_StatusTypeDef status = HAL_OK;
  260. if (pCallback == NULL)
  261. {
  262. /* Update the error code */
  263. hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
  264. return HAL_ERROR;
  265. }
  266. /* Process locked */
  267. __HAL_LOCK(hrng);
  268. if (HAL_RNG_STATE_READY == hrng->State)
  269. {
  270. switch (CallbackID)
  271. {
  272. case HAL_RNG_ERROR_CB_ID :
  273. hrng->ErrorCallback = pCallback;
  274. break;
  275. case HAL_RNG_MSPINIT_CB_ID :
  276. hrng->MspInitCallback = pCallback;
  277. break;
  278. case HAL_RNG_MSPDEINIT_CB_ID :
  279. hrng->MspDeInitCallback = pCallback;
  280. break;
  281. default :
  282. /* Update the error code */
  283. hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
  284. /* Return error status */
  285. status = HAL_ERROR;
  286. break;
  287. }
  288. }
  289. else if (HAL_RNG_STATE_RESET == hrng->State)
  290. {
  291. switch (CallbackID)
  292. {
  293. case HAL_RNG_MSPINIT_CB_ID :
  294. hrng->MspInitCallback = pCallback;
  295. break;
  296. case HAL_RNG_MSPDEINIT_CB_ID :
  297. hrng->MspDeInitCallback = pCallback;
  298. break;
  299. default :
  300. /* Update the error code */
  301. hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
  302. /* Return error status */
  303. status = HAL_ERROR;
  304. break;
  305. }
  306. }
  307. else
  308. {
  309. /* Update the error code */
  310. hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
  311. /* Return error status */
  312. status = HAL_ERROR;
  313. }
  314. /* Release Lock */
  315. __HAL_UNLOCK(hrng);
  316. return status;
  317. }
  318. /**
  319. * @brief Unregister an RNG Callback
  320. * RNG callback is redirected to the weak predefined callback
  321. * @param hrng RNG handle
  322. * @param CallbackID ID of the callback to be unregistered
  323. * This parameter can be one of the following values:
  324. * @arg @ref HAL_RNG_ERROR_CB_ID Error callback ID
  325. * @arg @ref HAL_RNG_MSPINIT_CB_ID MspInit callback ID
  326. * @arg @ref HAL_RNG_MSPDEINIT_CB_ID MspDeInit callback ID
  327. * @retval HAL status
  328. */
  329. HAL_StatusTypeDef HAL_RNG_UnRegisterCallback(RNG_HandleTypeDef *hrng, HAL_RNG_CallbackIDTypeDef CallbackID)
  330. {
  331. HAL_StatusTypeDef status = HAL_OK;
  332. /* Process locked */
  333. __HAL_LOCK(hrng);
  334. if (HAL_RNG_STATE_READY == hrng->State)
  335. {
  336. switch (CallbackID)
  337. {
  338. case HAL_RNG_ERROR_CB_ID :
  339. hrng->ErrorCallback = HAL_RNG_ErrorCallback; /* Legacy weak ErrorCallback */
  340. break;
  341. case HAL_RNG_MSPINIT_CB_ID :
  342. hrng->MspInitCallback = HAL_RNG_MspInit; /* Legacy weak MspInit */
  343. break;
  344. case HAL_RNG_MSPDEINIT_CB_ID :
  345. hrng->MspDeInitCallback = HAL_RNG_MspDeInit; /* Legacy weak MspDeInit */
  346. break;
  347. default :
  348. /* Update the error code */
  349. hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
  350. /* Return error status */
  351. status = HAL_ERROR;
  352. break;
  353. }
  354. }
  355. else if (HAL_RNG_STATE_RESET == hrng->State)
  356. {
  357. switch (CallbackID)
  358. {
  359. case HAL_RNG_MSPINIT_CB_ID :
  360. hrng->MspInitCallback = HAL_RNG_MspInit; /* Legacy weak MspInit */
  361. break;
  362. case HAL_RNG_MSPDEINIT_CB_ID :
  363. hrng->MspDeInitCallback = HAL_RNG_MspDeInit; /* Legacy weak MspInit */
  364. break;
  365. default :
  366. /* Update the error code */
  367. hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
  368. /* Return error status */
  369. status = HAL_ERROR;
  370. break;
  371. }
  372. }
  373. else
  374. {
  375. /* Update the error code */
  376. hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
  377. /* Return error status */
  378. status = HAL_ERROR;
  379. }
  380. /* Release Lock */
  381. __HAL_UNLOCK(hrng);
  382. return status;
  383. }
  384. /**
  385. * @brief Register Data Ready RNG Callback
  386. * To be used instead of the weak HAL_RNG_ReadyDataCallback() predefined callback
  387. * @param hrng RNG handle
  388. * @param pCallback pointer to the Data Ready Callback function
  389. * @retval HAL status
  390. */
  391. HAL_StatusTypeDef HAL_RNG_RegisterReadyDataCallback(RNG_HandleTypeDef *hrng, pRNG_ReadyDataCallbackTypeDef pCallback)
  392. {
  393. HAL_StatusTypeDef status = HAL_OK;
  394. if (pCallback == NULL)
  395. {
  396. /* Update the error code */
  397. hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
  398. return HAL_ERROR;
  399. }
  400. /* Process locked */
  401. __HAL_LOCK(hrng);
  402. if (HAL_RNG_STATE_READY == hrng->State)
  403. {
  404. hrng->ReadyDataCallback = pCallback;
  405. }
  406. else
  407. {
  408. /* Update the error code */
  409. hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
  410. /* Return error status */
  411. status = HAL_ERROR;
  412. }
  413. /* Release Lock */
  414. __HAL_UNLOCK(hrng);
  415. return status;
  416. }
  417. /**
  418. * @brief UnRegister the Data Ready RNG Callback
  419. * Data Ready RNG Callback is redirected to the weak HAL_RNG_ReadyDataCallback() predefined callback
  420. * @param hrng RNG handle
  421. * @retval HAL status
  422. */
  423. HAL_StatusTypeDef HAL_RNG_UnRegisterReadyDataCallback(RNG_HandleTypeDef *hrng)
  424. {
  425. HAL_StatusTypeDef status = HAL_OK;
  426. /* Process locked */
  427. __HAL_LOCK(hrng);
  428. if (HAL_RNG_STATE_READY == hrng->State)
  429. {
  430. hrng->ReadyDataCallback = HAL_RNG_ReadyDataCallback; /* Legacy weak ReadyDataCallback */
  431. }
  432. else
  433. {
  434. /* Update the error code */
  435. hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
  436. /* Return error status */
  437. status = HAL_ERROR;
  438. }
  439. /* Release Lock */
  440. __HAL_UNLOCK(hrng);
  441. return status;
  442. }
  443. #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
  444. /**
  445. * @}
  446. */
  447. /** @addtogroup RNG_Exported_Functions_Group2
  448. * @brief Peripheral Control functions
  449. *
  450. @verbatim
  451. ===============================================================================
  452. ##### Peripheral Control functions #####
  453. ===============================================================================
  454. [..] This section provides functions allowing to:
  455. (+) Get the 32 bit Random number
  456. (+) Get the 32 bit Random number with interrupt enabled
  457. (+) Handle RNG interrupt request
  458. @endverbatim
  459. * @{
  460. */
  461. /**
  462. * @brief Generates a 32-bit random number.
  463. * @note Each time the random number data is read the RNG_FLAG_DRDY flag
  464. * is automatically cleared.
  465. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  466. * the configuration information for RNG.
  467. * @param random32bit pointer to generated random number variable if successful.
  468. * @retval HAL status
  469. */
  470. HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber(RNG_HandleTypeDef *hrng, uint32_t *random32bit)
  471. {
  472. uint32_t tickstart;
  473. HAL_StatusTypeDef status = HAL_OK;
  474. /* Process Locked */
  475. __HAL_LOCK(hrng);
  476. /* Check RNG peripheral state */
  477. if (hrng->State == HAL_RNG_STATE_READY)
  478. {
  479. /* Change RNG peripheral state */
  480. hrng->State = HAL_RNG_STATE_BUSY;
  481. /* Get tick */
  482. tickstart = HAL_GetTick();
  483. /* Check if data register contains valid random data */
  484. while (__HAL_RNG_GET_FLAG(hrng, RNG_FLAG_DRDY) == RESET)
  485. {
  486. if ((HAL_GetTick() - tickstart) > RNG_TIMEOUT_VALUE)
  487. {
  488. /* New check to avoid false timeout detection in case of preemption */
  489. if (__HAL_RNG_GET_FLAG(hrng, RNG_FLAG_DRDY) == RESET)
  490. {
  491. hrng->State = HAL_RNG_STATE_READY;
  492. hrng->ErrorCode = HAL_RNG_ERROR_TIMEOUT;
  493. /* Process Unlocked */
  494. __HAL_UNLOCK(hrng);
  495. return HAL_ERROR;
  496. }
  497. }
  498. }
  499. /* Get a 32bit Random number */
  500. hrng->RandomNumber = hrng->Instance->DR;
  501. *random32bit = hrng->RandomNumber;
  502. hrng->State = HAL_RNG_STATE_READY;
  503. }
  504. else
  505. {
  506. hrng->ErrorCode = HAL_RNG_ERROR_BUSY;
  507. status = HAL_ERROR;
  508. }
  509. /* Process Unlocked */
  510. __HAL_UNLOCK(hrng);
  511. return status;
  512. }
  513. /**
  514. * @brief Generates a 32-bit random number in interrupt mode.
  515. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  516. * the configuration information for RNG.
  517. * @retval HAL status
  518. */
  519. HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber_IT(RNG_HandleTypeDef *hrng)
  520. {
  521. HAL_StatusTypeDef status = HAL_OK;
  522. /* Process Locked */
  523. __HAL_LOCK(hrng);
  524. /* Check RNG peripheral state */
  525. if (hrng->State == HAL_RNG_STATE_READY)
  526. {
  527. /* Change RNG peripheral state */
  528. hrng->State = HAL_RNG_STATE_BUSY;
  529. /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */
  530. __HAL_RNG_ENABLE_IT(hrng);
  531. }
  532. else
  533. {
  534. /* Process Unlocked */
  535. __HAL_UNLOCK(hrng);
  536. hrng->ErrorCode = HAL_RNG_ERROR_BUSY;
  537. status = HAL_ERROR;
  538. }
  539. return status;
  540. }
  541. /**
  542. * @brief Returns generated random number in polling mode (Obsolete)
  543. * Use HAL_RNG_GenerateRandomNumber() API instead.
  544. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  545. * the configuration information for RNG.
  546. * @retval Random value
  547. */
  548. uint32_t HAL_RNG_GetRandomNumber(RNG_HandleTypeDef *hrng)
  549. {
  550. if (HAL_RNG_GenerateRandomNumber(hrng, &(hrng->RandomNumber)) == HAL_OK)
  551. {
  552. return hrng->RandomNumber;
  553. }
  554. else
  555. {
  556. return 0U;
  557. }
  558. }
  559. /**
  560. * @brief Returns a 32-bit random number with interrupt enabled (Obsolete),
  561. * Use HAL_RNG_GenerateRandomNumber_IT() API instead.
  562. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  563. * the configuration information for RNG.
  564. * @retval 32-bit random number
  565. */
  566. uint32_t HAL_RNG_GetRandomNumber_IT(RNG_HandleTypeDef *hrng)
  567. {
  568. uint32_t random32bit = 0U;
  569. /* Process locked */
  570. __HAL_LOCK(hrng);
  571. /* Change RNG peripheral state */
  572. hrng->State = HAL_RNG_STATE_BUSY;
  573. /* Get a 32bit Random number */
  574. random32bit = hrng->Instance->DR;
  575. /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */
  576. __HAL_RNG_ENABLE_IT(hrng);
  577. /* Return the 32 bit random number */
  578. return random32bit;
  579. }
  580. /**
  581. * @brief Handles RNG interrupt request.
  582. * @note In the case of a clock error, the RNG is no more able to generate
  583. * random numbers because the PLL48CLK clock is not correct. User has
  584. * to check that the clock controller is correctly configured to provide
  585. * the RNG clock and clear the CEIS bit using __HAL_RNG_CLEAR_IT().
  586. * The clock error has no impact on the previously generated
  587. * random numbers, and the RNG_DR register contents can be used.
  588. * @note In the case of a seed error, the generation of random numbers is
  589. * interrupted as long as the SECS bit is '1'. If a number is
  590. * available in the RNG_DR register, it must not be used because it may
  591. * not have enough entropy. In this case, it is recommended to clear the
  592. * SEIS bit using __HAL_RNG_CLEAR_IT(), then disable and enable
  593. * the RNG peripheral to reinitialize and restart the RNG.
  594. * @note User-written HAL_RNG_ErrorCallback() API is called once whether SEIS
  595. * or CEIS are set.
  596. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  597. * the configuration information for RNG.
  598. * @retval None
  599. */
  600. void HAL_RNG_IRQHandler(RNG_HandleTypeDef *hrng)
  601. {
  602. uint32_t rngclockerror = 0U;
  603. /* RNG clock error interrupt occurred */
  604. if (__HAL_RNG_GET_IT(hrng, RNG_IT_CEI) != RESET)
  605. {
  606. /* Update the error code */
  607. hrng->ErrorCode = HAL_RNG_ERROR_CLOCK;
  608. rngclockerror = 1U;
  609. }
  610. else if (__HAL_RNG_GET_IT(hrng, RNG_IT_SEI) != RESET)
  611. {
  612. /* Update the error code */
  613. hrng->ErrorCode = HAL_RNG_ERROR_SEED;
  614. rngclockerror = 1U;
  615. }
  616. else
  617. {
  618. /* Nothing to do */
  619. }
  620. if (rngclockerror == 1U)
  621. {
  622. /* Change RNG peripheral state */
  623. hrng->State = HAL_RNG_STATE_ERROR;
  624. #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
  625. /* Call registered Error callback */
  626. hrng->ErrorCallback(hrng);
  627. #else
  628. /* Call legacy weak Error callback */
  629. HAL_RNG_ErrorCallback(hrng);
  630. #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
  631. /* Clear the clock error flag */
  632. __HAL_RNG_CLEAR_IT(hrng, RNG_IT_CEI | RNG_IT_SEI);
  633. return;
  634. }
  635. /* Check RNG data ready interrupt occurred */
  636. if (__HAL_RNG_GET_IT(hrng, RNG_IT_DRDY) != RESET)
  637. {
  638. /* Generate random number once, so disable the IT */
  639. __HAL_RNG_DISABLE_IT(hrng);
  640. /* Get the 32bit Random number (DRDY flag automatically cleared) */
  641. hrng->RandomNumber = hrng->Instance->DR;
  642. if (hrng->State != HAL_RNG_STATE_ERROR)
  643. {
  644. /* Change RNG peripheral state */
  645. hrng->State = HAL_RNG_STATE_READY;
  646. /* Process Unlocked */
  647. __HAL_UNLOCK(hrng);
  648. #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
  649. /* Call registered Data Ready callback */
  650. hrng->ReadyDataCallback(hrng, hrng->RandomNumber);
  651. #else
  652. /* Call legacy weak Data Ready callback */
  653. HAL_RNG_ReadyDataCallback(hrng, hrng->RandomNumber);
  654. #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
  655. }
  656. }
  657. }
  658. /**
  659. * @brief Read latest generated random number.
  660. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  661. * the configuration information for RNG.
  662. * @retval random value
  663. */
  664. uint32_t HAL_RNG_ReadLastRandomNumber(RNG_HandleTypeDef *hrng)
  665. {
  666. return (hrng->RandomNumber);
  667. }
  668. /**
  669. * @brief Data Ready callback in non-blocking mode.
  670. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  671. * the configuration information for RNG.
  672. * @param random32bit generated random number.
  673. * @retval None
  674. */
  675. __weak void HAL_RNG_ReadyDataCallback(RNG_HandleTypeDef *hrng, uint32_t random32bit)
  676. {
  677. /* Prevent unused argument(s) compilation warning */
  678. UNUSED(hrng);
  679. UNUSED(random32bit);
  680. /* NOTE : This function should not be modified. When the callback is needed,
  681. function HAL_RNG_ReadyDataCallback must be implemented in the user file.
  682. */
  683. }
  684. /**
  685. * @brief RNG error callbacks.
  686. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  687. * the configuration information for RNG.
  688. * @retval None
  689. */
  690. __weak void HAL_RNG_ErrorCallback(RNG_HandleTypeDef *hrng)
  691. {
  692. /* Prevent unused argument(s) compilation warning */
  693. UNUSED(hrng);
  694. /* NOTE : This function should not be modified. When the callback is needed,
  695. function HAL_RNG_ErrorCallback must be implemented in the user file.
  696. */
  697. }
  698. /**
  699. * @}
  700. */
  701. /** @addtogroup RNG_Exported_Functions_Group3
  702. * @brief Peripheral State functions
  703. *
  704. @verbatim
  705. ===============================================================================
  706. ##### Peripheral State functions #####
  707. ===============================================================================
  708. [..]
  709. This subsection permits to get in run-time the status of the peripheral
  710. and the data flow.
  711. @endverbatim
  712. * @{
  713. */
  714. /**
  715. * @brief Returns the RNG state.
  716. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  717. * the configuration information for RNG.
  718. * @retval HAL state
  719. */
  720. HAL_RNG_StateTypeDef HAL_RNG_GetState(RNG_HandleTypeDef *hrng)
  721. {
  722. return hrng->State;
  723. }
  724. /**
  725. * @brief Return the RNG handle error code.
  726. * @param hrng: pointer to a RNG_HandleTypeDef structure.
  727. * @retval RNG Error Code
  728. */
  729. uint32_t HAL_RNG_GetError(RNG_HandleTypeDef *hrng)
  730. {
  731. /* Return RNG Error Code */
  732. return hrng->ErrorCode;
  733. }
  734. /**
  735. * @}
  736. */
  737. /**
  738. * @}
  739. */
  740. #endif /* HAL_RNG_MODULE_ENABLED */
  741. /**
  742. * @}
  743. */
  744. #endif /* RNG */
  745. /**
  746. * @}
  747. */