stm32f4xx_hal_rng.c 16 KB

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