]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M7_STM32F7_STM32756G-EVAL_IAR_Keil/ST_Library/stm32f7xx_hal_rcc.c
Final V8.2.1 release ready for tagging:
[freertos] / FreeRTOS / Demo / CORTEX_M7_STM32F7_STM32756G-EVAL_IAR_Keil / ST_Library / stm32f7xx_hal_rcc.c
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32f7xx_hal_rcc.c\r
4   * @author  MCD Application Team\r
5   * @version V1.0.0RC1\r
6   * @date    24-March-2015\r
7   * @brief   RCC HAL module driver.\r
8   *          This file provides firmware functions to manage the following \r
9   *          functionalities of the Reset and Clock Control (RCC) peripheral:\r
10   *           + Initialization and de-initialization functions\r
11   *           + Peripheral Control functions\r
12   *       \r
13   @verbatim                \r
14   ==============================================================================\r
15                       ##### RCC specific features #####\r
16   ==============================================================================\r
17     [..]  \r
18       After reset the device is running from Internal High Speed oscillator \r
19       (HSI 16MHz) with Flash 0 wait state, Flash prefetch buffer, D-Cache \r
20       and I-Cache are disabled, and all peripherals are off except internal\r
21       SRAM, Flash and JTAG.\r
22       (+) There is no prescaler on High speed (AHB) and Low speed (APB) busses;\r
23           all peripherals mapped on these busses are running at HSI speed.\r
24       (+) The clock for all peripherals is switched off, except the SRAM and FLASH.\r
25       (+) All GPIOs are in input floating state, except the JTAG pins which\r
26           are assigned to be used for debug purpose.\r
27     \r
28     [..]          \r
29       Once the device started from reset, the user application has to:        \r
30       (+) Configure the clock source to be used to drive the System clock\r
31           (if the application needs higher frequency/performance)\r
32       (+) Configure the System clock frequency and Flash settings  \r
33       (+) Configure the AHB and APB busses prescalers\r
34       (+) Enable the clock for the peripheral(s) to be used\r
35       (+) Configure the clock source(s) for peripherals which clocks are not\r
36           derived from the System clock (I2S, RTC, ADC, USB OTG FS/SDIO/RNG)\r
37 \r
38                       ##### RCC Limitations #####\r
39   ==============================================================================\r
40     [..]  \r
41       A delay between an RCC peripheral clock enable and the effective peripheral \r
42       enabling should be taken into account in order to manage the peripheral read/write \r
43       from/to registers.\r
44       (+) This delay depends on the peripheral mapping.\r
45       (+) If peripheral is mapped on AHB: the delay is 2 AHB clock cycle \r
46           after the clock enable bit is set on the hardware register\r
47       (+) If peripheral is mapped on APB: the delay is 2 APB clock cycle \r
48           after the clock enable bit is set on the hardware register\r
49 \r
50     [..]  \r
51       Workarounds:\r
52           (#) For AHB & APB peripherals, a dummy read to the peripheral register has been\r
53           inserted in each __HAL_RCC_PPP_CLK_ENABLE() macro.\r
54 \r
55   @endverbatim\r
56   ******************************************************************************\r
57   * @attention\r
58   *\r
59   * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>\r
60   *\r
61   * Redistribution and use in source and binary forms, with or without modification,\r
62   * are permitted provided that the following conditions are met:\r
63   *   1. Redistributions of source code must retain the above copyright notice,\r
64   *      this list of conditions and the following disclaimer.\r
65   *   2. Redistributions in binary form must reproduce the above copyright notice,\r
66   *      this list of conditions and the following disclaimer in the documentation\r
67   *      and/or other materials provided with the distribution.\r
68   *   3. Neither the name of STMicroelectronics nor the names of its contributors\r
69   *      may be used to endorse or promote products derived from this software\r
70   *      without specific prior written permission.\r
71   *\r
72   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"\r
73   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
74   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
75   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\r
76   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
77   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\r
78   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\r
79   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\r
80   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\r
81   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
82   *\r
83   ******************************************************************************\r
84   */ \r
85 \r
86 /* Includes ------------------------------------------------------------------*/\r
87 #include "stm32f7xx_hal.h"\r
88 \r
89 /** @addtogroup STM32F7xx_HAL_Driver\r
90   * @{\r
91   */\r
92 \r
93 /** @defgroup RCC RCC\r
94   * @brief RCC HAL module driver\r
95   * @{\r
96   */\r
97 \r
98 #ifdef HAL_RCC_MODULE_ENABLED\r
99 \r
100 /* Private typedef -----------------------------------------------------------*/\r
101 /* Private define ------------------------------------------------------------*/\r
102 /* Private macro -------------------------------------------------------------*/\r
103 /** @defgroup RCC_Private_Macros RCC Private Macros\r
104   * @{\r
105   */\r
106 \r
107 #define MCO1_CLK_ENABLE()   __HAL_RCC_GPIOA_CLK_ENABLE()\r
108 #define MCO1_GPIO_PORT        GPIOA\r
109 #define MCO1_PIN              GPIO_PIN_8\r
110 \r
111 #define MCO2_CLK_ENABLE()   __HAL_RCC_GPIOC_CLK_ENABLE()\r
112 #define MCO2_GPIO_PORT         GPIOC\r
113 #define MCO2_PIN               GPIO_PIN_9\r
114 \r
115 /**\r
116   * @}\r
117   */\r
118 /* Private variables ---------------------------------------------------------*/\r
119 /** @defgroup RCC_Private_Variables RCC Private Variables\r
120   * @{\r
121   */\r
122 const uint8_t APBAHBPrescTable[16] = {0, 0, 0, 0, 1, 2, 3, 4, 1, 2, 3, 4, 6, 7, 8, 9};\r
123 \r
124 /**\r
125   * @}\r
126   */\r
127 \r
128 /* Private function prototypes -----------------------------------------------*/\r
129 /* Exported functions ---------------------------------------------------------*/\r
130 \r
131 /** @defgroup RCC_Exported_Functions RCC Exported Functions\r
132   * @{\r
133   */\r
134 \r
135 /** @defgroup RCC_Exported_Functions_Group1 Initialization and de-initialization functions \r
136   *  @brief    Initialization and Configuration functions \r
137   *\r
138   @verbatim    \r
139   ===============================================================================\r
140 ##### Initialization and de-initialization functions #####\r
141   ===============================================================================\r
142     [..]\r
143       This section provides functions allowing to configure the internal/external oscillators\r
144       (HSE, HSI, LSE, LSI, PLL, CSS and MCO) and the System buses clocks (SYSCLK, AHB, APB1 \r
145       and APB2).\r
146 \r
147     [..] Internal/external clock and PLL configuration\r
148       (#) HSI (high-speed internal), 16 MHz factory-trimmed RC used directly or through\r
149           the PLL as System clock source.\r
150 \r
151       (#) LSI (low-speed internal), 32 KHz low consumption RC used as IWDG and/or RTC\r
152           clock source.\r
153 \r
154       (#) HSE (high-speed external), 4 to 26 MHz crystal oscillator used directly or\r
155           through the PLL as System clock source. Can be used also as RTC clock source.\r
156 \r
157       (#) LSE (low-speed external), 32 KHz oscillator used as RTC clock source.   \r
158 \r
159       (#) PLL (clocked by HSI or HSE), featuring two different output clocks:\r
160         (++) The first output is used to generate the high speed system clock (up to 200 MHz)\r
161         (++) The second output is used to generate the clock for the USB OTG FS (48 MHz),\r
162              the random analog generator (<=48 MHz) and the SDIO (<= 48 MHz).\r
163 \r
164       (#) CSS (Clock security system), once enable using the function HAL_RCC_EnableCSS()\r
165           and if a HSE clock failure occurs(HSE used directly or through PLL as System \r
166           clock source), the System clock is automatically switched to HSI and an interrupt\r
167           is generated if enabled. The interrupt is linked to the Cortex-M7 NMI \r
168           (Non-Maskable Interrupt) exception vector.   \r
169 \r
170       (#) MCO1 (microcontroller clock output), used to output HSI, LSE, HSE or PLL\r
171           clock (through a configurable prescaler) on PA8 pin.\r
172 \r
173       (#) MCO2 (microcontroller clock output), used to output HSE, PLL, SYSCLK or PLLI2S\r
174           clock (through a configurable prescaler) on PC9 pin.\r
175 \r
176     [..] System, AHB and APB busses clocks configuration  \r
177       (#) Several clock sources can be used to drive the System clock (SYSCLK): HSI,\r
178           HSE and PLL.\r
179           The AHB clock (HCLK) is derived from System clock through configurable \r
180           prescaler and used to clock the CPU, memory and peripherals mapped \r
181           on AHB bus (DMA, GPIO...). APB1 (PCLK1) and APB2 (PCLK2) clocks are derived \r
182           from AHB clock through configurable prescalers and used to clock \r
183           the peripherals mapped on these busses. You can use \r
184           "HAL_RCC_GetSysClockFreq()" function to retrieve the frequencies of these clocks.  \r
185 \r
186       -@- All the peripheral clocks are derived from the System clock (SYSCLK) except:\r
187           (+@) I2S: the I2S clock can be derived either from a specific PLL (PLLI2S) or\r
188               from an external clock mapped on the I2S_CKIN pin. \r
189               You have to use __HAL_RCC_PLLI2S_CONFIG() macro to configure this clock.\r
190           (+@)  SAI: the SAI clock can be derived either from a specific PLL (PLLI2S) or (PLLSAI) or\r
191               from an external clock mapped on the I2S_CKIN pin. \r
192                You have to use __HAL_RCC_PLLI2S_CONFIG() macro to configure this clock. \r
193           (+@) RTC: the RTC clock can be derived either from the LSI, LSE or HSE clock\r
194               divided by 2 to 31. You have to use __HAL_RCC_RTC_CONFIG() and __HAL_RCC_RTC_ENABLE()\r
195               macros to configure this clock. \r
196           (+@) USB OTG FS, SDIO and RTC: USB OTG FS require a frequency equal to 48 MHz\r
197               to work correctly, while the SDIO require a frequency equal or lower than\r
198               to 48. This clock is derived of the main PLL through PLLQ divider.\r
199           (+@) IWDG clock which is always the LSI clock.\r
200 @endverbatim\r
201   * @{\r
202   */\r
203 \r
204 /**\r
205   * @brief  Resets the RCC clock configuration to the default reset state.\r
206   * @note   The default reset state of the clock configuration is given below:\r
207   *            - HSI ON and used as system clock source\r
208   *            - HSE, PLL and PLLI2S OFF\r
209   *            - AHB, APB1 and APB2 prescaler set to 1.\r
210   *            - CSS, MCO1 and MCO2 OFF\r
211   *            - All interrupts disabled\r
212   * @note   This function doesn't modify the configuration of the\r
213   *            - Peripheral clocks  \r
214   *            - LSI, LSE and RTC clocks \r
215   * @retval None\r
216   */\r
217 void HAL_RCC_DeInit(void)\r
218 {\r
219   /* Set HSION bit */\r
220   SET_BIT(RCC->CR, RCC_CR_HSION | RCC_CR_HSITRIM_4); \r
221   \r
222   /* Reset CFGR register */\r
223   CLEAR_REG(RCC->CFGR);\r
224   \r
225   /* Reset HSEON, CSSON, PLLON, PLLI2S */\r
226   CLEAR_BIT(RCC->CR, RCC_CR_HSEON | RCC_CR_CSSON | RCC_CR_PLLON| RCC_CR_PLLI2SON); \r
227   \r
228   /* Reset PLLCFGR register */\r
229   CLEAR_REG(RCC->PLLCFGR);\r
230   SET_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLM_4 | RCC_PLLCFGR_PLLN_6 | RCC_PLLCFGR_PLLN_7 | RCC_PLLCFGR_PLLQ_2); \r
231   \r
232   /* Reset PLLI2SCFGR register */\r
233   CLEAR_REG(RCC->PLLI2SCFGR);\r
234   SET_BIT(RCC->PLLI2SCFGR,  RCC_PLLI2SCFGR_PLLI2SN_6 | RCC_PLLI2SCFGR_PLLI2SN_7 | RCC_PLLI2SCFGR_PLLI2SR_1);\r
235   \r
236   /* Reset HSEBYP bit */\r
237   CLEAR_BIT(RCC->CR, RCC_CR_HSEBYP);\r
238   \r
239   /* Disable all interrupts */\r
240   CLEAR_REG(RCC->CIR);\r
241 }\r
242 \r
243 /**\r
244   * @brief  Initializes the RCC Oscillators according to the specified parameters in the\r
245   *         RCC_OscInitTypeDef.\r
246   * @param  RCC_OscInitStruct: pointer to an RCC_OscInitTypeDef structure that\r
247   *         contains the configuration information for the RCC Oscillators.\r
248   * @note   The PLL is not disabled when used as system clock.\r
249   * @retval HAL status\r
250   */\r
251 HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef  *RCC_OscInitStruct)\r
252 {\r
253   uint32_t tickstart = 0;  \r
254  \r
255   /* Check the parameters */\r
256   assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType));\r
257   \r
258   /*------------------------------- HSE Configuration ------------------------*/ \r
259   if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE)\r
260   {\r
261     /* Check the parameters */\r
262     assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState));\r
263     /* When the HSE is used as system clock or clock source for PLL, It can not be disabled */\r
264     if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_HSE) \r
265        || ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && ((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLCFGR_PLLSRC_HSE)))\r
266     {\r
267           if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET) && (RCC_OscInitStruct->HSEState == RCC_HSE_OFF))\r
268       {\r
269         return HAL_ERROR;\r
270       }\r
271     }\r
272     else\r
273     {\r
274       /* Reset HSEON and HSEBYP bits before configuring the HSE --------------*/\r
275       __HAL_RCC_HSE_CONFIG(RCC_HSE_OFF);\r
276       \r
277       /* Get Start Tick*/\r
278       tickstart = HAL_GetTick();\r
279       \r
280       /* Wait till HSE is disabled */  \r
281       while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET)\r
282       {\r
283         if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)\r
284         {\r
285           return HAL_TIMEOUT;\r
286         }\r
287       }\r
288       \r
289       /* Set the new HSE configuration ---------------------------------------*/\r
290       __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState);\r
291       \r
292       /* Check the HSE State */\r
293       if(RCC_OscInitStruct->HSEState != RCC_HSE_OFF)\r
294       {\r
295         /* Get Start Tick*/\r
296         tickstart = HAL_GetTick();\r
297         \r
298         /* Wait till HSE is ready */  \r
299         while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)\r
300         {\r
301           if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)\r
302           {\r
303             return HAL_TIMEOUT;\r
304           }\r
305         }\r
306       }\r
307       else\r
308       {\r
309         /* Get Start Tick*/\r
310         tickstart = HAL_GetTick();\r
311         \r
312         /* Wait till HSE is bypassed or disabled */\r
313         while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET)\r
314         {\r
315            if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)\r
316           {\r
317             return HAL_TIMEOUT;\r
318           }\r
319         }\r
320       }\r
321     }\r
322   }\r
323   /*----------------------------- HSI Configuration --------------------------*/ \r
324   if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI)\r
325   {\r
326     /* Check the parameters */\r
327     assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState));\r
328     assert_param(IS_RCC_CALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue));\r
329     \r
330     /* Check if HSI is used as system clock or as PLL source when PLL is selected as system clock */ \r
331     if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_HSI) \r
332        || ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && ((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLCFGR_PLLSRC_HSI)))\r
333     {\r
334       /* When HSI is used as system clock it will not disabled */\r
335       if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET) && (RCC_OscInitStruct->HSIState != RCC_HSI_ON))\r
336       {\r
337         return HAL_ERROR;\r
338       }\r
339       /* Otherwise, just the calibration is allowed */\r
340       else\r
341       {\r
342         /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/\r
343         __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);\r
344       }\r
345     }\r
346     else\r
347     {\r
348       /* Check the HSI State */\r
349       if((RCC_OscInitStruct->HSIState)!= RCC_HSI_OFF)\r
350       {\r
351         /* Enable the Internal High Speed oscillator (HSI). */\r
352         __HAL_RCC_HSI_ENABLE();\r
353 \r
354         /* Get Start Tick*/\r
355         tickstart = HAL_GetTick();\r
356 \r
357         /* Wait till HSI is ready */  \r
358         while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)\r
359         {\r
360           if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)\r
361           {\r
362             return HAL_TIMEOUT;\r
363           }\r
364         }\r
365                 \r
366         /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/\r
367         __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);\r
368       }\r
369       else\r
370       {\r
371         /* Disable the Internal High Speed oscillator (HSI). */\r
372         __HAL_RCC_HSI_DISABLE();\r
373 \r
374         /* Get Start Tick*/\r
375         tickstart = HAL_GetTick();\r
376       \r
377         /* Wait till HSI is ready */  \r
378         while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET)\r
379         {\r
380           if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)\r
381           {\r
382             return HAL_TIMEOUT;\r
383           } \r
384         } \r
385       }\r
386     }\r
387   }\r
388   /*------------------------------ LSI Configuration -------------------------*/\r
389   if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI)\r
390   {\r
391     /* Check the parameters */\r
392     assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState));\r
393 \r
394     /* Check the LSI State */\r
395     if((RCC_OscInitStruct->LSIState)!= RCC_LSI_OFF)\r
396     {\r
397       /* Enable the Internal Low Speed oscillator (LSI). */\r
398       __HAL_RCC_LSI_ENABLE();\r
399       \r
400       /* Get Start Tick*/\r
401       tickstart = HAL_GetTick();\r
402       \r
403       /* Wait till LSI is ready */\r
404       while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) == RESET)\r
405       {\r
406         if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)\r
407         {\r
408           return HAL_TIMEOUT;\r
409         }\r
410       }\r
411     }\r
412     else\r
413     {\r
414       /* Disable the Internal Low Speed oscillator (LSI). */\r
415       __HAL_RCC_LSI_DISABLE();\r
416       \r
417       /* Get Start Tick*/\r
418       tickstart = HAL_GetTick();\r
419       \r
420       /* Wait till LSI is ready */  \r
421       while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) != RESET)\r
422       {\r
423         if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)\r
424         {\r
425           return HAL_TIMEOUT;\r
426         }\r
427       }\r
428     }\r
429   }\r
430   /*------------------------------ LSE Configuration -------------------------*/ \r
431   if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE)\r
432   {\r
433     /* Check the parameters */\r
434     assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState));\r
435     \r
436     /* Enable Power Clock*/\r
437     __HAL_RCC_PWR_CLK_ENABLE();\r
438     \r
439     /* Enable write access to Backup domain */\r
440     PWR->CR1 |= PWR_CR1_DBP;\r
441     \r
442     /* Wait for Backup domain Write protection disable */\r
443     tickstart = HAL_GetTick();\r
444     \r
445     while((PWR->CR1 & PWR_CR1_DBP) == RESET)\r
446     {\r
447       if((HAL_GetTick() - tickstart ) > RCC_DBP_TIMEOUT_VALUE)\r
448       {\r
449         return HAL_TIMEOUT;\r
450       }      \r
451     }\r
452     \r
453     /* Reset LSEON and LSEBYP bits before configuring the LSE ----------------*/\r
454     __HAL_RCC_LSE_CONFIG(RCC_LSE_OFF);\r
455     \r
456     /* Get Start Tick*/\r
457     tickstart = HAL_GetTick();\r
458     \r
459     /* Wait till LSE is ready */  \r
460     while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) != RESET)\r
461     {\r
462       if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)\r
463       {\r
464         return HAL_TIMEOUT;\r
465       }    \r
466     } \r
467     \r
468     /* Set the new LSE configuration -----------------------------------------*/\r
469     __HAL_RCC_LSE_CONFIG(RCC_OscInitStruct->LSEState);\r
470     /* Check the LSE State */\r
471     if((RCC_OscInitStruct->LSEState) != RCC_LSE_OFF)\r
472     {\r
473       /* Get Start Tick*/\r
474       tickstart = HAL_GetTick();\r
475       \r
476       /* Wait till LSE is ready */  \r
477       while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)\r
478       {\r
479         if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)\r
480         {\r
481           return HAL_TIMEOUT;\r
482         }       \r
483       }\r
484     }\r
485     else\r
486     {\r
487       /* Get Start Tick*/\r
488       tickstart = HAL_GetTick();\r
489       \r
490       /* Wait till LSE is ready */  \r
491       while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) != RESET)\r
492       {\r
493         if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)\r
494         {\r
495           return HAL_TIMEOUT;\r
496         }       \r
497       }\r
498     }\r
499   }\r
500   /*-------------------------------- PLL Configuration -----------------------*/\r
501   /* Check the parameters */\r
502   assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState));\r
503   if ((RCC_OscInitStruct->PLL.PLLState) != RCC_PLL_NONE)\r
504   {\r
505     /* Check if the PLL is used as system clock or not */\r
506     if(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_PLLCLK)\r
507     { \r
508       if((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_ON)\r
509       {\r
510         /* Check the parameters */\r
511         assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource));\r
512         assert_param(IS_RCC_PLLM_VALUE(RCC_OscInitStruct->PLL.PLLM));\r
513         assert_param(IS_RCC_PLLN_VALUE(RCC_OscInitStruct->PLL.PLLN));\r
514         assert_param(IS_RCC_PLLP_VALUE(RCC_OscInitStruct->PLL.PLLP));\r
515         assert_param(IS_RCC_PLLQ_VALUE(RCC_OscInitStruct->PLL.PLLQ));\r
516         \r
517         /* Disable the main PLL. */\r
518         __HAL_RCC_PLL_DISABLE();\r
519         \r
520         /* Get Start Tick*/\r
521         tickstart = HAL_GetTick();\r
522         \r
523         /* Wait till PLL is ready */  \r
524         while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)\r
525         {\r
526           if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)\r
527           {\r
528             return HAL_TIMEOUT;\r
529           }\r
530         }\r
531         \r
532         /* Configure the main PLL clock source, multiplication and division factors. */\r
533         __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource,\r
534                              RCC_OscInitStruct->PLL.PLLM,\r
535                              RCC_OscInitStruct->PLL.PLLN,\r
536                              RCC_OscInitStruct->PLL.PLLP,\r
537                              RCC_OscInitStruct->PLL.PLLQ);\r
538         /* Enable the main PLL. */\r
539         __HAL_RCC_PLL_ENABLE();\r
540 \r
541         /* Get Start Tick*/\r
542         tickstart = HAL_GetTick();\r
543         \r
544         /* Wait till PLL is ready */  \r
545         while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)\r
546         {\r
547           if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)\r
548           {\r
549             return HAL_TIMEOUT;\r
550           } \r
551         }\r
552       }\r
553       else\r
554       {\r
555         /* Disable the main PLL. */\r
556         __HAL_RCC_PLL_DISABLE();\r
557  \r
558         /* Get Start Tick*/\r
559         tickstart = HAL_GetTick();\r
560         \r
561         /* Wait till PLL is ready */  \r
562         while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)\r
563         {\r
564           if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)\r
565           {\r
566             return HAL_TIMEOUT;\r
567           }\r
568         }\r
569       }\r
570     }\r
571     else\r
572     {\r
573       return HAL_ERROR;\r
574     }\r
575   }\r
576   return HAL_OK;\r
577 }\r
578  \r
579 /**\r
580   * @brief  Initializes the CPU, AHB and APB busses clocks according to the specified \r
581   *         parameters in the RCC_ClkInitStruct.\r
582   * @param  RCC_ClkInitStruct: pointer to an RCC_OscInitTypeDef structure that\r
583   *         contains the configuration information for the RCC peripheral.\r
584   * @param  FLatency: FLASH Latency, this parameter depend on device selected\r
585   * \r
586   * @note   The SystemCoreClock CMSIS variable is used to store System Clock Frequency \r
587   *         and updated by HAL_RCC_GetHCLKFreq() function called within this function\r
588   *\r
589   * @note   The HSI is used (enabled by hardware) as system clock source after\r
590   *         startup from Reset, wake-up from STOP and STANDBY mode, or in case\r
591   *         of failure of the HSE used directly or indirectly as system clock\r
592   *         (if the Clock Security System CSS is enabled).\r
593   *           \r
594   * @note   A switch from one clock source to another occurs only if the target\r
595   *         clock source is ready (clock stable after startup delay or PLL locked). \r
596   *         If a clock source which is not yet ready is selected, the switch will\r
597   *         occur when the clock source will be ready. \r
598   *         You can use HAL_RCC_GetClockConfig() function to know which clock is\r
599   *         currently used as system clock source.\r
600   * @note   Depending on the device voltage range, the software has to set correctly\r
601   *         HPRE[3:0] bits to ensure that HCLK not exceed the maximum allowed frequency\r
602   *         (for more details refer to section above "Initialization/de-initialization functions")\r
603   * @retval None\r
604   */\r
605 HAL_StatusTypeDef HAL_RCC_ClockConfig(RCC_ClkInitTypeDef  *RCC_ClkInitStruct, uint32_t FLatency)\r
606 {\r
607   uint32_t tickstart = 0;\r
608  \r
609   /* Check the parameters */\r
610   assert_param(IS_RCC_CLOCKTYPE(RCC_ClkInitStruct->ClockType));\r
611   assert_param(IS_FLASH_LATENCY(FLatency));\r
612  \r
613   /* To correctly read data from FLASH memory, the number of wait states (LATENCY) \r
614      must be correctly programmed according to the frequency of the CPU clock \r
615      (HCLK) and the supply voltage of the device. */\r
616   \r
617   /* Increasing the CPU frequency */\r
618   if(FLatency > (FLASH->ACR & FLASH_ACR_LATENCY))\r
619   {    \r
620     /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */\r
621     __HAL_FLASH_SET_LATENCY(FLatency);\r
622     \r
623     /* Check that the new number of wait states is taken into account to access the Flash\r
624     memory by reading the FLASH_ACR register */\r
625     if((FLASH->ACR & FLASH_ACR_LATENCY) != FLatency)\r
626     {\r
627       return HAL_ERROR;\r
628     }\r
629 \r
630     /*-------------------------- HCLK Configuration --------------------------*/\r
631     if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)\r
632     {\r
633       assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));\r
634       MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider);\r
635     }\r
636 \r
637     /*------------------------- SYSCLK Configuration ---------------------------*/ \r
638     if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_SYSCLK) == RCC_CLOCKTYPE_SYSCLK)\r
639     {    \r
640       assert_param(IS_RCC_SYSCLKSOURCE(RCC_ClkInitStruct->SYSCLKSource));\r
641       \r
642       /* HSE is selected as System Clock Source */\r
643       if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)\r
644       {\r
645         /* Check the HSE ready flag */  \r
646         if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)\r
647         {\r
648           return HAL_ERROR;\r
649         }\r
650       }\r
651       /* PLL is selected as System Clock Source */\r
652       else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)\r
653       {\r
654         /* Check the PLL ready flag */  \r
655         if(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)\r
656         {\r
657           return HAL_ERROR;\r
658         }\r
659       }\r
660       /* HSI is selected as System Clock Source */\r
661       else\r
662       {\r
663         /* Check the HSI ready flag */  \r
664         if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)\r
665         {\r
666           return HAL_ERROR;\r
667         }\r
668       }\r
669 \r
670       __HAL_RCC_SYSCLK_CONFIG(RCC_ClkInitStruct->SYSCLKSource);\r
671       /* Get Start Tick*/\r
672       tickstart = HAL_GetTick();\r
673       \r
674       if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)\r
675       {\r
676         while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_HSE)\r
677         {\r
678           if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)\r
679           {\r
680             return HAL_TIMEOUT;\r
681           }\r
682         }\r
683       }\r
684       else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)\r
685       {\r
686         while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_PLLCLK)\r
687         {\r
688           if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)\r
689           {\r
690             return HAL_TIMEOUT;\r
691           }\r
692         }\r
693       }\r
694       else\r
695       {\r
696         while(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_HSI)\r
697         {\r
698           if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)\r
699           {\r
700             return HAL_TIMEOUT;\r
701           }\r
702         }\r
703       }\r
704     }    \r
705   }\r
706   /* Decreasing the CPU frequency */\r
707   else\r
708   {\r
709     /*-------------------------- HCLK Configuration --------------------------*/\r
710     if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)\r
711     {\r
712       assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));\r
713       MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider);\r
714     }\r
715 \r
716     /*------------------------- SYSCLK Configuration -------------------------*/\r
717     if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_SYSCLK) == RCC_CLOCKTYPE_SYSCLK)\r
718     {    \r
719       assert_param(IS_RCC_SYSCLKSOURCE(RCC_ClkInitStruct->SYSCLKSource));\r
720       \r
721       /* HSE is selected as System Clock Source */\r
722       if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)\r
723       {\r
724         /* Check the HSE ready flag */  \r
725         if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)\r
726         {\r
727           return HAL_ERROR;\r
728         }\r
729       }\r
730       /* PLL is selected as System Clock Source */\r
731       else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)\r
732       {\r
733         /* Check the PLL ready flag */  \r
734         if(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)\r
735         {\r
736           return HAL_ERROR;\r
737         }\r
738       }\r
739       /* HSI is selected as System Clock Source */\r
740       else\r
741       {\r
742         /* Check the HSI ready flag */  \r
743         if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)\r
744         {\r
745           return HAL_ERROR;\r
746         }\r
747       }\r
748       __HAL_RCC_SYSCLK_CONFIG(RCC_ClkInitStruct->SYSCLKSource);\r
749       /* Get Start Tick*/\r
750       tickstart = HAL_GetTick();\r
751       \r
752       if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)\r
753       {\r
754         while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_HSE)\r
755         {\r
756           if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)\r
757           {\r
758             return HAL_TIMEOUT;\r
759           } \r
760         }\r
761       }\r
762       else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)\r
763       {\r
764         while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_PLLCLK)\r
765         {\r
766           if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)\r
767           {\r
768             return HAL_TIMEOUT;\r
769           } \r
770         }\r
771       }\r
772       else\r
773       {\r
774         while(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_HSI)\r
775         {\r
776           if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)\r
777           {\r
778             return HAL_TIMEOUT;\r
779           }\r
780         }\r
781       }\r
782     }\r
783     \r
784     /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */\r
785     __HAL_FLASH_SET_LATENCY(FLatency);\r
786     \r
787     /* Check that the new number of wait states is taken into account to access the Flash\r
788     memory by reading the FLASH_ACR register */\r
789     if((FLASH->ACR & FLASH_ACR_LATENCY) != FLatency)\r
790     {\r
791       return HAL_ERROR;\r
792     }\r
793  }\r
794 \r
795   /*-------------------------- PCLK1 Configuration ---------------------------*/ \r
796   if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)\r
797   {\r
798     assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB1CLKDivider));\r
799     MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE1, RCC_ClkInitStruct->APB1CLKDivider);\r
800   }\r
801   \r
802   /*-------------------------- PCLK2 Configuration ---------------------------*/ \r
803   if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2)\r
804   {\r
805     assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB2CLKDivider));\r
806     MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE2, ((RCC_ClkInitStruct->APB2CLKDivider) << 3));\r
807   }\r
808 \r
809   /* Configure the source of time base considering new system clocks settings*/\r
810   HAL_InitTick (TICK_INT_PRIORITY);\r
811   \r
812   return HAL_OK;\r
813 }\r
814 \r
815 /**\r
816   * @}\r
817   */\r
818 \r
819 /** @defgroup RCC_Exported_Functions_Group2 Peripheral Control functions \r
820   *  @brief   RCC clocks control functions \r
821   *\r
822   @verbatim   \r
823   ===============================================================================\r
824                   ##### Peripheral Control functions #####\r
825   ===============================================================================  \r
826     [..]\r
827     This subsection provides a set of functions allowing to control the RCC Clocks \r
828     frequencies.\r
829       \r
830 @endverbatim\r
831   * @{\r
832   */\r
833 \r
834 /**\r
835   * @brief  Selects the clock source to output on MCO1 pin(PA8) or on MCO2 pin(PC9).\r
836   * @note   PA8/PC9 should be configured in alternate function mode.\r
837   * @param  RCC_MCOx: specifies the output direction for the clock source.\r
838   *          This parameter can be one of the following values:\r
839   *            @arg RCC_MCO1: Clock source to output on MCO1 pin(PA8).\r
840   *            @arg RCC_MCO2: Clock source to output on MCO2 pin(PC9).\r
841   * @param  RCC_MCOSource: specifies the clock source to output.\r
842   *          This parameter can be one of the following values:\r
843   *            @arg RCC_MCO1SOURCE_HSI: HSI clock selected as MCO1 source\r
844   *            @arg RCC_MCO1SOURCE_LSE: LSE clock selected as MCO1 source\r
845   *            @arg RCC_MCO1SOURCE_HSE: HSE clock selected as MCO1 source\r
846   *            @arg RCC_MCO1SOURCE_PLLCLK: main PLL clock selected as MCO1 source\r
847   *            @arg RCC_MCO2SOURCE_SYSCLK: System clock (SYSCLK) selected as MCO2 source\r
848   *            @arg RCC_MCO2SOURCE_PLLI2SCLK: PLLI2S clock selected as MCO2 source\r
849   *            @arg RCC_MCO2SOURCE_HSE: HSE clock selected as MCO2 source\r
850   *            @arg RCC_MCO2SOURCE_PLLCLK: main PLL clock selected as MCO2 source\r
851   * @param  RCC_MCODiv: specifies the MCOx prescaler.\r
852   *          This parameter can be one of the following values:\r
853   *            @arg RCC_MCODIV_1: no division applied to MCOx clock\r
854   *            @arg RCC_MCODIV_2: division by 2 applied to MCOx clock\r
855   *            @arg RCC_MCODIV_3: division by 3 applied to MCOx clock\r
856   *            @arg RCC_MCODIV_4: division by 4 applied to MCOx clock\r
857   *            @arg RCC_MCODIV_5: division by 5 applied to MCOx clock\r
858   * @retval None\r
859   */\r
860 void HAL_RCC_MCOConfig(uint32_t RCC_MCOx, uint32_t RCC_MCOSource, uint32_t RCC_MCODiv)\r
861 {\r
862   GPIO_InitTypeDef GPIO_InitStruct;\r
863   /* Check the parameters */\r
864   assert_param(IS_RCC_MCO(RCC_MCOx));\r
865   assert_param(IS_RCC_MCODIV(RCC_MCODiv));\r
866   /* RCC_MCO1 */\r
867   if(RCC_MCOx == RCC_MCO1)\r
868   {\r
869     assert_param(IS_RCC_MCO1SOURCE(RCC_MCOSource));\r
870     \r
871     /* MCO1 Clock Enable */\r
872     MCO1_CLK_ENABLE();\r
873     \r
874     /* Configure the MCO1 pin in alternate function mode */    \r
875     GPIO_InitStruct.Pin = MCO1_PIN;\r
876     GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;\r
877     GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;\r
878     GPIO_InitStruct.Pull = GPIO_NOPULL;\r
879     GPIO_InitStruct.Alternate = GPIO_AF0_MCO;\r
880     HAL_GPIO_Init(MCO1_GPIO_PORT, &GPIO_InitStruct);\r
881     \r
882     /* Mask MCO1 and MCO1PRE[2:0] bits then Select MCO1 clock source and prescaler */\r
883     MODIFY_REG(RCC->CFGR, (RCC_CFGR_MCO1 | RCC_CFGR_MCO1PRE), (RCC_MCOSource | RCC_MCODiv));\r
884   }\r
885   else\r
886   {\r
887     assert_param(IS_RCC_MCO2SOURCE(RCC_MCOSource));\r
888     \r
889     /* MCO2 Clock Enable */\r
890     MCO2_CLK_ENABLE();\r
891     \r
892     /* Configure the MCO2 pin in alternate function mode */\r
893     GPIO_InitStruct.Pin = MCO2_PIN;\r
894     GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;\r
895     GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;\r
896     GPIO_InitStruct.Pull = GPIO_NOPULL;\r
897     GPIO_InitStruct.Alternate = GPIO_AF0_MCO;\r
898     HAL_GPIO_Init(MCO2_GPIO_PORT, &GPIO_InitStruct);\r
899     \r
900     /* Mask MCO2 and MCO2PRE[2:0] bits then Select MCO2 clock source and prescaler */\r
901     MODIFY_REG(RCC->CFGR, (RCC_CFGR_MCO2 | RCC_CFGR_MCO2PRE), (RCC_MCOSource | (RCC_MCODiv << 3)));\r
902   }\r
903 }\r
904 \r
905 /**\r
906   * @brief  Enables the Clock Security System.\r
907   * @note   If a failure is detected on the HSE oscillator clock, this oscillator\r
908   *         is automatically disabled and an interrupt is generated to inform the\r
909   *         software about the failure (Clock Security System Interrupt, CSSI),\r
910   *         allowing the MCU to perform rescue operations. The CSSI is linked to \r
911   *         the Cortex-M7 NMI (Non-Maskable Interrupt) exception vector.  \r
912   * @retval None\r
913   */\r
914 void HAL_RCC_EnableCSS(void)\r
915 {\r
916   SET_BIT(RCC->CR, RCC_CR_CSSON);\r
917 }\r
918 \r
919 /**\r
920   * @brief  Disables the Clock Security System.\r
921   * @retval None\r
922   */\r
923 void HAL_RCC_DisableCSS(void)\r
924 {\r
925   CLEAR_BIT(RCC->CR, RCC_CR_CSSON);\r
926 }\r
927 \r
928 /**\r
929   * @brief  Returns the SYSCLK frequency\r
930   *        \r
931   * @note   The system frequency computed by this function is not the real \r
932   *         frequency in the chip. It is calculated based on the predefined \r
933   *         constant and the selected clock source:\r
934   * @note     If SYSCLK source is HSI, function returns values based on HSI_VALUE(*)\r
935   * @note     If SYSCLK source is HSE, function returns values based on HSE_VALUE(**)\r
936   * @note     If SYSCLK source is PLL, function returns values based on HSE_VALUE(**) \r
937   *           or HSI_VALUE(*) multiplied/divided by the PLL factors.         \r
938   * @note     (*) HSI_VALUE is a constant defined in stm32f7xx_hal_conf.h file (default value\r
939   *               16 MHz) but the real value may vary depending on the variations\r
940   *               in voltage and temperature.\r
941   * @note     (**) HSE_VALUE is a constant defined in stm32f7xx_hal_conf.h file (default value\r
942   *                25 MHz), user has to ensure that HSE_VALUE is same as the real\r
943   *                frequency of the crystal used. Otherwise, this function may\r
944   *                have wrong result.\r
945   *                  \r
946   * @note   The result of this function could be not correct when using fractional\r
947   *         value for HSE crystal.\r
948   *           \r
949   * @note   This function can be used by the user application to compute the \r
950   *         baudrate for the communication peripherals or configure other parameters.\r
951   *           \r
952   * @note   Each time SYSCLK changes, this function must be called to update the\r
953   *         right SYSCLK value. Otherwise, any configuration based on this function will be incorrect.\r
954   *         \r
955   *               \r
956   * @retval SYSCLK frequency\r
957   */\r
958 uint32_t HAL_RCC_GetSysClockFreq(void)\r
959 {\r
960   uint32_t pllm = 0, pllvco = 0, pllp = 0;\r
961   uint32_t sysclockfreq = 0;\r
962 \r
963   /* Get SYSCLK source -------------------------------------------------------*/\r
964   switch (RCC->CFGR & RCC_CFGR_SWS)\r
965   {\r
966     case RCC_SYSCLKSOURCE_STATUS_HSI:  /* HSI used as system clock source */\r
967     {\r
968       sysclockfreq = HSI_VALUE;\r
969        break;\r
970     }\r
971     case RCC_SYSCLKSOURCE_STATUS_HSE:  /* HSE used as system clock  source */\r
972     {\r
973       sysclockfreq = HSE_VALUE;\r
974       break;\r
975     }\r
976     case RCC_SYSCLKSOURCE_STATUS_PLLCLK:  /* PLL used as system clock  source */\r
977     {\r
978       /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM) * PLLN\r
979       SYSCLK = PLL_VCO / PLLP */\r
980       pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;\r
981       if (__HAL_RCC_GET_PLL_OSCSOURCE() != RCC_PLLCFGR_PLLSRC_HSI)\r
982       {\r
983         /* HSE used as PLL clock source */\r
984         pllvco = ((HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> POSITION_VAL(RCC_PLLCFGR_PLLN)));\r
985       }\r
986       else\r
987       {\r
988         /* HSI used as PLL clock source */\r
989         pllvco = ((HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> POSITION_VAL(RCC_PLLCFGR_PLLN)));    \r
990       }\r
991       pllp = ((((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >> POSITION_VAL(RCC_PLLCFGR_PLLP)) + 1 ) *2);\r
992       \r
993       sysclockfreq = pllvco/pllp;\r
994       break;\r
995     }\r
996     default:\r
997     {\r
998       sysclockfreq = HSI_VALUE;\r
999       break;\r
1000     }\r
1001   }\r
1002   return sysclockfreq;\r
1003 }\r
1004 \r
1005 /**\r
1006   * @brief  Returns the HCLK frequency     \r
1007   * @note   Each time HCLK changes, this function must be called to update the\r
1008   *         right HCLK value. Otherwise, any configuration based on this function will be incorrect.\r
1009   * \r
1010   * @note   The SystemCoreClock CMSIS variable is used to store System Clock Frequency \r
1011   *         and updated within this function\r
1012   * @retval HCLK frequency\r
1013   */\r
1014 uint32_t HAL_RCC_GetHCLKFreq(void)\r
1015 {\r
1016   SystemCoreClock = HAL_RCC_GetSysClockFreq() >> APBAHBPrescTable[(RCC->CFGR & RCC_CFGR_HPRE)>> POSITION_VAL(RCC_CFGR_HPRE)];\r
1017   return SystemCoreClock;\r
1018 }\r
1019 \r
1020 /**\r
1021   * @brief  Returns the PCLK1 frequency     \r
1022   * @note   Each time PCLK1 changes, this function must be called to update the\r
1023   *         right PCLK1 value. Otherwise, any configuration based on this function will be incorrect.\r
1024   * @retval PCLK1 frequency\r
1025   */\r
1026 uint32_t HAL_RCC_GetPCLK1Freq(void)\r
1027 {  \r
1028   /* Get HCLK source and Compute PCLK1 frequency ---------------------------*/\r
1029   return (HAL_RCC_GetHCLKFreq() >> APBAHBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE1)>> POSITION_VAL(RCC_CFGR_PPRE1)]);\r
1030 }\r
1031 \r
1032 /**\r
1033   * @brief  Returns the PCLK2 frequency     \r
1034   * @note   Each time PCLK2 changes, this function must be called to update the\r
1035   *         right PCLK2 value. Otherwise, any configuration based on this function will be incorrect.\r
1036   * @retval PCLK2 frequency\r
1037   */\r
1038 uint32_t HAL_RCC_GetPCLK2Freq(void)\r
1039 {\r
1040   /* Get HCLK source and Compute PCLK2 frequency ---------------------------*/\r
1041   return (HAL_RCC_GetHCLKFreq()>> APBAHBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE2)>> POSITION_VAL(RCC_CFGR_PPRE2)]);\r
1042\r
1043 \r
1044 /**\r
1045   * @brief  Configures the RCC_OscInitStruct according to the internal \r
1046   * RCC configuration registers.\r
1047   * @param  RCC_OscInitStruct: pointer to an RCC_OscInitTypeDef structure that \r
1048   * will be configured.\r
1049   * @retval None\r
1050   */\r
1051 void HAL_RCC_GetOscConfig(RCC_OscInitTypeDef  *RCC_OscInitStruct)\r
1052 {\r
1053   /* Set all possible values for the Oscillator type parameter ---------------*/\r
1054   RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_LSI;\r
1055   \r
1056   /* Get the HSE configuration -----------------------------------------------*/\r
1057   if((RCC->CR &RCC_CR_HSEBYP) == RCC_CR_HSEBYP)\r
1058   {\r
1059     RCC_OscInitStruct->HSEState = RCC_HSE_BYPASS;\r
1060   }\r
1061   else if((RCC->CR &RCC_CR_HSEON) == RCC_CR_HSEON)\r
1062   {\r
1063     RCC_OscInitStruct->HSEState = RCC_HSE_ON;\r
1064   }\r
1065   else\r
1066   {\r
1067     RCC_OscInitStruct->HSEState = RCC_HSE_OFF;\r
1068   }\r
1069   \r
1070   /* Get the HSI configuration -----------------------------------------------*/\r
1071   if((RCC->CR &RCC_CR_HSION) == RCC_CR_HSION)\r
1072   {\r
1073     RCC_OscInitStruct->HSIState = RCC_HSI_ON;\r
1074   }\r
1075   else\r
1076   {\r
1077     RCC_OscInitStruct->HSIState = RCC_HSI_OFF;\r
1078   }\r
1079   \r
1080   RCC_OscInitStruct->HSICalibrationValue = (uint32_t)((RCC->CR &RCC_CR_HSITRIM) >> POSITION_VAL(RCC_CR_HSITRIM));\r
1081   \r
1082   /* Get the LSE configuration -----------------------------------------------*/\r
1083   if((RCC->BDCR &RCC_BDCR_LSEBYP) == RCC_BDCR_LSEBYP)\r
1084   {\r
1085     RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS;\r
1086   }\r
1087   else if((RCC->BDCR &RCC_BDCR_LSEON) == RCC_BDCR_LSEON)\r
1088   {\r
1089     RCC_OscInitStruct->LSEState = RCC_LSE_ON;\r
1090   }\r
1091   else\r
1092   {\r
1093     RCC_OscInitStruct->LSEState = RCC_LSE_OFF;\r
1094   }\r
1095   \r
1096   /* Get the LSI configuration -----------------------------------------------*/\r
1097   if((RCC->CSR &RCC_CSR_LSION) == RCC_CSR_LSION)\r
1098   {\r
1099     RCC_OscInitStruct->LSIState = RCC_LSI_ON;\r
1100   }\r
1101   else\r
1102   {\r
1103     RCC_OscInitStruct->LSIState = RCC_LSI_OFF;\r
1104   }\r
1105   \r
1106   /* Get the PLL configuration -----------------------------------------------*/\r
1107   if((RCC->CR &RCC_CR_PLLON) == RCC_CR_PLLON)\r
1108   {\r
1109     RCC_OscInitStruct->PLL.PLLState = RCC_PLL_ON;\r
1110   }\r
1111   else\r
1112   {\r
1113     RCC_OscInitStruct->PLL.PLLState = RCC_PLL_OFF;\r
1114   }\r
1115   RCC_OscInitStruct->PLL.PLLSource = (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC);\r
1116   RCC_OscInitStruct->PLL.PLLM = (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM);\r
1117   RCC_OscInitStruct->PLL.PLLN = (uint32_t)((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> POSITION_VAL(RCC_PLLCFGR_PLLN));\r
1118   RCC_OscInitStruct->PLL.PLLP = (uint32_t)((((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) + RCC_PLLCFGR_PLLP_0) << 1) >> POSITION_VAL(RCC_PLLCFGR_PLLP));\r
1119   RCC_OscInitStruct->PLL.PLLQ = (uint32_t)((RCC->PLLCFGR & RCC_PLLCFGR_PLLQ) >> POSITION_VAL(RCC_PLLCFGR_PLLQ));\r
1120 }\r
1121 \r
1122 /**\r
1123   * @brief  Configures the RCC_ClkInitStruct according to the internal \r
1124   * RCC configuration registers.\r
1125   * @param  RCC_ClkInitStruct: pointer to an RCC_ClkInitTypeDef structure that \r
1126   * will be configured.\r
1127   * @param  pFLatency: Pointer on the Flash Latency.\r
1128   * @retval None\r
1129   */\r
1130 void HAL_RCC_GetClockConfig(RCC_ClkInitTypeDef  *RCC_ClkInitStruct, uint32_t *pFLatency)\r
1131 {\r
1132   /* Set all possible values for the Clock type parameter --------------------*/\r
1133   RCC_ClkInitStruct->ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;\r
1134    \r
1135   /* Get the SYSCLK configuration --------------------------------------------*/ \r
1136   RCC_ClkInitStruct->SYSCLKSource = (uint32_t)(RCC->CFGR & RCC_CFGR_SW);\r
1137   \r
1138   /* Get the HCLK configuration ----------------------------------------------*/ \r
1139   RCC_ClkInitStruct->AHBCLKDivider = (uint32_t)(RCC->CFGR & RCC_CFGR_HPRE); \r
1140   \r
1141   /* Get the APB1 configuration ----------------------------------------------*/ \r
1142   RCC_ClkInitStruct->APB1CLKDivider = (uint32_t)(RCC->CFGR & RCC_CFGR_PPRE1);   \r
1143   \r
1144   /* Get the APB2 configuration ----------------------------------------------*/ \r
1145   RCC_ClkInitStruct->APB2CLKDivider = (uint32_t)((RCC->CFGR & RCC_CFGR_PPRE2) >> 3);\r
1146   \r
1147   /* Get the Flash Wait State (Latency) configuration ------------------------*/   \r
1148   *pFLatency = (uint32_t)(FLASH->ACR & FLASH_ACR_LATENCY); \r
1149 }\r
1150 \r
1151 /**\r
1152   * @brief This function handles the RCC CSS interrupt request.\r
1153   * @note This API should be called under the NMI_Handler().\r
1154   * @retval None\r
1155   */\r
1156 void HAL_RCC_NMI_IRQHandler(void)\r
1157 {\r
1158   /* Check RCC CSSF flag  */\r
1159   if(__HAL_RCC_GET_IT(RCC_IT_CSS))\r
1160   {\r
1161     /* RCC Clock Security System interrupt user callback */\r
1162     HAL_RCC_CSSCallback();\r
1163 \r
1164     /* Clear RCC CSS pending bit */\r
1165     __HAL_RCC_CLEAR_IT(RCC_IT_CSS);\r
1166   }\r
1167 }\r
1168 \r
1169 /**\r
1170   * @brief  RCC Clock Security System interrupt callback\r
1171   * @retval None\r
1172   */\r
1173 __weak void HAL_RCC_CSSCallback(void)\r
1174 {\r
1175   /* NOTE : This function Should not be modified, when the callback is needed,\r
1176             the HAL_RCC_CSSCallback could be implemented in the user file\r
1177    */ \r
1178 }\r
1179 \r
1180 /**\r
1181   * @}\r
1182   */\r
1183 \r
1184 /**\r
1185   * @}\r
1186   */\r
1187 \r
1188 #endif /* HAL_RCC_MODULE_ENABLED */\r
1189 /**\r
1190   * @}\r
1191   */\r
1192 \r
1193 /**\r
1194   * @}\r
1195   */\r
1196 \r
1197 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/\r