mbed library sources

Fork of mbed-src by mbed official

Revision:
532:fe11edbda85c
Parent:
384:ef87175507f1
Child:
613:bc40b8d2aec4
--- a/targets/cmsis/TARGET_STM/TARGET_STM32F4/stm32f4xx_hal_rng.c	Thu Apr 30 13:00:08 2015 +0100
+++ b/targets/cmsis/TARGET_STM/TARGET_STM32F4/stm32f4xx_hal_rng.c	Thu Apr 30 13:45:11 2015 +0100
@@ -2,8 +2,8 @@
   ******************************************************************************
   * @file    stm32f4xx_hal_rng.c
   * @author  MCD Application Team
-  * @version V1.1.0
-  * @date    19-June-2014
+  * @version V1.3.0
+  * @date    09-March-2015
   * @brief   RNG HAL module driver.
   *          This file provides firmware functions to manage the following 
   *          functionalities of the Random Number Generator (RNG) peripheral:
@@ -15,20 +15,21 @@
   ==============================================================================
                      ##### How to use this driver #####
   ==============================================================================
-    [..]
+  [..]
       The RNG HAL driver can be used as follows:
 
-         (#) Enable the RNG controller clock using __RNG_CLK_ENABLE() macro.
-         (#) Activate the RNG peripheral using __HAL_RNG_ENABLE() macro.
-         (#) Wait until the 32 bit Random Number Generator contains a valid 
-             random data using (polling/interrupt) mode.   
-         (#) Get the 32 bit random number using HAL_RNG_GetRandomNumber() function.
+      (#) Enable the RNG controller clock using __HAL_RCC_RNG_CLK_ENABLE() macro 
+          in HAL_RNG_MspInit().
+      (#) Activate the RNG peripheral using HAL_RNG_Init() function.
+      (#) Wait until the 32 bit Random Number Generator contains a valid 
+          random data using (polling/interrupt) mode.   
+      (#) Get the 32 bit random number using HAL_RNG_GenerateRandomNumber() function.
   
   @endverbatim
   ******************************************************************************
   * @attention
   *
-  * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
+  * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
   *
   * Redistribution and use in source and binary forms, with or without modification,
   * are permitted provided that the following conditions are met:
@@ -62,8 +63,7 @@
   * @{
   */
 
-/** @defgroup RNG 
-  * @brief RNG HAL module driver.
+/** @addtogroup RNG 
   * @{
   */
 
@@ -72,22 +72,31 @@
 #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) ||\
     defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)
 
-/* Private typedef -----------------------------------------------------------*/
-/* Private define ------------------------------------------------------------*/
-#define RNG_TIMEOUT_VALUE     1000
-/* Private macro -------------------------------------------------------------*/
+
+/* Private types -------------------------------------------------------------*/
+/* Private defines -----------------------------------------------------------*/
 /* Private variables ---------------------------------------------------------*/
-/* Private function prototypes -----------------------------------------------*/
+/* Private constants ---------------------------------------------------------*/
+/** @addtogroup RNG_Private_Constants
+  * @{
+  */
+#define RNG_TIMEOUT_VALUE     2
+/**
+  * @}
+  */ 
+/* Private macros ------------------------------------------------------------*/
+/* Private functions prototypes ----------------------------------------------*/
 /* Private functions ---------------------------------------------------------*/
+/* Exported functions --------------------------------------------------------*/
 
-/** @defgroup RNG_Private_Functions
+/** @addtogroup RNG_Exported_Functions
   * @{
   */
 
-/** @defgroup RNG_Group1 Initialization and de-initialization functions 
- *  @brief    Initialization and Configuration functions. 
+/** @addtogroup RNG_Exported_Functions_Group1
+ *  @brief   Initialization and de-initialization functions
  *
-@verbatim    
+@verbatim
  ===============================================================================
           ##### Initialization and de-initialization functions #####
  ===============================================================================
@@ -97,14 +106,13 @@
       (+) DeInitialize the RNG peripheral
       (+) Initialize the RNG MSP
       (+) DeInitialize RNG MSP 
- 
+
 @endverbatim
   * @{
   */
-
+  
 /**
-  * @brief  Initializes the RNG according to the specified
-  *         parameters in the RNG_InitTypeDef and creates the associated handle.
+  * @brief  Initializes the RNG peripheral and creates the associated handle.
   * @param  hrng: pointer to a RNG_HandleTypeDef structure that contains
   *                the configuration information for RNG.
   * @retval HAL status
@@ -116,12 +124,17 @@
   {
     return HAL_ERROR;
   }
-
+  
+  __HAL_LOCK(hrng);
+  
   if(hrng->State == HAL_RNG_STATE_RESET)
   {  
+    /* Allocate lock resource and initialize it */
+    hrng->Lock = HAL_UNLOCKED;
     /* Init the low level hardware */
     HAL_RNG_MspInit(hrng);
   }
+  
   /* Change RNG peripheral state */
   hrng->State = HAL_RNG_STATE_BUSY;
 
@@ -130,7 +143,9 @@
 
   /* Initialize the RNG state */
   hrng->State = HAL_RNG_STATE_READY;
-
+  
+  __HAL_UNLOCK(hrng);
+  
   /* Return function status */
   return HAL_OK;
 }
@@ -143,22 +158,16 @@
   */
 HAL_StatusTypeDef HAL_RNG_DeInit(RNG_HandleTypeDef *hrng)
 { 
-  /* Check the RNG peripheral state */
-  if(hrng->State == HAL_RNG_STATE_BUSY)
+  /* Check the RNG handle allocation */
+  if(hrng == HAL_NULL)
   {
-    return HAL_BUSY;
+    return HAL_ERROR;
   }
+  /* Disable the RNG Peripheral */
+  CLEAR_BIT(hrng->Instance->CR, RNG_CR_IE | RNG_CR_RNGEN);
   
-  /* Update the RNG state */  
-  hrng->State = HAL_RNG_STATE_BUSY;
-  
-  /* Disable the RNG Peripheral */
-  __HAL_RNG_DISABLE(hrng);
-  
-  /* Set the RNG registers to their reset values */
-  hrng->Instance->CR &= 0xFFFFFFF3;
-  hrng->Instance->SR &= 0xFFFFFF98;
-  hrng->Instance->DR &= 0x0;
+  /* Clear RNG interrupt status flags */
+  CLEAR_BIT(hrng->Instance->SR, RNG_SR_CEIS | RNG_SR_SEIS);
   
   /* DeInit the low level hardware */
   HAL_RNG_MspDeInit(hrng);
@@ -181,8 +190,8 @@
   */
 __weak void HAL_RNG_MspInit(RNG_HandleTypeDef *hrng)
 {
-  /* NOTE : This function Should not be modified, when the callback is needed,
-            the HAL_RNG_MspInit could be implemented in the user file
+  /* NOTE : This function should not be modified. When the callback is needed,
+            function HAL_RNG_MspInit must be implemented in the user file.
    */
 }
 
@@ -194,8 +203,8 @@
   */
 __weak void HAL_RNG_MspDeInit(RNG_HandleTypeDef *hrng)
 {
-  /* NOTE : This function Should not be modified, when the callback is needed,
-            the HAL_RNG_MspDeInit could be implemented in the user file
+  /* NOTE : This function should not be modified. When the callback is needed,
+            function HAL_RNG_MspDeInit must be implemented in the user file.
    */
 }
 
@@ -203,8 +212,8 @@
   * @}
   */
 
-/** @defgroup RNG_Group2 Peripheral Control functions 
- *  @brief    management functions. 
+/** @addtogroup RNG_Exported_Functions_Group2
+ *  @brief   Peripheral Control functions 
  *
 @verbatim   
  ===============================================================================
@@ -215,51 +224,182 @@
       (+) Get the 32 bit Random number with interrupt enabled
       (+) Handle RNG interrupt request 
 
-
 @endverbatim
   * @{
   */
-
+   
 /**
-  * @brief  Returns a 32-bit random number.
+  * @brief  Generates a 32-bit random number.
   * @note   Each time the random number data is read the RNG_FLAG_DRDY flag 
   *         is automatically cleared.
   * @param  hrng: pointer to a RNG_HandleTypeDef structure that contains
   *                the configuration information for RNG.
-  * @retval 32-bit random number
+  * @param  random32bit: pointer to generated random number variable if successful.
+  * @retval HAL status
   */
-uint32_t HAL_RNG_GetRandomNumber(RNG_HandleTypeDef *hrng)
+
+HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber(RNG_HandleTypeDef *hrng, uint32_t *random32bit)
 {
-  uint32_t random32bit = 0;
   uint32_t tickstart = 0;    
-  
+  HAL_StatusTypeDef status = HAL_OK;
+
   /* Process Locked */
   __HAL_LOCK(hrng); 
   
-  /* Get tick */
-  tickstart = HAL_GetTick();
-  
-  /* Check if data register contains valid random data */
-  while(__HAL_RNG_GET_FLAG(hrng, RNG_FLAG_DRDY) == RESET)
+  /* Check RNG peripheral state */
+  if(hrng->State == HAL_RNG_STATE_READY)
   {
-    if((HAL_GetTick() - tickstart ) > RNG_TIMEOUT_VALUE)
-    {    
-      return HAL_TIMEOUT;
-    } 
+    /* Change RNG peripheral state */  
+    hrng->State = HAL_RNG_STATE_BUSY;  
+
+    /* Get tick */
+    tickstart = HAL_GetTick();
+  
+    /* Check if data register contains valid random data */
+    while(__HAL_RNG_GET_FLAG(hrng, RNG_FLAG_DRDY) == RESET)
+    {
+      if((HAL_GetTick() - tickstart ) > RNG_TIMEOUT_VALUE)
+      {    
+        hrng->State = HAL_RNG_STATE_ERROR;
+
+        /* Process Unlocked */
+        __HAL_UNLOCK(hrng);
+      
+        return HAL_TIMEOUT;
+      } 
+    }
+  
+    /* Get a 32bit Random number */
+    hrng->RandomNumber = hrng->Instance->DR;
+    *random32bit = hrng->RandomNumber;
+  
+    hrng->State = HAL_RNG_STATE_READY;
   }
-  
-  /* Get a 32bit Random number */ 
-  random32bit = hrng->Instance->DR;
+  else
+  {
+    status = HAL_ERROR;
+  }
   
   /* Process Unlocked */
   __HAL_UNLOCK(hrng);
   
-  /* Return the 32 bit random number */   
-  return random32bit;
+  return status;
+}
+
+/**
+  * @brief  Generates a 32-bit random number in interrupt mode.
+  * @param  hrng: pointer to a RNG_HandleTypeDef structure that contains
+  *                the configuration information for RNG.
+  * @retval HAL status
+  */
+HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber_IT(RNG_HandleTypeDef *hrng)
+{
+  HAL_StatusTypeDef status = HAL_OK;
+  
+  /* Process Locked */
+  __HAL_LOCK(hrng);
+  
+  /* Check RNG peripheral state */
+  if(hrng->State == HAL_RNG_STATE_READY)
+  {
+    /* Change RNG peripheral state */  
+    hrng->State = HAL_RNG_STATE_BUSY;  
+  
+    /* Process Unlocked */
+    __HAL_UNLOCK(hrng);
+    
+    /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */ 
+    __HAL_RNG_ENABLE_IT(hrng);
+  }
+  else
+  {
+    /* Process Unlocked */
+    __HAL_UNLOCK(hrng);
+    
+    status = HAL_ERROR;
+  }
+  
+  return status;
 }
 
 /**
-  * @brief  Returns a 32-bit random number with interrupt enabled.
+  * @brief  Handles RNG interrupt request.
+  * @note   In the case of a clock error, the RNG is no more able to generate 
+  *         random numbers because the PLL48CLK clock is not correct. User has 
+  *         to check that the clock controller is correctly configured to provide
+  *         the RNG clock and clear the CEIS bit using __HAL_RNG_CLEAR_IT(). 
+  *         The clock error has no impact on the previously generated 
+  *         random numbers, and the RNG_DR register contents can be used.
+  * @note   In the case of a seed error, the generation of random numbers is 
+  *         interrupted as long as the SECS bit is '1'. If a number is 
+  *         available in the RNG_DR register, it must not be used because it may 
+  *         not have enough entropy. In this case, it is recommended to clear the 
+  *         SEIS bit using __HAL_RNG_CLEAR_IT(), then disable and enable 
+  *         the RNG peripheral to reinitialize and restart the RNG.
+  * @note   User-written HAL_RNG_ErrorCallback() API is called once whether SEIS
+  *         or CEIS are set.  
+  * @param  hrng: pointer to a RNG_HandleTypeDef structure that contains
+  *                the configuration information for RNG.
+  * @retval None
+
+  */
+void HAL_RNG_IRQHandler(RNG_HandleTypeDef *hrng)
+{
+  /* RNG clock error interrupt occurred */
+  if((__HAL_RNG_GET_IT(hrng, RNG_IT_CEI) != RESET) ||  (__HAL_RNG_GET_IT(hrng, RNG_IT_SEI) != RESET))
+  { 
+    /* Change RNG peripheral state */
+    hrng->State = HAL_RNG_STATE_ERROR;
+  
+    HAL_RNG_ErrorCallback(hrng);
+    
+    /* Clear the clock error flag */
+    __HAL_RNG_CLEAR_IT(hrng, RNG_IT_CEI|RNG_IT_SEI);
+    
+  }
+  
+  /* Check RNG data ready interrupt occurred */    
+  if(__HAL_RNG_GET_IT(hrng, RNG_IT_DRDY) != RESET)
+  {
+    /* Generate random number once, so disable the IT */
+    __HAL_RNG_DISABLE_IT(hrng);
+    
+    /* Get the 32bit Random number (DRDY flag automatically cleared) */ 
+    hrng->RandomNumber = hrng->Instance->DR;
+    
+    if(hrng->State != HAL_RNG_STATE_ERROR)
+    {
+      /* Change RNG peripheral state */
+      hrng->State = HAL_RNG_STATE_READY; 
+      
+      /* Data Ready callback */ 
+      HAL_RNG_ReadyDataCallback(hrng, hrng->RandomNumber);
+    } 
+  }
+} 
+
+/**
+  * @brief  Returns generated random number in polling mode (Obsolete)
+  *         Use HAL_RNG_GenerateRandomNumber() API instead.
+  * @param  hrng: pointer to a RNG_HandleTypeDef structure that contains
+  *                the configuration information for RNG.
+  * @retval Random value
+  */
+uint32_t HAL_RNG_GetRandomNumber(RNG_HandleTypeDef *hrng)
+{
+  if(HAL_RNG_GenerateRandomNumber(hrng, &(hrng->RandomNumber)) == HAL_OK)
+  {
+    return hrng->RandomNumber; 
+  }
+  else
+  {
+    return 0;
+  }
+}
+
+/**
+  * @brief  Returns a 32-bit random number with interrupt enabled (Obsolete),
+  *         Use HAL_RNG_GenerateRandomNumber_IT() API instead.
   * @param  hrng: pointer to a RNG_HandleTypeDef structure that contains
   *                the configuration information for RNG.
   * @retval 32-bit random number
@@ -268,7 +408,7 @@
 {
   uint32_t random32bit = 0;
   
-  /* Process Locked */
+  /* Process locked */
   __HAL_LOCK(hrng);
   
   /* Change RNG peripheral state */  
@@ -285,84 +425,27 @@
 }
 
 /**
-  * @brief  Handles RNG interrupt request.
-  * @note   In the case of a clock error, the RNG is no more able to generate 
-  *         random numbers because the PLL48CLK clock is not correct. User has 
-  *         to check that the clock controller is correctly configured to provide
-  *         the RNG clock and clear the CEIS bit using __HAL_RNG_CLEAR_FLAG(). 
-  *         The clock error has no impact on the previously generated 
-  *         random numbers, and the RNG_DR register contents can be used.
-  * @note   In the case of a seed error, the generation of random numbers is 
-  *         interrupted as long as the SECS bit is '1'. If a number is 
-  *         available in the RNG_DR register, it must not be used because it may 
-  *         not have enough entropy. In this case, it is recommended to clear the 
-  *         SEIS bit using __HAL_RNG_CLEAR_FLAG(), then disable and enable 
-  *         the RNG peripheral to reinitialize and restart the RNG.
+  * @brief  Read latest generated random number. 
   * @param  hrng: pointer to a RNG_HandleTypeDef structure that contains
   *                the configuration information for RNG.
-  * @retval None
-
+  * @retval random value
   */
-void HAL_RNG_IRQHandler(RNG_HandleTypeDef *hrng)
+uint32_t HAL_RNG_ReadLastRandomNumber(RNG_HandleTypeDef *hrng)
 {
-  /* RNG clock error interrupt occurred */
-  if(__HAL_RNG_GET_FLAG(hrng, RNG_IT_CEI) != RESET)
-  { 
-    HAL_RNG_ErrorCallback(hrng);
-    
-    /* Clear the clock error flag */
-    __HAL_RNG_CLEAR_FLAG(hrng, RNG_IT_CEI);
-    
-    /* Change RNG peripheral state */
-    hrng->State = HAL_RNG_STATE_ERROR;
-    
-    /* Process Unlocked */
-    __HAL_UNLOCK(hrng);
-  }
-  
-  /* RNG seed error interrupt occurred */
-  if(__HAL_RNG_GET_FLAG(hrng, RNG_IT_SEI) != RESET)
-  { 
-    HAL_RNG_ErrorCallback(hrng);
-    
-    /* Clear the seed error flag */
-    __HAL_RNG_CLEAR_FLAG(hrng, RNG_IT_SEI);
-    
-    /* Change RNG peripheral state */
-    hrng->State = HAL_RNG_STATE_ERROR;
-
-    /* Process Unlocked */
-    __HAL_UNLOCK(hrng);    
-  }
-  
-  /* Check RNG data ready flag */    
-  if(__HAL_RNG_GET_FLAG(hrng, RNG_FLAG_DRDY) != RESET)
-  {
-    /* Data Ready callback */ 
-    HAL_RNG_ReadyCallback(hrng);
-    
-    /* Change RNG peripheral state */
-    hrng->State = HAL_RNG_STATE_READY; 
-      
-    /* Clear the RNG Data Ready flag */
-    __HAL_RNG_CLEAR_FLAG(hrng, RNG_FLAG_DRDY);
-    
-    /* Process Unlocked */
-    __HAL_UNLOCK(hrng);
-  }
-} 
+  return(hrng->RandomNumber);
+}
 
 /**
   * @brief  Data Ready callback in non-blocking mode. 
   * @param  hrng: pointer to a RNG_HandleTypeDef structure that contains
   *                the configuration information for RNG.
+  * @param  random32bit: generated random number.
   * @retval None
   */
-
-__weak void HAL_RNG_ReadyCallback(RNG_HandleTypeDef* hrng)
+__weak void HAL_RNG_ReadyDataCallback(RNG_HandleTypeDef *hrng, uint32_t random32bit)
 {
-  /* NOTE : This function Should not be modified, when the callback is needed,
-            the HAL_RNG_ReadyCallback could be implemented in the user file
+  /* NOTE : This function should not be modified. When the callback is needed,
+            function HAL_RNG_ReadyDataCallback must be implemented in the user file.
    */
 }
 
@@ -374,17 +457,17 @@
   */
 __weak void HAL_RNG_ErrorCallback(RNG_HandleTypeDef *hrng)
 {
-  /* NOTE : This function Should not be modified, when the callback is needed,
-            the HAL_RNG_ErrorCallback could be implemented in the user file
-   */ 
+  /* NOTE : This function should not be modified. When the callback is needed,
+            function HAL_RNG_ErrorCallback must be implemented in the user file.
+   */
 }
- 
 /**
   * @}
-  */
+  */ 
 
-/** @defgroup RNG_Group3 Peripheral State functions 
- *  @brief    Peripheral State functions. 
+  
+/** @addtogroup RNG_Exported_Functions_Group3
+ *  @brief   Peripheral State functions 
  *
 @verbatim   
  ===============================================================================
@@ -397,7 +480,7 @@
 @endverbatim
   * @{
   */
-
+  
 /**
   * @brief  Returns the RNG state.
   * @param  hrng: pointer to a RNG_HandleTypeDef structure that contains
@@ -412,7 +495,7 @@
 /**
   * @}
   */
-
+  
 /**
   * @}
   */
@@ -420,6 +503,7 @@
 #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx */
 
 #endif /* HAL_RNG_MODULE_ENABLED */
+
 /**
   * @}
   */