stm32l4xx_hal_rng.c 15 KB

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