stm32f4xx_hal_rng.c 17 KB

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