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