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