STM32L443xx HAL User Manual
stm32l4xx_hal_rng.h
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32l4xx_hal_rng.h
00004   * @author  MCD Application Team
00005   * @brief   Header file of RNG HAL module.
00006   ******************************************************************************
00007   * @attention
00008   *
00009   * Copyright (c) 2017 STMicroelectronics.
00010   * All rights reserved.
00011   *
00012   * This software is licensed under terms that can be found in the LICENSE file
00013   * in the root directory of this software component.
00014   * If no LICENSE file comes with this software, it is provided AS-IS.
00015   *
00016   ******************************************************************************
00017   */
00018 
00019 /* Define to prevent recursive inclusion -------------------------------------*/
00020 #ifndef STM32L4xx_HAL_RNG_H
00021 #define STM32L4xx_HAL_RNG_H
00022 
00023 #ifdef __cplusplus
00024  extern "C" {
00025 #endif
00026 
00027 /* Includes ------------------------------------------------------------------*/
00028 #include "stm32l4xx_hal_def.h"
00029 
00030 /** @addtogroup STM32L4xx_HAL_Driver
00031   * @{
00032   */
00033 
00034 #if defined (RNG)
00035 
00036 /** @defgroup RNG RNG
00037   * @brief RNG HAL module driver
00038   * @{
00039   */
00040 
00041 /* Exported types ------------------------------------------------------------*/
00042 
00043 /** @defgroup RNG_Exported_Types RNG Exported Types
00044   * @{
00045   */
00046 
00047 /** @defgroup RNG_Exported_Types_Group1 RNG Init Structure definition
00048   * @{
00049   */
00050 #if defined(RNG_CR_CED)
00051 typedef struct
00052 {
00053   uint32_t                    ClockErrorDetection; /*!< CED Clock error detection */
00054 } RNG_InitTypeDef;
00055 #endif /* defined(RNG_CR_CED) */
00056 
00057 /**
00058   * @}
00059   */
00060 
00061 /** @defgroup RNG_Exported_Types_Group2 RNG State Structure definition
00062   * @{
00063   */
00064 typedef enum
00065 {
00066   HAL_RNG_STATE_RESET     = 0x00U,  /*!< RNG not yet initialized or disabled */
00067   HAL_RNG_STATE_READY     = 0x01U,  /*!< RNG initialized and ready for use   */
00068   HAL_RNG_STATE_BUSY      = 0x02U,  /*!< RNG internal process is ongoing     */
00069   HAL_RNG_STATE_TIMEOUT   = 0x03U,  /*!< RNG timeout state                   */
00070   HAL_RNG_STATE_ERROR     = 0x04U   /*!< RNG error state                     */
00071 
00072 } HAL_RNG_StateTypeDef;
00073 
00074 /**
00075   * @}
00076   */
00077 
00078 /** @defgroup RNG_Exported_Types_Group3 RNG Handle Structure definition
00079   * @{
00080   */
00081 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
00082 typedef struct  __RNG_HandleTypeDef
00083 #else
00084 typedef struct
00085 #endif /* (USE_HAL_RNG_REGISTER_CALLBACKS) */
00086 {
00087   RNG_TypeDef                 *Instance;    /*!< Register base address   */
00088 #if defined(RNG_CR_CED)
00089 
00090   RNG_InitTypeDef             Init;         /*!< RNG configuration parameters */
00091 #endif /* defined(RNG_CR_CED) */
00092 
00093   HAL_LockTypeDef             Lock;         /*!< RNG locking object      */
00094 
00095   __IO HAL_RNG_StateTypeDef   State;        /*!< RNG communication state */
00096 
00097   __IO  uint32_t              ErrorCode;     /*!< RNG Error code               */
00098 
00099   uint32_t                    RandomNumber; /*!< Last Generated RNG Data */
00100 
00101 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
00102   void (* ReadyDataCallback)(struct __RNG_HandleTypeDef *hrng, uint32_t random32bit);  /*!< RNG Data Ready Callback    */
00103   void (* ErrorCallback)(struct __RNG_HandleTypeDef *hrng);                            /*!< RNG Error Callback         */
00104 
00105   void (* MspInitCallback)(struct __RNG_HandleTypeDef *hrng);                          /*!< RNG Msp Init callback      */
00106   void (* MspDeInitCallback)(struct __RNG_HandleTypeDef *hrng);                        /*!< RNG Msp DeInit callback    */
00107 #endif  /* USE_HAL_RNG_REGISTER_CALLBACKS */
00108 
00109 } RNG_HandleTypeDef;
00110 
00111 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
00112 /**
00113   * @brief  HAL RNG Callback ID enumeration definition
00114   */
00115 typedef enum
00116 {
00117   HAL_RNG_ERROR_CB_ID                   = 0x00U,     /*!< RNG Error Callback ID          */
00118 
00119   HAL_RNG_MSPINIT_CB_ID                 = 0x01U,     /*!< RNG MspInit callback ID        */
00120   HAL_RNG_MSPDEINIT_CB_ID               = 0x02U      /*!< RNG MspDeInit callback ID      */
00121 
00122 } HAL_RNG_CallbackIDTypeDef;
00123 
00124 /**
00125   * @brief  HAL RNG Callback pointer definition
00126   */
00127 typedef  void (*pRNG_CallbackTypeDef)(RNG_HandleTypeDef *hrng);                                  /*!< pointer to a common RNG callback function */
00128 typedef  void (*pRNG_ReadyDataCallbackTypeDef)(RNG_HandleTypeDef *hrng, uint32_t random32bit);   /*!< pointer to an RNG Data Ready specific callback function */
00129 
00130 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
00131 
00132 /**
00133   * @}
00134   */
00135 
00136 /**
00137   * @}
00138   */
00139 
00140 /* Exported constants --------------------------------------------------------*/
00141 /** @defgroup RNG_Exported_Constants RNG Exported Constants
00142   * @{
00143   */
00144 
00145 /** @defgroup RNG_Exported_Constants_Group1 RNG Interrupt definition
00146   * @{
00147   */
00148 #define RNG_IT_DRDY  RNG_SR_DRDY  /*!< Data Ready interrupt  */
00149 #define RNG_IT_CEI   RNG_SR_CEIS  /*!< Clock error interrupt */
00150 #define RNG_IT_SEI   RNG_SR_SEIS  /*!< Seed error interrupt  */
00151 /**
00152   * @}
00153   */
00154 
00155 /** @defgroup RNG_Exported_Constants_Group2 RNG Flag definition
00156   * @{
00157   */
00158 #define RNG_FLAG_DRDY   RNG_SR_DRDY  /*!< Data ready                 */
00159 #define RNG_FLAG_CECS   RNG_SR_CECS  /*!< Clock error current status */
00160 #define RNG_FLAG_SECS   RNG_SR_SECS  /*!< Seed error current status  */
00161 /**
00162   * @}
00163   */
00164 
00165 #if defined(RNG_CR_CED)
00166 /** @defgroup RNG_Exported_Constants_Group3 RNG Clock Error Detection
00167   * @{
00168   */
00169 #define RNG_CED_ENABLE          0x00000000U /*!< Clock error detection Enabled  */
00170 #define RNG_CED_DISABLE         RNG_CR_CED  /*!< Clock error detection Disabled */
00171 /**
00172   * @}
00173   */
00174 
00175 #endif /* defined(RNG_CR_CED) */
00176 /** @defgroup RNG_Error_Definition   RNG Error Definition
00177   * @{
00178   */
00179 #define  HAL_RNG_ERROR_NONE             0x00000000U    /*!< No error             */
00180 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
00181 #define  HAL_RNG_ERROR_INVALID_CALLBACK 0x00000001U    /*!< Invalid Callback error  */
00182 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
00183 #define  HAL_RNG_ERROR_TIMEOUT          0x00000002U    /*!< Timeout error        */
00184 #define  HAL_RNG_ERROR_BUSY             0x00000004U    /*!< Busy error        */
00185 #define  HAL_RNG_ERROR_SEED             0x00000008U    /*!< Seed error        */
00186 #define  HAL_RNG_ERROR_CLOCK            0x00000010U   /*!< Clock error        */
00187 /**
00188   * @}
00189   */
00190 
00191 /**
00192   * @}
00193   */
00194 
00195 /* Exported macros -----------------------------------------------------------*/
00196 /** @defgroup RNG_Exported_Macros RNG Exported Macros
00197   * @{
00198   */
00199 
00200 /** @brief Reset RNG handle state
00201   * @param  __HANDLE__ RNG Handle
00202   * @retval None
00203   */
00204 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
00205 #define __HAL_RNG_RESET_HANDLE_STATE(__HANDLE__)  do{                                                   \
00206                                                        (__HANDLE__)->State = HAL_RNG_STATE_RESET;       \
00207                                                        (__HANDLE__)->MspInitCallback = NULL;            \
00208                                                        (__HANDLE__)->MspDeInitCallback = NULL;          \
00209                                                     } while(0U)
00210 #else
00211 #define __HAL_RNG_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_RNG_STATE_RESET)
00212 #endif /*USE_HAL_RNG_REGISTER_CALLBACKS */
00213 
00214 /**
00215   * @brief  Enables the RNG peripheral.
00216   * @param  __HANDLE__ RNG Handle
00217   * @retval None
00218   */
00219 #define __HAL_RNG_ENABLE(__HANDLE__) ((__HANDLE__)->Instance->CR |=  RNG_CR_RNGEN)
00220 
00221 /**
00222   * @brief  Disables the RNG peripheral.
00223   * @param  __HANDLE__ RNG Handle
00224   * @retval None
00225   */
00226 #define __HAL_RNG_DISABLE(__HANDLE__) ((__HANDLE__)->Instance->CR &= ~RNG_CR_RNGEN)
00227 
00228 /**
00229   * @brief  Check the selected RNG flag status.
00230   * @param  __HANDLE__ RNG Handle
00231   * @param  __FLAG__ RNG flag
00232   *          This parameter can be one of the following values:
00233   *            @arg RNG_FLAG_DRDY:  Data ready
00234   *            @arg RNG_FLAG_CECS:  Clock error current status
00235   *            @arg RNG_FLAG_SECS:  Seed error current status
00236   * @retval The new state of __FLAG__ (SET or RESET).
00237   */
00238 #define __HAL_RNG_GET_FLAG(__HANDLE__, __FLAG__) (((__HANDLE__)->Instance->SR & (__FLAG__)) == (__FLAG__))
00239 
00240 /**
00241   * @brief  Clears the selected RNG flag status.
00242   * @param  __HANDLE__ RNG handle
00243   * @param  __FLAG__ RNG flag to clear
00244   * @note   WARNING: This is a dummy macro for HAL code alignment,
00245   *         flags RNG_FLAG_DRDY, RNG_FLAG_CECS and RNG_FLAG_SECS are read-only.
00246   * @retval None
00247   */
00248 #define __HAL_RNG_CLEAR_FLAG(__HANDLE__, __FLAG__)                      /* dummy  macro */
00249 
00250 /**
00251   * @brief  Enables the RNG interrupts.
00252   * @param  __HANDLE__ RNG Handle
00253   * @retval None
00254   */
00255 #define __HAL_RNG_ENABLE_IT(__HANDLE__) ((__HANDLE__)->Instance->CR |=  RNG_CR_IE)
00256 
00257 /**
00258   * @brief  Disables the RNG interrupts.
00259   * @param  __HANDLE__ RNG Handle
00260   * @retval None
00261   */
00262 #define __HAL_RNG_DISABLE_IT(__HANDLE__) ((__HANDLE__)->Instance->CR &= ~RNG_CR_IE)
00263 
00264 /**
00265   * @brief  Checks whether the specified RNG interrupt has occurred or not.
00266   * @param  __HANDLE__ RNG Handle
00267   * @param  __INTERRUPT__ specifies the RNG interrupt status flag to check.
00268   *         This parameter can be one of the following values:
00269   *            @arg RNG_IT_DRDY: Data ready interrupt
00270   *            @arg RNG_IT_CEI: Clock error interrupt
00271   *            @arg RNG_IT_SEI: Seed error interrupt
00272   * @retval The new state of __INTERRUPT__ (SET or RESET).
00273   */
00274 #define __HAL_RNG_GET_IT(__HANDLE__, __INTERRUPT__) (((__HANDLE__)->Instance->SR & (__INTERRUPT__)) == (__INTERRUPT__))
00275 
00276 /**
00277   * @brief  Clear the RNG interrupt status flags.
00278   * @param  __HANDLE__ RNG Handle
00279   * @param  __INTERRUPT__ specifies the RNG interrupt status flag to clear.
00280   *          This parameter can be one of the following values:
00281   *            @arg RNG_IT_CEI: Clock error interrupt
00282   *            @arg RNG_IT_SEI: Seed error interrupt
00283   * @note   RNG_IT_DRDY flag is read-only, reading RNG_DR register automatically clears RNG_IT_DRDY.
00284   * @retval None
00285   */
00286 #define __HAL_RNG_CLEAR_IT(__HANDLE__, __INTERRUPT__) (((__HANDLE__)->Instance->SR) = ~(__INTERRUPT__))
00287 
00288 /**
00289   * @}
00290   */
00291 
00292 #if defined (RNG_CR_CONDRST)
00293 /* Include HASH HAL Extended module */
00294 #include "stm32l4xx_hal_rng_ex.h"
00295 #endif  /* CONDRST */
00296 /* Exported functions --------------------------------------------------------*/
00297 /** @defgroup RNG_Exported_Functions RNG Exported Functions
00298   * @{
00299   */
00300 
00301 /** @defgroup RNG_Exported_Functions_Group1 Initialization and configuration functions
00302   * @{
00303   */
00304 HAL_StatusTypeDef HAL_RNG_Init(RNG_HandleTypeDef *hrng);
00305 HAL_StatusTypeDef HAL_RNG_DeInit(RNG_HandleTypeDef *hrng);
00306 void HAL_RNG_MspInit(RNG_HandleTypeDef *hrng);
00307 void HAL_RNG_MspDeInit(RNG_HandleTypeDef *hrng);
00308 
00309 /* Callbacks Register/UnRegister functions  ***********************************/
00310 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
00311 HAL_StatusTypeDef HAL_RNG_RegisterCallback(RNG_HandleTypeDef *hrng, HAL_RNG_CallbackIDTypeDef CallbackID, pRNG_CallbackTypeDef pCallback);
00312 HAL_StatusTypeDef HAL_RNG_UnRegisterCallback(RNG_HandleTypeDef *hrng, HAL_RNG_CallbackIDTypeDef CallbackID);
00313 
00314 HAL_StatusTypeDef HAL_RNG_RegisterReadyDataCallback(RNG_HandleTypeDef *hrng, pRNG_ReadyDataCallbackTypeDef pCallback);
00315 HAL_StatusTypeDef HAL_RNG_UnRegisterReadyDataCallback(RNG_HandleTypeDef *hrng);
00316 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
00317 
00318 /**
00319   * @}
00320   */
00321 
00322 /** @defgroup RNG_Exported_Functions_Group2 Peripheral Control functions
00323   * @{
00324   */
00325 uint32_t HAL_RNG_GetRandomNumber(RNG_HandleTypeDef *hrng);    /* Obsolete, use HAL_RNG_GenerateRandomNumber() instead    */
00326 uint32_t HAL_RNG_GetRandomNumber_IT(RNG_HandleTypeDef *hrng); /* Obsolete, use HAL_RNG_GenerateRandomNumber_IT() instead */
00327 HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber(RNG_HandleTypeDef *hrng, uint32_t *random32bit);
00328 HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber_IT(RNG_HandleTypeDef *hrng);
00329 uint32_t HAL_RNG_ReadLastRandomNumber(RNG_HandleTypeDef *hrng);
00330 
00331 void HAL_RNG_IRQHandler(RNG_HandleTypeDef *hrng);
00332 void HAL_RNG_ErrorCallback(RNG_HandleTypeDef *hrng);
00333 void HAL_RNG_ReadyDataCallback(RNG_HandleTypeDef *hrng, uint32_t random32bit);
00334 
00335 /**
00336   * @}
00337   */
00338 
00339 /** @defgroup RNG_Exported_Functions_Group3 Peripheral State functions
00340   * @{
00341   */
00342 HAL_RNG_StateTypeDef HAL_RNG_GetState(RNG_HandleTypeDef *hrng);
00343 uint32_t             HAL_RNG_GetError(RNG_HandleTypeDef *hrng);
00344 /**
00345   * @}
00346   */
00347 
00348 /**
00349   * @}
00350   */
00351 
00352 /* Private macros ------------------------------------------------------------*/
00353 /** @defgroup RNG_Private_Macros RNG Private Macros
00354   * @{
00355   */
00356 #define IS_RNG_IT(IT) (((IT) == RNG_IT_CEI) || \
00357                        ((IT) == RNG_IT_SEI))
00358 
00359 #define IS_RNG_FLAG(FLAG) (((FLAG) == RNG_FLAG_DRDY) || \
00360                             ((FLAG) == RNG_FLAG_CECS) || \
00361                             ((FLAG) == RNG_FLAG_SECS))
00362 
00363 #if defined(RNG_CR_CED)
00364 /**
00365   * @brief Verify the RNG Clock Error Detection mode.
00366   * @param __MODE__ RNG Clock Error Detection mode
00367   * @retval SET (__MODE__ is valid) or RESET (__MODE__ is invalid)
00368   */
00369 #define IS_RNG_CED(__MODE__)   (((__MODE__) == RNG_CED_ENABLE) || \
00370                                 ((__MODE__) == RNG_CED_DISABLE))
00371 #endif /* defined(RNG_CR_CED) */
00372 /**
00373   * @}
00374   */
00375 
00376 /**
00377   * @}
00378   */
00379 
00380 #endif /* RNG */
00381 
00382 /**
00383   * @}
00384   */
00385 
00386 #ifdef __cplusplus
00387 }
00388 #endif
00389 
00390 
00391 #endif /* STM32L4xx_HAL_RNG_H */