stm32l4xx_hal_rng.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_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 "stm32l4xx_hal.h"
  56. /** @addtogroup STM32L4xx_HAL_Driver
  57. * @{
  58. */
  59. /** @defgroup RNG RNG
  60. * @brief RNG HAL module driver.
  61. * @{
  62. */
  63. #ifdef HAL_RNG_MODULE_ENABLED
  64. /* Private types -------------------------------------------------------------*/
  65. /* Private defines -----------------------------------------------------------*/
  66. /** @defgroup RNG_Private_Constants RNG_Private_Constants
  67. * @{
  68. */
  69. #define RNG_TIMEOUT_VALUE 2
  70. /**
  71. * @}
  72. */
  73. /* Private macros ------------------------------------------------------------*/
  74. /* Private variables ---------------------------------------------------------*/
  75. /* Private function 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 (MCU Specific Package)
  93. (+) DeInitialize the RNG MSP
  94. @endverbatim
  95. * @{
  96. */
  97. /**
  98. * @brief Initialize the RNG peripheral and initialize the associated handle.
  99. * @param hrng: pointer to a RNG_HandleTypeDef structure.
  100. * @retval HAL status
  101. */
  102. HAL_StatusTypeDef HAL_RNG_Init(RNG_HandleTypeDef *hrng)
  103. {
  104. /* Check the RNG handle allocation */
  105. if(hrng == NULL)
  106. {
  107. return HAL_ERROR;
  108. }
  109. assert_param(IS_RNG_ALL_INSTANCE(hrng->Instance));
  110. #if defined(RNG_CR_CED)
  111. assert_param(IS_RNG_CED(hrng->Init.ClockErrorDetection));
  112. #endif /* defined(RNG_CR_CED) */
  113. if(hrng->State == HAL_RNG_STATE_RESET)
  114. {
  115. /* Allocate lock resource and initialize it */
  116. hrng->Lock = HAL_UNLOCKED;
  117. /* Init the low level hardware */
  118. HAL_RNG_MspInit(hrng);
  119. }
  120. /* Change RNG peripheral state */
  121. hrng->State = HAL_RNG_STATE_BUSY;
  122. #if defined(RNG_CR_CED)
  123. /* Clock Error Detection configuration */
  124. MODIFY_REG(hrng->Instance->CR, RNG_CR_CED, hrng->Init.ClockErrorDetection);
  125. #endif /* defined(RNG_CR_CED) */
  126. /* Enable the RNG Peripheral */
  127. __HAL_RNG_ENABLE(hrng);
  128. /* Initialize the RNG state */
  129. hrng->State = HAL_RNG_STATE_READY;
  130. /* Return function status */
  131. return HAL_OK;
  132. }
  133. /**
  134. * @brief DeInitialize the RNG peripheral.
  135. * @param hrng: pointer to a RNG_HandleTypeDef structure.
  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. #if defined(RNG_CR_CED)
  146. /* Clear Clock Error Detection bit */
  147. CLEAR_BIT(hrng->Instance->CR, RNG_CR_CED);
  148. #endif /* defined(RNG_CR_CED) */
  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 Initialize the RNG MSP.
  164. * @param hrng: pointer to a RNG_HandleTypeDef structure.
  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 DeInitialize the RNG MSP.
  177. * @param hrng: pointer to a RNG_HandleTypeDef structure.
  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 Management 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 Generate 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.
  210. * @param random32bit: pointer to generated random number variable if successful.
  211. * @retval HAL status
  212. */
  213. HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber(RNG_HandleTypeDef *hrng, uint32_t *random32bit)
  214. {
  215. uint32_t tickstart = 0;
  216. HAL_StatusTypeDef status = HAL_OK;
  217. /* Process Locked */
  218. __HAL_LOCK(hrng);
  219. /* Check RNS peripheral state */
  220. if(hrng->State == HAL_RNG_STATE_READY)
  221. {
  222. /* Change RNG peripheral state */
  223. hrng->State = HAL_RNG_STATE_BUSY;
  224. /* Get tick */
  225. tickstart = HAL_GetTick();
  226. /* Check if data register contains valid random data */
  227. while(__HAL_RNG_GET_FLAG(hrng, RNG_FLAG_DRDY) == RESET)
  228. {
  229. if((HAL_GetTick() - tickstart ) > RNG_TIMEOUT_VALUE)
  230. {
  231. hrng->State = HAL_RNG_STATE_ERROR;
  232. /* Process Unlocked */
  233. __HAL_UNLOCK(hrng);
  234. return HAL_TIMEOUT;
  235. }
  236. }
  237. /* Get a 32bit Random number */
  238. hrng->RandomNumber = hrng->Instance->DR;
  239. *random32bit = hrng->RandomNumber;
  240. hrng->State = HAL_RNG_STATE_READY;
  241. }
  242. else
  243. {
  244. status = HAL_ERROR;
  245. }
  246. /* Process Unlocked */
  247. __HAL_UNLOCK(hrng);
  248. return status;
  249. }
  250. /**
  251. * @brief Generate a 32-bit random number in interrupt mode.
  252. * @param hrng: pointer to a RNG_HandleTypeDef structure.
  253. * @retval HAL status
  254. */
  255. HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber_IT(RNG_HandleTypeDef *hrng)
  256. {
  257. HAL_StatusTypeDef status = HAL_OK;
  258. /* Process Locked */
  259. __HAL_LOCK(hrng);
  260. /* Check RNG peripheral state */
  261. if(hrng->State == HAL_RNG_STATE_READY)
  262. {
  263. /* Change RNG peripheral state */
  264. hrng->State = HAL_RNG_STATE_BUSY;
  265. /* Process Unlocked */
  266. __HAL_UNLOCK(hrng);
  267. /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */
  268. __HAL_RNG_ENABLE_IT(hrng);
  269. }
  270. else
  271. {
  272. /* Process Unlocked */
  273. __HAL_UNLOCK(hrng);
  274. status = HAL_ERROR;
  275. }
  276. return status;
  277. }
  278. /**
  279. * @brief Handle RNG interrupt request.
  280. * @note In the case of a clock error, the RNG is no more able to generate
  281. * random numbers because the PLL48CLK clock is not correct. User has
  282. * to check that the clock controller is correctly configured to provide
  283. * the RNG clock and clear the CEIS bit using __HAL_RNG_CLEAR_IT().
  284. * The clock error has no impact on the previously generated
  285. * random numbers, and the RNG_DR register contents can be used.
  286. * @note In the case of a seed error, the generation of random numbers is
  287. * interrupted as long as the SECS bit is '1'. If a number is
  288. * available in the RNG_DR register, it must not be used because it may
  289. * not have enough entropy. In this case, it is recommended to clear the
  290. * SEIS bit using __HAL_RNG_CLEAR_IT(), then disable and enable
  291. * the RNG peripheral to reinitialize and restart the RNG.
  292. * @note User-written HAL_RNG_ErrorCallback() API is called once whether SEIS
  293. * or CEIS are set.
  294. * @param hrng: pointer to a RNG_HandleTypeDef structure.
  295. * @retval None
  296. */
  297. void HAL_RNG_IRQHandler(RNG_HandleTypeDef *hrng)
  298. {
  299. /* RNG clock error interrupt occurred */
  300. if((__HAL_RNG_GET_IT(hrng, RNG_IT_CEI) != RESET) || (__HAL_RNG_GET_IT(hrng, RNG_IT_SEI) != RESET))
  301. {
  302. /* Change RNG peripheral state */
  303. hrng->State = HAL_RNG_STATE_ERROR;
  304. HAL_RNG_ErrorCallback(hrng);
  305. /* Clear the clock error flag */
  306. __HAL_RNG_CLEAR_IT(hrng, RNG_IT_CEI|RNG_IT_SEI);
  307. }
  308. /* Check RNG data ready interrupt occurred */
  309. if(__HAL_RNG_GET_IT(hrng, RNG_IT_DRDY) != RESET)
  310. {
  311. /* Generate random number once, so disable the IT */
  312. __HAL_RNG_DISABLE_IT(hrng);
  313. /* Get the 32bit Random number (DRDY flag automatically cleared) */
  314. hrng->RandomNumber = hrng->Instance->DR;
  315. if(hrng->State != HAL_RNG_STATE_ERROR)
  316. {
  317. /* Change RNG peripheral state */
  318. hrng->State = HAL_RNG_STATE_READY;
  319. /* Data Ready callback */
  320. HAL_RNG_ReadyDataCallback(hrng, hrng->RandomNumber);
  321. }
  322. }
  323. }
  324. /**
  325. * @brief Return generated random number in polling mode (Obsolete).
  326. * @note Use HAL_RNG_GenerateRandomNumber() API instead.
  327. * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
  328. * the configuration information for RNG.
  329. * @retval random value
  330. */
  331. uint32_t HAL_RNG_GetRandomNumber(RNG_HandleTypeDef *hrng)
  332. {
  333. if(HAL_RNG_GenerateRandomNumber(hrng, &(hrng->RandomNumber)) == HAL_OK)
  334. {
  335. return hrng->RandomNumber;
  336. }
  337. else
  338. {
  339. return 0;
  340. }
  341. }
  342. /**
  343. * @brief Return a 32-bit random number with interrupt enabled (Obsolete).
  344. * @note Use HAL_RNG_GenerateRandomNumber_IT() API instead.
  345. * @param hrng: RNG handle
  346. * @retval 32-bit random number
  347. */
  348. uint32_t HAL_RNG_GetRandomNumber_IT(RNG_HandleTypeDef *hrng)
  349. {
  350. uint32_t random32bit = 0;
  351. /* Process locked */
  352. __HAL_LOCK(hrng);
  353. /* Change RNG peripheral state */
  354. hrng->State = HAL_RNG_STATE_BUSY;
  355. /* Get a 32bit Random number */
  356. random32bit = hrng->Instance->DR;
  357. /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */
  358. __HAL_RNG_ENABLE_IT(hrng);
  359. /* Return the 32 bit random number */
  360. return random32bit;
  361. }
  362. /**
  363. * @brief Read latest generated random number.
  364. * @param hrng: pointer to a RNG_HandleTypeDef structure.
  365. * @retval random value
  366. */
  367. uint32_t HAL_RNG_ReadLastRandomNumber(RNG_HandleTypeDef *hrng)
  368. {
  369. return(hrng->RandomNumber);
  370. }
  371. /**
  372. * @brief Data Ready callback in non-blocking mode.
  373. * @param hrng: pointer to a RNG_HandleTypeDef structure.
  374. * @param random32bit: generated random value
  375. * @retval None
  376. */
  377. __weak void HAL_RNG_ReadyDataCallback(RNG_HandleTypeDef *hrng, uint32_t random32bit)
  378. {
  379. /* Prevent unused argument(s) compilation warning */
  380. UNUSED(hrng);
  381. UNUSED(random32bit);
  382. /* NOTE : This function should not be modified. When the callback is needed,
  383. function HAL_RNG_ReadyDataCallback must be implemented in the user file.
  384. */
  385. }
  386. /**
  387. * @brief RNG error callback.
  388. * @param hrng: pointer to a RNG_HandleTypeDef structure.
  389. * @retval None
  390. */
  391. __weak void HAL_RNG_ErrorCallback(RNG_HandleTypeDef *hrng)
  392. {
  393. /* Prevent unused argument(s) compilation warning */
  394. UNUSED(hrng);
  395. /* NOTE : This function should not be modified. When the callback is needed,
  396. function HAL_RNG_ErrorCallback must be implemented in the user file.
  397. */
  398. }
  399. /**
  400. * @}
  401. */
  402. /** @addtogroup RNG_Exported_Functions_Group3
  403. * @brief Peripheral State functions.
  404. *
  405. @verbatim
  406. ===============================================================================
  407. ##### Peripheral State functions #####
  408. ===============================================================================
  409. [..]
  410. This subsection permits to get in run-time the status of the peripheral.
  411. @endverbatim
  412. * @{
  413. */
  414. /**
  415. * @brief Return the RNG handle state.
  416. * @param hrng: pointer to a RNG_HandleTypeDef structure.
  417. * @retval HAL state
  418. */
  419. HAL_RNG_StateTypeDef HAL_RNG_GetState(RNG_HandleTypeDef *hrng)
  420. {
  421. /* Return RNG handle state */
  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****/