stm32f4xx_hal_rng.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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. @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) 2016 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 "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, 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. hrng->ErrorCode = HAL_RNG_ERROR_BUSY;
  502. status = HAL_ERROR;
  503. }
  504. /* Process Unlocked */
  505. __HAL_UNLOCK(hrng);
  506. return status;
  507. }
  508. /**
  509. * @brief Generates a 32-bit random number in interrupt mode.
  510. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  511. * the configuration information for RNG.
  512. * @retval HAL status
  513. */
  514. HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber_IT(RNG_HandleTypeDef *hrng)
  515. {
  516. HAL_StatusTypeDef status = HAL_OK;
  517. /* Process Locked */
  518. __HAL_LOCK(hrng);
  519. /* Check RNG peripheral state */
  520. if (hrng->State == HAL_RNG_STATE_READY)
  521. {
  522. /* Change RNG peripheral state */
  523. hrng->State = HAL_RNG_STATE_BUSY;
  524. /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */
  525. __HAL_RNG_ENABLE_IT(hrng);
  526. }
  527. else
  528. {
  529. /* Process Unlocked */
  530. __HAL_UNLOCK(hrng);
  531. hrng->ErrorCode = HAL_RNG_ERROR_BUSY;
  532. status = HAL_ERROR;
  533. }
  534. return status;
  535. }
  536. /**
  537. * @brief Returns generated random number in polling mode (Obsolete)
  538. * Use HAL_RNG_GenerateRandomNumber() API instead.
  539. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  540. * the configuration information for RNG.
  541. * @retval Random value
  542. */
  543. uint32_t HAL_RNG_GetRandomNumber(RNG_HandleTypeDef *hrng)
  544. {
  545. if (HAL_RNG_GenerateRandomNumber(hrng, &(hrng->RandomNumber)) == HAL_OK)
  546. {
  547. return hrng->RandomNumber;
  548. }
  549. else
  550. {
  551. return 0U;
  552. }
  553. }
  554. /**
  555. * @brief Returns a 32-bit random number with interrupt enabled (Obsolete),
  556. * Use HAL_RNG_GenerateRandomNumber_IT() API instead.
  557. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  558. * the configuration information for RNG.
  559. * @retval 32-bit random number
  560. */
  561. uint32_t HAL_RNG_GetRandomNumber_IT(RNG_HandleTypeDef *hrng)
  562. {
  563. uint32_t random32bit = 0U;
  564. /* Process locked */
  565. __HAL_LOCK(hrng);
  566. /* Change RNG peripheral state */
  567. hrng->State = HAL_RNG_STATE_BUSY;
  568. /* Get a 32bit Random number */
  569. random32bit = hrng->Instance->DR;
  570. /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */
  571. __HAL_RNG_ENABLE_IT(hrng);
  572. /* Return the 32 bit random number */
  573. return random32bit;
  574. }
  575. /**
  576. * @brief Handles RNG interrupt request.
  577. * @note In the case of a clock error, the RNG is no more able to generate
  578. * random numbers because the PLL48CLK clock is not correct. User has
  579. * to check that the clock controller is correctly configured to provide
  580. * the RNG clock and clear the CEIS bit using __HAL_RNG_CLEAR_IT().
  581. * The clock error has no impact on the previously generated
  582. * random numbers, and the RNG_DR register contents can be used.
  583. * @note In the case of a seed error, the generation of random numbers is
  584. * interrupted as long as the SECS bit is '1'. If a number is
  585. * available in the RNG_DR register, it must not be used because it may
  586. * not have enough entropy. In this case, it is recommended to clear the
  587. * SEIS bit using __HAL_RNG_CLEAR_IT(), then disable and enable
  588. * the RNG peripheral to reinitialize and restart the RNG.
  589. * @note User-written HAL_RNG_ErrorCallback() API is called once whether SEIS
  590. * or CEIS are set.
  591. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  592. * the configuration information for RNG.
  593. * @retval None
  594. */
  595. void HAL_RNG_IRQHandler(RNG_HandleTypeDef *hrng)
  596. {
  597. uint32_t rngclockerror = 0U;
  598. /* RNG clock error interrupt occurred */
  599. if (__HAL_RNG_GET_IT(hrng, RNG_IT_CEI) != RESET)
  600. {
  601. /* Update the error code */
  602. hrng->ErrorCode = HAL_RNG_ERROR_CLOCK;
  603. rngclockerror = 1U;
  604. }
  605. else if (__HAL_RNG_GET_IT(hrng, RNG_IT_SEI) != RESET)
  606. {
  607. /* Update the error code */
  608. hrng->ErrorCode = HAL_RNG_ERROR_SEED;
  609. rngclockerror = 1U;
  610. }
  611. else
  612. {
  613. /* Nothing to do */
  614. }
  615. if (rngclockerror == 1U)
  616. {
  617. /* Change RNG peripheral state */
  618. hrng->State = HAL_RNG_STATE_ERROR;
  619. #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
  620. /* Call registered Error callback */
  621. hrng->ErrorCallback(hrng);
  622. #else
  623. /* Call legacy weak Error callback */
  624. HAL_RNG_ErrorCallback(hrng);
  625. #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
  626. /* Clear the clock error flag */
  627. __HAL_RNG_CLEAR_IT(hrng, RNG_IT_CEI | RNG_IT_SEI);
  628. }
  629. /* Check RNG data ready interrupt occurred */
  630. if (__HAL_RNG_GET_IT(hrng, RNG_IT_DRDY) != RESET)
  631. {
  632. /* Generate random number once, so disable the IT */
  633. __HAL_RNG_DISABLE_IT(hrng);
  634. /* Get the 32bit Random number (DRDY flag automatically cleared) */
  635. hrng->RandomNumber = hrng->Instance->DR;
  636. if (hrng->State != HAL_RNG_STATE_ERROR)
  637. {
  638. /* Change RNG peripheral state */
  639. hrng->State = HAL_RNG_STATE_READY;
  640. /* Process Unlocked */
  641. __HAL_UNLOCK(hrng);
  642. #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
  643. /* Call registered Data Ready callback */
  644. hrng->ReadyDataCallback(hrng, hrng->RandomNumber);
  645. #else
  646. /* Call legacy weak Data Ready callback */
  647. HAL_RNG_ReadyDataCallback(hrng, hrng->RandomNumber);
  648. #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
  649. }
  650. }
  651. }
  652. /**
  653. * @brief Read latest generated random number.
  654. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  655. * the configuration information for RNG.
  656. * @retval random value
  657. */
  658. uint32_t HAL_RNG_ReadLastRandomNumber(RNG_HandleTypeDef *hrng)
  659. {
  660. return (hrng->RandomNumber);
  661. }
  662. /**
  663. * @brief Data Ready callback in non-blocking mode.
  664. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  665. * the configuration information for RNG.
  666. * @param random32bit generated random number.
  667. * @retval None
  668. */
  669. __weak void HAL_RNG_ReadyDataCallback(RNG_HandleTypeDef *hrng, uint32_t random32bit)
  670. {
  671. /* Prevent unused argument(s) compilation warning */
  672. UNUSED(hrng);
  673. UNUSED(random32bit);
  674. /* NOTE : This function should not be modified. When the callback is needed,
  675. function HAL_RNG_ReadyDataCallback must be implemented in the user file.
  676. */
  677. }
  678. /**
  679. * @brief RNG error callbacks.
  680. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  681. * the configuration information for RNG.
  682. * @retval None
  683. */
  684. __weak void HAL_RNG_ErrorCallback(RNG_HandleTypeDef *hrng)
  685. {
  686. /* Prevent unused argument(s) compilation warning */
  687. UNUSED(hrng);
  688. /* NOTE : This function should not be modified. When the callback is needed,
  689. function HAL_RNG_ErrorCallback must be implemented in the user file.
  690. */
  691. }
  692. /**
  693. * @}
  694. */
  695. /** @addtogroup RNG_Exported_Functions_Group3
  696. * @brief Peripheral State functions
  697. *
  698. @verbatim
  699. ===============================================================================
  700. ##### Peripheral State functions #####
  701. ===============================================================================
  702. [..]
  703. This subsection permits to get in run-time the status of the peripheral
  704. and the data flow.
  705. @endverbatim
  706. * @{
  707. */
  708. /**
  709. * @brief Returns the RNG state.
  710. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  711. * the configuration information for RNG.
  712. * @retval HAL state
  713. */
  714. HAL_RNG_StateTypeDef HAL_RNG_GetState(RNG_HandleTypeDef *hrng)
  715. {
  716. return hrng->State;
  717. }
  718. /**
  719. * @brief Return the RNG handle error code.
  720. * @param hrng: pointer to a RNG_HandleTypeDef structure.
  721. * @retval RNG Error Code
  722. */
  723. uint32_t HAL_RNG_GetError(RNG_HandleTypeDef *hrng)
  724. {
  725. /* Return RNG Error Code */
  726. return hrng->ErrorCode;
  727. }
  728. /**
  729. * @}
  730. */
  731. /**
  732. * @}
  733. */
  734. #endif /* HAL_RNG_MODULE_ENABLED */
  735. /**
  736. * @}
  737. */
  738. #endif /* RNG */
  739. /**
  740. * @}
  741. */
  742. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/