stm32f4xx_hal_rng.c 16 KB

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