You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

300 lines
8.8 KiB

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