]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F4/stm32f4xx_hal_rng.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32F4 / stm32f4xx_hal_rng.c
1 /**
2 ******************************************************************************
3 * @file stm32f4xx_hal_rng.c
4 * @author MCD Application Team
5 * @version V1.1.0
6 * @date 19-June-2014
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
21 (#) Enable the RNG controller clock using __RNG_CLK_ENABLE() macro.
22 (#) Activate the RNG peripheral using __HAL_RNG_ENABLE() macro.
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_GetRandomNumber() function.
26
27 @endverbatim
28 ******************************************************************************
29 * @attention
30 *
31 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
32 *
33 * Redistribution and use in source and binary forms, with or without modification,
34 * are permitted provided that the following conditions are met:
35 * 1. Redistributions of source code must retain the above copyright notice,
36 * this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright notice,
38 * this list of conditions and the following disclaimer in the documentation
39 * and/or other materials provided with the distribution.
40 * 3. Neither the name of STMicroelectronics nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
45 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
47 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
50 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
51 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
52 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 *
55 ******************************************************************************
56 */
57
58 /* Includes ------------------------------------------------------------------*/
59 #include "stm32f4xx_hal.h"
60
61 /** @addtogroup STM32F4xx_HAL_Driver
62 * @{
63 */
64
65 /** @defgroup RNG
66 * @brief RNG HAL module driver.
67 * @{
68 */
69
70 #ifdef HAL_RNG_MODULE_ENABLED
71
72 #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) ||\
73 defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)
74
75 /* Private typedef -----------------------------------------------------------*/
76 /* Private define ------------------------------------------------------------*/
77 #define RNG_TIMEOUT_VALUE 1000
78 /* Private macro -------------------------------------------------------------*/
79 /* Private variables ---------------------------------------------------------*/
80 /* Private function prototypes -----------------------------------------------*/
81 /* Private functions ---------------------------------------------------------*/
82
83 /** @defgroup RNG_Private_Functions
84 * @{
85 */
86
87 /** @defgroup RNG_Group1 Initialization and de-initialization functions
88 * @brief Initialization and Configuration 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
101 @endverbatim
102 * @{
103 */
104
105 /**
106 * @brief Initializes the RNG according to the specified
107 * parameters in the RNG_InitTypeDef and creates the associated handle.
108 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
109 * the configuration information for RNG.
110 * @retval HAL status
111 */
112 HAL_StatusTypeDef HAL_RNG_Init(RNG_HandleTypeDef *hrng)
113 {
114 /* Check the RNG handle allocation */
115 if(hrng == HAL_NULL)
116 {
117 return HAL_ERROR;
118 }
119
120 if(hrng->State == HAL_RNG_STATE_RESET)
121 {
122 /* Init the low level hardware */
123 HAL_RNG_MspInit(hrng);
124 }
125 /* Change RNG peripheral state */
126 hrng->State = HAL_RNG_STATE_BUSY;
127
128 /* Enable the RNG Peripheral */
129 __HAL_RNG_ENABLE(hrng);
130
131 /* Initialize the RNG state */
132 hrng->State = HAL_RNG_STATE_READY;
133
134 /* Return function status */
135 return HAL_OK;
136 }
137
138 /**
139 * @brief DeInitializes the RNG peripheral.
140 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
141 * the configuration information for RNG.
142 * @retval HAL status
143 */
144 HAL_StatusTypeDef HAL_RNG_DeInit(RNG_HandleTypeDef *hrng)
145 {
146 /* Check the RNG peripheral state */
147 if(hrng->State == HAL_RNG_STATE_BUSY)
148 {
149 return HAL_BUSY;
150 }
151
152 /* Update the RNG state */
153 hrng->State = HAL_RNG_STATE_BUSY;
154
155 /* Disable the RNG Peripheral */
156 __HAL_RNG_DISABLE(hrng);
157
158 /* Set the RNG registers to their reset values */
159 hrng->Instance->CR &= 0xFFFFFFF3;
160 hrng->Instance->SR &= 0xFFFFFF98;
161 hrng->Instance->DR &= 0x0;
162
163 /* DeInit the low level hardware */
164 HAL_RNG_MspDeInit(hrng);
165
166 /* Update the RNG state */
167 hrng->State = HAL_RNG_STATE_RESET;
168
169 /* Release Lock */
170 __HAL_UNLOCK(hrng);
171
172 /* Return the function status */
173 return HAL_OK;
174 }
175
176 /**
177 * @brief Initializes the RNG MSP.
178 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
179 * the configuration information for RNG.
180 * @retval None
181 */
182 __weak void HAL_RNG_MspInit(RNG_HandleTypeDef *hrng)
183 {
184 /* NOTE : This function Should not be modified, when the callback is needed,
185 the HAL_RNG_MspInit could be implemented in the user file
186 */
187 }
188
189 /**
190 * @brief DeInitializes the RNG MSP.
191 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
192 * the configuration information for RNG.
193 * @retval None
194 */
195 __weak void HAL_RNG_MspDeInit(RNG_HandleTypeDef *hrng)
196 {
197 /* NOTE : This function Should not be modified, when the callback is needed,
198 the HAL_RNG_MspDeInit could be implemented in the user file
199 */
200 }
201
202 /**
203 * @}
204 */
205
206 /** @defgroup RNG_Group2 Peripheral Control functions
207 * @brief management functions.
208 *
209 @verbatim
210 ===============================================================================
211 ##### Peripheral Control functions #####
212 ===============================================================================
213 [..] This section provides functions allowing to:
214 (+) Get the 32 bit Random number
215 (+) Get the 32 bit Random number with interrupt enabled
216 (+) Handle RNG interrupt request
217
218
219 @endverbatim
220 * @{
221 */
222
223 /**
224 * @brief Returns a 32-bit random number.
225 * @note Each time the random number data is read the RNG_FLAG_DRDY flag
226 * is automatically cleared.
227 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
228 * the configuration information for RNG.
229 * @retval 32-bit random number
230 */
231 uint32_t HAL_RNG_GetRandomNumber(RNG_HandleTypeDef *hrng)
232 {
233 uint32_t random32bit = 0;
234 uint32_t tickstart = 0;
235
236 /* Process Locked */
237 __HAL_LOCK(hrng);
238
239 /* Get tick */
240 tickstart = HAL_GetTick();
241
242 /* Check if data register contains valid random data */
243 while(__HAL_RNG_GET_FLAG(hrng, RNG_FLAG_DRDY) == RESET)
244 {
245 if((HAL_GetTick() - tickstart ) > RNG_TIMEOUT_VALUE)
246 {
247 return HAL_TIMEOUT;
248 }
249 }
250
251 /* Get a 32bit Random number */
252 random32bit = hrng->Instance->DR;
253
254 /* Process Unlocked */
255 __HAL_UNLOCK(hrng);
256
257 /* Return the 32 bit random number */
258 return random32bit;
259 }
260
261 /**
262 * @brief Returns a 32-bit random number with interrupt enabled.
263 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
264 * the configuration information for RNG.
265 * @retval 32-bit random number
266 */
267 uint32_t HAL_RNG_GetRandomNumber_IT(RNG_HandleTypeDef *hrng)
268 {
269 uint32_t random32bit = 0;
270
271 /* Process Locked */
272 __HAL_LOCK(hrng);
273
274 /* Change RNG peripheral state */
275 hrng->State = HAL_RNG_STATE_BUSY;
276
277 /* Get a 32bit Random number */
278 random32bit = hrng->Instance->DR;
279
280 /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */
281 __HAL_RNG_ENABLE_IT(hrng);
282
283 /* Return the 32 bit random number */
284 return random32bit;
285 }
286
287 /**
288 * @brief Handles RNG interrupt request.
289 * @note In the case of a clock error, the RNG is no more able to generate
290 * random numbers because the PLL48CLK clock is not correct. User has
291 * to check that the clock controller is correctly configured to provide
292 * the RNG clock and clear the CEIS bit using __HAL_RNG_CLEAR_FLAG().
293 * The clock error has no impact on the previously generated
294 * random numbers, and the RNG_DR register contents can be used.
295 * @note In the case of a seed error, the generation of random numbers is
296 * interrupted as long as the SECS bit is '1'. If a number is
297 * available in the RNG_DR register, it must not be used because it may
298 * not have enough entropy. In this case, it is recommended to clear the
299 * SEIS bit using __HAL_RNG_CLEAR_FLAG(), then disable and enable
300 * the RNG peripheral to reinitialize and restart the RNG.
301 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
302 * the configuration information for RNG.
303 * @retval None
304
305 */
306 void HAL_RNG_IRQHandler(RNG_HandleTypeDef *hrng)
307 {
308 /* RNG clock error interrupt occurred */
309 if(__HAL_RNG_GET_FLAG(hrng, RNG_IT_CEI) != RESET)
310 {
311 HAL_RNG_ErrorCallback(hrng);
312
313 /* Clear the clock error flag */
314 __HAL_RNG_CLEAR_FLAG(hrng, RNG_IT_CEI);
315
316 /* Change RNG peripheral state */
317 hrng->State = HAL_RNG_STATE_ERROR;
318
319 /* Process Unlocked */
320 __HAL_UNLOCK(hrng);
321 }
322
323 /* RNG seed error interrupt occurred */
324 if(__HAL_RNG_GET_FLAG(hrng, RNG_IT_SEI) != RESET)
325 {
326 HAL_RNG_ErrorCallback(hrng);
327
328 /* Clear the seed error flag */
329 __HAL_RNG_CLEAR_FLAG(hrng, RNG_IT_SEI);
330
331 /* Change RNG peripheral state */
332 hrng->State = HAL_RNG_STATE_ERROR;
333
334 /* Process Unlocked */
335 __HAL_UNLOCK(hrng);
336 }
337
338 /* Check RNG data ready flag */
339 if(__HAL_RNG_GET_FLAG(hrng, RNG_FLAG_DRDY) != RESET)
340 {
341 /* Data Ready callback */
342 HAL_RNG_ReadyCallback(hrng);
343
344 /* Change RNG peripheral state */
345 hrng->State = HAL_RNG_STATE_READY;
346
347 /* Clear the RNG Data Ready flag */
348 __HAL_RNG_CLEAR_FLAG(hrng, RNG_FLAG_DRDY);
349
350 /* Process Unlocked */
351 __HAL_UNLOCK(hrng);
352 }
353 }
354
355 /**
356 * @brief Data Ready callback in non-blocking mode.
357 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
358 * the configuration information for RNG.
359 * @retval None
360 */
361
362 __weak void HAL_RNG_ReadyCallback(RNG_HandleTypeDef* hrng)
363 {
364 /* NOTE : This function Should not be modified, when the callback is needed,
365 the HAL_RNG_ReadyCallback could be implemented in the user file
366 */
367 }
368
369 /**
370 * @brief RNG error callbacks.
371 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
372 * the configuration information for RNG.
373 * @retval None
374 */
375 __weak void HAL_RNG_ErrorCallback(RNG_HandleTypeDef *hrng)
376 {
377 /* NOTE : This function Should not be modified, when the callback is needed,
378 the HAL_RNG_ErrorCallback could be implemented in the user file
379 */
380 }
381
382 /**
383 * @}
384 */
385
386 /** @defgroup RNG_Group3 Peripheral State functions
387 * @brief Peripheral State functions.
388 *
389 @verbatim
390 ===============================================================================
391 ##### Peripheral State functions #####
392 ===============================================================================
393 [..]
394 This subsection permits to get in run-time the status of the peripheral
395 and the data flow.
396
397 @endverbatim
398 * @{
399 */
400
401 /**
402 * @brief Returns the RNG state.
403 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
404 * the configuration information for RNG.
405 * @retval HAL state
406 */
407 HAL_RNG_StateTypeDef HAL_RNG_GetState(RNG_HandleTypeDef *hrng)
408 {
409 return hrng->State;
410 }
411
412 /**
413 * @}
414 */
415
416 /**
417 * @}
418 */
419
420 #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx */
421
422 #endif /* HAL_RNG_MODULE_ENABLED */
423 /**
424 * @}
425 */
426
427 /**
428 * @}
429 */
430
431 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Imprint / Impressum