2 ******************************************************************************
3 * @file stm32f3xx_hal_rcc_ex.c
4 * @author MCD Application Team
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
12 ******************************************************************************
15 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
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.
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.
39 ******************************************************************************
42 /* Includes ------------------------------------------------------------------*/
43 #include "stm32f3xx_hal.h"
45 /** @addtogroup STM32F3xx_HAL_Driver
49 /** @defgroup RCCEx RCC Extended HAL module driver
50 * @brief RCC Extended HAL module driver.
54 #ifdef HAL_RCC_MODULE_ENABLED
56 /* Private typedef -----------------------------------------------------------*/
57 /* Private define ------------------------------------------------------------*/
58 /** @defgroup RCCEx_Private_Define RCC Extended Private Define
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 */
70 /* Private macro -------------------------------------------------------------*/
71 /* Private variables ---------------------------------------------------------*/
72 /** @defgroup RCCEx_Private_Variables RCC Extented Private Variables
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};
83 /* Private function prototypes -----------------------------------------------*/
84 /* Exported functions ---------------------------------------------------------*/
86 /** @defgroup RCCEx_Exported_Functions RCC Extended Exported Functions
90 /** @defgroup RCCEx_Exported_Functions_Group1 Extended Peripheral Control functions
91 * @brief Extended Peripheral Control functions
94 ===============================================================================
95 ##### Extended Peripheral Control functions #####
96 ===============================================================================
98 This subsection provides a set of functions allowing to control the RCC Clocks
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.
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).
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.
124 HAL_StatusTypeDef HAL_RCCEx_PeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
126 uint32_t tickstart = 0;
129 /* Check the parameters */
130 assert_param(IS_RCC_PERIPHCLK(PeriphClkInit->PeriphClockSelection));
133 /*---------------------------- RTC configuration -------------------------------*/
134 if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_RTC) == (RCC_PERIPHCLK_RTC))
136 /* Enable Power Clock*/
139 /* Enable write access to Backup domain */
140 SET_BIT(PWR->CR, PWR_CR_DBP);
142 /* Wait for Backup domain Write protection disable */
143 tickstart = HAL_GetTick();
145 while((PWR->CR & PWR_CR_DBP) == RESET)
147 if((HAL_GetTick()-tickstart) > DBP_TIMEOUT_VALUE)
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))
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 */
165 /* If LSE is selected as RTC clock source, wait for LSE reactivation */
166 if(PeriphClkInit->RTCClockSelection == RCC_RTCCLKSOURCE_LSE)
169 tickstart = HAL_GetTick();
171 /* Wait till LSE is ready */
172 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
174 if((HAL_GetTick()-tickstart) > LSE_TIMEOUT_VALUE)
180 __HAL_RCC_RTC_CONFIG(PeriphClkInit->RTCClockSelection);
183 /*------------------------------- USART1 Configuration ------------------------*/
184 if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_USART1) == RCC_PERIPHCLK_USART1)
186 /* Check the parameters */
187 assert_param(IS_RCC_USART1CLKSOURCE(PeriphClkInit->Usart1ClockSelection));
189 /* Configure the USART1 clock source */
190 __HAL_RCC_USART1_CONFIG(PeriphClkInit->Usart1ClockSelection);
193 /*----------------------------- USART2 Configuration --------------------------*/
194 if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_USART2) == RCC_PERIPHCLK_USART2)
196 /* Check the parameters */
197 assert_param(IS_RCC_USART2CLKSOURCE(PeriphClkInit->Usart2ClockSelection));
199 /* Configure the USART2 clock source */
200 __HAL_RCC_USART2_CONFIG(PeriphClkInit->Usart2ClockSelection);
203 /*------------------------------ USART3 Configuration ------------------------*/
204 if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_USART3) == RCC_PERIPHCLK_USART3)
206 /* Check the parameters */
207 assert_param(IS_RCC_USART3CLKSOURCE(PeriphClkInit->Usart3ClockSelection));
209 /* Configure the USART3 clock source */
210 __HAL_RCC_USART3_CONFIG(PeriphClkInit->Usart3ClockSelection);
213 /*------------------------------ I2C1 Configuration ------------------------*/
214 if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2C1) == RCC_PERIPHCLK_I2C1)
216 /* Check the parameters */
217 assert_param(IS_RCC_I2C1CLKSOURCE(PeriphClkInit->I2c1ClockSelection));
219 /* Configure the I2C1 clock source */
220 __HAL_RCC_I2C1_CONFIG(PeriphClkInit->I2c1ClockSelection);
223 #if defined(STM32F302xE) || defined(STM32F303xE) || \
224 defined(STM32F302xC) || defined(STM32F303xC) || \
225 defined(STM32F302x8) || \
227 /*------------------------------ USB Configuration ------------------------*/
228 if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_USB) == RCC_PERIPHCLK_USB)
230 /* Check the parameters */
231 assert_param(IS_RCC_USBCLKSOURCE(PeriphClkInit->USBClockSelection));
233 /* Configure the USB clock source */
234 __HAL_RCC_USB_CONFIG(PeriphClkInit->USBClockSelection);
237 #endif /* STM32F302xE || STM32F303xE || */
238 /* STM32F302xC || STM32F303xC || */
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)
247 /*------------------------------ I2C2 Configuration ------------------------*/
248 if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2C2) == RCC_PERIPHCLK_I2C2)
250 /* Check the parameters */
251 assert_param(IS_RCC_I2C2CLKSOURCE(PeriphClkInit->I2c2ClockSelection));
253 /* Configure the I2C2 clock source */
254 __HAL_RCC_I2C2_CONFIG(PeriphClkInit->I2c2ClockSelection);
257 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
258 /* STM32F302xC || STM32F303xC || STM32F358xx || */
259 /* STM32F301x8 || STM32F302x8 || STM32F318xx || */
260 /* STM32F373xC || STM32F378xx */
262 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
263 defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
265 /*------------------------------ I2C3 Configuration ------------------------*/
266 if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2C3) == RCC_PERIPHCLK_I2C3)
268 /* Check the parameters */
269 assert_param(IS_RCC_I2C3CLKSOURCE(PeriphClkInit->I2c3ClockSelection));
271 /* Configure the I2C3 clock source */
272 __HAL_RCC_I2C3_CONFIG(PeriphClkInit->I2c3ClockSelection);
274 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
275 /* STM32F301x8 || STM32F302x8 || STM32F318xx */
277 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
278 defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx)
280 /*------------------------------ UART4 Configuration ------------------------*/
281 if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_UART4) == RCC_PERIPHCLK_UART4)
283 /* Check the parameters */
284 assert_param(IS_RCC_UART4CLKSOURCE(PeriphClkInit->Uart4ClockSelection));
286 /* Configure the UART4 clock source */
287 __HAL_RCC_UART4_CONFIG(PeriphClkInit->Uart4ClockSelection);
290 /*------------------------------ UART5 Configuration ------------------------*/
291 if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_UART5) == RCC_PERIPHCLK_UART5)
293 /* Check the parameters */
294 assert_param(IS_RCC_UART5CLKSOURCE(PeriphClkInit->Uart5ClockSelection));
296 /* Configure the UART5 clock source */
297 __HAL_RCC_UART5_CONFIG(PeriphClkInit->Uart5ClockSelection);
300 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
301 /* STM32F302xC || STM32F303xC || STM32F358xx */
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)
309 /* Check the parameters */
310 assert_param(IS_RCC_I2SCLKSOURCE(PeriphClkInit->I2sClockSelection));
312 /* Configure the I2S clock source */
313 __HAL_RCC_I2S_CONFIG(PeriphClkInit->I2sClockSelection);
316 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
317 /* STM32F302xC || STM32F303xC || STM32F358xx || */
318 /* STM32F301x8 || STM32F302x8 || STM32F318xx */
320 #if defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
322 /*------------------------------ ADC1 clock Configuration ------------------*/
323 if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_ADC1) == RCC_PERIPHCLK_ADC1)
325 /* Check the parameters */
326 assert_param(IS_RCC_ADC1PLLCLK_DIV(PeriphClkInit->Adc1ClockSelection));
328 /* Configure the ADC1 clock source */
329 __HAL_RCC_ADC1_CONFIG(PeriphClkInit->Adc1ClockSelection);
332 #endif /* STM32F301x8 || STM32F302x8 || STM32F318xx */
334 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
335 defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
336 defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx)
338 /*------------------------------ ADC1 & ADC2 clock Configuration -------------*/
339 if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_ADC12) == RCC_PERIPHCLK_ADC12)
341 /* Check the parameters */
342 assert_param(IS_RCC_ADC12PLLCLK_DIV(PeriphClkInit->Adc12ClockSelection));
344 /* Configure the ADC12 clock source */
345 __HAL_RCC_ADC12_CONFIG(PeriphClkInit->Adc12ClockSelection);
348 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
349 /* STM32F302xC || STM32F303xC || STM32F358xx || */
350 /* STM32F303x8 || STM32F334x8 || STM32F328xx */
352 #if defined(STM32F303xE) || defined(STM32F398xx) || \
353 defined(STM32F303xC) || defined(STM32F358xx)
355 /*------------------------------ ADC3 & ADC4 clock Configuration -------------*/
356 if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_ADC34) == RCC_PERIPHCLK_ADC34)
358 /* Check the parameters */
359 assert_param(IS_RCC_ADC34PLLCLK_DIV(PeriphClkInit->Adc34ClockSelection));
361 /* Configure the ADC34 clock source */
362 __HAL_RCC_ADC34_CONFIG(PeriphClkInit->Adc34ClockSelection);
365 #endif /* STM32F303xE || STM32F398xx || */
366 /* STM32F303xC || STM32F358xx */
368 #if defined(STM32F373xC) || defined(STM32F378xx)
370 /*------------------------------ ADC1 clock Configuration ------------------*/
371 if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_ADC1) == RCC_PERIPHCLK_ADC1)
373 /* Check the parameters */
374 assert_param(IS_RCC_ADC1PCLK2_DIV(PeriphClkInit->Adc1ClockSelection));
376 /* Configure the ADC1 clock source */
377 __HAL_RCC_ADC1_CONFIG(PeriphClkInit->Adc1ClockSelection);
380 #endif /* STM32F373xC || STM32F378xx */
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)
387 /*------------------------------ TIM1 clock Configuration ----------------*/
388 if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM1) == RCC_PERIPHCLK_TIM1)
390 /* Check the parameters */
391 assert_param(IS_RCC_TIM1CLKSOURCE(PeriphClkInit->Tim1ClockSelection));
393 /* Configure the TIM1 clock source */
394 __HAL_RCC_TIM1_CONFIG(PeriphClkInit->Tim1ClockSelection);
397 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
398 /* STM32F302xC || STM32F303xC || STM32F358xx || */
399 /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
400 /* STM32F301x8 || STM32F302x8 || STM32F318xx */
402 #if defined(STM32F303xE) || defined(STM32F398xx) || \
403 defined(STM32F303xC) || defined(STM32F358xx)
405 /*------------------------------ TIM8 clock Configuration ----------------*/
406 if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM8) == RCC_PERIPHCLK_TIM8)
408 /* Check the parameters */
409 assert_param(IS_RCC_TIM8CLKSOURCE(PeriphClkInit->Tim8ClockSelection));
411 /* Configure the TIM8 clock source */
412 __HAL_RCC_TIM8_CONFIG(PeriphClkInit->Tim8ClockSelection);
415 #endif /* STM32F303xE || STM32F398xx || */
416 /* STM32F303xC || STM32F358xx */
418 #if defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
420 /*------------------------------ TIM15 clock Configuration ----------------*/
421 if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM15) == RCC_PERIPHCLK_TIM15)
423 /* Check the parameters */
424 assert_param(IS_RCC_TIM15CLKSOURCE(PeriphClkInit->Tim15ClockSelection));
426 /* Configure the TIM15 clock source */
427 __HAL_RCC_TIM15_CONFIG(PeriphClkInit->Tim15ClockSelection);
430 /*------------------------------ TIM16 clock Configuration ----------------*/
431 if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM16) == RCC_PERIPHCLK_TIM16)
433 /* Check the parameters */
434 assert_param(IS_RCC_TIM16CLKSOURCE(PeriphClkInit->Tim16ClockSelection));
436 /* Configure the TIM16 clock source */
437 __HAL_RCC_TIM16_CONFIG(PeriphClkInit->Tim16ClockSelection);
440 /*------------------------------ TIM17 clock Configuration ----------------*/
441 if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM17) == RCC_PERIPHCLK_TIM17)
443 /* Check the parameters */
444 assert_param(IS_RCC_TIM17CLKSOURCE(PeriphClkInit->Tim17ClockSelection));
446 /* Configure the TIM17 clock source */
447 __HAL_RCC_TIM17_CONFIG(PeriphClkInit->Tim17ClockSelection);
450 #endif /* STM32F301x8 || STM32F302x8 || STM32F318xx */
452 #if defined(STM32F334x8)
454 /*------------------------------ HRTIM1 clock Configuration ----------------*/
455 if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_HRTIM1) == RCC_PERIPHCLK_HRTIM1)
457 /* Check the parameters */
458 assert_param(IS_RCC_HRTIM1CLKSOURCE(PeriphClkInit->Hrtim1ClockSelection));
460 /* Configure the HRTIM1 clock source */
461 __HAL_RCC_HRTIM1_CONFIG(PeriphClkInit->Hrtim1ClockSelection);
464 #endif /* STM32F334x8 */
466 #if defined(STM32F373xC) || defined(STM32F378xx)
468 /*------------------------------ SDADC clock Configuration -------------------*/
469 if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_SDADC) == RCC_PERIPHCLK_SDADC)
471 /* Check the parameters */
472 assert_param(IS_RCC_SDADCSYSCLK_DIV(PeriphClkInit->SdadcClockSelection));
474 /* Configure the SDADC clock prescaler */
475 __HAL_RCC_SDADC_CONFIG(PeriphClkInit->SdadcClockSelection);
478 /*------------------------------ CEC clock Configuration -------------------*/
479 if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_CEC) == RCC_PERIPHCLK_CEC)
481 /* Check the parameters */
482 assert_param(IS_RCC_CECCLKSOURCE(PeriphClkInit->CecClockSelection));
484 /* Configure the CEC clock source */
485 __HAL_RCC_CEC_CONFIG(PeriphClkInit->CecClockSelection);
488 #endif /* STM32F373xC || STM32F378xx */
490 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx)
492 /*------------------------------ TIM2 clock Configuration -------------------*/
493 if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM2) == RCC_PERIPHCLK_TIM2)
495 /* Check the parameters */
496 assert_param(IS_RCC_TIM2CLKSOURCE(PeriphClkInit->Tim2ClockSelection));
498 /* Configure the CEC clock source */
499 __HAL_RCC_TIM2_CONFIG(PeriphClkInit->Tim2ClockSelection);
502 /*------------------------------ TIM3 clock Configuration -------------------*/
503 if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM34) == RCC_PERIPHCLK_TIM34)
505 /* Check the parameters */
506 assert_param(IS_RCC_TIM3CLKSOURCE(PeriphClkInit->Tim34ClockSelection));
508 /* Configure the CEC clock source */
509 __HAL_RCC_TIM34_CONFIG(PeriphClkInit->Tim34ClockSelection);
512 /*------------------------------ TIM15 clock Configuration ------------------*/
513 if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM15) == RCC_PERIPHCLK_TIM15)
515 /* Check the parameters */
516 assert_param(IS_RCC_TIM15CLKSOURCE(PeriphClkInit->Tim15ClockSelection));
518 /* Configure the CEC clock source */
519 __HAL_RCC_TIM15_CONFIG(PeriphClkInit->Tim15ClockSelection);
522 /*------------------------------ TIM16 clock Configuration ------------------*/
523 if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM16) == RCC_PERIPHCLK_TIM16)
525 /* Check the parameters */
526 assert_param(IS_RCC_TIM16CLKSOURCE(PeriphClkInit->Tim16ClockSelection));
528 /* Configure the CEC clock source */
529 __HAL_RCC_TIM16_CONFIG(PeriphClkInit->Tim16ClockSelection);
532 /*------------------------------ TIM17 clock Configuration ------------------*/
533 if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM17) == RCC_PERIPHCLK_TIM17)
535 /* Check the parameters */
536 assert_param(IS_RCC_TIM17CLKSOURCE(PeriphClkInit->Tim17ClockSelection));
538 /* Configure the CEC clock source */
539 __HAL_RCC_TIM17_CONFIG(PeriphClkInit->Tim17ClockSelection);
542 #endif /* STM32F302xE || STM32F303xE || STM32F398xx */
544 #if defined(STM32F303xE) || defined(STM32F398xx)
545 /*------------------------------ TIM20 clock Configuration ------------------*/
546 if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_TIM20) == RCC_PERIPHCLK_TIM20)
548 /* Check the parameters */
549 assert_param(IS_RCC_TIM20CLKSOURCE(PeriphClkInit->Tim20ClockSelection));
551 /* Configure the CEC clock source */
552 __HAL_RCC_TIM20_CONFIG(PeriphClkInit->Tim20ClockSelection);
554 #endif /* STM32F303xE || STM32F398xx */
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).
568 void HAL_RCCEx_GetPeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
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;
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();
586 #if defined(STM32F302xE) || defined(STM32F303xE) || \
587 defined(STM32F302xC) || defined(STM32F303xC) || \
588 defined(STM32F302x8) || \
591 PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_USB;
592 /* Get the USB clock configuration -----------------------------------------*/
593 PeriphClkInit->USBClockSelection = __HAL_RCC_GET_USB_SOURCE();
595 #endif /* STM32F302xE || STM32F303xE || */
596 /* STM32F302xC || STM32F303xC || */
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)
605 PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_I2C2;
606 /* Get the I2C2 clock configuration -----------------------------------------*/
607 PeriphClkInit->I2c2ClockSelection = __HAL_RCC_GET_I2C2_SOURCE();
609 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
610 /* STM32F302xC || STM32F303xC || STM32F358xx || */
611 /* STM32F301x8 || STM32F302x8 || STM32F318xx || */
612 /* STM32F373xC || STM32F378xx */
614 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
615 defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
617 PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_I2C3;
618 /* Get the I2C3 clock configuration -----------------------------------------*/
619 PeriphClkInit->I2c3ClockSelection = __HAL_RCC_GET_I2C3_SOURCE();
621 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
622 /* STM32F301x8 || STM32F302x8 || STM32F318xx */
624 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
625 defined(STM32F302xC) || defined(STM32F303xC) ||defined(STM32F358xx)
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();
633 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
634 /* STM32F302xC || STM32F303xC || STM32F358xx */
636 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
637 defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
638 defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
640 PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_I2S;
641 /* Get the I2S clock configuration -----------------------------------------*/
642 PeriphClkInit->I2sClockSelection = __HAL_RCC_GET_I2S_SOURCE();
644 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
645 /* STM32F302xC || STM32F303xC || STM32F358xx || */
646 /* STM32F301x8 || STM32F302x8 || STM32F318xx || */
648 #if defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx) || \
649 defined(STM32F373xC) || defined(STM32F378xx)
651 PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_ADC1;
652 /* Get the ADC1 clock configuration -----------------------------------------*/
653 PeriphClkInit->Adc1ClockSelection = __HAL_RCC_GET_ADC1_SOURCE();
655 #endif /* STM32F301x8 || STM32F302x8 || STM32F318xx || */
656 /* STM32F373xC || STM32F378xx */
658 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
659 defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
660 defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx)
662 PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_ADC12;
663 /* Get the ADC1 & ADC2 clock configuration -----------------------------------------*/
664 PeriphClkInit->Adc12ClockSelection = __HAL_RCC_GET_ADC12_SOURCE();
666 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
667 /* STM32F302xC || STM32F303xC || STM32F358xx || */
668 /* STM32F303x8 || STM32F334x8 || STM32F328xx */
670 #if defined(STM32F303xE) || defined(STM32F398xx) || \
671 defined(STM32F303xC) || defined(STM32F358xx)
673 PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_ADC34;
674 /* Get the ADC3 & ADC4 clock configuration -----------------------------------------*/
675 PeriphClkInit->Adc34ClockSelection = __HAL_RCC_GET_ADC34_SOURCE();
677 #endif /* STM32F303xE || STM32F398xx || */
678 /* STM32F303xC || STM32F358xx */
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)
685 PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_TIM1;
686 /* Get the TIM1 clock configuration -----------------------------------------*/
687 PeriphClkInit->Tim1ClockSelection = __HAL_RCC_GET_TIM1_SOURCE();
689 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
690 /* STM32F302xC || STM32F303xC || STM32F358xx || */
691 /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
692 /* STM32F301x8 || STM32F302x8 || STM32F318xx */
694 #if defined(STM32F303xE) || defined(STM32F398xx) || \
695 defined(STM32F303xC) || defined(STM32F358xx)
697 PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_TIM8;
698 /* Get the TIM8 clock configuration -----------------------------------------*/
699 PeriphClkInit->Tim8ClockSelection = __HAL_RCC_GET_TIM8_SOURCE();
701 #endif /* STM32F303xE || STM32F398xx || */
702 /* STM32F303xC || STM32F358xx */
704 #if defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
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();
714 #endif /* STM32F301x8 || STM32F302x8 || STM32F318xx */
716 #if defined(STM32F334x8)
718 PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_HRTIM1;
719 /* Get the HRTIM1 clock configuration -----------------------------------------*/
720 PeriphClkInit->Hrtim1ClockSelection = __HAL_RCC_GET_HRTIM1_SOURCE();
722 #endif /* STM32F334x8 */
724 #if defined(STM32F373xC) || defined(STM32F378xx)
726 PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_SDADC;
727 /* Get the SDADC clock configuration -----------------------------------------*/
728 PeriphClkInit->SdadcClockSelection = __HAL_RCC_GET_SDADC_SOURCE();
730 PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_CEC;
731 /* Get the CEC clock configuration -----------------------------------------*/
732 PeriphClkInit->CecClockSelection = __HAL_RCC_GET_CEC_SOURCE();
734 #endif /* STM32F373xC || STM32F378xx */
736 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx)
738 PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_TIM2;
739 /* Get the TIM2 clock configuration -----------------------------------------*/
740 PeriphClkInit->Tim2ClockSelection = __HAL_RCC_GET_TIM2_SOURCE();
742 PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_TIM34;
743 /* Get the TIM3 clock configuration -----------------------------------------*/
744 PeriphClkInit->Tim34ClockSelection = __HAL_RCC_GET_TIM34_SOURCE();
746 PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_TIM15;
747 /* Get the TIM15 clock configuration -----------------------------------------*/
748 PeriphClkInit->Tim15ClockSelection = __HAL_RCC_GET_TIM15_SOURCE();
750 PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_TIM16;
751 /* Get the TIM16 clock configuration -----------------------------------------*/
752 PeriphClkInit->Tim16ClockSelection = __HAL_RCC_GET_TIM16_SOURCE();
754 PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_TIM17;
755 /* Get the TIM17 clock configuration -----------------------------------------*/
756 PeriphClkInit->Tim17ClockSelection = __HAL_RCC_GET_TIM17_SOURCE();
758 #endif /* STM32F302xE || STM32F303xE || STM32F398xx */
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 */
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.
775 HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
777 uint32_t tickstart = 0;
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)
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)))
791 if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET) && (RCC_OscInitStruct->HSEState != RCC_HSE_ON))
798 /* Reset HSEON and HSEBYP bits before configuring the HSE --------------*/
799 __HAL_RCC_HSE_CONFIG(RCC_HSE_OFF);
802 tickstart = HAL_GetTick();
804 /* Wait till HSE is bypassed or disabled */
805 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET)
807 if((HAL_GetTick()-tickstart) > HSE_TIMEOUT_VALUE)
813 /* Set the new HSE configuration ---------------------------------------*/
814 __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState);
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 */
827 /* Check the HSE State */
828 if(RCC_OscInitStruct->HSEState == RCC_HSE_ON)
831 tickstart = HAL_GetTick();
833 /* Wait till HSE is ready */
834 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)
836 if((HAL_GetTick()-tickstart) > HSE_TIMEOUT_VALUE)
845 tickstart = HAL_GetTick();
847 /* Wait till HSE is bypassed or disabled */
848 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET)
850 if((HAL_GetTick()-tickstart) > HSE_TIMEOUT_VALUE)
858 /*----------------------------- HSI Configuration --------------------------*/
859 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI)
861 /* Check the parameters */
862 assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState));
863 assert_param(IS_RCC_CALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue));
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)))
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))
874 /* Otherwise, just the calibration is allowed */
877 /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
878 __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
883 /* Check the HSI State */
884 if(RCC_OscInitStruct->HSIState != RCC_HSI_OFF)
886 /* Enable the Internal High Speed oscillator (HSI). */
887 __HAL_RCC_HSI_ENABLE();
890 tickstart = HAL_GetTick();
892 /* Wait till HSI is ready */
893 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)
895 if((HAL_GetTick()-tickstart) > HSI_TIMEOUT_VALUE)
901 /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
902 __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
906 /* Disable the Internal High Speed oscillator (HSI). */
907 __HAL_RCC_HSI_DISABLE();
910 tickstart = HAL_GetTick();
912 /* Wait till HSI is ready */
913 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET)
915 if((HAL_GetTick()-tickstart) > HSI_TIMEOUT_VALUE)
923 /*------------------------------ LSI Configuration -------------------------*/
924 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI)
926 /* Check the parameters */
927 assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState));
929 /* Check the LSI State */
930 if(RCC_OscInitStruct->LSIState != RCC_LSI_OFF)
932 /* Enable the Internal Low Speed oscillator (LSI). */
933 __HAL_RCC_LSI_ENABLE();
936 tickstart = HAL_GetTick();
938 /* Wait till LSI is ready */
939 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) == RESET)
941 if((HAL_GetTick()-tickstart) > LSI_TIMEOUT_VALUE)
949 /* Disable the Internal Low Speed oscillator (LSI). */
950 __HAL_RCC_LSI_DISABLE();
953 tickstart = HAL_GetTick();
955 /* Wait till LSI is ready */
956 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) != RESET)
958 if((HAL_GetTick()-tickstart) > LSI_TIMEOUT_VALUE)
965 /*------------------------------ LSE Configuration -------------------------*/
966 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE)
968 /* Check the parameters */
969 assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState));
971 /* Enable Power Clock*/
974 /* Enable write access to Backup domain */
975 SET_BIT(PWR->CR, PWR_CR_DBP);
977 /* Wait for Backup domain Write protection disable */
978 tickstart = HAL_GetTick();
980 while((PWR->CR & PWR_CR_DBP) == RESET)
982 if((HAL_GetTick()-tickstart) > DBP_TIMEOUT_VALUE)
988 /* Reset LSEON and LSEBYP bits before configuring the LSE ----------------*/
989 __HAL_RCC_LSE_CONFIG(RCC_LSE_OFF);
992 tickstart = HAL_GetTick();
994 /* Wait till LSE is ready */
995 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) != RESET)
997 if((HAL_GetTick()-tickstart) > LSE_TIMEOUT_VALUE)
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)
1009 tickstart = HAL_GetTick();
1011 /* Wait till LSE is ready */
1012 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
1014 if((HAL_GetTick()-tickstart) > LSE_TIMEOUT_VALUE)
1023 tickstart = HAL_GetTick();
1025 /* Wait till LSE is ready */
1026 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) != RESET)
1028 if((HAL_GetTick()-tickstart) > LSE_TIMEOUT_VALUE)
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)
1040 /* Check if the PLL is used as system clock or not */
1041 if(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_PLLCLK)
1043 if((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_ON)
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 */
1052 /* Disable the main PLL. */
1053 __HAL_RCC_PLL_DISABLE();
1056 tickstart = HAL_GetTick();
1058 /* Wait till PLL is ready */
1059 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
1061 if((HAL_GetTick()-tickstart) > PLL_TIMEOUT_VALUE)
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);
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 */
1083 /* Enable the main PLL. */
1084 __HAL_RCC_PLL_ENABLE();
1087 tickstart = HAL_GetTick();
1089 /* Wait till PLL is ready */
1090 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
1092 if((HAL_GetTick()-tickstart) > PLL_TIMEOUT_VALUE)
1100 /* Disable the main PLL. */
1101 __HAL_RCC_PLL_DISABLE();
1103 tickstart = HAL_GetTick();
1105 /* Wait till PLL is ready */
1106 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
1108 if((HAL_GetTick()-tickstart) > PLL_TIMEOUT_VALUE)
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.
1130 void HAL_RCC_GetOscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
1132 /* Check the parameters */
1133 assert_param(RCC_OscInitStruct != HAL_NULL);
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;
1138 /* Get the HSE configuration -----------------------------------------------*/
1139 if((RCC->CR & RCC_CR_HSEBYP) == RCC_CR_HSEBYP)
1141 RCC_OscInitStruct->HSEState = RCC_HSE_BYPASS;
1143 else if((RCC->CR & RCC_CR_HSEON) == RCC_CR_HSEON)
1145 RCC_OscInitStruct->HSEState = RCC_HSE_ON;
1149 RCC_OscInitStruct->HSEState = RCC_HSE_OFF;
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 */
1162 /* Get the HSI configuration -----------------------------------------------*/
1163 if((RCC->CR & RCC_CR_HSION) == RCC_CR_HSION)
1165 RCC_OscInitStruct->HSIState = RCC_HSI_ON;
1169 RCC_OscInitStruct->HSIState = RCC_HSI_OFF;
1172 RCC_OscInitStruct->HSICalibrationValue = (uint32_t)((RCC->CR &RCC_CR_HSITRIM) >> POSITION_VAL(RCC_CR_HSITRIM));
1174 /* Get the LSE configuration -----------------------------------------------*/
1175 if((RCC->BDCR & RCC_BDCR_LSEBYP) == RCC_BDCR_LSEBYP)
1177 RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS;
1179 else if((RCC->BDCR & RCC_BDCR_LSEON) == RCC_BDCR_LSEON)
1181 RCC_OscInitStruct->LSEState = RCC_LSE_ON;
1185 RCC_OscInitStruct->LSEState = RCC_LSE_OFF;
1188 /* Get the LSI configuration -----------------------------------------------*/
1189 if((RCC->CSR & RCC_CSR_LSION) == RCC_CSR_LSION)
1191 RCC_OscInitStruct->LSIState = RCC_LSI_ON;
1195 RCC_OscInitStruct->LSIState = RCC_LSI_OFF;
1198 /* Get the PLL configuration -----------------------------------------------*/
1199 if((RCC->CR & RCC_CR_PLLON) == RCC_CR_PLLON)
1201 RCC_OscInitStruct->PLL.PLLState = RCC_PLL_ON;
1205 RCC_OscInitStruct->PLL.PLLState = RCC_PLL_OFF;
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 */
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
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.
1232 * @note The result of this function could be not correct when using fractional
1233 * value for HSE crystal.
1235 * @note This function can be used by the user application to compute the
1236 * baudrate for the communication peripherals or configure other parameters.
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.
1241 * @retval SYSCLK frequency
1243 uint32_t HAL_RCC_GetSysClockFreq(void)
1245 uint32_t tmpreg = 0, prediv = 0, pllmul = 0, pllclk = 0;
1246 uint32_t sysclockfreq = 0;
1250 /* Get SYSCLK source -------------------------------------------------------*/
1251 switch (tmpreg & RCC_CFGR_SWS)
1253 case RCC_SYSCLKSOURCE_STATUS_HSE: /* HSE used as system clock source */
1254 sysclockfreq = HSE_VALUE;
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)
1266 /* HSE used as PLL clock source : PLLCLK = HSE/PREDIV * PLLMUL */
1267 pllclk = (HSE_VALUE/prediv) * pllmul;
1271 /* HSI used as PLL clock source : PLLCLK = HSI/2 * PLLMUL */
1272 pllclk = (HSI_VALUE >> 1) * pllmul;
1275 if ((tmpreg & RCC_CFGR_PLLSRC_HSE_PREDIV) == RCC_CFGR_PLLSRC_HSE_PREDIV)
1277 /* HSE used as PLL clock source : PLLCLK = HSE/PREDIV * PLLMUL */
1278 pllclk = (HSE_VALUE/prediv) * pllmul;
1282 /* HSI used as PLL clock source : PLLCLK = HSI/PREDIV * PLLMUL */
1283 pllclk = (HSI_VALUE/prediv) * pllmul;
1285 #endif /* STM32F302xC || STM32F303xC || STM32F358xx || */
1286 /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
1287 /* STM32F301x8 || STM32F302x8 || STM32F318xx */
1288 /* STM32F373xC || STM32F378xx */
1289 sysclockfreq = pllclk;
1292 case RCC_SYSCLKSOURCE_STATUS_HSI: /* HSI used as system clock source */
1294 sysclockfreq = HSI_VALUE;
1297 return sysclockfreq;
1308 #endif /* HAL_RCC_MODULE_ENABLED */
1317 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/