stm32f4xx_rng.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_rng.c
  4. * @author MCD Application Team
  5. * @version V1.8.1
  6. * @date 27-January-2022
  7. * @brief This file provides firmware functions to manage the following
  8. * functionalities of the Random Number Generator (RNG) peripheral:
  9. * + Initialization and Configuration
  10. * + Get 32 bit Random number
  11. * + Interrupts and flags management
  12. *
  13. @verbatim
  14. ===================================================================
  15. ##### How to use this driver #####
  16. ===================================================================
  17. [..]
  18. (#) Enable The RNG controller clock using
  19. RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_RNG, ENABLE) function.
  20. (#) Activate the RNG peripheral using RNG_Cmd() function.
  21. (#) Wait until the 32 bit Random number Generator contains a valid random data
  22. (using polling/interrupt mode). For more details, refer to "Interrupts and
  23. flags management functions" module description.
  24. (#) Get the 32 bit Random number using RNG_GetRandomNumber() function
  25. (#) To get another 32 bit Random number, go to step 3.
  26. @endverbatim
  27. *
  28. ******************************************************************************
  29. * @attention
  30. *
  31. * Copyright (c) 2016 STMicroelectronics.
  32. * All rights reserved.
  33. *
  34. * This software is licensed under terms that can be found in the LICENSE file
  35. * in the root directory of this software component.
  36. * If no LICENSE file comes with this software, it is provided AS-IS.
  37. *
  38. ******************************************************************************
  39. */
  40. /* Includes ------------------------------------------------------------------*/
  41. #include "stm32f4xx_rng.h"
  42. #include "stm32f4xx_rcc.h"
  43. /** @addtogroup STM32F4xx_StdPeriph_Driver
  44. * @{
  45. */
  46. /** @defgroup RNG
  47. * @brief RNG driver modules
  48. * @{
  49. */
  50. #if defined(STM32F40_41xxx) || defined(STM32F427_437xx) || defined(STM32F410xx) || defined(STM32F412xG) || defined(STM32F413_423xx) || defined(STM32F429_439xx) || defined(STM32F469_479xx)
  51. /* Private typedef -----------------------------------------------------------*/
  52. /* Private define ------------------------------------------------------------*/
  53. /* Private macro -------------------------------------------------------------*/
  54. /* Private variables ---------------------------------------------------------*/
  55. /* Private function prototypes -----------------------------------------------*/
  56. /* Private functions ---------------------------------------------------------*/
  57. /** @defgroup RNG_Private_Functions
  58. * @{
  59. */
  60. /** @defgroup RNG_Group1 Initialization and Configuration functions
  61. * @brief Initialization and Configuration functions
  62. *
  63. @verbatim
  64. ===============================================================================
  65. ##### Initialization and Configuration functions #####
  66. ===============================================================================
  67. [..] This section provides functions allowing to
  68. (+) Initialize the RNG peripheral
  69. (+) Enable or disable the RNG peripheral
  70. @endverbatim
  71. * @{
  72. */
  73. /**
  74. * @brief De-initializes the RNG peripheral registers to their default reset values.
  75. * @param None
  76. * @retval None
  77. */
  78. void RNG_DeInit(void)
  79. {
  80. #if defined(STM32F40_41xxx) || defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F469_479xx)
  81. /* Enable RNG reset state */
  82. RCC_AHB2PeriphResetCmd(RCC_AHB2Periph_RNG, ENABLE);
  83. /* Release RNG from reset state */
  84. RCC_AHB2PeriphResetCmd(RCC_AHB2Periph_RNG, DISABLE);
  85. #endif /* STM32F40_41xxx || STM32F427_437xx || STM32F429_439xx || STM32F469_479xx */
  86. #if defined(STM32F410xx)
  87. /* Enable RNG reset state */
  88. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_RNG, ENABLE);
  89. /* Release RNG from reset state */
  90. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_RNG, DISABLE);
  91. #endif /* STM32F410xx*/
  92. }
  93. /**
  94. * @brief Enables or disables the RNG peripheral.
  95. * @param NewState: new state of the RNG peripheral.
  96. * This parameter can be: ENABLE or DISABLE.
  97. * @retval None
  98. */
  99. void RNG_Cmd(FunctionalState NewState)
  100. {
  101. /* Check the parameters */
  102. assert_param(IS_FUNCTIONAL_STATE(NewState));
  103. if (NewState != DISABLE)
  104. {
  105. /* Enable the RNG */
  106. RNG->CR |= RNG_CR_RNGEN;
  107. }
  108. else
  109. {
  110. /* Disable the RNG */
  111. RNG->CR &= ~RNG_CR_RNGEN;
  112. }
  113. }
  114. /**
  115. * @}
  116. */
  117. /** @defgroup RNG_Group2 Get 32 bit Random number function
  118. * @brief Get 32 bit Random number function
  119. *
  120. @verbatim
  121. ===============================================================================
  122. ##### Get 32 bit Random number function #####
  123. ===============================================================================
  124. [..] This section provides a function allowing to get the 32 bit Random number
  125. (@) Before to call this function you have to wait till DRDY flag is set,
  126. using RNG_GetFlagStatus(RNG_FLAG_DRDY) function.
  127. @endverbatim
  128. * @{
  129. */
  130. /**
  131. * @brief Returns a 32-bit random number.
  132. *
  133. * @note Before to call this function you have to wait till DRDY (data ready)
  134. * flag is set, using RNG_GetFlagStatus(RNG_FLAG_DRDY) function.
  135. * @note Each time the Random number data is read (using RNG_GetRandomNumber()
  136. * function), the RNG_FLAG_DRDY flag is automatically cleared.
  137. * @note In the case of a seed error, the generation of random numbers is
  138. * interrupted for as long as the SECS bit is '1'. If a number is
  139. * available in the RNG_DR register, it must not be used because it may
  140. * not have enough entropy. In this case, it is recommended to clear the
  141. * SEIS bit(using RNG_ClearFlag(RNG_FLAG_SECS) function), then disable
  142. * and enable the RNG peripheral (using RNG_Cmd() function) to
  143. * reinitialize and restart the RNG.
  144. * @note In the case of a clock error, the RNG is no more able to generate
  145. * random numbers because the PLL48CLK clock is not correct. User have
  146. * to check that the clock controller is correctly configured to provide
  147. * the RNG clock and clear the CEIS bit (using RNG_ClearFlag(RNG_FLAG_CECS)
  148. * function) . The clock error has no impact on the previously generated
  149. * random numbers, and the RNG_DR register contents can be used.
  150. *
  151. * @param None
  152. * @retval 32-bit random number.
  153. */
  154. uint32_t RNG_GetRandomNumber(void)
  155. {
  156. /* Return the 32 bit random number from the DR register */
  157. return RNG->DR;
  158. }
  159. /**
  160. * @}
  161. */
  162. /** @defgroup RNG_Group3 Interrupts and flags management functions
  163. * @brief Interrupts and flags management functions
  164. *
  165. @verbatim
  166. ===============================================================================
  167. ##### Interrupts and flags management functions #####
  168. ===============================================================================
  169. [..] This section provides functions allowing to configure the RNG Interrupts and
  170. to get the status and clear flags and Interrupts pending bits.
  171. [..] The RNG provides 3 Interrupts sources and 3 Flags:
  172. *** Flags : ***
  173. ===============
  174. [..]
  175. (#) RNG_FLAG_DRDY : In the case of the RNG_DR register contains valid
  176. random data. it is cleared by reading the valid data(using
  177. RNG_GetRandomNumber() function).
  178. (#) RNG_FLAG_CECS : In the case of a seed error detection.
  179. (#) RNG_FLAG_SECS : In the case of a clock error detection.
  180. *** Interrupts ***
  181. ==================
  182. [..] If enabled, an RNG interrupt is pending :
  183. (#) In the case of the RNG_DR register contains valid random data.
  184. This interrupt source is cleared once the RNG_DR register has been read
  185. (using RNG_GetRandomNumber() function) until a new valid value is
  186. computed; or
  187. (#) In the case of a seed error : One of the following faulty sequences has
  188. been detected:
  189. (++) More than 64 consecutive bits at the same value (0 or 1)
  190. (++) More than 32 consecutive alternance of 0 and 1 (0101010101...01)
  191. This interrupt source is cleared using RNG_ClearITPendingBit(RNG_IT_SEI)
  192. function; or
  193. (#) In the case of a clock error : the PLL48CLK (RNG peripheral clock source)
  194. was not correctly detected (fPLL48CLK< fHCLK/16). This interrupt source is
  195. cleared using RNG_ClearITPendingBit(RNG_IT_CEI) function.
  196. -@- note In this case, User have to check that the clock controller is
  197. correctly configured to provide the RNG clock.
  198. *** Managing the RNG controller events : ***
  199. ============================================
  200. [..] The user should identify which mode will be used in his application to manage
  201. the RNG controller events: Polling mode or Interrupt mode.
  202. (#) In the Polling Mode it is advised to use the following functions:
  203. (++) RNG_GetFlagStatus() : to check if flags events occur.
  204. (++) RNG_ClearFlag() : to clear the flags events.
  205. -@@- RNG_FLAG_DRDY can not be cleared by RNG_ClearFlag(). it is cleared only
  206. by reading the Random number data.
  207. (#) In the Interrupt Mode it is advised to use the following functions:
  208. (++) RNG_ITConfig() : to enable or disable the interrupt source.
  209. (++) RNG_GetITStatus() : to check if Interrupt occurs.
  210. (++) RNG_ClearITPendingBit() : to clear the Interrupt pending Bit
  211. (corresponding Flag).
  212. @endverbatim
  213. * @{
  214. */
  215. /**
  216. * @brief Enables or disables the RNG interrupt.
  217. * @note The RNG provides 3 interrupt sources,
  218. * - Computed data is ready event (DRDY), and
  219. * - Seed error Interrupt (SEI) and
  220. * - Clock error Interrupt (CEI),
  221. * all these interrupts sources are enabled by setting the IE bit in
  222. * CR register. However, each interrupt have its specific status bit
  223. * (see RNG_GetITStatus() function) and clear bit except the DRDY event
  224. * (see RNG_ClearITPendingBit() function).
  225. * @param NewState: new state of the RNG interrupt.
  226. * This parameter can be: ENABLE or DISABLE.
  227. * @retval None
  228. */
  229. void RNG_ITConfig(FunctionalState NewState)
  230. {
  231. /* Check the parameters */
  232. assert_param(IS_FUNCTIONAL_STATE(NewState));
  233. if (NewState != DISABLE)
  234. {
  235. /* Enable the RNG interrupt */
  236. RNG->CR |= RNG_CR_IE;
  237. }
  238. else
  239. {
  240. /* Disable the RNG interrupt */
  241. RNG->CR &= ~RNG_CR_IE;
  242. }
  243. }
  244. /**
  245. * @brief Checks whether the specified RNG flag is set or not.
  246. * @param RNG_FLAG: specifies the RNG flag to check.
  247. * This parameter can be one of the following values:
  248. * @arg RNG_FLAG_DRDY: Data Ready flag.
  249. * @arg RNG_FLAG_CECS: Clock Error Current flag.
  250. * @arg RNG_FLAG_SECS: Seed Error Current flag.
  251. * @retval The new state of RNG_FLAG (SET or RESET).
  252. */
  253. FlagStatus RNG_GetFlagStatus(uint8_t RNG_FLAG)
  254. {
  255. FlagStatus bitstatus = RESET;
  256. /* Check the parameters */
  257. assert_param(IS_RNG_GET_FLAG(RNG_FLAG));
  258. /* Check the status of the specified RNG flag */
  259. if ((RNG->SR & RNG_FLAG) != (uint8_t)RESET)
  260. {
  261. /* RNG_FLAG is set */
  262. bitstatus = SET;
  263. }
  264. else
  265. {
  266. /* RNG_FLAG is reset */
  267. bitstatus = RESET;
  268. }
  269. /* Return the RNG_FLAG status */
  270. return bitstatus;
  271. }
  272. /**
  273. * @brief Clears the RNG flags.
  274. * @param RNG_FLAG: specifies the flag to clear.
  275. * This parameter can be any combination of the following values:
  276. * @arg RNG_FLAG_CECS: Clock Error Current flag.
  277. * @arg RNG_FLAG_SECS: Seed Error Current flag.
  278. * @note RNG_FLAG_DRDY can not be cleared by RNG_ClearFlag() function.
  279. * This flag is cleared only by reading the Random number data (using
  280. * RNG_GetRandomNumber() function).
  281. * @retval None
  282. */
  283. void RNG_ClearFlag(uint8_t RNG_FLAG)
  284. {
  285. /* Check the parameters */
  286. assert_param(IS_RNG_CLEAR_FLAG(RNG_FLAG));
  287. /* Clear the selected RNG flags */
  288. RNG->SR = ~(uint32_t)(((uint32_t)RNG_FLAG) << 4);
  289. }
  290. /**
  291. * @brief Checks whether the specified RNG interrupt has occurred or not.
  292. * @param RNG_IT: specifies the RNG interrupt source to check.
  293. * This parameter can be one of the following values:
  294. * @arg RNG_IT_CEI: Clock Error Interrupt.
  295. * @arg RNG_IT_SEI: Seed Error Interrupt.
  296. * @retval The new state of RNG_IT (SET or RESET).
  297. */
  298. ITStatus RNG_GetITStatus(uint8_t RNG_IT)
  299. {
  300. ITStatus bitstatus = RESET;
  301. /* Check the parameters */
  302. assert_param(IS_RNG_GET_IT(RNG_IT));
  303. /* Check the status of the specified RNG interrupt */
  304. if ((RNG->SR & RNG_IT) != (uint8_t)RESET)
  305. {
  306. /* RNG_IT is set */
  307. bitstatus = SET;
  308. }
  309. else
  310. {
  311. /* RNG_IT is reset */
  312. bitstatus = RESET;
  313. }
  314. /* Return the RNG_IT status */
  315. return bitstatus;
  316. }
  317. /**
  318. * @brief Clears the RNG interrupt pending bit(s).
  319. * @param RNG_IT: specifies the RNG interrupt pending bit(s) to clear.
  320. * This parameter can be any combination of the following values:
  321. * @arg RNG_IT_CEI: Clock Error Interrupt.
  322. * @arg RNG_IT_SEI: Seed Error Interrupt.
  323. * @retval None
  324. */
  325. void RNG_ClearITPendingBit(uint8_t RNG_IT)
  326. {
  327. /* Check the parameters */
  328. assert_param(IS_RNG_IT(RNG_IT));
  329. /* Clear the selected RNG interrupt pending bit */
  330. RNG->SR = (uint8_t)~RNG_IT;
  331. }
  332. /**
  333. * @}
  334. */
  335. /**
  336. * @}
  337. */
  338. #endif /* STM32F40_41xxx || STM32F427_437xx || STM32F410xx || STM32F412xG || STM32F413_423xx || STM32F429_439xx || STM32F469_479xx */
  339. /**
  340. * @}
  341. */
  342. /**
  343. * @}
  344. */