STM32F479xx HAL User Manual
|
00001 /** 00002 ****************************************************************************** 00003 * @file stm32f4xx_hal_rng.c 00004 * @author MCD Application Team 00005 * @brief RNG HAL module driver. 00006 * This file provides firmware functions to manage the following 00007 * functionalities of the Random Number Generator (RNG) peripheral: 00008 * + Initialization and configuration functions 00009 * + Peripheral Control functions 00010 * + Peripheral State functions 00011 * 00012 @verbatim 00013 ============================================================================== 00014 ##### How to use this driver ##### 00015 ============================================================================== 00016 [..] 00017 The RNG HAL driver can be used as follows: 00018 00019 (#) Enable the RNG controller clock using __HAL_RCC_RNG_CLK_ENABLE() macro 00020 in HAL_RNG_MspInit(). 00021 (#) Activate the RNG peripheral using HAL_RNG_Init() function. 00022 (#) Wait until the 32 bit Random Number Generator contains a valid 00023 random data using (polling/interrupt) mode. 00024 (#) Get the 32 bit random number using HAL_RNG_GenerateRandomNumber() function. 00025 00026 ##### Callback registration ##### 00027 ================================== 00028 00029 [..] 00030 The compilation define USE_HAL_RNG_REGISTER_CALLBACKS when set to 1 00031 allows the user to configure dynamically the driver callbacks. 00032 00033 [..] 00034 Use Function HAL_RNG_RegisterCallback() to register a user callback. 00035 Function HAL_RNG_RegisterCallback() allows to register following callbacks: 00036 (+) ErrorCallback : RNG Error Callback. 00037 (+) MspInitCallback : RNG MspInit. 00038 (+) MspDeInitCallback : RNG MspDeInit. 00039 This function takes as parameters the HAL peripheral handle, the Callback ID 00040 and a pointer to the user callback function. 00041 00042 [..] 00043 Use function HAL_RNG_UnRegisterCallback() to reset a callback to the default 00044 weak (surcharged) function. 00045 HAL_RNG_UnRegisterCallback() takes as parameters the HAL peripheral handle, 00046 and the Callback ID. 00047 This function allows to reset following callbacks: 00048 (+) ErrorCallback : RNG Error Callback. 00049 (+) MspInitCallback : RNG MspInit. 00050 (+) MspDeInitCallback : RNG MspDeInit. 00051 00052 [..] 00053 For specific callback ReadyDataCallback, use dedicated register callbacks: 00054 respectively HAL_RNG_RegisterReadyDataCallback() , HAL_RNG_UnRegisterReadyDataCallback(). 00055 00056 [..] 00057 By default, after the HAL_RNG_Init() and when the state is HAL_RNG_STATE_RESET 00058 all callbacks are set to the corresponding weak (surcharged) functions: 00059 example HAL_RNG_ErrorCallback(). 00060 Exception done for MspInit and MspDeInit functions that are respectively 00061 reset to the legacy weak (surcharged) functions in the HAL_RNG_Init() 00062 and HAL_RNG_DeInit() only when these callbacks are null (not registered beforehand). 00063 If not, MspInit or MspDeInit are not null, the HAL_RNG_Init() and HAL_RNG_DeInit() 00064 keep and use the user MspInit/MspDeInit callbacks (registered beforehand). 00065 00066 [..] 00067 Callbacks can be registered/unregistered in HAL_RNG_STATE_READY state only. 00068 Exception done MspInit/MspDeInit that can be registered/unregistered 00069 in HAL_RNG_STATE_READY or HAL_RNG_STATE_RESET state, thus registered (user) 00070 MspInit/DeInit callbacks can be used during the Init/DeInit. 00071 In that case first register the MspInit/MspDeInit user callbacks 00072 using HAL_RNG_RegisterCallback() before calling HAL_RNG_DeInit() 00073 or HAL_RNG_Init() function. 00074 00075 [..] 00076 When The compilation define USE_HAL_RNG_REGISTER_CALLBACKS is set to 0 or 00077 not defined, the callback registration feature is not available 00078 and weak (surcharged) callbacks are used. 00079 00080 @endverbatim 00081 ****************************************************************************** 00082 * @attention 00083 * 00084 * <h2><center>© Copyright (c) 2016 STMicroelectronics. 00085 * All rights reserved.</center></h2> 00086 * 00087 * This software component is licensed by ST under BSD 3-Clause license, 00088 * the "License"; You may not use this file except in compliance with the 00089 * License. You may obtain a copy of the License at: 00090 * opensource.org/licenses/BSD-3-Clause 00091 * 00092 ****************************************************************************** 00093 */ 00094 00095 /* Includes ------------------------------------------------------------------*/ 00096 #include "stm32f4xx_hal.h" 00097 00098 /** @addtogroup STM32F4xx_HAL_Driver 00099 * @{ 00100 */ 00101 00102 #if defined (RNG) 00103 00104 /** @addtogroup RNG 00105 * @brief RNG HAL module driver. 00106 * @{ 00107 */ 00108 00109 #ifdef HAL_RNG_MODULE_ENABLED 00110 00111 /* Private types -------------------------------------------------------------*/ 00112 /* Private defines -----------------------------------------------------------*/ 00113 /* Private variables ---------------------------------------------------------*/ 00114 /* Private constants ---------------------------------------------------------*/ 00115 /** @defgroup RNG_Private_Constants RNG Private Constants 00116 * @{ 00117 */ 00118 #define RNG_TIMEOUT_VALUE 2U 00119 /** 00120 * @} 00121 */ 00122 /* Private macros ------------------------------------------------------------*/ 00123 /* Private functions prototypes ----------------------------------------------*/ 00124 /* Private functions ---------------------------------------------------------*/ 00125 /* Exported functions --------------------------------------------------------*/ 00126 00127 /** @addtogroup RNG_Exported_Functions 00128 * @{ 00129 */ 00130 00131 /** @addtogroup RNG_Exported_Functions_Group1 00132 * @brief Initialization and configuration functions 00133 * 00134 @verbatim 00135 =============================================================================== 00136 ##### Initialization and configuration functions ##### 00137 =============================================================================== 00138 [..] This section provides functions allowing to: 00139 (+) Initialize the RNG according to the specified parameters 00140 in the RNG_InitTypeDef and create the associated handle 00141 (+) DeInitialize the RNG peripheral 00142 (+) Initialize the RNG MSP 00143 (+) DeInitialize RNG MSP 00144 00145 @endverbatim 00146 * @{ 00147 */ 00148 00149 /** 00150 * @brief Initializes the RNG peripheral and creates the associated handle. 00151 * @param hrng pointer to a RNG_HandleTypeDef structure that contains 00152 * the configuration information for RNG. 00153 * @retval HAL status 00154 */ 00155 HAL_StatusTypeDef HAL_RNG_Init(RNG_HandleTypeDef *hrng) 00156 { 00157 /* Check the RNG handle allocation */ 00158 if (hrng == NULL) 00159 { 00160 return HAL_ERROR; 00161 } 00162 /* Check the parameters */ 00163 assert_param(IS_RNG_ALL_INSTANCE(hrng->Instance)); 00164 00165 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1) 00166 if (hrng->State == HAL_RNG_STATE_RESET) 00167 { 00168 /* Allocate lock resource and initialize it */ 00169 hrng->Lock = HAL_UNLOCKED; 00170 00171 hrng->ReadyDataCallback = HAL_RNG_ReadyDataCallback; /* Legacy weak ReadyDataCallback */ 00172 hrng->ErrorCallback = HAL_RNG_ErrorCallback; /* Legacy weak ErrorCallback */ 00173 00174 if (hrng->MspInitCallback == NULL) 00175 { 00176 hrng->MspInitCallback = HAL_RNG_MspInit; /* Legacy weak MspInit */ 00177 } 00178 00179 /* Init the low level hardware */ 00180 hrng->MspInitCallback(hrng); 00181 } 00182 #else 00183 if (hrng->State == HAL_RNG_STATE_RESET) 00184 { 00185 /* Allocate lock resource and initialize it */ 00186 hrng->Lock = HAL_UNLOCKED; 00187 00188 /* Init the low level hardware */ 00189 HAL_RNG_MspInit(hrng); 00190 } 00191 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */ 00192 00193 /* Change RNG peripheral state */ 00194 hrng->State = HAL_RNG_STATE_BUSY; 00195 00196 00197 /* Enable the RNG Peripheral */ 00198 __HAL_RNG_ENABLE(hrng); 00199 00200 /* Initialize the RNG state */ 00201 hrng->State = HAL_RNG_STATE_READY; 00202 00203 /* Initialise the error code */ 00204 hrng->ErrorCode = HAL_RNG_ERROR_NONE; 00205 00206 /* Return function status */ 00207 return HAL_OK; 00208 } 00209 00210 /** 00211 * @brief DeInitializes the RNG peripheral. 00212 * @param hrng pointer to a RNG_HandleTypeDef structure that contains 00213 * the configuration information for RNG. 00214 * @retval HAL status 00215 */ 00216 HAL_StatusTypeDef HAL_RNG_DeInit(RNG_HandleTypeDef *hrng) 00217 { 00218 /* Check the RNG handle allocation */ 00219 if (hrng == NULL) 00220 { 00221 return HAL_ERROR; 00222 } 00223 00224 /* Disable the RNG Peripheral */ 00225 CLEAR_BIT(hrng->Instance->CR, RNG_CR_IE | RNG_CR_RNGEN); 00226 00227 /* Clear RNG interrupt status flags */ 00228 CLEAR_BIT(hrng->Instance->SR, RNG_SR_CEIS | RNG_SR_SEIS); 00229 00230 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1) 00231 if (hrng->MspDeInitCallback == NULL) 00232 { 00233 hrng->MspDeInitCallback = HAL_RNG_MspDeInit; /* Legacy weak MspDeInit */ 00234 } 00235 00236 /* DeInit the low level hardware */ 00237 hrng->MspDeInitCallback(hrng); 00238 #else 00239 /* DeInit the low level hardware */ 00240 HAL_RNG_MspDeInit(hrng); 00241 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */ 00242 00243 /* Update the RNG state */ 00244 hrng->State = HAL_RNG_STATE_RESET; 00245 00246 /* Initialise the error code */ 00247 hrng->ErrorCode = HAL_RNG_ERROR_NONE; 00248 00249 /* Release Lock */ 00250 __HAL_UNLOCK(hrng); 00251 00252 /* Return the function status */ 00253 return HAL_OK; 00254 } 00255 00256 /** 00257 * @brief Initializes the RNG MSP. 00258 * @param hrng pointer to a RNG_HandleTypeDef structure that contains 00259 * the configuration information for RNG. 00260 * @retval None 00261 */ 00262 __weak void HAL_RNG_MspInit(RNG_HandleTypeDef *hrng) 00263 { 00264 /* Prevent unused argument(s) compilation warning */ 00265 UNUSED(hrng); 00266 /* NOTE : This function should not be modified. When the callback is needed, 00267 function HAL_RNG_MspInit must be implemented in the user file. 00268 */ 00269 } 00270 00271 /** 00272 * @brief DeInitializes the RNG MSP. 00273 * @param hrng pointer to a RNG_HandleTypeDef structure that contains 00274 * the configuration information for RNG. 00275 * @retval None 00276 */ 00277 __weak void HAL_RNG_MspDeInit(RNG_HandleTypeDef *hrng) 00278 { 00279 /* Prevent unused argument(s) compilation warning */ 00280 UNUSED(hrng); 00281 /* NOTE : This function should not be modified. When the callback is needed, 00282 function HAL_RNG_MspDeInit must be implemented in the user file. 00283 */ 00284 } 00285 00286 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1) 00287 /** 00288 * @brief Register a User RNG Callback 00289 * To be used instead of the weak predefined callback 00290 * @param hrng RNG handle 00291 * @param CallbackID ID of the callback to be registered 00292 * This parameter can be one of the following values: 00293 * @arg @ref HAL_RNG_ERROR_CB_ID Error callback ID 00294 * @arg @ref HAL_RNG_MSPINIT_CB_ID MspInit callback ID 00295 * @arg @ref HAL_RNG_MSPDEINIT_CB_ID MspDeInit callback ID 00296 * @param pCallback pointer to the Callback function 00297 * @retval HAL status 00298 */ 00299 HAL_StatusTypeDef HAL_RNG_RegisterCallback(RNG_HandleTypeDef *hrng, HAL_RNG_CallbackIDTypeDef CallbackID, 00300 pRNG_CallbackTypeDef pCallback) 00301 { 00302 HAL_StatusTypeDef status = HAL_OK; 00303 00304 if (pCallback == NULL) 00305 { 00306 /* Update the error code */ 00307 hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK; 00308 return HAL_ERROR; 00309 } 00310 /* Process locked */ 00311 __HAL_LOCK(hrng); 00312 00313 if (HAL_RNG_STATE_READY == hrng->State) 00314 { 00315 switch (CallbackID) 00316 { 00317 case HAL_RNG_ERROR_CB_ID : 00318 hrng->ErrorCallback = pCallback; 00319 break; 00320 00321 case HAL_RNG_MSPINIT_CB_ID : 00322 hrng->MspInitCallback = pCallback; 00323 break; 00324 00325 case HAL_RNG_MSPDEINIT_CB_ID : 00326 hrng->MspDeInitCallback = pCallback; 00327 break; 00328 00329 default : 00330 /* Update the error code */ 00331 hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK; 00332 /* Return error status */ 00333 status = HAL_ERROR; 00334 break; 00335 } 00336 } 00337 else if (HAL_RNG_STATE_RESET == hrng->State) 00338 { 00339 switch (CallbackID) 00340 { 00341 case HAL_RNG_MSPINIT_CB_ID : 00342 hrng->MspInitCallback = pCallback; 00343 break; 00344 00345 case HAL_RNG_MSPDEINIT_CB_ID : 00346 hrng->MspDeInitCallback = pCallback; 00347 break; 00348 00349 default : 00350 /* Update the error code */ 00351 hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK; 00352 /* Return error status */ 00353 status = HAL_ERROR; 00354 break; 00355 } 00356 } 00357 else 00358 { 00359 /* Update the error code */ 00360 hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK; 00361 /* Return error status */ 00362 status = HAL_ERROR; 00363 } 00364 00365 /* Release Lock */ 00366 __HAL_UNLOCK(hrng); 00367 return status; 00368 } 00369 00370 /** 00371 * @brief Unregister an RNG Callback 00372 * RNG callabck is redirected to the weak predefined callback 00373 * @param hrng RNG handle 00374 * @param CallbackID ID of the callback to be unregistered 00375 * This parameter can be one of the following values: 00376 * @arg @ref HAL_RNG_ERROR_CB_ID Error callback ID 00377 * @arg @ref HAL_RNG_MSPINIT_CB_ID MspInit callback ID 00378 * @arg @ref HAL_RNG_MSPDEINIT_CB_ID MspDeInit callback ID 00379 * @retval HAL status 00380 */ 00381 HAL_StatusTypeDef HAL_RNG_UnRegisterCallback(RNG_HandleTypeDef *hrng, HAL_RNG_CallbackIDTypeDef CallbackID) 00382 { 00383 HAL_StatusTypeDef status = HAL_OK; 00384 00385 /* Process locked */ 00386 __HAL_LOCK(hrng); 00387 00388 if (HAL_RNG_STATE_READY == hrng->State) 00389 { 00390 switch (CallbackID) 00391 { 00392 case HAL_RNG_ERROR_CB_ID : 00393 hrng->ErrorCallback = HAL_RNG_ErrorCallback; /* Legacy weak ErrorCallback */ 00394 break; 00395 00396 case HAL_RNG_MSPINIT_CB_ID : 00397 hrng->MspInitCallback = HAL_RNG_MspInit; /* Legacy weak MspInit */ 00398 break; 00399 00400 case HAL_RNG_MSPDEINIT_CB_ID : 00401 hrng->MspDeInitCallback = HAL_RNG_MspDeInit; /* Legacy weak MspDeInit */ 00402 break; 00403 00404 default : 00405 /* Update the error code */ 00406 hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK; 00407 /* Return error status */ 00408 status = HAL_ERROR; 00409 break; 00410 } 00411 } 00412 else if (HAL_RNG_STATE_RESET == hrng->State) 00413 { 00414 switch (CallbackID) 00415 { 00416 case HAL_RNG_MSPINIT_CB_ID : 00417 hrng->MspInitCallback = HAL_RNG_MspInit; /* Legacy weak MspInit */ 00418 break; 00419 00420 case HAL_RNG_MSPDEINIT_CB_ID : 00421 hrng->MspDeInitCallback = HAL_RNG_MspDeInit; /* Legacy weak MspInit */ 00422 break; 00423 00424 default : 00425 /* Update the error code */ 00426 hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK; 00427 /* Return error status */ 00428 status = HAL_ERROR; 00429 break; 00430 } 00431 } 00432 else 00433 { 00434 /* Update the error code */ 00435 hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK; 00436 /* Return error status */ 00437 status = HAL_ERROR; 00438 } 00439 00440 /* Release Lock */ 00441 __HAL_UNLOCK(hrng); 00442 return status; 00443 } 00444 00445 /** 00446 * @brief Register Data Ready RNG Callback 00447 * To be used instead of the weak HAL_RNG_ReadyDataCallback() predefined callback 00448 * @param hrng RNG handle 00449 * @param pCallback pointer to the Data Ready Callback function 00450 * @retval HAL status 00451 */ 00452 HAL_StatusTypeDef HAL_RNG_RegisterReadyDataCallback(RNG_HandleTypeDef *hrng, pRNG_ReadyDataCallbackTypeDef pCallback) 00453 { 00454 HAL_StatusTypeDef status = HAL_OK; 00455 00456 if (pCallback == NULL) 00457 { 00458 /* Update the error code */ 00459 hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK; 00460 return HAL_ERROR; 00461 } 00462 /* Process locked */ 00463 __HAL_LOCK(hrng); 00464 00465 if (HAL_RNG_STATE_READY == hrng->State) 00466 { 00467 hrng->ReadyDataCallback = pCallback; 00468 } 00469 else 00470 { 00471 /* Update the error code */ 00472 hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK; 00473 /* Return error status */ 00474 status = HAL_ERROR; 00475 } 00476 00477 /* Release Lock */ 00478 __HAL_UNLOCK(hrng); 00479 return status; 00480 } 00481 00482 /** 00483 * @brief UnRegister the Data Ready RNG Callback 00484 * Data Ready RNG Callback is redirected to the weak HAL_RNG_ReadyDataCallback() predefined callback 00485 * @param hrng RNG handle 00486 * @retval HAL status 00487 */ 00488 HAL_StatusTypeDef HAL_RNG_UnRegisterReadyDataCallback(RNG_HandleTypeDef *hrng) 00489 { 00490 HAL_StatusTypeDef status = HAL_OK; 00491 00492 /* Process locked */ 00493 __HAL_LOCK(hrng); 00494 00495 if (HAL_RNG_STATE_READY == hrng->State) 00496 { 00497 hrng->ReadyDataCallback = HAL_RNG_ReadyDataCallback; /* Legacy weak ReadyDataCallback */ 00498 } 00499 else 00500 { 00501 /* Update the error code */ 00502 hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK; 00503 /* Return error status */ 00504 status = HAL_ERROR; 00505 } 00506 00507 /* Release Lock */ 00508 __HAL_UNLOCK(hrng); 00509 return status; 00510 } 00511 00512 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */ 00513 00514 /** 00515 * @} 00516 */ 00517 00518 /** @addtogroup RNG_Exported_Functions_Group2 00519 * @brief Peripheral Control functions 00520 * 00521 @verbatim 00522 =============================================================================== 00523 ##### Peripheral Control functions ##### 00524 =============================================================================== 00525 [..] This section provides functions allowing to: 00526 (+) Get the 32 bit Random number 00527 (+) Get the 32 bit Random number with interrupt enabled 00528 (+) Handle RNG interrupt request 00529 00530 @endverbatim 00531 * @{ 00532 */ 00533 00534 /** 00535 * @brief Generates a 32-bit random number. 00536 * @note Each time the random number data is read the RNG_FLAG_DRDY flag 00537 * is automatically cleared. 00538 * @param hrng pointer to a RNG_HandleTypeDef structure that contains 00539 * the configuration information for RNG. 00540 * @param random32bit pointer to generated random number variable if successful. 00541 * @retval HAL status 00542 */ 00543 00544 HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber(RNG_HandleTypeDef *hrng, uint32_t *random32bit) 00545 { 00546 uint32_t tickstart; 00547 HAL_StatusTypeDef status = HAL_OK; 00548 00549 /* Process Locked */ 00550 __HAL_LOCK(hrng); 00551 00552 /* Check RNG peripheral state */ 00553 if (hrng->State == HAL_RNG_STATE_READY) 00554 { 00555 /* Change RNG peripheral state */ 00556 hrng->State = HAL_RNG_STATE_BUSY; 00557 00558 /* Get tick */ 00559 tickstart = HAL_GetTick(); 00560 00561 /* Check if data register contains valid random data */ 00562 while (__HAL_RNG_GET_FLAG(hrng, RNG_FLAG_DRDY) == RESET) 00563 { 00564 if ((HAL_GetTick() - tickstart) > RNG_TIMEOUT_VALUE) 00565 { 00566 /* New check to avoid false timeout detection in case of preemption */ 00567 if (__HAL_RNG_GET_FLAG(hrng, RNG_FLAG_DRDY) == RESET) 00568 { 00569 hrng->State = HAL_RNG_STATE_READY; 00570 hrng->ErrorCode = HAL_RNG_ERROR_TIMEOUT; 00571 /* Process Unlocked */ 00572 __HAL_UNLOCK(hrng); 00573 return HAL_ERROR; 00574 } 00575 } 00576 } 00577 00578 /* Get a 32bit Random number */ 00579 hrng->RandomNumber = hrng->Instance->DR; 00580 *random32bit = hrng->RandomNumber; 00581 00582 hrng->State = HAL_RNG_STATE_READY; 00583 } 00584 else 00585 { 00586 hrng->ErrorCode = HAL_RNG_ERROR_BUSY; 00587 status = HAL_ERROR; 00588 } 00589 00590 /* Process Unlocked */ 00591 __HAL_UNLOCK(hrng); 00592 00593 return status; 00594 } 00595 00596 /** 00597 * @brief Generates a 32-bit random number in interrupt mode. 00598 * @param hrng pointer to a RNG_HandleTypeDef structure that contains 00599 * the configuration information for RNG. 00600 * @retval HAL status 00601 */ 00602 HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber_IT(RNG_HandleTypeDef *hrng) 00603 { 00604 HAL_StatusTypeDef status = HAL_OK; 00605 00606 /* Process Locked */ 00607 __HAL_LOCK(hrng); 00608 00609 /* Check RNG peripheral state */ 00610 if (hrng->State == HAL_RNG_STATE_READY) 00611 { 00612 /* Change RNG peripheral state */ 00613 hrng->State = HAL_RNG_STATE_BUSY; 00614 00615 /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */ 00616 __HAL_RNG_ENABLE_IT(hrng); 00617 } 00618 else 00619 { 00620 /* Process Unlocked */ 00621 __HAL_UNLOCK(hrng); 00622 00623 hrng->ErrorCode = HAL_RNG_ERROR_BUSY; 00624 status = HAL_ERROR; 00625 } 00626 00627 return status; 00628 } 00629 00630 /** 00631 * @brief Returns generated random number in polling mode (Obsolete) 00632 * Use HAL_RNG_GenerateRandomNumber() API instead. 00633 * @param hrng pointer to a RNG_HandleTypeDef structure that contains 00634 * the configuration information for RNG. 00635 * @retval Random value 00636 */ 00637 uint32_t HAL_RNG_GetRandomNumber(RNG_HandleTypeDef *hrng) 00638 { 00639 if (HAL_RNG_GenerateRandomNumber(hrng, &(hrng->RandomNumber)) == HAL_OK) 00640 { 00641 return hrng->RandomNumber; 00642 } 00643 else 00644 { 00645 return 0U; 00646 } 00647 } 00648 00649 /** 00650 * @brief Returns a 32-bit random number with interrupt enabled (Obsolete), 00651 * Use HAL_RNG_GenerateRandomNumber_IT() API instead. 00652 * @param hrng pointer to a RNG_HandleTypeDef structure that contains 00653 * the configuration information for RNG. 00654 * @retval 32-bit random number 00655 */ 00656 uint32_t HAL_RNG_GetRandomNumber_IT(RNG_HandleTypeDef *hrng) 00657 { 00658 uint32_t random32bit = 0U; 00659 00660 /* Process locked */ 00661 __HAL_LOCK(hrng); 00662 00663 /* Change RNG peripheral state */ 00664 hrng->State = HAL_RNG_STATE_BUSY; 00665 00666 /* Get a 32bit Random number */ 00667 random32bit = hrng->Instance->DR; 00668 00669 /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */ 00670 __HAL_RNG_ENABLE_IT(hrng); 00671 00672 /* Return the 32 bit random number */ 00673 return random32bit; 00674 } 00675 00676 /** 00677 * @brief Handles RNG interrupt request. 00678 * @note In the case of a clock error, the RNG is no more able to generate 00679 * random numbers because the PLL48CLK clock is not correct. User has 00680 * to check that the clock controller is correctly configured to provide 00681 * the RNG clock and clear the CEIS bit using __HAL_RNG_CLEAR_IT(). 00682 * The clock error has no impact on the previously generated 00683 * random numbers, and the RNG_DR register contents can be used. 00684 * @note In the case of a seed error, the generation of random numbers is 00685 * interrupted as long as the SECS bit is '1'. If a number is 00686 * available in the RNG_DR register, it must not be used because it may 00687 * not have enough entropy. In this case, it is recommended to clear the 00688 * SEIS bit using __HAL_RNG_CLEAR_IT(), then disable and enable 00689 * the RNG peripheral to reinitialize and restart the RNG. 00690 * @note User-written HAL_RNG_ErrorCallback() API is called once whether SEIS 00691 * or CEIS are set. 00692 * @param hrng pointer to a RNG_HandleTypeDef structure that contains 00693 * the configuration information for RNG. 00694 * @retval None 00695 00696 */ 00697 void HAL_RNG_IRQHandler(RNG_HandleTypeDef *hrng) 00698 { 00699 uint32_t rngclockerror = 0U; 00700 00701 /* RNG clock error interrupt occurred */ 00702 if (__HAL_RNG_GET_IT(hrng, RNG_IT_CEI) != RESET) 00703 { 00704 /* Update the error code */ 00705 hrng->ErrorCode = HAL_RNG_ERROR_CLOCK; 00706 rngclockerror = 1U; 00707 } 00708 else if (__HAL_RNG_GET_IT(hrng, RNG_IT_SEI) != RESET) 00709 { 00710 /* Update the error code */ 00711 hrng->ErrorCode = HAL_RNG_ERROR_SEED; 00712 rngclockerror = 1U; 00713 } 00714 else 00715 { 00716 /* Nothing to do */ 00717 } 00718 00719 if (rngclockerror == 1U) 00720 { 00721 /* Change RNG peripheral state */ 00722 hrng->State = HAL_RNG_STATE_ERROR; 00723 00724 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1) 00725 /* Call registered Error callback */ 00726 hrng->ErrorCallback(hrng); 00727 #else 00728 /* Call legacy weak Error callback */ 00729 HAL_RNG_ErrorCallback(hrng); 00730 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */ 00731 00732 /* Clear the clock error flag */ 00733 __HAL_RNG_CLEAR_IT(hrng, RNG_IT_CEI | RNG_IT_SEI); 00734 00735 return; 00736 } 00737 00738 /* Check RNG data ready interrupt occurred */ 00739 if (__HAL_RNG_GET_IT(hrng, RNG_IT_DRDY) != RESET) 00740 { 00741 /* Generate random number once, so disable the IT */ 00742 __HAL_RNG_DISABLE_IT(hrng); 00743 00744 /* Get the 32bit Random number (DRDY flag automatically cleared) */ 00745 hrng->RandomNumber = hrng->Instance->DR; 00746 00747 if (hrng->State != HAL_RNG_STATE_ERROR) 00748 { 00749 /* Change RNG peripheral state */ 00750 hrng->State = HAL_RNG_STATE_READY; 00751 /* Process Unlocked */ 00752 __HAL_UNLOCK(hrng); 00753 00754 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1) 00755 /* Call registered Data Ready callback */ 00756 hrng->ReadyDataCallback(hrng, hrng->RandomNumber); 00757 #else 00758 /* Call legacy weak Data Ready callback */ 00759 HAL_RNG_ReadyDataCallback(hrng, hrng->RandomNumber); 00760 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */ 00761 } 00762 } 00763 } 00764 00765 /** 00766 * @brief Read latest generated random number. 00767 * @param hrng pointer to a RNG_HandleTypeDef structure that contains 00768 * the configuration information for RNG. 00769 * @retval random value 00770 */ 00771 uint32_t HAL_RNG_ReadLastRandomNumber(RNG_HandleTypeDef *hrng) 00772 { 00773 return (hrng->RandomNumber); 00774 } 00775 00776 /** 00777 * @brief Data Ready callback in non-blocking mode. 00778 * @param hrng pointer to a RNG_HandleTypeDef structure that contains 00779 * the configuration information for RNG. 00780 * @param random32bit generated random number. 00781 * @retval None 00782 */ 00783 __weak void HAL_RNG_ReadyDataCallback(RNG_HandleTypeDef *hrng, uint32_t random32bit) 00784 { 00785 /* Prevent unused argument(s) compilation warning */ 00786 UNUSED(hrng); 00787 UNUSED(random32bit); 00788 /* NOTE : This function should not be modified. When the callback is needed, 00789 function HAL_RNG_ReadyDataCallback must be implemented in the user file. 00790 */ 00791 } 00792 00793 /** 00794 * @brief RNG error callbacks. 00795 * @param hrng pointer to a RNG_HandleTypeDef structure that contains 00796 * the configuration information for RNG. 00797 * @retval None 00798 */ 00799 __weak void HAL_RNG_ErrorCallback(RNG_HandleTypeDef *hrng) 00800 { 00801 /* Prevent unused argument(s) compilation warning */ 00802 UNUSED(hrng); 00803 /* NOTE : This function should not be modified. When the callback is needed, 00804 function HAL_RNG_ErrorCallback must be implemented in the user file. 00805 */ 00806 } 00807 /** 00808 * @} 00809 */ 00810 00811 00812 /** @addtogroup RNG_Exported_Functions_Group3 00813 * @brief Peripheral State functions 00814 * 00815 @verbatim 00816 =============================================================================== 00817 ##### Peripheral State functions ##### 00818 =============================================================================== 00819 [..] 00820 This subsection permits to get in run-time the status of the peripheral 00821 and the data flow. 00822 00823 @endverbatim 00824 * @{ 00825 */ 00826 00827 /** 00828 * @brief Returns the RNG state. 00829 * @param hrng pointer to a RNG_HandleTypeDef structure that contains 00830 * the configuration information for RNG. 00831 * @retval HAL state 00832 */ 00833 HAL_RNG_StateTypeDef HAL_RNG_GetState(RNG_HandleTypeDef *hrng) 00834 { 00835 return hrng->State; 00836 } 00837 00838 /** 00839 * @brief Return the RNG handle error code. 00840 * @param hrng: pointer to a RNG_HandleTypeDef structure. 00841 * @retval RNG Error Code 00842 */ 00843 uint32_t HAL_RNG_GetError(RNG_HandleTypeDef *hrng) 00844 { 00845 /* Return RNG Error Code */ 00846 return hrng->ErrorCode; 00847 } 00848 /** 00849 * @} 00850 */ 00851 00852 /** 00853 * @} 00854 */ 00855 00856 00857 #endif /* HAL_RNG_MODULE_ENABLED */ 00858 /** 00859 * @} 00860 */ 00861 00862 #endif /* RNG */ 00863 00864 /** 00865 * @} 00866 */ 00867 00868 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/