stm32f4xx_hal_rng.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_rng.c
  4. * @author MCD Application Team
  5. * @version V1.7.1
  6. * @date 14-April-2017
  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) 2017 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. #include "stm32f4xx_hal_rng.h"
  59. /** @addtogroup STM32F4xx_HAL_Driver
  60. * @{
  61. */
  62. /** @addtogroup RNG
  63. * @{
  64. */
  65. //#ifdef HAL_RNG_MODULE_ENABLED
  66. #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) ||\
  67. defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) ||\
  68. defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) || defined(STM32F469xx) ||\
  69. defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) ||\
  70. defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
  71. /* Private types -------------------------------------------------------------*/
  72. /* Private defines -----------------------------------------------------------*/
  73. /* Private variables ---------------------------------------------------------*/
  74. /* Private constants ---------------------------------------------------------*/
  75. /** @addtogroup RNG_Private_Constants
  76. * @{
  77. */
  78. #define RNG_TIMEOUT_VALUE 2U
  79. /**
  80. * @}
  81. */
  82. /* Private macros ------------------------------------------------------------*/
  83. /* Private functions prototypes ----------------------------------------------*/
  84. /* Private functions ---------------------------------------------------------*/
  85. /* Exported functions --------------------------------------------------------*/
  86. /** @addtogroup RNG_Exported_Functions
  87. * @{
  88. */
  89. /** @addtogroup RNG_Exported_Functions_Group1
  90. * @brief Initialization and de-initialization functions
  91. *
  92. @verbatim
  93. ===============================================================================
  94. ##### Initialization and de-initialization functions #####
  95. ===============================================================================
  96. [..] This section provides functions allowing to:
  97. (+) Initialize the RNG according to the specified parameters
  98. in the RNG_InitTypeDef and create the associated handle
  99. (+) DeInitialize the RNG peripheral
  100. (+) Initialize the RNG MSP
  101. (+) DeInitialize RNG MSP
  102. @endverbatim
  103. * @{
  104. */
  105. /**
  106. * @brief Initializes the RNG peripheral and creates the associated handle.
  107. * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
  108. * the configuration information for RNG.
  109. * @retval HAL status
  110. */
  111. HAL_StatusTypeDef HAL_RNG_Init(RNG_HandleTypeDef *hrng)
  112. {
  113. /* Check the RNG handle allocation */
  114. if(hrng == NULL)
  115. {
  116. return HAL_ERROR;
  117. }
  118. __HAL_LOCK(hrng);
  119. if(hrng->State == HAL_RNG_STATE_RESET)
  120. {
  121. /* Allocate lock resource and initialize it */
  122. hrng->Lock = HAL_UNLOCKED;
  123. /* Init the low level hardware */
  124. HAL_RNG_MspInit(hrng);
  125. }
  126. /* Change RNG peripheral state */
  127. hrng->State = HAL_RNG_STATE_BUSY;
  128. /* Enable the RNG Peripheral */
  129. __HAL_RNG_ENABLE(hrng);
  130. /* Initialize the RNG state */
  131. hrng->State = HAL_RNG_STATE_READY;
  132. __HAL_UNLOCK(hrng);
  133. /* Return function status */
  134. return HAL_OK;
  135. }
  136. /**
  137. * @brief DeInitializes the RNG peripheral.
  138. * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
  139. * the configuration information for RNG.
  140. * @retval HAL status
  141. */
  142. HAL_StatusTypeDef HAL_RNG_DeInit(RNG_HandleTypeDef *hrng)
  143. {
  144. /* Check the RNG handle allocation */
  145. if(hrng == NULL)
  146. {
  147. return HAL_ERROR;
  148. }
  149. /* Disable the RNG Peripheral */
  150. CLEAR_BIT(hrng->Instance->CR, RNG_CR_IE | RNG_CR_RNGEN);
  151. /* Clear RNG interrupt status flags */
  152. CLEAR_BIT(hrng->Instance->SR, RNG_SR_CEIS | RNG_SR_SEIS);
  153. /* DeInit the low level hardware */
  154. HAL_RNG_MspDeInit(hrng);
  155. /* Update the RNG state */
  156. hrng->State = HAL_RNG_STATE_RESET;
  157. /* Release Lock */
  158. __HAL_UNLOCK(hrng);
  159. /* Return the function status */
  160. return HAL_OK;
  161. }
  162. /**
  163. * @brief Initializes the RNG MSP.
  164. * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
  165. * the configuration information for RNG.
  166. * @retval None
  167. */
  168. __weak void HAL_RNG_MspInit(RNG_HandleTypeDef *hrng)
  169. {
  170. /* Prevent unused argument(s) compilation warning */
  171. UNUSED(hrng);
  172. /* NOTE : This function should not be modified. When the callback is needed,
  173. function HAL_RNG_MspInit must be implemented in the user file.
  174. */
  175. }
  176. /**
  177. * @brief DeInitializes the RNG MSP.
  178. * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
  179. * the configuration information for RNG.
  180. * @retval None
  181. */
  182. __weak void HAL_RNG_MspDeInit(RNG_HandleTypeDef *hrng)
  183. {
  184. /* Prevent unused argument(s) compilation warning */
  185. UNUSED(hrng);
  186. /* NOTE : This function should not be modified. When the callback is needed,
  187. function HAL_RNG_MspDeInit must be implemented in the user file.
  188. */
  189. }
  190. /**
  191. * @}
  192. */
  193. /** @addtogroup RNG_Exported_Functions_Group2
  194. * @brief Peripheral Control functions
  195. *
  196. @verbatim
  197. ===============================================================================
  198. ##### Peripheral Control functions #####
  199. ===============================================================================
  200. [..] This section provides functions allowing to:
  201. (+) Get the 32 bit Random number
  202. (+) Get the 32 bit Random number with interrupt enabled
  203. (+) Handle RNG interrupt request
  204. @endverbatim
  205. * @{
  206. */
  207. /**
  208. * @brief Generates a 32-bit random number.
  209. * @note Each time the random number data is read the RNG_FLAG_DRDY flag
  210. * is automatically cleared.
  211. * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
  212. * the configuration information for RNG.
  213. * @param random32bit: pointer to generated random number variable if successful.
  214. * @retval HAL status
  215. */
  216. HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber(RNG_HandleTypeDef *hrng, uint32_t *random32bit)
  217. {
  218. uint32_t tickstart = 0U;
  219. HAL_StatusTypeDef status = HAL_OK;
  220. /* Process Locked */
  221. __HAL_LOCK(hrng);
  222. /* Check RNG peripheral state */
  223. if(hrng->State == HAL_RNG_STATE_READY)
  224. {
  225. /* Change RNG peripheral state */
  226. hrng->State = HAL_RNG_STATE_BUSY;
  227. /* Get tick */
  228. tickstart = HAL_GetTick();
  229. /* Check if data register contains valid random data */
  230. while(__HAL_RNG_GET_FLAG(hrng, RNG_FLAG_DRDY) == RESET)
  231. {
  232. if((HAL_GetTick() - tickstart ) > RNG_TIMEOUT_VALUE)
  233. {
  234. hrng->State = HAL_RNG_STATE_ERROR;
  235. /* Process Unlocked */
  236. __HAL_UNLOCK(hrng);
  237. return HAL_TIMEOUT;
  238. }
  239. }
  240. /* Get a 32bit Random number */
  241. hrng->RandomNumber = hrng->Instance->DR;
  242. *random32bit = hrng->RandomNumber;
  243. hrng->State = HAL_RNG_STATE_READY;
  244. }
  245. else
  246. {
  247. status = HAL_ERROR;
  248. }
  249. /* Process Unlocked */
  250. __HAL_UNLOCK(hrng);
  251. return status;
  252. }
  253. /**
  254. * @brief Generates a 32-bit random number in interrupt mode.
  255. * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
  256. * the configuration information for RNG.
  257. * @retval HAL status
  258. */
  259. HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber_IT(RNG_HandleTypeDef *hrng)
  260. {
  261. HAL_StatusTypeDef status = HAL_OK;
  262. /* Process Locked */
  263. __HAL_LOCK(hrng);
  264. /* Check RNG peripheral state */
  265. if(hrng->State == HAL_RNG_STATE_READY)
  266. {
  267. /* Change RNG peripheral state */
  268. hrng->State = HAL_RNG_STATE_BUSY;
  269. /* Process Unlocked */
  270. __HAL_UNLOCK(hrng);
  271. /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */
  272. __HAL_RNG_ENABLE_IT(hrng);
  273. }
  274. else
  275. {
  276. /* Process Unlocked */
  277. __HAL_UNLOCK(hrng);
  278. status = HAL_ERROR;
  279. }
  280. return status;
  281. }
  282. /**
  283. * @brief Handles RNG interrupt request.
  284. * @note In the case of a clock error, the RNG is no more able to generate
  285. * random numbers because the PLL48CLK clock is not correct. User has
  286. * to check that the clock controller is correctly configured to provide
  287. * the RNG clock and clear the CEIS bit using __HAL_RNG_CLEAR_IT().
  288. * The clock error has no impact on the previously generated
  289. * random numbers, and the RNG_DR register contents can be used.
  290. * @note In the case of a seed error, the generation of random numbers is
  291. * interrupted as long as the SECS bit is '1'. If a number is
  292. * available in the RNG_DR register, it must not be used because it may
  293. * not have enough entropy. In this case, it is recommended to clear the
  294. * SEIS bit using __HAL_RNG_CLEAR_IT(), then disable and enable
  295. * the RNG peripheral to reinitialize and restart the RNG.
  296. * @note User-written HAL_RNG_ErrorCallback() API is called once whether SEIS
  297. * or CEIS are set.
  298. * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
  299. * the configuration information for RNG.
  300. * @retval None
  301. */
  302. void HAL_RNG_IRQHandler(RNG_HandleTypeDef *hrng)
  303. {
  304. /* RNG clock error interrupt occurred */
  305. if((__HAL_RNG_GET_IT(hrng, RNG_IT_CEI) != RESET) || (__HAL_RNG_GET_IT(hrng, RNG_IT_SEI) != RESET))
  306. {
  307. /* Change RNG peripheral state */
  308. hrng->State = HAL_RNG_STATE_ERROR;
  309. HAL_RNG_ErrorCallback(hrng);
  310. /* Clear the clock error flag */
  311. __HAL_RNG_CLEAR_IT(hrng, RNG_IT_CEI|RNG_IT_SEI);
  312. }
  313. /* Check RNG data ready interrupt occurred */
  314. if(__HAL_RNG_GET_IT(hrng, RNG_IT_DRDY) != RESET)
  315. {
  316. /* Generate random number once, so disable the IT */
  317. __HAL_RNG_DISABLE_IT(hrng);
  318. /* Get the 32bit Random number (DRDY flag automatically cleared) */
  319. hrng->RandomNumber = hrng->Instance->DR;
  320. if(hrng->State != HAL_RNG_STATE_ERROR)
  321. {
  322. /* Change RNG peripheral state */
  323. hrng->State = HAL_RNG_STATE_READY;
  324. /* Data Ready callback */
  325. HAL_RNG_ReadyDataCallback(hrng, hrng->RandomNumber);
  326. }
  327. }
  328. }
  329. /**
  330. * @brief Returns generated random number in polling mode (Obsolete)
  331. * Use HAL_RNG_GenerateRandomNumber() API instead.
  332. * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
  333. * the configuration information for RNG.
  334. * @retval Random value
  335. */
  336. uint32_t HAL_RNG_GetRandomNumber(RNG_HandleTypeDef *hrng)
  337. {
  338. if(HAL_RNG_GenerateRandomNumber(hrng, &(hrng->RandomNumber)) == HAL_OK)
  339. {
  340. return hrng->RandomNumber;
  341. }
  342. else
  343. {
  344. return 0U;
  345. }
  346. }
  347. /**
  348. * @brief Returns a 32-bit random number with interrupt enabled (Obsolete),
  349. * Use HAL_RNG_GenerateRandomNumber_IT() API instead.
  350. * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
  351. * the configuration information for RNG.
  352. * @retval 32-bit random number
  353. */
  354. uint32_t HAL_RNG_GetRandomNumber_IT(RNG_HandleTypeDef *hrng)
  355. {
  356. uint32_t random32bit = 0U;
  357. /* Process locked */
  358. __HAL_LOCK(hrng);
  359. /* Change RNG peripheral state */
  360. hrng->State = HAL_RNG_STATE_BUSY;
  361. /* Get a 32bit Random number */
  362. random32bit = hrng->Instance->DR;
  363. /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */
  364. __HAL_RNG_ENABLE_IT(hrng);
  365. /* Return the 32 bit random number */
  366. return random32bit;
  367. }
  368. /**
  369. * @brief Read latest generated random number.
  370. * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
  371. * the configuration information for RNG.
  372. * @retval random value
  373. */
  374. uint32_t HAL_RNG_ReadLastRandomNumber(RNG_HandleTypeDef *hrng)
  375. {
  376. return(hrng->RandomNumber);
  377. }
  378. /**
  379. * @brief Data Ready callback in non-blocking mode.
  380. * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
  381. * the configuration information for RNG.
  382. * @param random32bit: generated random number.
  383. * @retval None
  384. */
  385. __weak void HAL_RNG_ReadyDataCallback(RNG_HandleTypeDef *hrng, uint32_t random32bit)
  386. {
  387. /* Prevent unused argument(s) compilation warning */
  388. UNUSED(hrng);
  389. UNUSED(random32bit);
  390. /* NOTE : This function should not be modified. When the callback is needed,
  391. function HAL_RNG_ReadyDataCallback must be implemented in the user file.
  392. */
  393. }
  394. /**
  395. * @brief RNG error callbacks.
  396. * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
  397. * the configuration information for RNG.
  398. * @retval None
  399. */
  400. __weak void HAL_RNG_ErrorCallback(RNG_HandleTypeDef *hrng)
  401. {
  402. /* Prevent unused argument(s) compilation warning */
  403. UNUSED(hrng);
  404. /* NOTE : This function should not be modified. When the callback is needed,
  405. function HAL_RNG_ErrorCallback must be implemented in the user file.
  406. */
  407. }
  408. /**
  409. * @}
  410. */
  411. /** @addtogroup RNG_Exported_Functions_Group3
  412. * @brief Peripheral State functions
  413. *
  414. @verbatim
  415. ===============================================================================
  416. ##### Peripheral State functions #####
  417. ===============================================================================
  418. [..]
  419. This subsection permits to get in run-time the status of the peripheral
  420. and the data flow.
  421. @endverbatim
  422. * @{
  423. */
  424. /**
  425. * @brief Returns the RNG state.
  426. * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
  427. * the configuration information for RNG.
  428. * @retval HAL state
  429. */
  430. HAL_RNG_StateTypeDef HAL_RNG_GetState(RNG_HandleTypeDef *hrng)
  431. {
  432. return hrng->State;
  433. }
  434. /**
  435. * @}
  436. */
  437. /**
  438. * @}
  439. */
  440. #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx ||\
  441. STM32F429xx || STM32F439xx || STM32F410xx || STM32F469xx || STM32F479xx || STM32F412Zx ||\
  442. STM32F412Vx || STM32F412Rx || STM32F412Cx || STM32F413xx || STM32F423xx */
  443. //#endif /* HAL_RNG_MODULE_ENABLED */
  444. /**
  445. * @}
  446. */
  447. /**
  448. * @}
  449. */
  450. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/