stm32l4xx_hal_rng_ex.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_rng_ex.c
  4. * @author MCD Application Team
  5. * @brief Extended RNG HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Random Number Generator (RNG) peripheral:
  8. * + Lock configuration functions
  9. * + Reset the RNG
  10. *
  11. ******************************************************************************
  12. * @attention
  13. *
  14. * Copyright (c) 2017 STMicroelectronics.
  15. * All rights reserved.
  16. *
  17. * This software is licensed under terms that can be found in the LICENSE file
  18. * in the root directory of this software component.
  19. * If no LICENSE file comes with this software, it is provided AS-IS.
  20. *
  21. ******************************************************************************
  22. */
  23. /* Includes ------------------------------------------------------------------*/
  24. #include "stm32l4xx_hal.h"
  25. /** @addtogroup STM32L4xx_HAL_Driver
  26. * @{
  27. */
  28. #if defined (RNG)
  29. /** @addtogroup RNGEx
  30. * @brief RNG Extended HAL module driver.
  31. * @{
  32. */
  33. #ifdef HAL_RNG_MODULE_ENABLED
  34. #if defined (RNG_CR_CONDRST)
  35. /* Private types -------------------------------------------------------------*/
  36. /* Private defines -----------------------------------------------------------*/
  37. #if defined(RNG_VER_3_2) || defined(RNG_VER_3_1) || defined(RNG_VER_3_0)
  38. /** @addtogroup RNGEx_Private_Defines
  39. * @{
  40. */
  41. /* Health test control register information to use in CCM algorithm are defined in CMSIS Device file.
  42. - RNG_HTCFG : Default HTCR register value for best latency and NIST Compliance
  43. - RNG_HTCFG_1 : Magic number value that must be written to RNG_HTCR register
  44. immediately before reading or writing RNG_HTCR register */
  45. /**
  46. * @}
  47. */
  48. #endif
  49. /* Private variables ---------------------------------------------------------*/
  50. /* Private constants ---------------------------------------------------------*/
  51. /** @defgroup RNGEx_Private_Constants RNG Private Constants
  52. * @{
  53. */
  54. #define RNG_TIMEOUT_VALUE 2U
  55. /**
  56. * @}
  57. */
  58. /* Private macros ------------------------------------------------------------*/
  59. /* Private functions prototypes ----------------------------------------------*/
  60. /* Private functions ---------------------------------------------------------*/
  61. /* Exported functions --------------------------------------------------------*/
  62. /** @addtogroup RNGEx_Exported_Functions
  63. * @{
  64. */
  65. /** @addtogroup RNGEx_Exported_Functions_Group1
  66. * @brief Configuration functions
  67. *
  68. @verbatim
  69. ===============================================================================
  70. ##### Configuration and lock functions #####
  71. ===============================================================================
  72. [..] This section provides functions allowing to:
  73. (+) Configure the RNG with the specified parameters in the RNG_ConfigTypeDef
  74. (+) Lock RNG configuration Allows user to lock a configuration until next reset.
  75. @endverbatim
  76. * @{
  77. */
  78. /**
  79. * @brief Configure the RNG with the specified parameters in the
  80. * RNG_ConfigTypeDef.
  81. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  82. * the configuration information for RNG.
  83. * @param pConf pointer to a RNG_ConfigTypeDef structure that contains
  84. * the configuration information for RNG module
  85. * @retval HAL status
  86. */
  87. HAL_StatusTypeDef HAL_RNGEx_SetConfig(RNG_HandleTypeDef *hrng, RNG_ConfigTypeDef *pConf)
  88. {
  89. uint32_t tickstart;
  90. uint32_t cr_value;
  91. HAL_StatusTypeDef status ;
  92. /* Check the RNG handle allocation */
  93. if ((hrng == NULL)||(pConf == NULL))
  94. {
  95. return HAL_ERROR;
  96. }
  97. /* Check the parameters */
  98. assert_param(IS_RNG_ALL_INSTANCE(hrng->Instance));
  99. assert_param(IS_RNG_CLOCK_DIVIDER(pConf->ClockDivider));
  100. assert_param(IS_RNG_NIST_COMPLIANCE(pConf->NistCompliance));
  101. assert_param(IS_RNG_CONFIG1(pConf->Config1));
  102. assert_param(IS_RNG_CONFIG2(pConf->Config2));
  103. assert_param(IS_RNG_CONFIG3(pConf->Config3));
  104. /* Check RNG peripheral state */
  105. if (hrng->State == HAL_RNG_STATE_READY)
  106. {
  107. /* Change RNG peripheral state */
  108. hrng->State = HAL_RNG_STATE_BUSY;
  109. /* Disable RNG */
  110. __HAL_RNG_DISABLE(hrng);
  111. /* RNG CR register configuration. Set value in CR register for :
  112. - NIST Compliance setting
  113. - Clock divider value
  114. - CONFIG 1, CONFIG 2 and CONFIG 3 values */
  115. cr_value = (uint32_t) ( pConf->ClockDivider | pConf->NistCompliance
  116. | (pConf->Config1 << RNG_CR_RNG_CONFIG1_Pos)
  117. | (pConf->Config2 << RNG_CR_RNG_CONFIG2_Pos)
  118. | (pConf->Config3 << RNG_CR_RNG_CONFIG3_Pos));
  119. MODIFY_REG(hrng->Instance->CR, RNG_CR_NISTC | RNG_CR_CLKDIV | RNG_CR_RNG_CONFIG1
  120. | RNG_CR_RNG_CONFIG2 | RNG_CR_RNG_CONFIG3,
  121. (uint32_t) (RNG_CR_CONDRST | cr_value));
  122. #if defined(RNG_VER_3_2) || defined(RNG_VER_3_1) || defined(RNG_VER_3_0)
  123. /*!< magic number must be written immediately before to RNG_HTCRG */
  124. WRITE_REG(hrng->Instance->HTCR, RNG_HTCFG_1);
  125. /* for best latency and to be compliant with NIST */
  126. WRITE_REG(hrng->Instance->HTCR, RNG_HTCFG);
  127. #endif
  128. /* Writing bits CONDRST=0*/
  129. CLEAR_BIT(hrng->Instance->CR, RNG_CR_CONDRST);
  130. /* Get tick */
  131. tickstart = HAL_GetTick();
  132. /* Wait for conditioning reset process to be completed */
  133. while(HAL_IS_BIT_SET(hrng->Instance->CR, RNG_CR_CONDRST))
  134. {
  135. if((HAL_GetTick() - tickstart ) > RNG_TIMEOUT_VALUE)
  136. {
  137. hrng->State = HAL_RNG_STATE_READY;
  138. hrng->ErrorCode = HAL_RNG_ERROR_TIMEOUT;
  139. return HAL_ERROR;
  140. }
  141. }
  142. /* Enable RNG */
  143. __HAL_RNG_ENABLE(hrng);
  144. /* Initialize the RNG state */
  145. hrng->State = HAL_RNG_STATE_READY;
  146. /* function status */
  147. status = HAL_OK;
  148. }
  149. else
  150. {
  151. hrng->ErrorCode = HAL_RNG_ERROR_BUSY;
  152. status = HAL_ERROR;
  153. }
  154. /* Return the function status */
  155. return status;
  156. }
  157. /**
  158. * @brief Get the RNG Configuration and fill parameters in the
  159. * RNG_ConfigTypeDef.
  160. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  161. * the configuration information for RNG.
  162. * @param pConf pointer to a RNG_ConfigTypeDef structure that contains
  163. * the configuration information for RNG module
  164. * @retval HAL status
  165. */
  166. HAL_StatusTypeDef HAL_RNGEx_GetConfig(RNG_HandleTypeDef *hrng, RNG_ConfigTypeDef *pConf)
  167. {
  168. HAL_StatusTypeDef status ;
  169. /* Check the RNG handle allocation */
  170. if ((hrng == NULL)||(pConf == NULL))
  171. {
  172. return HAL_ERROR;
  173. }
  174. /* Check RNG peripheral state */
  175. if (hrng->State == HAL_RNG_STATE_READY)
  176. {
  177. /* Change RNG peripheral state */
  178. hrng->State = HAL_RNG_STATE_BUSY;
  179. /* Get RNG parameters */
  180. pConf->Config1 = (uint32_t) ((hrng->Instance->CR & RNG_CR_RNG_CONFIG1) >> RNG_CR_RNG_CONFIG1_Pos) ;
  181. pConf->Config2 = (uint32_t) ((hrng->Instance->CR & RNG_CR_RNG_CONFIG2) >> RNG_CR_RNG_CONFIG2_Pos);
  182. pConf->Config3 = (uint32_t)((hrng->Instance->CR & RNG_CR_RNG_CONFIG3) >> RNG_CR_RNG_CONFIG3_Pos);
  183. pConf->ClockDivider = (hrng->Instance->CR & RNG_CR_CLKDIV);
  184. pConf->NistCompliance = (hrng->Instance->CR & RNG_CR_NISTC);
  185. /* Initialize the RNG state */
  186. hrng->State = HAL_RNG_STATE_READY;
  187. /* function status */
  188. status = HAL_OK;
  189. }
  190. else
  191. {
  192. hrng->ErrorCode |= HAL_RNG_ERROR_BUSY;
  193. status = HAL_ERROR;
  194. }
  195. /* Return the function status */
  196. return status;
  197. }
  198. /**
  199. * @brief RNG current configuration lock.
  200. * @note This function allows to lock RNG peripheral configuration.
  201. * Once locked, HW RNG reset has to be performed prior any further
  202. * configuration update.
  203. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  204. * the configuration information for RNG.
  205. * @retval HAL status
  206. */
  207. HAL_StatusTypeDef HAL_RNGEx_LockConfig(RNG_HandleTypeDef *hrng)
  208. {
  209. HAL_StatusTypeDef status;
  210. /* Check the RNG handle allocation */
  211. if (hrng == NULL)
  212. {
  213. return HAL_ERROR;
  214. }
  215. /* Check RNG peripheral state */
  216. if(hrng->State == HAL_RNG_STATE_READY)
  217. {
  218. /* Change RNG peripheral state */
  219. hrng->State = HAL_RNG_STATE_BUSY;
  220. /* Perform RNG configuration Lock */
  221. MODIFY_REG(hrng->Instance->CR, RNG_CR_CONFIGLOCK, RNG_CR_CONFIGLOCK);
  222. /* Change RNG peripheral state */
  223. hrng->State = HAL_RNG_STATE_READY;
  224. /* function status */
  225. status = HAL_OK;
  226. }
  227. else
  228. {
  229. hrng->ErrorCode = HAL_RNG_ERROR_BUSY;
  230. status = HAL_ERROR;
  231. }
  232. /* Return the function status */
  233. return status;
  234. }
  235. /**
  236. * @}
  237. */
  238. /**
  239. * @}
  240. */
  241. #endif /* CONDRST */
  242. #endif /* HAL_RNG_MODULE_ENABLED */
  243. /**
  244. * @}
  245. */
  246. #endif /* RNG */
  247. /**
  248. * @}
  249. */