stm32f7xx_hal_rng.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /**
  2. ******************************************************************************
  3. * @file stm32f7xx_hal_rng.c
  4. * @author MCD Application Team
  5. * @brief RNG HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Random Number Generator (RNG) peripheral:
  8. * + Initialization/de-initialization functions
  9. * + Peripheral Control functions
  10. * + Peripheral State functions
  11. *
  12. @verbatim
  13. ==============================================================================
  14. ##### How to use this driver #####
  15. ==============================================================================
  16. [..]
  17. The RNG HAL driver can be used as follows:
  18. (#) Enable the RNG controller clock using __HAL_RCC_RNG_CLK_ENABLE() macro
  19. in HAL_RNG_MspInit().
  20. (#) Activate the RNG peripheral using HAL_RNG_Init() function.
  21. (#) Wait until the 32 bit Random Number Generator contains a valid
  22. random data using (polling/interrupt) mode.
  23. (#) Get the 32 bit random number using HAL_RNG_GenerateRandomNumber() function.
  24. @endverbatim
  25. ******************************************************************************
  26. * @attention
  27. *
  28. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  29. *
  30. * Redistribution and use in source and binary forms, with or without modification,
  31. * are permitted provided that the following conditions are met:
  32. * 1. Redistributions of source code must retain the above copyright notice,
  33. * this list of conditions and the following disclaimer.
  34. * 2. Redistributions in binary form must reproduce the above copyright notice,
  35. * this list of conditions and the following disclaimer in the documentation
  36. * and/or other materials provided with the distribution.
  37. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  38. * may be used to endorse or promote products derived from this software
  39. * without specific prior written permission.
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  42. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  44. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  47. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  48. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  49. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  50. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  51. *
  52. ******************************************************************************
  53. */
  54. /* Includes ------------------------------------------------------------------*/
  55. #include "stm32f7xx_hal.h"
  56. /** @addtogroup STM32F7xx_HAL_Driver
  57. * @{
  58. */
  59. /** @addtogroup RNG
  60. * @{
  61. */
  62. #ifdef HAL_RNG_MODULE_ENABLED
  63. /* Private types -------------------------------------------------------------*/
  64. /* Private defines -----------------------------------------------------------*/
  65. /* Private variables ---------------------------------------------------------*/
  66. /* Private constants ---------------------------------------------------------*/
  67. /** @addtogroup RNG_Private_Constants
  68. * @{
  69. */
  70. #define RNG_TIMEOUT_VALUE 2U
  71. /**
  72. * @}
  73. */
  74. /* Private macros ------------------------------------------------------------*/
  75. /* Private functions prototypes ----------------------------------------------*/
  76. /* Private functions ---------------------------------------------------------*/
  77. /* Exported functions --------------------------------------------------------*/
  78. /** @addtogroup RNG_Exported_Functions
  79. * @{
  80. */
  81. /** @addtogroup RNG_Exported_Functions_Group1
  82. * @brief Initialization and de-initialization functions
  83. *
  84. @verbatim
  85. ===============================================================================
  86. ##### Initialization and de-initialization functions #####
  87. ===============================================================================
  88. [..] This section provides functions allowing to:
  89. (+) Initialize the RNG according to the specified parameters
  90. in the RNG_InitTypeDef and create the associated handle
  91. (+) DeInitialize the RNG peripheral
  92. (+) Initialize the RNG MSP
  93. (+) DeInitialize RNG MSP
  94. @endverbatim
  95. * @{
  96. */
  97. /**
  98. * @brief Initializes the RNG peripheral and creates the associated handle.
  99. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  100. * the configuration information for RNG.
  101. * @retval HAL status
  102. */
  103. HAL_StatusTypeDef HAL_RNG_Init(RNG_HandleTypeDef *hrng)
  104. {
  105. /* Check the RNG handle allocation */
  106. if(hrng == NULL)
  107. {
  108. return HAL_ERROR;
  109. }
  110. if(hrng->State == HAL_RNG_STATE_RESET)
  111. {
  112. /* Allocate lock resource and initialize it */
  113. hrng->Lock = HAL_UNLOCKED;
  114. /* Init the low level hardware */
  115. HAL_RNG_MspInit(hrng);
  116. }
  117. /* Change RNG peripheral state */
  118. hrng->State = HAL_RNG_STATE_BUSY;
  119. /* Enable the RNG Peripheral */
  120. __HAL_RNG_ENABLE(hrng);
  121. /* Initialize the RNG state */
  122. hrng->State = HAL_RNG_STATE_READY;
  123. /* Return function status */
  124. return HAL_OK;
  125. }
  126. /**
  127. * @brief DeInitializes the RNG peripheral.
  128. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  129. * the configuration information for RNG.
  130. * @retval HAL status
  131. */
  132. HAL_StatusTypeDef HAL_RNG_DeInit(RNG_HandleTypeDef *hrng)
  133. {
  134. /* Check the RNG handle allocation */
  135. if(hrng == NULL)
  136. {
  137. return HAL_ERROR;
  138. }
  139. /* Disable the RNG Peripheral */
  140. CLEAR_BIT(hrng->Instance->CR, RNG_CR_IE | RNG_CR_RNGEN);
  141. /* Clear RNG interrupt status flags */
  142. CLEAR_BIT(hrng->Instance->SR, RNG_SR_CEIS | RNG_SR_SEIS);
  143. /* DeInit the low level hardware */
  144. HAL_RNG_MspDeInit(hrng);
  145. /* Update the RNG state */
  146. hrng->State = HAL_RNG_STATE_RESET;
  147. /* Release Lock */
  148. __HAL_UNLOCK(hrng);
  149. /* Return the function status */
  150. return HAL_OK;
  151. }
  152. /**
  153. * @brief Initializes the RNG MSP.
  154. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  155. * the configuration information for RNG.
  156. * @retval None
  157. */
  158. __weak void HAL_RNG_MspInit(RNG_HandleTypeDef *hrng)
  159. {
  160. /* Prevent unused argument(s) compilation warning */
  161. UNUSED(hrng);
  162. /* NOTE : This function should not be modified. When the callback is needed,
  163. function HAL_RNG_MspInit must be implemented in the user file.
  164. */
  165. }
  166. /**
  167. * @brief DeInitializes the RNG MSP.
  168. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  169. * the configuration information for RNG.
  170. * @retval None
  171. */
  172. __weak void HAL_RNG_MspDeInit(RNG_HandleTypeDef *hrng)
  173. {
  174. /* Prevent unused argument(s) compilation warning */
  175. UNUSED(hrng);
  176. /* NOTE : This function should not be modified. When the callback is needed,
  177. function HAL_RNG_MspDeInit must be implemented in the user file.
  178. */
  179. }
  180. /**
  181. * @}
  182. */
  183. /** @addtogroup RNG_Exported_Functions_Group2
  184. * @brief Peripheral Control functions
  185. *
  186. @verbatim
  187. ===============================================================================
  188. ##### Peripheral Control functions #####
  189. ===============================================================================
  190. [..] This section provides functions allowing to:
  191. (+) Get the 32 bit Random number
  192. (+) Get the 32 bit Random number with interrupt enabled
  193. (+) Handle RNG interrupt request
  194. @endverbatim
  195. * @{
  196. */
  197. /**
  198. * @brief Generates a 32-bit random number.
  199. * @note Each time the random number data is read the RNG_FLAG_DRDY flag
  200. * is automatically cleared.
  201. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  202. * the configuration information for RNG.
  203. * @param random32bit pointer to generated random number variable if successful.
  204. * @retval HAL status
  205. */
  206. HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber(RNG_HandleTypeDef *hrng, uint32_t *random32bit)
  207. {
  208. uint32_t tickstart = 0U;
  209. HAL_StatusTypeDef status = HAL_OK;
  210. /* Process Locked */
  211. __HAL_LOCK(hrng);
  212. /* Check RNG peripheral state */
  213. if(hrng->State == HAL_RNG_STATE_READY)
  214. {
  215. /* Change RNG peripheral state */
  216. hrng->State = HAL_RNG_STATE_BUSY;
  217. /* Get tick */
  218. tickstart = HAL_GetTick();
  219. /* Check if data register contains valid random data */
  220. while(__HAL_RNG_GET_FLAG(hrng, RNG_FLAG_DRDY) == RESET)
  221. {
  222. if((HAL_GetTick() - tickstart ) > RNG_TIMEOUT_VALUE)
  223. {
  224. hrng->State = HAL_RNG_STATE_ERROR;
  225. /* Process Unlocked */
  226. __HAL_UNLOCK(hrng);
  227. return HAL_TIMEOUT;
  228. }
  229. }
  230. /* Get a 32bit Random number */
  231. hrng->RandomNumber = hrng->Instance->DR;
  232. *random32bit = hrng->RandomNumber;
  233. hrng->State = HAL_RNG_STATE_READY;
  234. }
  235. else
  236. {
  237. status = HAL_ERROR;
  238. }
  239. /* Process Unlocked */
  240. __HAL_UNLOCK(hrng);
  241. return status;
  242. }
  243. /**
  244. * @brief Generates a 32-bit random number in interrupt mode.
  245. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  246. * the configuration information for RNG.
  247. * @retval HAL status
  248. */
  249. HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber_IT(RNG_HandleTypeDef *hrng)
  250. {
  251. HAL_StatusTypeDef status = HAL_OK;
  252. /* Process Locked */
  253. __HAL_LOCK(hrng);
  254. /* Check RNG peripheral state */
  255. if(hrng->State == HAL_RNG_STATE_READY)
  256. {
  257. /* Change RNG peripheral state */
  258. hrng->State = HAL_RNG_STATE_BUSY;
  259. /* Process Unlocked */
  260. __HAL_UNLOCK(hrng);
  261. /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */
  262. __HAL_RNG_ENABLE_IT(hrng);
  263. }
  264. else
  265. {
  266. /* Process Unlocked */
  267. __HAL_UNLOCK(hrng);
  268. status = HAL_ERROR;
  269. }
  270. return status;
  271. }
  272. /**
  273. * @brief Handles RNG interrupt request.
  274. * @note In the case of a clock error, the RNG is no more able to generate
  275. * random numbers because the PLL48CLK clock is not correct. User has
  276. * to check that the clock controller is correctly configured to provide
  277. * the RNG clock and clear the CEIS bit using __HAL_RNG_CLEAR_IT().
  278. * The clock error has no impact on the previously generated
  279. * random numbers, and the RNG_DR register contents can be used.
  280. * @note In the case of a seed error, the generation of random numbers is
  281. * interrupted as long as the SECS bit is '1'. If a number is
  282. * available in the RNG_DR register, it must not be used because it may
  283. * not have enough entropy. In this case, it is recommended to clear the
  284. * SEIS bit using __HAL_RNG_CLEAR_IT(), then disable and enable
  285. * the RNG peripheral to reinitialize and restart the RNG.
  286. * @note User-written HAL_RNG_ErrorCallback() API is called once whether SEIS
  287. * or CEIS are set.
  288. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  289. * the configuration information for RNG.
  290. * @retval None
  291. */
  292. void HAL_RNG_IRQHandler(RNG_HandleTypeDef *hrng)
  293. {
  294. /* RNG clock error interrupt occurred */
  295. if((__HAL_RNG_GET_IT(hrng, RNG_IT_CEI) != RESET) || (__HAL_RNG_GET_IT(hrng, RNG_IT_SEI) != RESET))
  296. {
  297. /* Change RNG peripheral state */
  298. hrng->State = HAL_RNG_STATE_ERROR;
  299. HAL_RNG_ErrorCallback(hrng);
  300. /* Clear the clock error flag */
  301. __HAL_RNG_CLEAR_IT(hrng, RNG_IT_CEI|RNG_IT_SEI);
  302. }
  303. /* Check RNG data ready interrupt occurred */
  304. if(__HAL_RNG_GET_IT(hrng, RNG_IT_DRDY) != RESET)
  305. {
  306. /* Generate random number once, so disable the IT */
  307. __HAL_RNG_DISABLE_IT(hrng);
  308. /* Get the 32bit Random number (DRDY flag automatically cleared) */
  309. hrng->RandomNumber = hrng->Instance->DR;
  310. if(hrng->State != HAL_RNG_STATE_ERROR)
  311. {
  312. /* Change RNG peripheral state */
  313. hrng->State = HAL_RNG_STATE_READY;
  314. /* Data Ready callback */
  315. HAL_RNG_ReadyDataCallback(hrng, hrng->RandomNumber);
  316. }
  317. }
  318. }
  319. /**
  320. * @brief Returns generated random number in polling mode (Obsolete)
  321. * Use HAL_RNG_GenerateRandomNumber() API instead.
  322. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  323. * the configuration information for RNG.
  324. * @retval Random value
  325. */
  326. uint32_t HAL_RNG_GetRandomNumber(RNG_HandleTypeDef *hrng)
  327. {
  328. if(HAL_RNG_GenerateRandomNumber(hrng, &(hrng->RandomNumber)) == HAL_OK)
  329. {
  330. return hrng->RandomNumber;
  331. }
  332. else
  333. {
  334. return 0U;
  335. }
  336. }
  337. /**
  338. * @brief Returns a 32-bit random number with interrupt enabled (Obsolete),
  339. * Use HAL_RNG_GenerateRandomNumber_IT() API instead.
  340. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  341. * the configuration information for RNG.
  342. * @retval 32-bit random number
  343. */
  344. uint32_t HAL_RNG_GetRandomNumber_IT(RNG_HandleTypeDef *hrng)
  345. {
  346. uint32_t random32bit = 0U;
  347. /* Process locked */
  348. __HAL_LOCK(hrng);
  349. /* Change RNG peripheral state */
  350. hrng->State = HAL_RNG_STATE_BUSY;
  351. /* Get a 32bit Random number */
  352. random32bit = hrng->Instance->DR;
  353. /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */
  354. __HAL_RNG_ENABLE_IT(hrng);
  355. /* Return the 32 bit random number */
  356. return random32bit;
  357. }
  358. /**
  359. * @brief Read latest generated random number.
  360. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  361. * the configuration information for RNG.
  362. * @retval random value
  363. */
  364. uint32_t HAL_RNG_ReadLastRandomNumber(RNG_HandleTypeDef *hrng)
  365. {
  366. return(hrng->RandomNumber);
  367. }
  368. /**
  369. * @brief Data Ready callback in non-blocking mode.
  370. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  371. * the configuration information for RNG.
  372. * @param random32bit generated random number.
  373. * @retval None
  374. */
  375. __weak void HAL_RNG_ReadyDataCallback(RNG_HandleTypeDef *hrng, uint32_t random32bit)
  376. {
  377. /* Prevent unused argument(s) compilation warning */
  378. UNUSED(hrng);
  379. UNUSED(random32bit);
  380. /* NOTE : This function should not be modified. When the callback is needed,
  381. function HAL_RNG_ReadyDataCallback must be implemented in the user file.
  382. */
  383. }
  384. /**
  385. * @brief RNG error callbacks.
  386. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  387. * the configuration information for RNG.
  388. * @retval None
  389. */
  390. __weak void HAL_RNG_ErrorCallback(RNG_HandleTypeDef *hrng)
  391. {
  392. /* Prevent unused argument(s) compilation warning */
  393. UNUSED(hrng);
  394. /* NOTE : This function should not be modified. When the callback is needed,
  395. function HAL_RNG_ErrorCallback must be implemented in the user file.
  396. */
  397. }
  398. /**
  399. * @}
  400. */
  401. /** @addtogroup RNG_Exported_Functions_Group3
  402. * @brief Peripheral State functions
  403. *
  404. @verbatim
  405. ===============================================================================
  406. ##### Peripheral State functions #####
  407. ===============================================================================
  408. [..]
  409. This subsection permits to get in run-time the status of the peripheral
  410. and the data flow.
  411. @endverbatim
  412. * @{
  413. */
  414. /**
  415. * @brief Returns the RNG state.
  416. * @param hrng pointer to a RNG_HandleTypeDef structure that contains
  417. * the configuration information for RNG.
  418. * @retval HAL state
  419. */
  420. HAL_RNG_StateTypeDef HAL_RNG_GetState(RNG_HandleTypeDef *hrng)
  421. {
  422. return hrng->State;
  423. }
  424. /**
  425. * @}
  426. */
  427. /**
  428. * @}
  429. */
  430. #endif /* HAL_RNG_MODULE_ENABLED */
  431. /**
  432. * @}
  433. */
  434. /**
  435. * @}
  436. */
  437. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/