STM32F439xx HAL User Manual
stm32f4xx_hal_rng.c
Go to the documentation of this file.
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/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   @endverbatim
00027   ******************************************************************************
00028   * @attention
00029   *
00030   * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
00031   *
00032   * Redistribution and use in source and binary forms, with or without modification,
00033   * are permitted provided that the following conditions are met:
00034   *   1. Redistributions of source code must retain the above copyright notice,
00035   *      this list of conditions and the following disclaimer.
00036   *   2. Redistributions in binary form must reproduce the above copyright notice,
00037   *      this list of conditions and the following disclaimer in the documentation
00038   *      and/or other materials provided with the distribution.
00039   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00040   *      may be used to endorse or promote products derived from this software
00041   *      without specific prior written permission.
00042   *
00043   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00044   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00045   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00046   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00047   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00048   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00049   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00050   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00051   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00052   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00053   *
00054   ******************************************************************************
00055   */ 
00056 
00057 /* Includes ------------------------------------------------------------------*/
00058 #include "stm32f4xx_hal.h"
00059 
00060 /** @addtogroup STM32F4xx_HAL_Driver
00061   * @{
00062   */
00063 
00064 /** @addtogroup RNG 
00065   * @{
00066   */
00067 
00068 #ifdef HAL_RNG_MODULE_ENABLED
00069 
00070 #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) ||\
00071     defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) ||\
00072     defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) || defined(STM32F469xx) ||\
00073     defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) ||\
00074     defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
00075 
00076 
00077 /* Private types -------------------------------------------------------------*/
00078 /* Private defines -----------------------------------------------------------*/
00079 /* Private variables ---------------------------------------------------------*/
00080 /* Private constants ---------------------------------------------------------*/
00081 /** @addtogroup RNG_Private_Constants
00082   * @{
00083   */
00084 #define RNG_TIMEOUT_VALUE     2U
00085 /**
00086   * @}
00087   */ 
00088 /* Private macros ------------------------------------------------------------*/
00089 /* Private functions prototypes ----------------------------------------------*/
00090 /* Private functions ---------------------------------------------------------*/
00091 /* Exported functions --------------------------------------------------------*/
00092 
00093 /** @addtogroup RNG_Exported_Functions
00094   * @{
00095   */
00096 
00097 /** @addtogroup RNG_Exported_Functions_Group1
00098  *  @brief   Initialization and de-initialization functions
00099  *
00100 @verbatim
00101  ===============================================================================
00102           ##### Initialization and de-initialization functions #####
00103  ===============================================================================
00104     [..]  This section provides functions allowing to:
00105       (+) Initialize the RNG according to the specified parameters 
00106           in the RNG_InitTypeDef and create the associated handle
00107       (+) DeInitialize the RNG peripheral
00108       (+) Initialize the RNG MSP
00109       (+) DeInitialize RNG MSP 
00110 
00111 @endverbatim
00112   * @{
00113   */
00114   
00115 /**
00116   * @brief  Initializes the RNG peripheral and creates the associated handle.
00117   * @param  hrng pointer to a RNG_HandleTypeDef structure that contains
00118   *                the configuration information for RNG.
00119   * @retval HAL status
00120   */
00121 HAL_StatusTypeDef HAL_RNG_Init(RNG_HandleTypeDef *hrng)
00122 { 
00123   /* Check the RNG handle allocation */
00124   if(hrng == NULL)
00125   {
00126     return HAL_ERROR;
00127   }
00128 
00129   if(hrng->State == HAL_RNG_STATE_RESET)
00130   {  
00131     /* Allocate lock resource and initialize it */
00132     hrng->Lock = HAL_UNLOCKED;
00133     /* Init the low level hardware */
00134     HAL_RNG_MspInit(hrng);
00135   }
00136   
00137   /* Change RNG peripheral state */
00138   hrng->State = HAL_RNG_STATE_BUSY;
00139 
00140   /* Enable the RNG Peripheral */
00141   __HAL_RNG_ENABLE(hrng);
00142 
00143   /* Initialize the RNG state */
00144   hrng->State = HAL_RNG_STATE_READY;
00145 
00146   /* Return function status */
00147   return HAL_OK;
00148 }
00149 
00150 /**
00151   * @brief  DeInitializes the RNG peripheral. 
00152   * @param  hrng pointer to a RNG_HandleTypeDef structure that contains
00153   *                the configuration information for RNG.
00154   * @retval HAL status
00155   */
00156 HAL_StatusTypeDef HAL_RNG_DeInit(RNG_HandleTypeDef *hrng)
00157 { 
00158   /* Check the RNG handle allocation */
00159   if(hrng == NULL)
00160   {
00161     return HAL_ERROR;
00162   }
00163   /* Disable the RNG Peripheral */
00164   CLEAR_BIT(hrng->Instance->CR, RNG_CR_IE | RNG_CR_RNGEN);
00165   
00166   /* Clear RNG interrupt status flags */
00167   CLEAR_BIT(hrng->Instance->SR, RNG_SR_CEIS | RNG_SR_SEIS);
00168   
00169   /* DeInit the low level hardware */
00170   HAL_RNG_MspDeInit(hrng);
00171   
00172   /* Update the RNG state */
00173   hrng->State = HAL_RNG_STATE_RESET; 
00174 
00175   /* Release Lock */
00176   __HAL_UNLOCK(hrng);
00177   
00178   /* Return the function status */
00179   return HAL_OK;
00180 }
00181 
00182 /**
00183   * @brief  Initializes the RNG MSP.
00184   * @param  hrng pointer to a RNG_HandleTypeDef structure that contains
00185   *                the configuration information for RNG.
00186   * @retval None
00187   */
00188 __weak void HAL_RNG_MspInit(RNG_HandleTypeDef *hrng)
00189 {
00190   /* Prevent unused argument(s) compilation warning */
00191   UNUSED(hrng);
00192   /* NOTE : This function should not be modified. When the callback is needed,
00193             function HAL_RNG_MspInit must be implemented in the user file.
00194    */
00195 }
00196 
00197 /**
00198   * @brief  DeInitializes the RNG MSP.
00199   * @param  hrng pointer to a RNG_HandleTypeDef structure that contains
00200   *                the configuration information for RNG.
00201   * @retval None
00202   */
00203 __weak void HAL_RNG_MspDeInit(RNG_HandleTypeDef *hrng)
00204 {
00205   /* Prevent unused argument(s) compilation warning */
00206   UNUSED(hrng);
00207   /* NOTE : This function should not be modified. When the callback is needed,
00208             function HAL_RNG_MspDeInit must be implemented in the user file.
00209    */
00210 }
00211 
00212 /**
00213   * @}
00214   */
00215 
00216 /** @addtogroup RNG_Exported_Functions_Group2
00217  *  @brief   Peripheral Control functions 
00218  *
00219 @verbatim   
00220  ===============================================================================
00221                       ##### Peripheral Control functions #####
00222  ===============================================================================  
00223     [..]  This section provides functions allowing to:
00224       (+) Get the 32 bit Random number
00225       (+) Get the 32 bit Random number with interrupt enabled
00226       (+) Handle RNG interrupt request 
00227 
00228 @endverbatim
00229   * @{
00230   */
00231    
00232 /**
00233   * @brief  Generates a 32-bit random number.
00234   * @note   Each time the random number data is read the RNG_FLAG_DRDY flag 
00235   *         is automatically cleared.
00236   * @param  hrng pointer to a RNG_HandleTypeDef structure that contains
00237   *                the configuration information for RNG.
00238   * @param  random32bit pointer to generated random number variable if successful.
00239   * @retval HAL status
00240   */
00241 
00242 HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber(RNG_HandleTypeDef *hrng, uint32_t *random32bit)
00243 {
00244   uint32_t tickstart = 0U;    
00245   HAL_StatusTypeDef status = HAL_OK;
00246 
00247   /* Process Locked */
00248   __HAL_LOCK(hrng); 
00249   
00250   /* Check RNG peripheral state */
00251   if(hrng->State == HAL_RNG_STATE_READY)
00252   {
00253     /* Change RNG peripheral state */  
00254     hrng->State = HAL_RNG_STATE_BUSY;  
00255 
00256     /* Get tick */
00257     tickstart = HAL_GetTick();
00258   
00259     /* Check if data register contains valid random data */
00260     while(__HAL_RNG_GET_FLAG(hrng, RNG_FLAG_DRDY) == RESET)
00261     {
00262       if((HAL_GetTick() - tickstart ) > RNG_TIMEOUT_VALUE)
00263       {    
00264         hrng->State = HAL_RNG_STATE_ERROR;
00265 
00266         /* Process Unlocked */
00267         __HAL_UNLOCK(hrng);
00268       
00269         return HAL_TIMEOUT;
00270       } 
00271     }
00272   
00273     /* Get a 32bit Random number */
00274     hrng->RandomNumber = hrng->Instance->DR;
00275     *random32bit = hrng->RandomNumber;
00276   
00277     hrng->State = HAL_RNG_STATE_READY;
00278   }
00279   else
00280   {
00281     status = HAL_ERROR;
00282   }
00283   
00284   /* Process Unlocked */
00285   __HAL_UNLOCK(hrng);
00286   
00287   return status;
00288 }
00289 
00290 /**
00291   * @brief  Generates a 32-bit random number in interrupt mode.
00292   * @param  hrng pointer to a RNG_HandleTypeDef structure that contains
00293   *                the configuration information for RNG.
00294   * @retval HAL status
00295   */
00296 HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber_IT(RNG_HandleTypeDef *hrng)
00297 {
00298   HAL_StatusTypeDef status = HAL_OK;
00299   
00300   /* Process Locked */
00301   __HAL_LOCK(hrng);
00302   
00303   /* Check RNG peripheral state */
00304   if(hrng->State == HAL_RNG_STATE_READY)
00305   {
00306     /* Change RNG peripheral state */  
00307     hrng->State = HAL_RNG_STATE_BUSY;  
00308   
00309     /* Process Unlocked */
00310     __HAL_UNLOCK(hrng);
00311     
00312     /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */ 
00313     __HAL_RNG_ENABLE_IT(hrng);
00314   }
00315   else
00316   {
00317     /* Process Unlocked */
00318     __HAL_UNLOCK(hrng);
00319     
00320     status = HAL_ERROR;
00321   }
00322   
00323   return status;
00324 }
00325 
00326 /**
00327   * @brief  Handles RNG interrupt request.
00328   * @note   In the case of a clock error, the RNG is no more able to generate 
00329   *         random numbers because the PLL48CLK clock is not correct. User has 
00330   *         to check that the clock controller is correctly configured to provide
00331   *         the RNG clock and clear the CEIS bit using __HAL_RNG_CLEAR_IT(). 
00332   *         The clock error has no impact on the previously generated 
00333   *         random numbers, and the RNG_DR register contents can be used.
00334   * @note   In the case of a seed error, the generation of random numbers is 
00335   *         interrupted as long as the SECS bit is '1'. If a number is 
00336   *         available in the RNG_DR register, it must not be used because it may 
00337   *         not have enough entropy. In this case, it is recommended to clear the 
00338   *         SEIS bit using __HAL_RNG_CLEAR_IT(), then disable and enable 
00339   *         the RNG peripheral to reinitialize and restart the RNG.
00340   * @note   User-written HAL_RNG_ErrorCallback() API is called once whether SEIS
00341   *         or CEIS are set.  
00342   * @param  hrng pointer to a RNG_HandleTypeDef structure that contains
00343   *                the configuration information for RNG.
00344   * @retval None
00345 
00346   */
00347 void HAL_RNG_IRQHandler(RNG_HandleTypeDef *hrng)
00348 {
00349   /* RNG clock error interrupt occurred */
00350   if((__HAL_RNG_GET_IT(hrng, RNG_IT_CEI) != RESET) ||  (__HAL_RNG_GET_IT(hrng, RNG_IT_SEI) != RESET))
00351   { 
00352     /* Change RNG peripheral state */
00353     hrng->State = HAL_RNG_STATE_ERROR;
00354   
00355     HAL_RNG_ErrorCallback(hrng);
00356     
00357     /* Clear the clock error flag */
00358     __HAL_RNG_CLEAR_IT(hrng, RNG_IT_CEI|RNG_IT_SEI);
00359     
00360   }
00361   
00362   /* Check RNG data ready interrupt occurred */    
00363   if(__HAL_RNG_GET_IT(hrng, RNG_IT_DRDY) != RESET)
00364   {
00365     /* Generate random number once, so disable the IT */
00366     __HAL_RNG_DISABLE_IT(hrng);
00367     
00368     /* Get the 32bit Random number (DRDY flag automatically cleared) */ 
00369     hrng->RandomNumber = hrng->Instance->DR;
00370     
00371     if(hrng->State != HAL_RNG_STATE_ERROR)
00372     {
00373       /* Change RNG peripheral state */
00374       hrng->State = HAL_RNG_STATE_READY; 
00375       
00376       /* Data Ready callback */ 
00377       HAL_RNG_ReadyDataCallback(hrng, hrng->RandomNumber);
00378     } 
00379   }
00380 } 
00381 
00382 /**
00383   * @brief  Returns generated random number in polling mode (Obsolete)
00384   *         Use HAL_RNG_GenerateRandomNumber() API instead.
00385   * @param  hrng pointer to a RNG_HandleTypeDef structure that contains
00386   *                the configuration information for RNG.
00387   * @retval Random value
00388   */
00389 uint32_t HAL_RNG_GetRandomNumber(RNG_HandleTypeDef *hrng)
00390 {
00391   if(HAL_RNG_GenerateRandomNumber(hrng, &(hrng->RandomNumber)) == HAL_OK)
00392   {
00393     return hrng->RandomNumber; 
00394   }
00395   else
00396   {
00397     return 0U;
00398   }
00399 }
00400 
00401 /**
00402   * @brief  Returns a 32-bit random number with interrupt enabled (Obsolete),
00403   *         Use HAL_RNG_GenerateRandomNumber_IT() API instead.
00404   * @param  hrng pointer to a RNG_HandleTypeDef structure that contains
00405   *                the configuration information for RNG.
00406   * @retval 32-bit random number
00407   */
00408 uint32_t HAL_RNG_GetRandomNumber_IT(RNG_HandleTypeDef *hrng)
00409 {
00410   uint32_t random32bit = 0U;
00411   
00412   /* Process locked */
00413   __HAL_LOCK(hrng);
00414   
00415   /* Change RNG peripheral state */  
00416   hrng->State = HAL_RNG_STATE_BUSY;  
00417   
00418   /* Get a 32bit Random number */ 
00419   random32bit = hrng->Instance->DR;
00420   
00421   /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */ 
00422   __HAL_RNG_ENABLE_IT(hrng); 
00423   
00424   /* Return the 32 bit random number */   
00425   return random32bit;
00426 }
00427 
00428 /**
00429   * @brief  Read latest generated random number. 
00430   * @param  hrng pointer to a RNG_HandleTypeDef structure that contains
00431   *                the configuration information for RNG.
00432   * @retval random value
00433   */
00434 uint32_t HAL_RNG_ReadLastRandomNumber(RNG_HandleTypeDef *hrng)
00435 {
00436   return(hrng->RandomNumber);
00437 }
00438 
00439 /**
00440   * @brief  Data Ready callback in non-blocking mode. 
00441   * @param  hrng pointer to a RNG_HandleTypeDef structure that contains
00442   *                the configuration information for RNG.
00443   * @param  random32bit generated random number.
00444   * @retval None
00445   */
00446 __weak void HAL_RNG_ReadyDataCallback(RNG_HandleTypeDef *hrng, uint32_t random32bit)
00447 {
00448   /* Prevent unused argument(s) compilation warning */
00449   UNUSED(hrng);
00450   UNUSED(random32bit);
00451   /* NOTE : This function should not be modified. When the callback is needed,
00452             function HAL_RNG_ReadyDataCallback must be implemented in the user file.
00453    */
00454 }
00455 
00456 /**
00457   * @brief  RNG error callbacks.
00458   * @param  hrng pointer to a RNG_HandleTypeDef structure that contains
00459   *                the configuration information for RNG.
00460   * @retval None
00461   */
00462 __weak void HAL_RNG_ErrorCallback(RNG_HandleTypeDef *hrng)
00463 {
00464   /* Prevent unused argument(s) compilation warning */
00465   UNUSED(hrng);
00466   /* NOTE : This function should not be modified. When the callback is needed,
00467             function HAL_RNG_ErrorCallback must be implemented in the user file.
00468    */
00469 }
00470 /**
00471   * @}
00472   */ 
00473 
00474   
00475 /** @addtogroup RNG_Exported_Functions_Group3
00476  *  @brief   Peripheral State functions 
00477  *
00478 @verbatim   
00479  ===============================================================================
00480                       ##### Peripheral State functions #####
00481  ===============================================================================  
00482     [..]
00483     This subsection permits to get in run-time the status of the peripheral 
00484     and the data flow.
00485 
00486 @endverbatim
00487   * @{
00488   */
00489   
00490 /**
00491   * @brief  Returns the RNG state.
00492   * @param  hrng pointer to a RNG_HandleTypeDef structure that contains
00493   *                the configuration information for RNG.
00494   * @retval HAL state
00495   */
00496 HAL_RNG_StateTypeDef HAL_RNG_GetState(RNG_HandleTypeDef *hrng)
00497 {
00498   return hrng->State;
00499 }
00500 
00501 /**
00502   * @}
00503   */
00504   
00505 /**
00506   * @}
00507   */
00508 
00509 #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx ||\
00510           STM32F429xx || STM32F439xx || STM32F410xx || STM32F469xx || STM32F479xx || STM32F412Zx ||\
00511           STM32F412Vx || STM32F412Rx || STM32F412Cx || STM32F413xx || STM32F423xx */
00512 
00513 #endif /* HAL_RNG_MODULE_ENABLED */
00514 
00515 /**
00516   * @}
00517   */
00518 
00519 /**
00520   * @}
00521   */
00522 
00523 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/