stm32g0xx_hal_rng.c 26 KB

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