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.

860 lines
25 KiB

12 months ago
  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 (overridden) 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 (overridden) functions:
  63. example HAL_RNG_ErrorCallback().
  64. Exception done for MspInit and MspDeInit functions that are respectively
  65. reset to the legacy weak (overridden) 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 (overridden) 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. if (HAL_RNG_STATE_READY == hrng->State)
  267. {
  268. switch (CallbackID)
  269. {
  270. case HAL_RNG_ERROR_CB_ID :
  271. hrng->ErrorCallback = pCallback;
  272. break;
  273. case HAL_RNG_MSPINIT_CB_ID :
  274. hrng->MspInitCallback = pCallback;
  275. break;
  276. case HAL_RNG_MSPDEINIT_CB_ID :
  277. hrng->MspDeInitCallback = pCallback;
  278. break;
  279. default :
  280. /* Update the error code */
  281. hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
  282. /* Return error status */
  283. status = HAL_ERROR;
  284. break;
  285. }
  286. }
  287. else if (HAL_RNG_STATE_RESET == hrng->State)
  288. {
  289. switch (CallbackID)
  290. {
  291. case HAL_RNG_MSPINIT_CB_ID :
  292. hrng->MspInitCallback = pCallback;
  293. break;
  294. case HAL_RNG_MSPDEINIT_CB_ID :
  295. hrng->MspDeInitCallback = pCallback;
  296. break;
  297. default :
  298. /* Update the error code */
  299. hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
  300. /* Return error status */
  301. status = HAL_ERROR;
  302. break;
  303. }
  304. }
  305. else
  306. {
  307. /* Update the error code */
  308. hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
  309. /* Return error status */
  310. status = HAL_ERROR;
  311. }
  312. return status;
  313. }
  314. /**
  315. * @brief Unregister an RNG Callback
  316. * RNG callback is redirected to the weak predefined callback
  317. * @param hrng RNG handle
  318. * @param CallbackID ID of the callback to be unregistered
  319. * This parameter can be one of the following values:
  320. * @arg @ref HAL_RNG_ERROR_CB_ID Error callback ID
  321. * @arg @ref HAL_RNG_MSPINIT_CB_ID MspInit callback ID
  322. * @arg @ref HAL_RNG_MSPDEINIT_CB_ID MspDeInit callback ID
  323. * @retval HAL status
  324. */
  325. HAL_StatusTypeDef HAL_RNG_UnRegisterCallback(RNG_HandleTypeDef *hrng, HAL_RNG_CallbackIDTypeDef CallbackID)
  326. {
  327. HAL_StatusTypeDef status = HAL_OK;
  328. if (HAL_RNG_STATE_READY == hrng->State)
  329. {
  330. switch (CallbackID)
  331. {
  332. case HAL_RNG_ERROR_CB_ID :
  333. hrng->ErrorCallback = HAL_RNG_ErrorCallback; /* Legacy weak ErrorCallback */
  334. break;
  335. case HAL_RNG_MSPINIT_CB_ID :
  336. hrng->MspInitCallback = HAL_RNG_MspInit; /* Legacy weak MspInit */
  337. break;
  338. case HAL_RNG_MSPDEINIT_CB_ID :
  339. hrng->MspDeInitCallback = HAL_RNG_MspDeInit; /* Legacy weak MspDeInit */
  340. break;
  341. default :
  342. /* Update the error code */
  343. hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
  344. /* Return error status */
  345. status = HAL_ERROR;
  346. break;
  347. }
  348. }
  349. else if (HAL_RNG_STATE_RESET == hrng->State)
  350. {
  351. switch (CallbackID)
  352. {
  353. case HAL_RNG_MSPINIT_CB_ID :
  354. hrng->MspInitCallback = HAL_RNG_MspInit; /* Legacy weak MspInit */
  355. break;
  356. case HAL_RNG_MSPDEINIT_CB_ID :
  357. hrng->MspDeInitCallback = HAL_RNG_MspDeInit; /* Legacy weak MspInit */
  358. break;
  359. default :
  360. /* Update the error code */
  361. hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
  362. /* Return error status */
  363. status = HAL_ERROR;
  364. break;
  365. }
  366. }
  367. else
  368. {
  369. /* Update the error code */
  370. hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
  371. /* Return error status */
  372. status = HAL_ERROR;
  373. }
  374. return status;
  375. }
  376. /**
  377. * @brief Register Data Ready RNG Callback
  378. * To be used instead of the weak HAL_RNG_ReadyDataCallback() predefined callback
  379. * @param hrng RNG handle
  380. * @param pCallback pointer to the Data Ready Callback function
  381. * @retval HAL status
  382. */
  383. HAL_StatusTypeDef HAL_RNG_RegisterReadyDataCallback(RNG_HandleTypeDef *hrng, pRNG_ReadyDataCallbackTypeDef pCallback)
  384. {
  385. HAL_StatusTypeDef status = HAL_OK;
  386. if (pCallback == NULL)
  387. {
  388. /* Update the error code */
  389. hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
  390. return HAL_ERROR;
  391. }
  392. /* Process locked */
  393. __HAL_LOCK(hrng);
  394. if (HAL_RNG_STATE_READY == hrng->State)
  395. {
  396. hrng->ReadyDataCallback = pCallback;
  397. }
  398. else
  399. {
  400. /* Update the error code */
  401. hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
  402. /* Return error status */
  403. status = HAL_ERROR;
  404. }
  405. /* Release Lock */
  406. __HAL_UNLOCK(hrng);
  407. return status;
  408. }
  409. /**
  410. * @brief UnRegister the Data Ready RNG Callback
  411. * Data Ready RNG Callback is redirected to the weak HAL_RNG_ReadyDataCallback() predefined callback
  412. * @param hrng RNG handle
  413. * @retval HAL status
  414. */
  415. HAL_StatusTypeDef HAL_RNG_UnRegisterReadyDataCallback(RNG_HandleTypeDef *hrng)
  416. {
  417. HAL_StatusTypeDef status = HAL_OK;
  418. /* Process locked */
  419. __HAL_LOCK(hrng);
  420. if (HAL_RNG_STATE_READY == hrng->State)
  421. {
  422. hrng->ReadyDataCallback = HAL_RNG_ReadyDataCallback; /* Legacy weak ReadyDataCallback */
  423. }
  424. else
  425. {
  426. /* Update the error code */
  427. hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
  428. /* Return error status */
  429. status = HAL_ERROR;
  430. }
  431. /* Release Lock */
  432. __HAL_UNLOCK(hrng);
  433. return status;
  434. }
  435. #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
  436. /**
  437. * @}
  438. */
  439. /** @addtogroup RNG_Exported_Functions_Group2
  440. * @brief Peripheral Control functions
  441. *
  442. @verbatim
  443. ===============================================================================
  444. ##### Peripheral Control functions #####
  445. ===============================================================================
  446. [..] This section provides functions allowing to:
  447. (+) Get the 32 bit Random number
  448. (+) Get the 32 bit Random number with interrupt enabled
  449. (+) Handle RNG interrupt request
  450. @endverbatim
  451. * @{
  452. */
  453. /**
  454. * @brief Generates a 32-bit random number.
  455. * @note Each time the random number data is read the RNG_FLAG_DRDY flag
  456. * is automatically cleared.
  457. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  458. * the configuration information for RNG.
  459. * @param random32bit pointer to generated random number variable if successful.
  460. * @retval HAL status
  461. */
  462. HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber(RNG_HandleTypeDef *hrng, uint32_t *random32bit)
  463. {
  464. uint32_t tickstart;
  465. HAL_StatusTypeDef status = HAL_OK;
  466. /* Process Locked */
  467. __HAL_LOCK(hrng);
  468. /* Check RNG peripheral state */
  469. if (hrng->State == HAL_RNG_STATE_READY)
  470. {
  471. /* Change RNG peripheral state */
  472. hrng->State = HAL_RNG_STATE_BUSY;
  473. /* Get tick */
  474. tickstart = HAL_GetTick();
  475. /* Check if data register contains valid random data */
  476. while (__HAL_RNG_GET_FLAG(hrng, RNG_FLAG_DRDY) == RESET)
  477. {
  478. if ((HAL_GetTick() - tickstart) > RNG_TIMEOUT_VALUE)
  479. {
  480. /* New check to avoid false timeout detection in case of preemption */
  481. if (__HAL_RNG_GET_FLAG(hrng, RNG_FLAG_DRDY) == RESET)
  482. {
  483. hrng->State = HAL_RNG_STATE_READY;
  484. hrng->ErrorCode = HAL_RNG_ERROR_TIMEOUT;
  485. /* Process Unlocked */
  486. __HAL_UNLOCK(hrng);
  487. return HAL_ERROR;
  488. }
  489. }
  490. }
  491. /* Get a 32bit Random number */
  492. hrng->RandomNumber = hrng->Instance->DR;
  493. *random32bit = hrng->RandomNumber;
  494. hrng->State = HAL_RNG_STATE_READY;
  495. }
  496. else
  497. {
  498. hrng->ErrorCode = HAL_RNG_ERROR_BUSY;
  499. status = HAL_ERROR;
  500. }
  501. /* Process Unlocked */
  502. __HAL_UNLOCK(hrng);
  503. return status;
  504. }
  505. /**
  506. * @brief Generates a 32-bit random number in interrupt mode.
  507. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  508. * the configuration information for RNG.
  509. * @retval HAL status
  510. */
  511. HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber_IT(RNG_HandleTypeDef *hrng)
  512. {
  513. HAL_StatusTypeDef status = HAL_OK;
  514. /* Process Locked */
  515. __HAL_LOCK(hrng);
  516. /* Check RNG peripheral state */
  517. if (hrng->State == HAL_RNG_STATE_READY)
  518. {
  519. /* Change RNG peripheral state */
  520. hrng->State = HAL_RNG_STATE_BUSY;
  521. /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */
  522. __HAL_RNG_ENABLE_IT(hrng);
  523. }
  524. else
  525. {
  526. /* Process Unlocked */
  527. __HAL_UNLOCK(hrng);
  528. hrng->ErrorCode = HAL_RNG_ERROR_BUSY;
  529. status = HAL_ERROR;
  530. }
  531. return status;
  532. }
  533. /**
  534. * @brief Returns generated random number in polling mode (Obsolete)
  535. * Use HAL_RNG_GenerateRandomNumber() API instead.
  536. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  537. * the configuration information for RNG.
  538. * @retval Random value
  539. */
  540. uint32_t HAL_RNG_GetRandomNumber(RNG_HandleTypeDef *hrng)
  541. {
  542. if (HAL_RNG_GenerateRandomNumber(hrng, &(hrng->RandomNumber)) == HAL_OK)
  543. {
  544. return hrng->RandomNumber;
  545. }
  546. else
  547. {
  548. return 0U;
  549. }
  550. }
  551. /**
  552. * @brief Returns a 32-bit random number with interrupt enabled (Obsolete),
  553. * Use HAL_RNG_GenerateRandomNumber_IT() API instead.
  554. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  555. * the configuration information for RNG.
  556. * @retval 32-bit random number
  557. */
  558. uint32_t HAL_RNG_GetRandomNumber_IT(RNG_HandleTypeDef *hrng)
  559. {
  560. uint32_t random32bit = 0U;
  561. /* Process locked */
  562. __HAL_LOCK(hrng);
  563. /* Change RNG peripheral state */
  564. hrng->State = HAL_RNG_STATE_BUSY;
  565. /* Get a 32bit Random number */
  566. random32bit = hrng->Instance->DR;
  567. /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */
  568. __HAL_RNG_ENABLE_IT(hrng);
  569. /* Return the 32 bit random number */
  570. return random32bit;
  571. }
  572. /**
  573. * @brief Handles RNG interrupt request.
  574. * @note In the case of a clock error, the RNG is no more able to generate
  575. * random numbers because the PLL48CLK clock is not correct. User has
  576. * to check that the clock controller is correctly configured to provide
  577. * the RNG clock and clear the CEIS bit using __HAL_RNG_CLEAR_IT().
  578. * The clock error has no impact on the previously generated
  579. * random numbers, and the RNG_DR register contents can be used.
  580. * @note In the case of a seed error, the generation of random numbers is
  581. * interrupted as long as the SECS bit is '1'. If a number is
  582. * available in the RNG_DR register, it must not be used because it may
  583. * not have enough entropy. In this case, it is recommended to clear the
  584. * SEIS bit using __HAL_RNG_CLEAR_IT(), then disable and enable
  585. * the RNG peripheral to reinitialize and restart the RNG.
  586. * @note User-written HAL_RNG_ErrorCallback() API is called once whether SEIS
  587. * or CEIS are set.
  588. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  589. * the configuration information for RNG.
  590. * @retval None
  591. */
  592. void HAL_RNG_IRQHandler(RNG_HandleTypeDef *hrng)
  593. {
  594. uint32_t rngclockerror = 0U;
  595. uint32_t itflag = hrng->Instance->SR;
  596. /* RNG clock error interrupt occurred */
  597. if ((itflag & RNG_IT_CEI) == RNG_IT_CEI)
  598. {
  599. /* Update the error code */
  600. hrng->ErrorCode = HAL_RNG_ERROR_CLOCK;
  601. rngclockerror = 1U;
  602. }
  603. else if ((itflag & RNG_IT_SEI) == RNG_IT_SEI)
  604. {
  605. /* Update the error code */
  606. hrng->ErrorCode = HAL_RNG_ERROR_SEED;
  607. rngclockerror = 1U;
  608. }
  609. else
  610. {
  611. /* Nothing to do */
  612. }
  613. if (rngclockerror == 1U)
  614. {
  615. /* Change RNG peripheral state */
  616. hrng->State = HAL_RNG_STATE_ERROR;
  617. #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
  618. /* Call registered Error callback */
  619. hrng->ErrorCallback(hrng);
  620. #else
  621. /* Call legacy weak Error callback */
  622. HAL_RNG_ErrorCallback(hrng);
  623. #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
  624. /* Clear the clock error flag */
  625. __HAL_RNG_CLEAR_IT(hrng, RNG_IT_CEI | RNG_IT_SEI);
  626. return;
  627. }
  628. /* Check RNG data ready interrupt occurred */
  629. if ((itflag & RNG_IT_DRDY) == RNG_IT_DRDY)
  630. {
  631. /* Generate random number once, so disable the IT */
  632. __HAL_RNG_DISABLE_IT(hrng);
  633. /* Get the 32bit Random number (DRDY flag automatically cleared) */
  634. hrng->RandomNumber = hrng->Instance->DR;
  635. if (hrng->State != HAL_RNG_STATE_ERROR)
  636. {
  637. /* Change RNG peripheral state */
  638. hrng->State = HAL_RNG_STATE_READY;
  639. /* Process Unlocked */
  640. __HAL_UNLOCK(hrng);
  641. #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
  642. /* Call registered Data Ready callback */
  643. hrng->ReadyDataCallback(hrng, hrng->RandomNumber);
  644. #else
  645. /* Call legacy weak Data Ready callback */
  646. HAL_RNG_ReadyDataCallback(hrng, hrng->RandomNumber);
  647. #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
  648. }
  649. }
  650. }
  651. /**
  652. * @brief Read latest generated random number.
  653. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  654. * the configuration information for RNG.
  655. * @retval random value
  656. */
  657. uint32_t HAL_RNG_ReadLastRandomNumber(const RNG_HandleTypeDef *hrng)
  658. {
  659. return (hrng->RandomNumber);
  660. }
  661. /**
  662. * @brief Data Ready callback in non-blocking mode.
  663. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  664. * the configuration information for RNG.
  665. * @param random32bit generated random number.
  666. * @retval None
  667. */
  668. __weak void HAL_RNG_ReadyDataCallback(RNG_HandleTypeDef *hrng, uint32_t random32bit)
  669. {
  670. /* Prevent unused argument(s) compilation warning */
  671. UNUSED(hrng);
  672. UNUSED(random32bit);
  673. /* NOTE : This function should not be modified. When the callback is needed,
  674. function HAL_RNG_ReadyDataCallback must be implemented in the user file.
  675. */
  676. }
  677. /**
  678. * @brief RNG error callbacks.
  679. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  680. * the configuration information for RNG.
  681. * @retval None
  682. */
  683. __weak void HAL_RNG_ErrorCallback(RNG_HandleTypeDef *hrng)
  684. {
  685. /* Prevent unused argument(s) compilation warning */
  686. UNUSED(hrng);
  687. /* NOTE : This function should not be modified. When the callback is needed,
  688. function HAL_RNG_ErrorCallback must be implemented in the user file.
  689. */
  690. }
  691. /**
  692. * @}
  693. */
  694. /** @addtogroup RNG_Exported_Functions_Group3
  695. * @brief Peripheral State functions
  696. *
  697. @verbatim
  698. ===============================================================================
  699. ##### Peripheral State functions #####
  700. ===============================================================================
  701. [..]
  702. This subsection permits to get in run-time the status of the peripheral
  703. and the data flow.
  704. @endverbatim
  705. * @{
  706. */
  707. /**
  708. * @brief Returns the RNG state.
  709. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  710. * the configuration information for RNG.
  711. * @retval HAL state
  712. */
  713. HAL_RNG_StateTypeDef HAL_RNG_GetState(const RNG_HandleTypeDef *hrng)
  714. {
  715. return hrng->State;
  716. }
  717. /**
  718. * @brief Return the RNG handle error code.
  719. * @param hrng: pointer to a RNG_HandleTypeDef structure.
  720. * @retval RNG Error Code
  721. */
  722. uint32_t HAL_RNG_GetError(const RNG_HandleTypeDef *hrng)
  723. {
  724. /* Return RNG Error Code */
  725. return hrng->ErrorCode;
  726. }
  727. /**
  728. * @}
  729. */
  730. /**
  731. * @}
  732. */
  733. #endif /* HAL_RNG_MODULE_ENABLED */
  734. /**
  735. * @}
  736. */
  737. #endif /* RNG */
  738. /**
  739. * @}
  740. */