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.
 
 
 

834 lines
25 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32wbxx_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) 2019 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 "stm32wbxx_hal.h"
  86. /** @addtogroup STM32WBxx_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. assert_param(IS_RNG_CED(hrng->Init.ClockErrorDetection));
  145. #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
  146. if (hrng->State == HAL_RNG_STATE_RESET)
  147. {
  148. /* Allocate lock resource and initialize it */
  149. hrng->Lock = HAL_UNLOCKED;
  150. hrng->ReadyDataCallback = HAL_RNG_ReadyDataCallback; /* Legacy weak ReadyDataCallback */
  151. hrng->ErrorCallback = HAL_RNG_ErrorCallback; /* Legacy weak ErrorCallback */
  152. if (hrng->MspInitCallback == NULL)
  153. {
  154. hrng->MspInitCallback = HAL_RNG_MspInit; /* Legacy weak MspInit */
  155. }
  156. /* Init the low level hardware */
  157. hrng->MspInitCallback(hrng);
  158. }
  159. #else
  160. if (hrng->State == HAL_RNG_STATE_RESET)
  161. {
  162. /* Allocate lock resource and initialize it */
  163. hrng->Lock = HAL_UNLOCKED;
  164. /* Init the low level hardware */
  165. HAL_RNG_MspInit(hrng);
  166. }
  167. #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
  168. /* Change RNG peripheral state */
  169. hrng->State = HAL_RNG_STATE_BUSY;
  170. /* Clock Error Detection Configuration */
  171. MODIFY_REG(hrng->Instance->CR, RNG_CR_CED, hrng->Init.ClockErrorDetection);
  172. /* Enable the RNG Peripheral */
  173. __HAL_RNG_ENABLE(hrng);
  174. /* Initialize the RNG state */
  175. hrng->State = HAL_RNG_STATE_READY;
  176. /* Initialise the error code */
  177. hrng->ErrorCode = HAL_RNG_ERROR_NONE;
  178. /* Return function status */
  179. return HAL_OK;
  180. }
  181. /**
  182. * @brief DeInitializes the RNG peripheral.
  183. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  184. * the configuration information for RNG.
  185. * @retval HAL status
  186. */
  187. HAL_StatusTypeDef HAL_RNG_DeInit(RNG_HandleTypeDef *hrng)
  188. {
  189. /* Check the RNG handle allocation */
  190. if (hrng == NULL)
  191. {
  192. return HAL_ERROR;
  193. }
  194. /* Clear Clock Error Detection bit */
  195. CLEAR_BIT(hrng->Instance->CR, RNG_CR_CED);
  196. /* Disable the RNG Peripheral */
  197. CLEAR_BIT(hrng->Instance->CR, RNG_CR_IE | RNG_CR_RNGEN);
  198. /* Clear RNG interrupt status flags */
  199. CLEAR_BIT(hrng->Instance->SR, RNG_SR_CEIS | RNG_SR_SEIS);
  200. #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
  201. if (hrng->MspDeInitCallback == NULL)
  202. {
  203. hrng->MspDeInitCallback = HAL_RNG_MspDeInit; /* Legacy weak MspDeInit */
  204. }
  205. /* DeInit the low level hardware */
  206. hrng->MspDeInitCallback(hrng);
  207. #else
  208. /* DeInit the low level hardware */
  209. HAL_RNG_MspDeInit(hrng);
  210. #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
  211. /* Update the RNG state */
  212. hrng->State = HAL_RNG_STATE_RESET;
  213. /* Initialise the error code */
  214. hrng->ErrorCode = HAL_RNG_ERROR_NONE;
  215. /* Release Lock */
  216. __HAL_UNLOCK(hrng);
  217. /* Return the function status */
  218. return HAL_OK;
  219. }
  220. /**
  221. * @brief Initializes the RNG MSP.
  222. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  223. * the configuration information for RNG.
  224. * @retval None
  225. */
  226. __weak void HAL_RNG_MspInit(RNG_HandleTypeDef *hrng)
  227. {
  228. /* Prevent unused argument(s) compilation warning */
  229. UNUSED(hrng);
  230. /* NOTE : This function should not be modified. When the callback is needed,
  231. function HAL_RNG_MspInit must be implemented in the user file.
  232. */
  233. }
  234. /**
  235. * @brief DeInitializes the RNG MSP.
  236. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  237. * the configuration information for RNG.
  238. * @retval None
  239. */
  240. __weak void HAL_RNG_MspDeInit(RNG_HandleTypeDef *hrng)
  241. {
  242. /* Prevent unused argument(s) compilation warning */
  243. UNUSED(hrng);
  244. /* NOTE : This function should not be modified. When the callback is needed,
  245. function HAL_RNG_MspDeInit must be implemented in the user file.
  246. */
  247. }
  248. #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
  249. /**
  250. * @brief Register a User RNG Callback
  251. * To be used instead of the weak predefined callback
  252. * @param hrng RNG handle
  253. * @param CallbackID ID of the callback to be registered
  254. * This parameter can be one of the following values:
  255. * @arg @ref HAL_RNG_ERROR_CB_ID Error callback ID
  256. * @arg @ref HAL_RNG_MSPINIT_CB_ID MspInit callback ID
  257. * @arg @ref HAL_RNG_MSPDEINIT_CB_ID MspDeInit callback ID
  258. * @param pCallback pointer to the Callback function
  259. * @retval HAL status
  260. */
  261. HAL_StatusTypeDef HAL_RNG_RegisterCallback(RNG_HandleTypeDef *hrng, HAL_RNG_CallbackIDTypeDef CallbackID, pRNG_CallbackTypeDef pCallback)
  262. {
  263. HAL_StatusTypeDef status = HAL_OK;
  264. if (pCallback == NULL)
  265. {
  266. /* Update the error code */
  267. hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
  268. return HAL_ERROR;
  269. }
  270. /* Process locked */
  271. __HAL_LOCK(hrng);
  272. if (HAL_RNG_STATE_READY == hrng->State)
  273. {
  274. switch (CallbackID)
  275. {
  276. case HAL_RNG_ERROR_CB_ID :
  277. hrng->ErrorCallback = pCallback;
  278. break;
  279. case HAL_RNG_MSPINIT_CB_ID :
  280. hrng->MspInitCallback = pCallback;
  281. break;
  282. case HAL_RNG_MSPDEINIT_CB_ID :
  283. hrng->MspDeInitCallback = pCallback;
  284. break;
  285. default :
  286. /* Update the error code */
  287. hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
  288. /* Return error status */
  289. status = HAL_ERROR;
  290. break;
  291. }
  292. }
  293. else if (HAL_RNG_STATE_RESET == hrng->State)
  294. {
  295. switch (CallbackID)
  296. {
  297. case HAL_RNG_MSPINIT_CB_ID :
  298. hrng->MspInitCallback = pCallback;
  299. break;
  300. case HAL_RNG_MSPDEINIT_CB_ID :
  301. hrng->MspDeInitCallback = pCallback;
  302. break;
  303. default :
  304. /* Update the error code */
  305. hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
  306. /* Return error status */
  307. status = HAL_ERROR;
  308. break;
  309. }
  310. }
  311. else
  312. {
  313. /* Update the error code */
  314. hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
  315. /* Return error status */
  316. status = HAL_ERROR;
  317. }
  318. /* Release Lock */
  319. __HAL_UNLOCK(hrng);
  320. return status;
  321. }
  322. /**
  323. * @brief Unregister an RNG Callback
  324. * RNG callabck is redirected to the weak predefined callback
  325. * @param hrng RNG handle
  326. * @param CallbackID ID of the callback to be unregistered
  327. * This parameter can be one of the following values:
  328. * @arg @ref HAL_RNG_ERROR_CB_ID Error callback ID
  329. * @arg @ref HAL_RNG_MSPINIT_CB_ID MspInit callback ID
  330. * @arg @ref HAL_RNG_MSPDEINIT_CB_ID MspDeInit callback ID
  331. * @retval HAL status
  332. */
  333. HAL_StatusTypeDef HAL_RNG_UnRegisterCallback(RNG_HandleTypeDef *hrng, HAL_RNG_CallbackIDTypeDef CallbackID)
  334. {
  335. HAL_StatusTypeDef status = HAL_OK;
  336. /* Process locked */
  337. __HAL_LOCK(hrng);
  338. if (HAL_RNG_STATE_READY == hrng->State)
  339. {
  340. switch (CallbackID)
  341. {
  342. case HAL_RNG_ERROR_CB_ID :
  343. hrng->ErrorCallback = HAL_RNG_ErrorCallback; /* Legacy weak ErrorCallback */
  344. break;
  345. case HAL_RNG_MSPINIT_CB_ID :
  346. hrng->MspInitCallback = HAL_RNG_MspInit; /* Legacy weak MspInit */
  347. break;
  348. case HAL_RNG_MSPDEINIT_CB_ID :
  349. hrng->MspDeInitCallback = HAL_RNG_MspDeInit; /* Legacy weak MspDeInit */
  350. break;
  351. default :
  352. /* Update the error code */
  353. hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
  354. /* Return error status */
  355. status = HAL_ERROR;
  356. break;
  357. }
  358. }
  359. else if (HAL_RNG_STATE_RESET == hrng->State)
  360. {
  361. switch (CallbackID)
  362. {
  363. case HAL_RNG_MSPINIT_CB_ID :
  364. hrng->MspInitCallback = HAL_RNG_MspInit; /* Legacy weak MspInit */
  365. break;
  366. case HAL_RNG_MSPDEINIT_CB_ID :
  367. hrng->MspDeInitCallback = HAL_RNG_MspDeInit; /* Legacy weak MspInit */
  368. break;
  369. default :
  370. /* Update the error code */
  371. hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
  372. /* Return error status */
  373. status = HAL_ERROR;
  374. break;
  375. }
  376. }
  377. else
  378. {
  379. /* Update the error code */
  380. hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
  381. /* Return error status */
  382. status = HAL_ERROR;
  383. }
  384. /* Release Lock */
  385. __HAL_UNLOCK(hrng);
  386. return status;
  387. }
  388. /**
  389. * @brief Register Data Ready RNG Callback
  390. * To be used instead of the weak HAL_RNG_ReadyDataCallback() predefined callback
  391. * @param hrng RNG handle
  392. * @param pCallback pointer to the Data Ready Callback function
  393. * @retval HAL status
  394. */
  395. HAL_StatusTypeDef HAL_RNG_RegisterReadyDataCallback(RNG_HandleTypeDef *hrng, pRNG_ReadyDataCallbackTypeDef pCallback)
  396. {
  397. HAL_StatusTypeDef status = HAL_OK;
  398. if (pCallback == NULL)
  399. {
  400. /* Update the error code */
  401. hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
  402. return HAL_ERROR;
  403. }
  404. /* Process locked */
  405. __HAL_LOCK(hrng);
  406. if (HAL_RNG_STATE_READY == hrng->State)
  407. {
  408. hrng->ReadyDataCallback = pCallback;
  409. }
  410. else
  411. {
  412. /* Update the error code */
  413. hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
  414. /* Return error status */
  415. status = HAL_ERROR;
  416. }
  417. /* Release Lock */
  418. __HAL_UNLOCK(hrng);
  419. return status;
  420. }
  421. /**
  422. * @brief UnRegister the Data Ready RNG Callback
  423. * Data Ready RNG Callback is redirected to the weak HAL_RNG_ReadyDataCallback() predefined callback
  424. * @param hrng RNG handle
  425. * @retval HAL status
  426. */
  427. HAL_StatusTypeDef HAL_RNG_UnRegisterReadyDataCallback(RNG_HandleTypeDef *hrng)
  428. {
  429. HAL_StatusTypeDef status = HAL_OK;
  430. /* Process locked */
  431. __HAL_LOCK(hrng);
  432. if (HAL_RNG_STATE_READY == hrng->State)
  433. {
  434. hrng->ReadyDataCallback = HAL_RNG_ReadyDataCallback; /* Legacy weak ReadyDataCallback */
  435. }
  436. else
  437. {
  438. /* Update the error code */
  439. hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
  440. /* Return error status */
  441. status = HAL_ERROR;
  442. }
  443. /* Release Lock */
  444. __HAL_UNLOCK(hrng);
  445. return status;
  446. }
  447. #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
  448. /**
  449. * @}
  450. */
  451. /** @addtogroup RNG_Exported_Functions_Group2
  452. * @brief Peripheral Control functions
  453. *
  454. @verbatim
  455. ===============================================================================
  456. ##### Peripheral Control functions #####
  457. ===============================================================================
  458. [..] This section provides functions allowing to:
  459. (+) Get the 32 bit Random number
  460. (+) Get the 32 bit Random number with interrupt enabled
  461. (+) Handle RNG interrupt request
  462. @endverbatim
  463. * @{
  464. */
  465. /**
  466. * @brief Generates a 32-bit random number.
  467. * @note This function checks value of RNG_FLAG_DRDY flag to know if valid
  468. * random number is available in the DR register (RNG_FLAG_DRDY flag set
  469. * whenever a random number is available through the RNG_DR register).
  470. * After transitioning from 0 to 1 (random number available),
  471. * RNG_FLAG_DRDY flag remains high until output buffer becomes empty after reading
  472. * four words from the RNG_DR register, i.e. further function calls
  473. * will immediately return a new u32 random number (additional words are
  474. * available and can be read by the application, till RNG_FLAG_DRDY flag remains high).
  475. * @note When no more random number data is available in DR register, RNG_FLAG_DRDY
  476. * flag is automatically cleared.
  477. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  478. * the configuration information for RNG.
  479. * @param random32bit pointer to generated random number variable if successful.
  480. * @retval HAL status
  481. */
  482. HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber(RNG_HandleTypeDef *hrng, uint32_t *random32bit)
  483. {
  484. uint32_t tickstart;
  485. HAL_StatusTypeDef status = HAL_OK;
  486. /* Process Locked */
  487. __HAL_LOCK(hrng);
  488. /* Check RNG peripheral state */
  489. if (hrng->State == HAL_RNG_STATE_READY)
  490. {
  491. /* Change RNG peripheral state */
  492. hrng->State = HAL_RNG_STATE_BUSY;
  493. /* Get tick */
  494. tickstart = HAL_GetTick();
  495. /* Check if data register contains valid random data */
  496. while (__HAL_RNG_GET_FLAG(hrng, RNG_FLAG_DRDY) == RESET)
  497. {
  498. if ((HAL_GetTick() - tickstart) > RNG_TIMEOUT_VALUE)
  499. {
  500. hrng->State = HAL_RNG_STATE_READY;
  501. hrng->ErrorCode = HAL_RNG_ERROR_TIMEOUT;
  502. /* Process Unlocked */
  503. __HAL_UNLOCK(hrng);
  504. return HAL_ERROR;
  505. }
  506. }
  507. /* Get a 32bit Random number */
  508. hrng->RandomNumber = hrng->Instance->DR;
  509. *random32bit = hrng->RandomNumber;
  510. hrng->State = HAL_RNG_STATE_READY;
  511. }
  512. else
  513. {
  514. hrng->ErrorCode = HAL_RNG_ERROR_BUSY;
  515. status = HAL_ERROR;
  516. }
  517. /* Process Unlocked */
  518. __HAL_UNLOCK(hrng);
  519. return status;
  520. }
  521. /**
  522. * @brief Generates a 32-bit random number in interrupt mode.
  523. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  524. * the configuration information for RNG.
  525. * @retval HAL status
  526. */
  527. HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber_IT(RNG_HandleTypeDef *hrng)
  528. {
  529. HAL_StatusTypeDef status = HAL_OK;
  530. /* Process Locked */
  531. __HAL_LOCK(hrng);
  532. /* Check RNG peripheral state */
  533. if (hrng->State == HAL_RNG_STATE_READY)
  534. {
  535. /* Change RNG peripheral state */
  536. hrng->State = HAL_RNG_STATE_BUSY;
  537. /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */
  538. __HAL_RNG_ENABLE_IT(hrng);
  539. }
  540. else
  541. {
  542. /* Process Unlocked */
  543. __HAL_UNLOCK(hrng);
  544. hrng->ErrorCode = HAL_RNG_ERROR_BUSY;
  545. status = HAL_ERROR;
  546. }
  547. return status;
  548. }
  549. /**
  550. * @brief Handles RNG interrupt request.
  551. * @note In the case of a clock error, the RNG is no more able to generate
  552. * random numbers because the PLL48CLK clock is not correct. User has
  553. * to check that the clock controller is correctly configured to provide
  554. * the RNG clock and clear the CEIS bit using __HAL_RNG_CLEAR_IT().
  555. * The clock error has no impact on the previously generated
  556. * random numbers, and the RNG_DR register contents can be used.
  557. * @note In the case of a seed error, the generation of random numbers is
  558. * interrupted as long as the SECS bit is '1'. If a number is
  559. * available in the RNG_DR register, it must not be used because it may
  560. * not have enough entropy. In this case, it is recommended to clear the
  561. * SEIS bit using __HAL_RNG_CLEAR_IT(), then disable and enable
  562. * the RNG peripheral to reinitialize and restart the RNG.
  563. * @note User-written HAL_RNG_ErrorCallback() API is called once whether SEIS
  564. * or CEIS are set.
  565. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  566. * the configuration information for RNG.
  567. * @retval None
  568. */
  569. void HAL_RNG_IRQHandler(RNG_HandleTypeDef *hrng)
  570. {
  571. uint32_t rngclockerror = 0U;
  572. /* RNG clock error interrupt occurred */
  573. if (__HAL_RNG_GET_IT(hrng, RNG_IT_CEI) != RESET)
  574. {
  575. /* Update the error code */
  576. hrng->ErrorCode = HAL_RNG_ERROR_CLOCK;
  577. rngclockerror = 1U;
  578. }
  579. else if (__HAL_RNG_GET_IT(hrng, RNG_IT_SEI) != RESET)
  580. {
  581. /* Update the error code */
  582. hrng->ErrorCode = HAL_RNG_ERROR_SEED;
  583. rngclockerror = 1U;
  584. }
  585. else
  586. {
  587. /* Nothing to do */
  588. }
  589. if (rngclockerror == 1U)
  590. {
  591. /* Change RNG peripheral state */
  592. hrng->State = HAL_RNG_STATE_ERROR;
  593. #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
  594. /* Call registered Error callback */
  595. hrng->ErrorCallback(hrng);
  596. #else
  597. /* Call legacy weak Error callback */
  598. HAL_RNG_ErrorCallback(hrng);
  599. #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
  600. /* Clear the clock error flag */
  601. __HAL_RNG_CLEAR_IT(hrng, RNG_IT_CEI | RNG_IT_SEI);
  602. }
  603. /* Check RNG data ready interrupt occurred */
  604. if (__HAL_RNG_GET_IT(hrng, RNG_IT_DRDY) != RESET)
  605. {
  606. /* Generate random number once, so disable the IT */
  607. __HAL_RNG_DISABLE_IT(hrng);
  608. /* Get the 32bit Random number (DRDY flag automatically cleared) */
  609. hrng->RandomNumber = hrng->Instance->DR;
  610. if (hrng->State != HAL_RNG_STATE_ERROR)
  611. {
  612. /* Change RNG peripheral state */
  613. hrng->State = HAL_RNG_STATE_READY;
  614. /* Process Unlocked */
  615. __HAL_UNLOCK(hrng);
  616. #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
  617. /* Call registered Data Ready callback */
  618. hrng->ReadyDataCallback(hrng, hrng->RandomNumber);
  619. #else
  620. /* Call legacy weak Data Ready callback */
  621. HAL_RNG_ReadyDataCallback(hrng, hrng->RandomNumber);
  622. #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
  623. }
  624. }
  625. }
  626. /**
  627. * @brief Read latest generated random number.
  628. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  629. * the configuration information for RNG.
  630. * @retval random value
  631. */
  632. uint32_t HAL_RNG_ReadLastRandomNumber(RNG_HandleTypeDef *hrng)
  633. {
  634. return (hrng->RandomNumber);
  635. }
  636. /**
  637. * @brief Data Ready callback in non-blocking mode.
  638. * @note When RNG_FLAG_DRDY flag value is set, first random number has been read
  639. * from DR register in IRQ Handler and is provided as callback parameter.
  640. * Depending on valid data available in the conditioning output buffer,
  641. * additional words can be read by the application from DR register till
  642. * DRDY bit remains high.
  643. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  644. * the configuration information for RNG.
  645. * @param random32bit generated random number.
  646. * @retval None
  647. */
  648. __weak void HAL_RNG_ReadyDataCallback(RNG_HandleTypeDef *hrng, uint32_t random32bit)
  649. {
  650. /* Prevent unused argument(s) compilation warning */
  651. UNUSED(hrng);
  652. UNUSED(random32bit);
  653. /* NOTE : This function should not be modified. When the callback is needed,
  654. function HAL_RNG_ReadyDataCallback must be implemented in the user file.
  655. */
  656. }
  657. /**
  658. * @brief RNG error callbacks.
  659. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  660. * the configuration information for RNG.
  661. * @retval None
  662. */
  663. __weak void HAL_RNG_ErrorCallback(RNG_HandleTypeDef *hrng)
  664. {
  665. /* Prevent unused argument(s) compilation warning */
  666. UNUSED(hrng);
  667. /* NOTE : This function should not be modified. When the callback is needed,
  668. function HAL_RNG_ErrorCallback must be implemented in the user file.
  669. */
  670. }
  671. /**
  672. * @}
  673. */
  674. /** @addtogroup RNG_Exported_Functions_Group3
  675. * @brief Peripheral State functions
  676. *
  677. @verbatim
  678. ===============================================================================
  679. ##### Peripheral State functions #####
  680. ===============================================================================
  681. [..]
  682. This subsection permits to get in run-time the status of the peripheral
  683. and the data flow.
  684. @endverbatim
  685. * @{
  686. */
  687. /**
  688. * @brief Returns the RNG state.
  689. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  690. * the configuration information for RNG.
  691. * @retval HAL state
  692. */
  693. HAL_RNG_StateTypeDef HAL_RNG_GetState(RNG_HandleTypeDef *hrng)
  694. {
  695. return hrng->State;
  696. }
  697. /**
  698. * @brief Return the RNG handle error code.
  699. * @param hrng: pointer to a RNG_HandleTypeDef structure.
  700. * @retval RNG Error Code
  701. */
  702. uint32_t HAL_RNG_GetError(RNG_HandleTypeDef *hrng)
  703. {
  704. /* Return RNG Error Code */
  705. return hrng->ErrorCode;
  706. }
  707. /**
  708. * @}
  709. */
  710. /**
  711. * @}
  712. */
  713. #endif /* HAL_RNG_MODULE_ENABLED */
  714. /**
  715. * @}
  716. */
  717. #endif /* RNG */
  718. /**
  719. * @}
  720. */
  721. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/