]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F3/stm32f3xx_hal_rcc_ex.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32F3 / stm32f3xx_hal_rcc_ex.c
1 /**
2   ******************************************************************************
3   * @file    stm32f3xx_hal_rcc_ex.c
4   * @author  MCD Application Team
5   * @version V1.1.0
6   * @date    12-Sept-2014
7   * @brief   Extended RCC HAL module driver
8   *          This file provides firmware functions to manage the following 
9   *          functionalities RCC Extended peripheral:
10   *           + Extended Clock Source configuration functions
11   *  
12   ******************************************************************************
13   * @attention
14   *
15   * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
16   *
17   * Redistribution and use in source and binary forms, with or without modification,
18   * are permitted provided that the following conditions are met:
19   *   1. Redistributions of source code must retain the above copyright notice,
20   *      this list of conditions and the following disclaimer.
21   *   2. Redistributions in binary form must reproduce the above copyright notice,
22   *      this list of conditions and the following disclaimer in the documentation
23   *      and/or other materials provided with the distribution.
24   *   3. Neither the name of STMicroelectronics nor the names of its contributors
25   *      may be used to endorse or promote products derived from this software
26   *      without specific prior written permission.
27   *
28   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
32   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
34   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38   *
39   ******************************************************************************
40   */ 
41
42 /* Includes ------------------------------------------------------------------*/
43 #include "stm32f3xx_hal.h"
44
45 /** @addtogroup STM32F3xx_HAL_Driver
46   * @{
47   */
48
49 /** @defgroup RCCEx RCC Extended HAL module driver
50   * @brief RCC Extended HAL module driver.
51   * @{
52   */
53
54 #ifdef HAL_RCC_MODULE_ENABLED
55
56 /* Private typedef -----------------------------------------------------------*/
57 /* Private define ------------------------------------------------------------*/
58 /** @defgroup RCCEx_Private_Define RCC Extended Private Define
59   * @{
60   */
61 #define HSE_TIMEOUT_VALUE          HSE_STARTUP_TIMEOUT
62 #define HSI_TIMEOUT_VALUE          ((uint32_t)100)  /* 100 ms */
63 #define LSI_TIMEOUT_VALUE          ((uint32_t)100)  /* 100 ms */
64 #define PLL_TIMEOUT_VALUE          ((uint32_t)100)  /* 100 ms */
65 #define CLOCKSWITCH_TIMEOUT_VALUE  ((uint32_t)5000) /* 5 s    */
66 /**
67   * @}
68   */
69
70 /* Private macro -------------------------------------------------------------*/
71 /* Private variables ---------------------------------------------------------*/
72 /** @defgroup RCCEx_Private_Variables RCC Extented Private Variables
73   * @{
74   */
75 const uint8_t PLLMULFactorTable[16] = { 2,  3,  4,  5,  6,  7,  8,  9,
76                                        10, 11, 12, 13, 14, 15, 16, 16};
77 const uint8_t PredivFactorTable[16] = { 1, 2,  3,  4,  5,  6,  7,  8,
78                                          9,10, 11, 12, 13, 14, 15, 16};
79 /**
80   * @}
81   */
82
83 /* Private function prototypes -----------------------------------------------*/
84 /* Exported functions ---------------------------------------------------------*/
85
86 /** @defgroup RCCEx_Exported_Functions RCC Extended Exported Functions
87   * @{
88   */
89
90 /** @defgroup RCCEx_Exported_Functions_Group1 Extended Peripheral Control functions 
91   * @brief    Extended Peripheral Control functions
92   *
93 @verbatim   
94  ===============================================================================
95                 ##### Extended Peripheral Control functions  #####
96  ===============================================================================  
97     [..]
98     This subsection provides a set of functions allowing to control the RCC Clocks 
99     frequencies.
100     [..] 
101     (@) Important note: Care must be taken when HAL_RCCEx_PeriphCLKConfig() is used to
102         select the RTC clock source; in this case the Backup domain will be reset in  
103         order to modify the RTC Clock source, as consequence RTC registers (including 
104         the backup registers) and RCC_BDCR register are set to their reset values.
105       
106 @endverbatim
107   * @{
108   */
109
110 /**
111   * @brief  Initializes the RCC extended peripherals clocks according to the specified
112   *         parameters in the RCC_PeriphCLKInitTypeDef.
113   * @param  PeriphClkInit: pointer to an RCC_PeriphCLKInitTypeDef structure that
114   *         contains the configuration information for the Extended Peripherals clocks
115   *         (ADC, CEC, I2C, I2S, SDADC, HRTIM, TIM, USART, RTC and USB).
116   *
117   * @note   Care must be taken when HAL_RCCEx_PeriphCLKConfig() is used to select 
118   *         the RTC clock source; in this case the Backup domain will be reset in  
119   *         order to modify the RTC Clock source, as consequence RTC registers (including 
120   *         the backup registers) and RCC_BDCR register are set to their reset values.
121   *
122   * @retval HAL status
123   */
124 HAL_StatusTypeDef HAL_RCCEx_PeriphCLKConfig(RCC_PeriphCLKInitTypeDef  *PeriphClkInit)
125 {
126   uint32_t tickstart = 0;
127   uint32_t tmpreg = 0;
128     
129   /* Check the parameters */
130   assert_param(IS_RCC_PERIPHCLK(PeriphClkInit->PeriphClockSelection));
131   
132    
133   /*---------------------------- RTC configuration -------------------------------*/
134   if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_RTC) == (RCC_PERIPHCLK_RTC))
135   {
136     /* Enable Power Clock*/
137     __PWR_CLK_ENABLE();
138     
139     /* Enable write access to Backup domain */
140     SET_BIT(PWR->CR, PWR_CR_DBP);
141     
142     /* Wait for Backup domain Write protection disable */
143     tickstart = HAL_GetTick();
144     
145     while((PWR->CR & PWR_CR_DBP) == RESET)
146     {
147       if((HAL_GetTick()-tickstart) > DBP_TIMEOUT_VALUE)
148       {
149         return HAL_TIMEOUT;
150       }      
151     }
152     
153     /* Reset the Backup domain only if the RTC Clock source selction is modified */ 
154     if((RCC->BDCR & RCC_BDCR_RTCSEL) != (PeriphClkInit->RTCClockSelection & RCC_BDCR_RTCSEL))
155     {
156       /* Store the content of BDCR register before the reset of Backup Domain */
157       tmpreg = (RCC->BDCR & ~(RCC_BDCR_RTCSEL));
158       /* RTC Clock selection can be changed only if the Backup Domain is reset */
159       __HAL_RCC_BACKUPRESET_FORCE();
160       __HAL_RCC_BACKUPRESET_RELEASE();
161       /* Restore the Content of BDCR register */
162       RCC->BDCR = tmpreg;
163     }
164     
165     /* If LSE is selected as RTC clock source, wait for LSE reactivation */
166     if(PeriphClkInit->RTCClockSelection == RCC_RTCCLKSOURCE_LSE)
167     {
168       /* Get timeout */
169       tickstart = HAL_GetTick();
170       
171       /* Wait till LSE is ready */  
172       while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
173       {
174       if((HAL_GetTick()-tickstart) > LSE_TIMEOUT_VALUE)
175         {
176           return HAL_TIMEOUT;
177         }      
178       }  
179     }
180     __HAL_RCC_RTC_CONFIG(PeriphClkInit->RTCClockSelection); 
181   }
182   
183   /*------------------------------- USART1 Configuration ------------------------*/
184   if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_USART1) == RCC_PERIPHCLK_USART1)
185   {
186     /* Check the parameters */
187     assert_param(IS_RCC_USART1CLKSOURCE(PeriphClkInit->Usart1ClockSelection));
188     
189     /* Configure the USART1 clock source */
190     __HAL_RCC_USART1_CONFIG(PeriphClkInit->Usart1ClockSelection);
191   }
192   
193   /*----------------------------- USART2 Configuration --------------------------*/ 
194   if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_USART2) == RCC_PERIPHCLK_USART2)
195   {
196     /* Check the parameters */
197     assert_param(IS_RCC_USART2CLKSOURCE(PeriphClkInit->Usart2ClockSelection));
198     
199     /* Configure the USART2 clock source */
200     __HAL_RCC_USART2_CONFIG(PeriphClkInit->Usart2ClockSelection);
201   }
202   
203   /*------------------------------ USART3 Configuration ------------------------*/ 
204   if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_USART3) == RCC_PERIPHCLK_USART3)
205   {
206     /* Check the parameters */
207     assert_param(IS_RCC_USART3CLKSOURCE(PeriphClkInit->Usart3ClockSelection));
208     
209     /* Configure the USART3 clock source */
210     __HAL_RCC_USART3_CONFIG(PeriphClkInit->Usart3ClockSelection);
211   }
212
213   /*------------------------------ I2C1 Configuration ------------------------*/ 
214   if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2C1) == RCC_PERIPHCLK_I2C1)
215   {
216     /* Check the parameters */
217     assert_param(IS_RCC_I2C1CLKSOURCE(PeriphClkInit->I2c1ClockSelection));
218     
219     /* Configure the I2C1 clock source */
220     __HAL_RCC_I2C1_CONFIG(PeriphClkInit->I2c1ClockSelection);
221   }
222
223 #if defined(STM32F302xE) || defined(STM32F303xE) || \
224     defined(STM32F302xC) || defined(STM32F303xC) || \
225     defined(STM32F302x8)                         || \
226     defined(STM32F373xC)
227   /*------------------------------ USB Configuration ------------------------*/ 
228   if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_USB) == RCC_PERIPHCLK_USB)
229   {
230     /* Check the parameters */
231     assert_param(IS_RCC_USBCLKSOURCE(PeriphClkInit->USBClockSelection));
232     
233     /* Configure the USB clock source */
234     __HAL_RCC_USB_CONFIG(PeriphClkInit->USBClockSelection);
235   }
236
237 #endif /* STM32F302xE || STM32F303xE || */
238        /* STM32F302xC || STM32F303xC || */
239        /* STM32F302x8                || */
240        /* STM32F373xC                   */
241
242 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
243     defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
244     defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx) || \
245     defined(STM32F373xC) || defined(STM32F378xx)
246
247   /*------------------------------ I2C2 Configuration ------------------------*/ 
248   if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2C2) == RCC_PERIPHCLK_I2C2)
249   {
250     /* Check the parameters */
251     assert_param(IS_RCC_I2C2CLKSOURCE(PeriphClkInit->I2c2ClockSelection));
252     
253     /* Configure the I2C2 clock source */
254     __HAL_RCC_I2C2_CONFIG(PeriphClkInit->I2c2ClockSelection);
255   }
256
257 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
258        /* STM32F302xC || STM32F303xC || STM32F358xx || */
259        /* STM32F301x8 || STM32F302x8 || STM32F318xx || */
260        /* STM32F373xC || STM32F378xx                   */
261   
262 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
263     defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
264
265   /*------------------------------ I2C3 Configuration ------------------------*/ 
266   if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2C3) == RCC_PERIPHCLK_I2C3)
267   {
268     /* Check the parameters */
269     assert_param(IS_RCC_I2C3CLKSOURCE(PeriphClkInit->I2c3ClockSelection));
270     
271     /* Configure the I2C3 clock source */
272     __HAL_RCC_I2C3_CONFIG(PeriphClkInit->I2c3ClockSelection);
273   }
274 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
275        /* STM32F301x8 || STM32F302x8 || STM32F318xx    */
276   
277 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
278     defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx)
279
280   /*------------------------------ UART4 Configuration ------------------------*/ 
281   if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_UART4) == RCC_PERIPHCLK_UART4)
282   {
283     /* Check the parameters */
284     assert_param(IS_RCC_UART4CLKSOURCE(PeriphClkInit->Uart4ClockSelection));
285     
286     /* Configure the UART4 clock source */
287     __HAL_RCC_UART4_CONFIG(PeriphClkInit->Uart4ClockSelection);
288   }
289
290   /*------------------------------ UART5 Configuration ------------------------*/ 
291   if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_UART5) == RCC_PERIPHCLK_UART5)
292   {
293     /* Check the parameters */
294     assert_param(IS_RCC_UART5CLKSOURCE(PeriphClkInit->Uart5ClockSelection));
295     
296     /* Configure the UART5 clock source */
297     __HAL_RCC_UART5_CONFIG(PeriphClkInit->Uart5ClockSelection);
298   }
299
300 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
301        /* STM32F302xC || STM32F303xC || STM32F358xx    */
302
303 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
304     defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
305     defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
306   /*------------------------------ I2S Configuration ------------------------*/ 
307   if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S) == RCC_PERIPHCLK_I2S)
308   {
309     /* Check the parameters */
310     assert_param(IS_RCC_I2SCLKSOURCE(PeriphClkInit->I2sClockSelection));
311     
312     /* Configure the I2S clock source */
313     __HAL_RCC_I2S_CONFIG(PeriphClkInit->I2sClockSelection);
314   }
315
316 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
317        /* STM32F302xC || STM32F303xC || STM32F358xx || */
318        /* STM32F301x8 || STM32F302x8 || STM32F318xx    */
319   
320 #if defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
321       
322   /*------------------------------ ADC1 clock Configuration ------------------*/ 
323   if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_ADC1) == RCC_PERIPHCLK_ADC1)
324   {
325     /* Check the parameters */
326     assert_param(IS_RCC_ADC1PLLCLK_DIV(PeriphClkInit->Adc1ClockSelection));
327     
328     /* Configure the ADC1 clock source */
329     __HAL_RCC_ADC1_CONFIG(PeriphClkInit->Adc1ClockSelection);
330   }
331
332 #endif /* STM32F301x8 || STM32F302x8 || STM32F318xx */
333   
334 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
335     defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
336     defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx)
337       
338   /*------------------------------ ADC1 & ADC2 clock Configuration -------------*/ 
339   if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_ADC12) == RCC_PERIPHCLK_ADC12)
340   {
341     /* Check the parameters */
342     assert_param(IS_RCC_ADC12PLLCLK_DIV(PeriphClkInit->Adc12ClockSelection));
343     
344     /* Configure the ADC12 clock source */
345     __HAL_RCC_ADC12_CONFIG(PeriphClkInit->Adc12ClockSelection);
346   }
347
348 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
349        /* STM32F302xC || STM32F303xC || STM32F358xx || */
350        /* STM32F303x8 || STM32F334x8 || STM32F328xx    */    
351   
352 #if defined(STM32F303xE) || defined(STM32F398xx) || \
353     defined(STM32F303xC) || defined(STM32F358xx)
354
355   /*------------------------------ ADC3 & ADC4 clock Configuration -------------*/ 
356   if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_ADC34) == RCC_PERIPHCLK_ADC34)
357   {
358     /* Check the parameters */
359     assert_param(IS_RCC_ADC34PLLCLK_DIV(PeriphClkInit->Adc34ClockSelection));
360     
361     /* Configure the ADC34 clock source */
362     __HAL_RCC_ADC34_CONFIG(PeriphClkInit->Adc34ClockSelection);
363   }
364
365 #endif /* STM32F303xE || STM32F398xx || */
366        /* STM32F303xC || STM32F358xx    */
367
368 #if defined(STM32F373xC) || defined(STM32F378xx)
369       
370   /*------------------------------ ADC1 clock Configuration ------------------*/ 
371   if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_ADC1) == RCC_PERIPHCLK_ADC1)
372   {
373     /* Check the parameters */
374     assert_param(IS_RCC_ADC1PCLK2_DIV(PeriphClkInit->Adc1ClockSelection));
375     
376     /* Configure the ADC1 clock source */
377     __HAL_RCC_ADC1_CONFIG(PeriphClkInit->Adc1ClockSelection);
378   }
379
380 #endif /* STM32F373xC || STM32F378xx */
381
382 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
383     defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
384     defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
385     defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
386
387   /*------------------------------ TIM1 clock Configuration ----------------*/ 
388   if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM1) == RCC_PERIPHCLK_TIM1)
389   {
390     /* Check the parameters */
391     assert_param(IS_RCC_TIM1CLKSOURCE(PeriphClkInit->Tim1ClockSelection));
392     
393     /* Configure the TIM1 clock source */
394     __HAL_RCC_TIM1_CONFIG(PeriphClkInit->Tim1ClockSelection);
395   }
396
397 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
398        /* STM32F302xC || STM32F303xC || STM32F358xx || */
399        /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
400        /* STM32F301x8 || STM32F302x8 || STM32F318xx    */
401   
402 #if defined(STM32F303xE) || defined(STM32F398xx) || \
403     defined(STM32F303xC) || defined(STM32F358xx)
404
405   /*------------------------------ TIM8 clock Configuration ----------------*/ 
406   if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM8) == RCC_PERIPHCLK_TIM8)
407   {
408     /* Check the parameters */
409     assert_param(IS_RCC_TIM8CLKSOURCE(PeriphClkInit->Tim8ClockSelection));
410     
411     /* Configure the TIM8 clock source */
412     __HAL_RCC_TIM8_CONFIG(PeriphClkInit->Tim8ClockSelection);
413   }
414
415 #endif /* STM32F303xE || STM32F398xx || */
416        /* STM32F303xC || STM32F358xx    */
417
418 #if defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
419
420   /*------------------------------ TIM15 clock Configuration ----------------*/ 
421   if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM15) == RCC_PERIPHCLK_TIM15)
422   {
423     /* Check the parameters */
424     assert_param(IS_RCC_TIM15CLKSOURCE(PeriphClkInit->Tim15ClockSelection));
425     
426     /* Configure the TIM15 clock source */
427     __HAL_RCC_TIM15_CONFIG(PeriphClkInit->Tim15ClockSelection);
428   }
429
430   /*------------------------------ TIM16 clock Configuration ----------------*/ 
431   if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM16) == RCC_PERIPHCLK_TIM16)
432   {
433     /* Check the parameters */
434     assert_param(IS_RCC_TIM16CLKSOURCE(PeriphClkInit->Tim16ClockSelection));
435     
436     /* Configure the TIM16 clock source */
437     __HAL_RCC_TIM16_CONFIG(PeriphClkInit->Tim16ClockSelection);
438   }
439
440   /*------------------------------ TIM17 clock Configuration ----------------*/ 
441   if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM17) == RCC_PERIPHCLK_TIM17)
442   {
443     /* Check the parameters */
444     assert_param(IS_RCC_TIM17CLKSOURCE(PeriphClkInit->Tim17ClockSelection));
445     
446     /* Configure the TIM17 clock source */
447     __HAL_RCC_TIM17_CONFIG(PeriphClkInit->Tim17ClockSelection);
448   }
449
450 #endif /* STM32F301x8 || STM32F302x8 || STM32F318xx */
451
452 #if defined(STM32F334x8)
453
454   /*------------------------------ HRTIM1 clock Configuration ----------------*/ 
455   if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_HRTIM1) == RCC_PERIPHCLK_HRTIM1)
456   {
457     /* Check the parameters */
458     assert_param(IS_RCC_HRTIM1CLKSOURCE(PeriphClkInit->Hrtim1ClockSelection));
459     
460     /* Configure the HRTIM1 clock source */
461     __HAL_RCC_HRTIM1_CONFIG(PeriphClkInit->Hrtim1ClockSelection);
462   }
463
464 #endif /* STM32F334x8 */
465
466 #if defined(STM32F373xC) || defined(STM32F378xx)
467   
468   /*------------------------------ SDADC clock Configuration -------------------*/ 
469   if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SDADC) == RCC_PERIPHCLK_SDADC)
470   {
471     /* Check the parameters */
472     assert_param(IS_RCC_SDADCSYSCLK_DIV(PeriphClkInit->SdadcClockSelection));
473     
474     /* Configure the SDADC clock prescaler */
475     __HAL_RCC_SDADC_CONFIG(PeriphClkInit->SdadcClockSelection);
476   }
477
478   /*------------------------------ CEC clock Configuration -------------------*/ 
479   if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_CEC) == RCC_PERIPHCLK_CEC)
480   {
481     /* Check the parameters */
482     assert_param(IS_RCC_CECCLKSOURCE(PeriphClkInit->CecClockSelection));
483     
484     /* Configure the CEC clock source */
485     __HAL_RCC_CEC_CONFIG(PeriphClkInit->CecClockSelection);
486   }
487
488 #endif /* STM32F373xC || STM32F378xx */
489   
490 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx)
491   
492   /*------------------------------ TIM2 clock Configuration -------------------*/ 
493   if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM2) == RCC_PERIPHCLK_TIM2)
494   {
495     /* Check the parameters */
496     assert_param(IS_RCC_TIM2CLKSOURCE(PeriphClkInit->Tim2ClockSelection));
497     
498     /* Configure the CEC clock source */
499     __HAL_RCC_TIM2_CONFIG(PeriphClkInit->Tim2ClockSelection);
500   }
501
502   /*------------------------------ TIM3 clock Configuration -------------------*/ 
503   if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM34) == RCC_PERIPHCLK_TIM34)
504   {
505     /* Check the parameters */
506     assert_param(IS_RCC_TIM3CLKSOURCE(PeriphClkInit->Tim34ClockSelection));
507     
508     /* Configure the CEC clock source */
509     __HAL_RCC_TIM34_CONFIG(PeriphClkInit->Tim34ClockSelection);
510   }
511
512   /*------------------------------ TIM15 clock Configuration ------------------*/ 
513   if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM15) == RCC_PERIPHCLK_TIM15)
514   {
515     /* Check the parameters */
516     assert_param(IS_RCC_TIM15CLKSOURCE(PeriphClkInit->Tim15ClockSelection));
517     
518     /* Configure the CEC clock source */
519     __HAL_RCC_TIM15_CONFIG(PeriphClkInit->Tim15ClockSelection);
520   }
521
522   /*------------------------------ TIM16 clock Configuration ------------------*/ 
523   if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM16) == RCC_PERIPHCLK_TIM16)
524   {
525     /* Check the parameters */
526     assert_param(IS_RCC_TIM16CLKSOURCE(PeriphClkInit->Tim16ClockSelection));
527     
528     /* Configure the CEC clock source */
529     __HAL_RCC_TIM16_CONFIG(PeriphClkInit->Tim16ClockSelection);
530   }
531
532   /*------------------------------ TIM17 clock Configuration ------------------*/ 
533   if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM17) == RCC_PERIPHCLK_TIM17)
534   {
535     /* Check the parameters */
536     assert_param(IS_RCC_TIM17CLKSOURCE(PeriphClkInit->Tim17ClockSelection));
537     
538     /* Configure the CEC clock source */
539     __HAL_RCC_TIM17_CONFIG(PeriphClkInit->Tim17ClockSelection);
540   }
541
542 #endif /* STM32F302xE || STM32F303xE || STM32F398xx */  
543
544 #if defined(STM32F303xE) || defined(STM32F398xx)
545   /*------------------------------ TIM20 clock Configuration ------------------*/ 
546   if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM20) == RCC_PERIPHCLK_TIM20)
547   {
548     /* Check the parameters */
549     assert_param(IS_RCC_TIM20CLKSOURCE(PeriphClkInit->Tim20ClockSelection));
550     
551     /* Configure the CEC clock source */
552     __HAL_RCC_TIM20_CONFIG(PeriphClkInit->Tim20ClockSelection);
553   }
554 #endif /* STM32F303xE || STM32F398xx */  
555
556   
557   return HAL_OK;
558 }
559
560 /**
561   * @brief  Get the RCC_ClkInitStruct according to the internal
562   * RCC configuration registers.
563   * @param  PeriphClkInit: pointer to an RCC_PeriphCLKInitTypeDef structure that
564   *         returns the configuration information for the Extended Peripherals clocks
565   *         (ADC, CEC, I2C, I2S, SDADC, HRTIM, TIM, USART, RTC and USB clocks).
566   * @retval None
567   */
568 void HAL_RCCEx_GetPeriphCLKConfig(RCC_PeriphCLKInitTypeDef  *PeriphClkInit)
569 {
570   /* Set all possible values for the extended clock type parameter------------*/
571   /* Common part first */
572   PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_USART1 | RCC_PERIPHCLK_USART2 | RCC_PERIPHCLK_USART3 | \
573                                         RCC_PERIPHCLK_I2C1   | RCC_PERIPHCLK_RTC;
574   
575   /* Get the RTC configuration --------------------------------------------*/
576   PeriphClkInit->RTCClockSelection = __HAL_RCC_GET_RTC_SOURCE();
577   /* Get the USART1 clock configuration --------------------------------------------*/
578   PeriphClkInit->Usart1ClockSelection = __HAL_RCC_GET_USART1_SOURCE();
579   /* Get the USART2 clock configuration -----------------------------------------*/
580   PeriphClkInit->Usart2ClockSelection = __HAL_RCC_GET_USART2_SOURCE();
581    /* Get the USART3 clock configuration -----------------------------------------*/
582   PeriphClkInit->Usart3ClockSelection = __HAL_RCC_GET_USART3_SOURCE();
583   /* Get the I2C1 clock configuration -----------------------------------------*/
584   PeriphClkInit->I2c1ClockSelection = __HAL_RCC_GET_I2C1_SOURCE();
585
586 #if defined(STM32F302xE) || defined(STM32F303xE) || \
587     defined(STM32F302xC) || defined(STM32F303xC) || \
588     defined(STM32F302x8)                         || \
589     defined(STM32F373xC)
590
591   PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_USB;
592   /* Get the USB clock configuration -----------------------------------------*/
593   PeriphClkInit->USBClockSelection = __HAL_RCC_GET_USB_SOURCE();
594
595 #endif /* STM32F302xE || STM32F303xE || */
596        /* STM32F302xC || STM32F303xC || */
597        /* STM32F302x8                || */
598        /* STM32F373xC                   */
599
600 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
601     defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
602     defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx) || \
603     defined(STM32F373xC) || defined(STM32F378xx)
604
605   PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_I2C2;
606   /* Get the I2C2 clock configuration -----------------------------------------*/
607   PeriphClkInit->I2c2ClockSelection = __HAL_RCC_GET_I2C2_SOURCE();
608
609 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
610        /* STM32F302xC || STM32F303xC || STM32F358xx || */
611        /* STM32F301x8 || STM32F302x8 || STM32F318xx || */
612        /* STM32F373xC || STM32F378xx                   */
613   
614 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
615     defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
616
617   PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_I2C3;
618   /* Get the I2C3 clock configuration -----------------------------------------*/
619   PeriphClkInit->I2c3ClockSelection = __HAL_RCC_GET_I2C3_SOURCE();
620
621 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
622        /* STM32F301x8 || STM32F302x8 || STM32F318xx    */
623   
624 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
625     defined(STM32F302xC) || defined(STM32F303xC) ||defined(STM32F358xx)
626
627   PeriphClkInit->PeriphClockSelection |= (RCC_PERIPHCLK_UART4  | RCC_PERIPHCLK_UART5);
628   /* Get the UART4 clock configuration -----------------------------------------*/
629   PeriphClkInit->Uart4ClockSelection = __HAL_RCC_GET_UART4_SOURCE();
630   /* Get the UART5 clock configuration -----------------------------------------*/
631   PeriphClkInit->Uart5ClockSelection = __HAL_RCC_GET_UART5_SOURCE();
632
633 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
634        /* STM32F302xC || STM32F303xC || STM32F358xx */
635
636 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
637     defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
638     defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
639
640   PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_I2S;
641   /* Get the I2S clock configuration -----------------------------------------*/
642   PeriphClkInit->I2sClockSelection = __HAL_RCC_GET_I2S_SOURCE();
643
644 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
645        /* STM32F302xC || STM32F303xC || STM32F358xx || */
646        /* STM32F301x8 || STM32F302x8 || STM32F318xx || */
647   
648 #if defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx) || \
649     defined(STM32F373xC) || defined(STM32F378xx)
650       
651   PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_ADC1;
652   /* Get the ADC1 clock configuration -----------------------------------------*/
653   PeriphClkInit->Adc1ClockSelection = __HAL_RCC_GET_ADC1_SOURCE();
654
655 #endif /* STM32F301x8 || STM32F302x8 || STM32F318xx || */
656        /* STM32F373xC || STM32F378xx                   */
657
658 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
659     defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
660     defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx)
661
662   PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_ADC12;
663   /* Get the ADC1 & ADC2 clock configuration -----------------------------------------*/
664   PeriphClkInit->Adc12ClockSelection = __HAL_RCC_GET_ADC12_SOURCE();
665
666 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
667        /* STM32F302xC || STM32F303xC || STM32F358xx || */
668        /* STM32F303x8 || STM32F334x8 || STM32F328xx    */
669
670 #if defined(STM32F303xE) || defined(STM32F398xx) || \
671     defined(STM32F303xC) || defined(STM32F358xx)
672
673   PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_ADC34;
674    /* Get the ADC3 & ADC4 clock configuration -----------------------------------------*/
675   PeriphClkInit->Adc34ClockSelection = __HAL_RCC_GET_ADC34_SOURCE();
676
677 #endif /* STM32F303xE || STM32F398xx || */
678        /* STM32F303xC || STM32F358xx    */
679
680 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
681     defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
682     defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
683     defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
684
685   PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_TIM1;
686   /* Get the TIM1 clock configuration -----------------------------------------*/
687   PeriphClkInit->Tim1ClockSelection = __HAL_RCC_GET_TIM1_SOURCE();
688
689 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
690        /* STM32F302xC || STM32F303xC || STM32F358xx || */
691        /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
692        /* STM32F301x8 || STM32F302x8 || STM32F318xx    */
693   
694 #if defined(STM32F303xE) || defined(STM32F398xx) || \
695     defined(STM32F303xC) || defined(STM32F358xx)
696
697   PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_TIM8;
698   /* Get the TIM8 clock configuration -----------------------------------------*/
699   PeriphClkInit->Tim8ClockSelection = __HAL_RCC_GET_TIM8_SOURCE();
700
701 #endif /* STM32F303xE || STM32F398xx || */
702        /* STM32F303xC || STM32F358xx    */
703
704 #if defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
705
706   PeriphClkInit->PeriphClockSelection |= (RCC_PERIPHCLK_TIM15 | RCC_PERIPHCLK_TIM16 | RCC_PERIPHCLK_TIM17);
707   /* Get the TIM15 clock configuration -----------------------------------------*/
708   PeriphClkInit->Tim15ClockSelection = __HAL_RCC_GET_TIM15_SOURCE();
709   /* Get the TIM16 clock configuration -----------------------------------------*/
710   PeriphClkInit->Tim16ClockSelection = __HAL_RCC_GET_TIM16_SOURCE();
711   /* Get the TIM17 clock configuration -----------------------------------------*/
712   PeriphClkInit->Tim17ClockSelection = __HAL_RCC_GET_TIM17_SOURCE();
713
714 #endif /* STM32F301x8 || STM32F302x8 || STM32F318xx */
715
716 #if defined(STM32F334x8)
717
718   PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_HRTIM1;
719   /* Get the HRTIM1 clock configuration -----------------------------------------*/
720   PeriphClkInit->Hrtim1ClockSelection = __HAL_RCC_GET_HRTIM1_SOURCE();
721
722 #endif /* STM32F334x8 */
723
724 #if defined(STM32F373xC) || defined(STM32F378xx)
725
726   PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_SDADC;
727   /* Get the SDADC clock configuration -----------------------------------------*/
728   PeriphClkInit->SdadcClockSelection = __HAL_RCC_GET_SDADC_SOURCE();
729
730   PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_CEC;
731   /* Get the CEC clock configuration -----------------------------------------*/
732   PeriphClkInit->CecClockSelection = __HAL_RCC_GET_CEC_SOURCE();
733
734 #endif /* STM32F373xC || STM32F378xx */
735
736 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx)
737
738   PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_TIM2;
739   /* Get the TIM2 clock configuration -----------------------------------------*/
740   PeriphClkInit->Tim2ClockSelection = __HAL_RCC_GET_TIM2_SOURCE();
741
742   PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_TIM34;
743   /* Get the TIM3 clock configuration -----------------------------------------*/
744   PeriphClkInit->Tim34ClockSelection = __HAL_RCC_GET_TIM34_SOURCE();
745
746   PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_TIM15;
747   /* Get the TIM15 clock configuration -----------------------------------------*/
748   PeriphClkInit->Tim15ClockSelection = __HAL_RCC_GET_TIM15_SOURCE();
749
750   PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_TIM16;
751   /* Get the TIM16 clock configuration -----------------------------------------*/
752   PeriphClkInit->Tim16ClockSelection = __HAL_RCC_GET_TIM16_SOURCE();
753
754   PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_TIM17;
755   /* Get the TIM17 clock configuration -----------------------------------------*/
756   PeriphClkInit->Tim17ClockSelection = __HAL_RCC_GET_TIM17_SOURCE();
757
758 #endif /* STM32F302xE || STM32F303xE || STM32F398xx */
759   
760 #if defined (STM32F303xE) || defined(STM32F398xx)
761   PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_TIM20;
762   /* Get the TIM20 clock configuration -----------------------------------------*/
763   PeriphClkInit->Tim20ClockSelection = __HAL_RCC_GET_TIM20_SOURCE();
764 #endif /* STM32F303xE || STM32F398xx */
765 }
766
767 /**
768   * @brief  Initializes the RCC Oscillators according to the specified parameters in the
769   *         RCC_OscInitTypeDef.
770   * @param  RCC_OscInitStruct: pointer to an RCC_OscInitTypeDef structure that
771   *         contains the configuration information for the RCC Oscillators.
772   * @note   The PLL is not disabled when used as system clock.
773   * @retval HAL status
774   */
775 HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef  *RCC_OscInitStruct)
776 {
777   uint32_t tickstart = 0;
778
779   /* Check the parameters */
780   assert_param(RCC_OscInitStruct != HAL_NULL);
781   assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType));
782   /*------------------------------- HSE Configuration ------------------------*/
783   if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE)
784   {
785     /* Check the parameters */
786     assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState));
787     /* When the HSE is used as system clock or clock source for PLL in these cases HSE will not disabled */
788     if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_HSE) ||
789        ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && (__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSE)))
790     {
791       if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET) && (RCC_OscInitStruct->HSEState != RCC_HSE_ON))
792       {
793         return HAL_ERROR;
794       }
795     }
796     else
797     {
798       /* Reset HSEON and HSEBYP bits before configuring the HSE --------------*/
799       __HAL_RCC_HSE_CONFIG(RCC_HSE_OFF);
800       
801       /* Get timeout */
802       tickstart = HAL_GetTick();
803       
804       /* Wait till HSE is bypassed or disabled */
805       while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET)
806       {
807         if((HAL_GetTick()-tickstart) > HSE_TIMEOUT_VALUE)
808         {
809           return HAL_TIMEOUT;
810         }
811       }
812
813       /* Set the new HSE configuration ---------------------------------------*/
814       __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState);
815
816 #if defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
817     defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
818     defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx) || \
819     defined(STM32F373xC) || defined(STM32F378xx)
820       /* Configure the HSE predivision factor --------------------------------*/
821       __HAL_RCC_HSE_PREDIV_CONFIG(RCC_OscInitStruct->HSEPredivValue);
822 #endif /* STM32F302xC || STM32F303xC || STM32F358xx || */
823        /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
824        /* STM32F301x8 || STM32F302x8 || STM32F318xx    */
825        /* STM32F373xC || STM32F378xx                   */
826
827       /* Check the HSE State */
828       if(RCC_OscInitStruct->HSEState == RCC_HSE_ON)
829       {
830         /* Get timeout */
831         tickstart = HAL_GetTick();
832         
833         /* Wait till HSE is ready */  
834         while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)
835         {
836           if((HAL_GetTick()-tickstart) > HSE_TIMEOUT_VALUE)
837           {
838             return HAL_TIMEOUT;
839           }
840         }
841       }
842       else
843       {
844         /* Get timeout */
845         tickstart = HAL_GetTick();
846
847         /* Wait till HSE is bypassed or disabled */
848         while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET)
849         {
850           if((HAL_GetTick()-tickstart) > HSE_TIMEOUT_VALUE)
851           {
852             return HAL_TIMEOUT;
853           }
854         }
855       }
856     }
857   }
858   /*----------------------------- HSI Configuration --------------------------*/
859   if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI)
860   {
861     /* Check the parameters */
862     assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState));
863     assert_param(IS_RCC_CALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue));
864
865     /* Check if HSI is used as system clock or as PLL source when PLL is selected as system clock */    
866     if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_HSI) ||
867        ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && (__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSI)))
868     {
869       /* When the HSI is used as system clock it is not allowed to be disabled */
870       if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET) && (RCC_OscInitStruct->HSIState != RCC_HSI_ON))
871       {
872         return HAL_ERROR;
873       }
874       /* Otherwise, just the calibration is allowed */
875       else
876       {
877         /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
878         __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
879       }
880     }
881     else
882     {
883       /* Check the HSI State */
884       if(RCC_OscInitStruct->HSIState != RCC_HSI_OFF)
885       {
886         /* Enable the Internal High Speed oscillator (HSI). */
887         __HAL_RCC_HSI_ENABLE();
888
889         /* Get timeout */
890         tickstart = HAL_GetTick();
891
892         /* Wait till HSI is ready */  
893         while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)
894         {
895           if((HAL_GetTick()-tickstart) > HSI_TIMEOUT_VALUE)
896           {
897             return HAL_TIMEOUT;
898           }      
899         } 
900
901         /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
902         __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
903       }
904       else
905       {
906         /* Disable the Internal High Speed oscillator (HSI). */
907         __HAL_RCC_HSI_DISABLE();
908
909         /* Get timeout */
910         tickstart = HAL_GetTick();
911       
912         /* Wait till HSI is ready */  
913         while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET)
914         {
915           if((HAL_GetTick()-tickstart) > HSI_TIMEOUT_VALUE)
916           {
917             return HAL_TIMEOUT;
918           }
919         }
920       }
921     }
922   }
923   /*------------------------------ LSI Configuration -------------------------*/
924   if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI)
925   {
926     /* Check the parameters */
927     assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState));
928
929     /* Check the LSI State */
930     if(RCC_OscInitStruct->LSIState != RCC_LSI_OFF)
931     {
932       /* Enable the Internal Low Speed oscillator (LSI). */
933       __HAL_RCC_LSI_ENABLE();
934       
935       /* Get timeout */
936       tickstart = HAL_GetTick();
937       
938       /* Wait till LSI is ready */  
939       while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) == RESET)
940       {
941         if((HAL_GetTick()-tickstart) > LSI_TIMEOUT_VALUE)
942         {
943           return HAL_TIMEOUT;
944         }
945       }
946     }
947     else
948     {
949       /* Disable the Internal Low Speed oscillator (LSI). */
950       __HAL_RCC_LSI_DISABLE();
951       
952       /* Get timeout */
953       tickstart = HAL_GetTick();
954       
955       /* Wait till LSI is ready */  
956       while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) != RESET)
957       {
958         if((HAL_GetTick()-tickstart) > LSI_TIMEOUT_VALUE)
959         {
960           return HAL_TIMEOUT;
961         }
962       }
963     }
964   }
965   /*------------------------------ LSE Configuration -------------------------*/
966   if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE)
967   {
968     /* Check the parameters */
969     assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState));
970     
971     /* Enable Power Clock*/
972     __PWR_CLK_ENABLE();
973     
974     /* Enable write access to Backup domain */
975     SET_BIT(PWR->CR, PWR_CR_DBP);
976     
977     /* Wait for Backup domain Write protection disable */
978     tickstart = HAL_GetTick();
979     
980     while((PWR->CR & PWR_CR_DBP) == RESET)
981     {
982       if((HAL_GetTick()-tickstart) > DBP_TIMEOUT_VALUE)
983       {
984         return HAL_TIMEOUT;
985       }      
986     }
987
988     /* Reset LSEON and LSEBYP bits before configuring the LSE ----------------*/
989     __HAL_RCC_LSE_CONFIG(RCC_LSE_OFF);
990     
991     /* Get timeout */
992     tickstart = HAL_GetTick();
993       
994     /* Wait till LSE is ready */  
995     while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) != RESET)
996     {
997       if((HAL_GetTick()-tickstart) > LSE_TIMEOUT_VALUE)
998       {
999         return HAL_TIMEOUT;
1000       }
1001     }
1002
1003     /* Set the new LSE configuration -----------------------------------------*/
1004     __HAL_RCC_LSE_CONFIG(RCC_OscInitStruct->LSEState);
1005     /* Check the LSE State */
1006     if(RCC_OscInitStruct->LSEState == RCC_LSE_ON)
1007     {
1008       /* Get timeout */
1009       tickstart = HAL_GetTick();
1010       
1011       /* Wait till LSE is ready */  
1012       while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
1013       {
1014         if((HAL_GetTick()-tickstart) > LSE_TIMEOUT_VALUE)
1015         {
1016           return HAL_TIMEOUT;
1017         }
1018       }
1019     }
1020     else
1021     {
1022       /* Get timeout */
1023       tickstart = HAL_GetTick();
1024       
1025       /* Wait till LSE is ready */  
1026       while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) != RESET)
1027       {
1028         if((HAL_GetTick()-tickstart) > LSE_TIMEOUT_VALUE)
1029         {
1030           return HAL_TIMEOUT;
1031         }
1032       }
1033     }
1034   }
1035   /*-------------------------------- PLL Configuration -----------------------*/
1036   /* Check the parameters */
1037   assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState));
1038   if ((RCC_OscInitStruct->PLL.PLLState) != RCC_PLL_NONE)
1039   {
1040     /* Check if the PLL is used as system clock or not */
1041     if(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_PLLCLK)
1042     {
1043       if((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_ON)
1044       {
1045         /* Check the parameters */
1046         assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource));
1047         assert_param(IS_RCC_PLL_MUL(RCC_OscInitStruct->PLL.PLLMUL));
1048 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx)
1049         assert_param(IS_RCC_PREDIV(RCC_OscInitStruct->PLL.PREDIV));
1050 #endif /* STM32F302xE || STM32F303xE || STM32F398xx */
1051
1052         /* Disable the main PLL. */
1053         __HAL_RCC_PLL_DISABLE();
1054
1055         /* Get timeout */
1056         tickstart = HAL_GetTick();
1057         
1058         /* Wait till PLL is ready */  
1059         while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
1060         {
1061           if((HAL_GetTick()-tickstart) > PLL_TIMEOUT_VALUE)
1062           {
1063             return HAL_TIMEOUT;
1064           }
1065         }
1066 #if defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
1067     defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
1068     defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx) || \
1069     defined(STM32F373xC) || defined(STM32F378xx)
1070       /* Configure the main PLL clock source and multiplication factor. */
1071       __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource,
1072                            RCC_OscInitStruct->PLL.PLLMUL);
1073 #else
1074         /* Configure the main PLL clock source, predivider and multiplication factor. */
1075         __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource,
1076                              RCC_OscInitStruct->PLL.PREDIV,
1077                              RCC_OscInitStruct->PLL.PLLMUL);
1078 #endif /* STM32F302xC || STM32F303xC || STM32F358xx || */
1079        /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
1080        /* STM32F301x8 || STM32F302x8 || STM32F318xx    */
1081        /* STM32F373xC || STM32F378xx                   */
1082         
1083         /* Enable the main PLL. */
1084         __HAL_RCC_PLL_ENABLE();
1085
1086         /* Get timeout */
1087         tickstart = HAL_GetTick();
1088       
1089         /* Wait till PLL is ready */  
1090         while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
1091         {
1092           if((HAL_GetTick()-tickstart) > PLL_TIMEOUT_VALUE)
1093           {
1094             return HAL_TIMEOUT;
1095           }
1096         }
1097       }
1098       else
1099       {
1100         /* Disable the main PLL. */
1101         __HAL_RCC_PLL_DISABLE();
1102         /* Get timeout */
1103         tickstart = HAL_GetTick();
1104       
1105         /* Wait till PLL is ready */  
1106         while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
1107         {
1108           if((HAL_GetTick()-tickstart) > PLL_TIMEOUT_VALUE)
1109           {
1110             return HAL_TIMEOUT;
1111           }
1112         }
1113       }
1114     }
1115     else
1116     {
1117       return HAL_ERROR;
1118     }
1119   }
1120   return HAL_OK;
1121 }
1122
1123 /**
1124   * @brief  Configures the RCC_OscInitStruct according to the internal
1125   * RCC configuration registers.
1126   * @param  RCC_OscInitStruct: pointer to an RCC_OscInitTypeDef structure that
1127   * will be configured.
1128   * @retval None
1129   */
1130 void HAL_RCC_GetOscConfig(RCC_OscInitTypeDef  *RCC_OscInitStruct)
1131 {
1132   /* Check the parameters */
1133   assert_param(RCC_OscInitStruct != HAL_NULL);
1134
1135   /* Set all possible values for the Oscillator type parameter ---------------*/
1136   RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_LSI;
1137
1138   /* Get the HSE configuration -----------------------------------------------*/
1139   if((RCC->CR & RCC_CR_HSEBYP) == RCC_CR_HSEBYP)
1140   {
1141     RCC_OscInitStruct->HSEState = RCC_HSE_BYPASS;
1142   }
1143   else if((RCC->CR & RCC_CR_HSEON) == RCC_CR_HSEON)
1144   {
1145     RCC_OscInitStruct->HSEState = RCC_HSE_ON;
1146   }
1147   else
1148   {
1149     RCC_OscInitStruct->HSEState = RCC_HSE_OFF;
1150   }
1151   
1152 #if defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
1153     defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
1154     defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx) || \
1155     defined(STM32F373xC) || defined(STM32F378xx)
1156   RCC_OscInitStruct->HSEPredivValue = (uint32_t)(RCC->CFGR2 & RCC_CFGR2_PREDIV);
1157 #endif /* STM32F302xC || STM32F303xC || STM32F358xx || */
1158        /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
1159        /* STM32F301x8 || STM32F302x8 || STM32F318xx    */
1160        /* STM32F373xC || STM32F378xx                   */
1161
1162   /* Get the HSI configuration -----------------------------------------------*/
1163   if((RCC->CR & RCC_CR_HSION) == RCC_CR_HSION)
1164   {
1165     RCC_OscInitStruct->HSIState = RCC_HSI_ON;
1166   }
1167   else
1168   {
1169     RCC_OscInitStruct->HSIState = RCC_HSI_OFF;
1170   }
1171
1172   RCC_OscInitStruct->HSICalibrationValue = (uint32_t)((RCC->CR &RCC_CR_HSITRIM) >> POSITION_VAL(RCC_CR_HSITRIM));
1173
1174   /* Get the LSE configuration -----------------------------------------------*/
1175   if((RCC->BDCR & RCC_BDCR_LSEBYP) == RCC_BDCR_LSEBYP)
1176   {
1177     RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS;
1178   }
1179   else if((RCC->BDCR & RCC_BDCR_LSEON) == RCC_BDCR_LSEON)
1180   {
1181     RCC_OscInitStruct->LSEState = RCC_LSE_ON;
1182   }
1183   else
1184   {
1185     RCC_OscInitStruct->LSEState = RCC_LSE_OFF;
1186   }
1187
1188   /* Get the LSI configuration -----------------------------------------------*/
1189   if((RCC->CSR & RCC_CSR_LSION) == RCC_CSR_LSION)
1190   {
1191     RCC_OscInitStruct->LSIState = RCC_LSI_ON;
1192   }
1193   else
1194   {
1195     RCC_OscInitStruct->LSIState = RCC_LSI_OFF;
1196   }
1197
1198   /* Get the PLL configuration -----------------------------------------------*/
1199   if((RCC->CR & RCC_CR_PLLON) == RCC_CR_PLLON)
1200   {
1201     RCC_OscInitStruct->PLL.PLLState = RCC_PLL_ON;
1202   }
1203   else
1204   {
1205     RCC_OscInitStruct->PLL.PLLState = RCC_PLL_OFF;
1206   }
1207   
1208   RCC_OscInitStruct->PLL.PLLSource = (uint32_t)(RCC->CFGR & RCC_CFGR_PLLSRC);
1209   RCC_OscInitStruct->PLL.PLLMUL = (uint32_t)(RCC->CFGR & RCC_CFGR_PLLMUL);
1210 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx)
1211   RCC_OscInitStruct->PLL.PREDIV = (uint32_t)(RCC->CFGR2 & RCC_CFGR2_PREDIV);
1212 #endif /* STM32F302xE || STM32F303xE || STM32F398xx */
1213 }
1214
1215 /**
1216   * @brief  Returns the SYSCLK frequency
1217   * @note   The system frequency computed by this function is not the real
1218   *         frequency in the chip. It is calculated based on the predefined
1219   *         constant and the selected clock source:
1220   * @note     If SYSCLK source is HSI, function returns values based on HSI_VALUE(*)
1221   * @note     If SYSCLK source is HSE, function returns values based on HSE_VALUE
1222   *           divided by PREDIV factor(**)
1223   * @note     If SYSCLK source is PLL, function returns values based on HSE_VALUE
1224   *           divided by PREDIV factor(**) or HSI_VALUE(*) multiplied by the PLL factor.
1225   * @note     (*) HSI_VALUE is a constant defined in stm32f3xx.h file (default value
1226   *               8 MHz).
1227   * @note     (**) HSE_VALUE is a constant defined in stm32f3xx.h file (default value
1228   *                8 MHz), user has to ensure that HSE_VALUE is same as the real
1229   *                frequency of the crystal used. Otherwise, this function may
1230   *                have wrong result.
1231   *
1232   * @note   The result of this function could be not correct when using fractional
1233   *         value for HSE crystal.
1234   *
1235   * @note   This function can be used by the user application to compute the
1236   *         baudrate for the communication peripherals or configure other parameters.
1237   *
1238   * @note   Each time SYSCLK changes, this function must be called to update the
1239   *         right SYSCLK value. Otherwise, any configuration based on this function will be incorrect.
1240   *
1241   * @retval SYSCLK frequency
1242   */
1243 uint32_t HAL_RCC_GetSysClockFreq(void)
1244 {
1245   uint32_t tmpreg = 0, prediv = 0, pllmul = 0, pllclk = 0;
1246   uint32_t sysclockfreq = 0;
1247   
1248   tmpreg = RCC->CFGR;
1249
1250   /* Get SYSCLK source -------------------------------------------------------*/
1251   switch (tmpreg & RCC_CFGR_SWS)
1252   {
1253   case RCC_SYSCLKSOURCE_STATUS_HSE:    /* HSE used as system clock  source */
1254     sysclockfreq = HSE_VALUE;
1255     break;
1256
1257   case RCC_SYSCLKSOURCE_STATUS_PLLCLK: /* PLL used as system clock  source */
1258     pllmul = PLLMULFactorTable[(uint32_t)(tmpreg & RCC_CFGR_PLLMUL) >> POSITION_VAL(RCC_CFGR_PLLMUL)];
1259     prediv = PredivFactorTable[(uint32_t)(RCC->CFGR2 & RCC_CFGR2_PREDIV) >> POSITION_VAL(RCC_CFGR2_PREDIV)];
1260 #if defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
1261     defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
1262     defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx) || \
1263     defined(STM32F373xC) || defined(STM32F378xx)
1264     if ((tmpreg & RCC_CFGR_PLLSRC) != RCC_PLLSOURCE_HSI)
1265     {
1266       /* HSE used as PLL clock source : PLLCLK = HSE/PREDIV * PLLMUL */
1267       pllclk = (HSE_VALUE/prediv) * pllmul;
1268     }
1269     else
1270     {
1271       /* HSI used as PLL clock source : PLLCLK = HSI/2 * PLLMUL */
1272       pllclk = (HSI_VALUE >> 1) * pllmul;
1273     }
1274 #else
1275     if ((tmpreg & RCC_CFGR_PLLSRC_HSE_PREDIV) == RCC_CFGR_PLLSRC_HSE_PREDIV)
1276     {
1277       /* HSE used as PLL clock source : PLLCLK = HSE/PREDIV * PLLMUL */
1278       pllclk = (HSE_VALUE/prediv) * pllmul;
1279     }
1280     else
1281     {
1282       /* HSI used as PLL clock source : PLLCLK = HSI/PREDIV * PLLMUL */
1283       pllclk = (HSI_VALUE/prediv) * pllmul;
1284     }
1285 #endif /* STM32F302xC || STM32F303xC || STM32F358xx || */
1286        /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
1287        /* STM32F301x8 || STM32F302x8 || STM32F318xx    */
1288        /* STM32F373xC || STM32F378xx                   */
1289     sysclockfreq = pllclk;
1290     break;
1291
1292   case RCC_SYSCLKSOURCE_STATUS_HSI:    /* HSI used as system clock source */
1293   default:
1294     sysclockfreq = HSI_VALUE;
1295     break;
1296   }
1297   return sysclockfreq;
1298 }
1299
1300 /**
1301   * @}
1302   */
1303
1304 /**
1305   * @}
1306   */
1307
1308 #endif /* HAL_RCC_MODULE_ENABLED */
1309 /**
1310   * @}
1311   */
1312
1313 /**
1314   * @}
1315   */
1316
1317 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/