You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

523 lines
16 KiB

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