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.
 
 
 

855 lines
25 KiB

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