2 ******************************************************************************
3 * @file stm32f1xx_hal_rcc_ex.c
4 * @author MCD Application Team
6 * @date 15-December-2014
7 * @brief Extended RCC HAL module driver.
9 * This file provides firmware functions to manage the following
10 * functionalities RCC extension peripheral:
11 * + Extended Peripheral Control functions
13 ******************************************************************************
16 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
18 * Redistribution and use in source and binary forms, with or without modification,
19 * are permitted provided that the following conditions are met:
20 * 1. Redistributions of source code must retain the above copyright notice,
21 * this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright notice,
23 * this list of conditions and the following disclaimer in the documentation
24 * and/or other materials provided with the distribution.
25 * 3. Neither the name of STMicroelectronics nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
30 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
32 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
36 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
37 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 ******************************************************************************
43 /* Includes ------------------------------------------------------------------*/
44 #include "stm32f1xx_hal.h"
46 /** @addtogroup STM32F1xx_HAL_Driver
50 #ifdef HAL_RCC_MODULE_ENABLED
52 /** @defgroup RCCEx RCCEx
53 * @brief RCC Extension HAL module driver
57 /* Private typedef -----------------------------------------------------------*/
58 /* Private define ------------------------------------------------------------*/
59 /** @defgroup RCCEx_Private_Constants RCCEx Private Constants
62 #define PLL2_TIMEOUT_VALUE ((uint32_t)100) /* 100 ms */
64 /* Alias word address of PLL2ON bit */
65 #define PLL2ON_BITNUMBER POSITION_VAL(RCC_CR_PLL2ON)
66 #define CR_PLL2ON_BB ((uint32_t)(PERIPH_BB_BASE + (RCC_CR_OFFSET_BB * 32) + (PLL2ON_BITNUMBER * 4)))
72 /* Private macro -------------------------------------------------------------*/
73 /** @defgroup RCCEx_Private_Macros RCCEx Private Macros
80 /** @defgroup RCCEx_Private_Macros RCCEx Private Macros
89 /* Private variables ---------------------------------------------------------*/
90 /* Private function prototypes -----------------------------------------------*/
91 /* Private functions ---------------------------------------------------------*/
101 /** @addtogroup RCC_Exported_Functions
105 #if defined(STM32F105xC) || defined(STM32F107xC) || defined (STM32F100xB) || defined (STM32F100xE)
106 /** @addtogroup RCC_Exported_Functions_Group1
111 * @brief Resets the RCC clock configuration to the default reset state.
112 * @note The default reset state of the clock configuration is given below:
113 * - HSI ON and used as system clock source
115 * - AHB, APB1 and APB2 prescaler set to 1.
117 * - All interrupts disabled
118 * @note This function doesn't modify the configuration of the
119 * - Peripheral clocks
120 * - LSI, LSE and RTC clocks
123 void HAL_RCC_DeInit(void)
125 /* Switch SYSCLK to HSI */
126 CLEAR_BIT(RCC->CFGR, RCC_CFGR_SW);
128 /* Reset HSEON, CSSON, & PLLON bits */
129 CLEAR_BIT(RCC->CR, RCC_CR_HSEON | RCC_CR_CSSON | RCC_CR_PLLON);
131 /* Reset HSEBYP bit */
132 CLEAR_BIT(RCC->CR, RCC_CR_HSEBYP);
134 /* Reset CFGR register */
135 CLEAR_REG(RCC->CFGR);
137 /* Set HSITRIM bits to the reset value */
138 MODIFY_REG(RCC->CR, RCC_CR_HSITRIM, ((uint32_t)0x10 << POSITION_VAL(RCC_CR_HSITRIM)));
140 /* Reset CFGR2 register */
141 CLEAR_REG(RCC->CFGR2);
143 /* Disable all interrupts */
150 #endif /* STM32F105xC || STM32F107xC || STM32F100xB || STM32F100xE */
152 #if defined(STM32F105xC) || defined(STM32F107xC)
153 /** @addtogroup RCC_Exported_Functions_Group1
158 * @brief Initializes the RCC Oscillators according to the specified parameters in the
159 * RCC_OscInitTypeDef.
160 * @param RCC_OscInitStruct: pointer to an RCC_OscInitTypeDef structure that
161 * contains the configuration information for the RCC Oscillators.
162 * @note The PLL is not disabled when used as system clock.
163 * @note The PLL is not disabled when USB OTG FS clock is enabled (specific to devices with USB FS)
166 HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
168 uint32_t tickstart = 0;
170 /* Check the parameters */
171 assert_param(RCC_OscInitStruct != NULL);
172 assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType));
174 /*------------------------------- HSE Configuration ------------------------*/
175 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE)
177 /* Check the parameters */
178 assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState));
180 /* When the HSE is used as system clock or clock source for PLL in these cases it is not allowed to be disabled */
181 if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_HSE)
182 || ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && (__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSE)))
184 if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET) && (RCC_OscInitStruct->HSEState != RCC_HSE_ON) && (RCC_OscInitStruct->HSEState != RCC_HSE_BYPASS))
191 /* Reset HSEON and HSEBYP bits before configuring the HSE --------------*/
192 __HAL_RCC_HSE_CONFIG(RCC_HSE_OFF);
195 tickstart = HAL_GetTick();
197 /* Wait till HSE is disabled */
198 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET)
200 if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
206 /* Set the new HSE configuration ---------------------------------------*/
207 __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState);
209 /* Check the HSE State */
210 if(RCC_OscInitStruct->HSEState != RCC_HSE_OFF)
213 tickstart = HAL_GetTick();
215 /* Wait till HSE is ready */
216 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)
218 if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
227 tickstart = HAL_GetTick();
229 /* Wait till HSE is disabled */
230 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET)
232 if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
240 /*----------------------------- HSI Configuration --------------------------*/
241 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI)
243 /* Check the parameters */
244 assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState));
245 assert_param(IS_RCC_CALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue));
247 /* Check if HSI is used as system clock or as PLL source when PLL is selected as system clock */
248 if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_HSI)
249 || ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && (__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSI_DIV2)))
251 /* When HSI is used as system clock it will not disabled */
252 if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET) && (RCC_OscInitStruct->HSIState != RCC_HSI_ON))
256 /* Otherwise, just the calibration is allowed */
259 /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
260 __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
265 /* Check the HSI State */
266 if((RCC_OscInitStruct->HSIState)!= RCC_HSI_OFF)
268 /* Enable the Internal High Speed oscillator (HSI). */
269 __HAL_RCC_HSI_ENABLE();
272 tickstart = HAL_GetTick();
274 /* Wait till HSI is ready */
275 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)
277 if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
283 /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
284 __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
288 /* Disable the Internal High Speed oscillator (HSI). */
289 __HAL_RCC_HSI_DISABLE();
292 tickstart = HAL_GetTick();
294 /* Wait till HSI is disabled */
295 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET)
297 if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
305 /*------------------------------ LSI Configuration -------------------------*/
306 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI)
308 /* Check the parameters */
309 assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState));
311 /* Check the LSI State */
312 if((RCC_OscInitStruct->LSIState)!= RCC_LSI_OFF)
314 /* Enable the Internal Low Speed oscillator (LSI). */
315 __HAL_RCC_LSI_ENABLE();
318 tickstart = HAL_GetTick();
320 /* Wait till LSI is ready */
321 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) == RESET)
323 if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
328 /* To have a fully stabilized clock in the specified range, a software temporization of 1ms
334 /* Disable the Internal Low Speed oscillator (LSI). */
335 __HAL_RCC_LSI_DISABLE();
338 tickstart = HAL_GetTick();
340 /* Wait till LSI is disabled */
341 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) != RESET)
343 if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
350 /*------------------------------ LSE Configuration -------------------------*/
351 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE)
353 /* Check the parameters */
354 assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState));
356 /* Enable Power Clock*/
357 __HAL_RCC_PWR_CLK_ENABLE();
359 /* Enable write access to Backup domain */
360 SET_BIT(PWR->CR, PWR_CR_DBP);
362 /* Wait for Backup domain Write protection disable */
363 tickstart = HAL_GetTick();
365 while((PWR->CR & PWR_CR_DBP) == RESET)
367 if((HAL_GetTick() - tickstart ) > RCC_DBP_TIMEOUT_VALUE)
373 /* Reset LSEON and LSEBYP bits before configuring the LSE ----------------*/
374 __HAL_RCC_LSE_CONFIG(RCC_LSE_OFF);
377 tickstart = HAL_GetTick();
379 /* Wait till LSE is disabled */
380 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) != RESET)
382 if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
388 /* Set the new LSE configuration -----------------------------------------*/
389 __HAL_RCC_LSE_CONFIG(RCC_OscInitStruct->LSEState);
390 /* Check the LSE State */
391 if((RCC_OscInitStruct->LSEState) == RCC_LSE_ON)
394 tickstart = HAL_GetTick();
396 /* Wait till LSE is ready */
397 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
399 if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
408 tickstart = HAL_GetTick();
410 /* Wait till LSE is disabled */
411 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) != RESET)
413 if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
421 /*-------------------------------- PLL2 Configuration -----------------------*/
422 /* Check the parameters */
423 assert_param(IS_RCC_PLL2(RCC_OscInitStruct->PLL2.PLL2State));
424 if ((RCC_OscInitStruct->PLL2.PLL2State) != RCC_PLL2_NONE)
426 /* This bit can not be cleared if the PLL2 clock is used indirectly as system
427 clock (i.e. it is used as PLL clock entry that is used as system clock). */
428 if((__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSE) && \
429 (__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && \
430 ((READ_BIT(RCC->CFGR2,RCC_CFGR2_PREDIV1SRC)) == RCC_CFGR2_PREDIV1SRC_PLL2))
436 if((RCC_OscInitStruct->PLL2.PLL2State) == RCC_PLL2_ON)
438 /* Check the parameters */
439 assert_param(IS_RCC_PLL2_MUL(RCC_OscInitStruct->PLL2.PLL2MUL));
440 assert_param(IS_RCC_HSE_PREDIV2(RCC_OscInitStruct->PLL2.HSEPrediv2Value));
442 /* Prediv2 can be written only when the PLLI2S is disabled. */
443 /* Return an error only if new value is different from the programmed value */
444 if (HAL_IS_BIT_SET(RCC->CR,RCC_CR_PLL3ON) && \
445 (__HAL_RCC_HSE_GET_PREDIV2() != RCC_OscInitStruct->PLL2.HSEPrediv2Value))
450 /* Disable the main PLL2. */
451 __HAL_RCC_PLL2_DISABLE();
454 tickstart = HAL_GetTick();
456 /* Wait till PLL2 is disabled */
457 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLL2RDY) != RESET)
459 if((HAL_GetTick() - tickstart ) > PLL2_TIMEOUT_VALUE)
465 /* Configure the HSE prediv2 factor --------------------------------*/
466 __HAL_RCC_HSE_PREDIV2_CONFIG(RCC_OscInitStruct->PLL2.HSEPrediv2Value);
468 /* Configure the main PLL2 multiplication factors. */
469 __HAL_RCC_PLL2_CONFIG(RCC_OscInitStruct->PLL2.PLL2MUL);
471 /* Enable the main PLL2. */
472 __HAL_RCC_PLL2_ENABLE();
475 tickstart = HAL_GetTick();
477 /* Wait till PLL2 is ready */
478 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLL2RDY) == RESET)
480 if((HAL_GetTick() - tickstart ) > PLL2_TIMEOUT_VALUE)
488 /* Set PREDIV1 source to HSE */
489 CLEAR_BIT(RCC->CFGR2, RCC_CFGR2_PREDIV1SRC);
491 /* Disable the main PLL2. */
492 __HAL_RCC_PLL2_DISABLE();
495 tickstart = HAL_GetTick();
497 /* Wait till PLL2 is disabled */
498 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLL2RDY) != RESET)
500 if((HAL_GetTick() - tickstart ) > PLL2_TIMEOUT_VALUE)
509 /*-------------------------------- PLL Configuration -----------------------*/
510 /* Check the parameters */
511 assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState));
512 if ((RCC_OscInitStruct->PLL.PLLState) != RCC_PLL_NONE)
514 /* Check if the PLL is used as system clock or not */
515 if(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_PLLCLK)
517 if((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_ON)
519 /* Check the parameters */
520 assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource));
521 assert_param(IS_RCC_PLL_MUL(RCC_OscInitStruct->PLL.PLLMUL));
523 /* Disable the main PLL. */
524 __HAL_RCC_PLL_DISABLE();
527 tickstart = HAL_GetTick();
529 /* Wait till PLL is disabled */
530 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
532 if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
538 /* Configure the HSE prediv1 factor and source --------------------------------*/
539 /* It can be written only when the PLL is disabled. Not used in PLL source is different than HSE */
540 if(RCC_OscInitStruct->PLL.PLLSource == RCC_PLLSOURCE_HSE)
542 /* Check the parameter */
543 assert_param(IS_RCC_PREDIV1_SOURCE(RCC_OscInitStruct->Prediv1Source));
544 assert_param(IS_RCC_HSE_PREDIV(RCC_OscInitStruct->HSEPredivValue));
546 /* Set PREDIV1 source */
547 SET_BIT(RCC->CFGR2, RCC_OscInitStruct->Prediv1Source);
549 /* Set PREDIV1 Value */
550 __HAL_RCC_HSE_PREDIV_CONFIG(RCC_OscInitStruct->HSEPredivValue);
553 /* Configure the main PLL clock source and multiplication factors. */
554 __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource,
555 RCC_OscInitStruct->PLL.PLLMUL);
556 /* Enable the main PLL. */
557 __HAL_RCC_PLL_ENABLE();
560 tickstart = HAL_GetTick();
562 /* Wait till PLL is ready */
563 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
565 if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
573 /* Disable the main PLL. */
574 __HAL_RCC_PLL_DISABLE();
577 tickstart = HAL_GetTick();
579 /* Wait till PLL is disabled */
580 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
582 if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
601 #endif /* STM32F105xC STM32F107xC */
603 #if defined(STM32F101x6) || defined(STM32F101xB) || defined(STM32F101xE) || defined(STM32F101xG) || \
604 defined(STM32F102x6) || defined(STM32F102xB) || defined(STM32F103x6) || defined(STM32F103xB) || \
605 defined(STM32F103xE) || defined(STM32F103xG) || defined(STM32F105xC) || defined(STM32F107xC)
606 /** @addtogroup RCC_Exported_Functions_Group1
611 * @brief Initializes the CPU, AHB and APB busses clocks according to the specified
612 * parameters in the RCC_ClkInitStruct.
613 * @param RCC_ClkInitStruct: pointer to an RCC_OscInitTypeDef structure that
614 * contains the configuration information for the RCC peripheral.
615 * @param FLatency: FLASH Latency
616 * This parameter can be one of the following values:
617 * @arg FLASH_LATENCY_0: FLASH 0 Latency cycle
619 * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency
620 * and updated by HAL_RCC_GetHCLKFreq() function called within this function
622 * @note The HSI is used (enabled by hardware) as system clock source after
623 * startup from Reset, wake-up from STOP and STANDBY mode, or in case
624 * of failure of the HSE used directly or indirectly as system clock
625 * (if the Clock Security System CSS is enabled).
627 * @note A switch from one clock source to another occurs only if the target
628 * clock source is ready (clock stable after startup delay or PLL locked).
629 * If a clock source which is not yet ready is selected, the switch will
630 * occur when the clock source will be ready.
631 * You can use HAL_RCC_GetClockConfig() function to know which clock is
632 * currently used as system clock source.
635 HAL_StatusTypeDef HAL_RCC_ClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t FLatency)
637 uint32_t tickstart = 0;
639 /* Check the parameters */
640 assert_param(RCC_ClkInitStruct != NULL);
641 assert_param(IS_RCC_CLOCKTYPE(RCC_ClkInitStruct->ClockType));
642 assert_param(IS_FLASH_LATENCY(FLatency));
644 /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
645 must be correctly programmed according to the frequency of the CPU clock
646 (HCLK) of the device. */
648 /* Increasing the CPU frequency */
649 if(FLatency > (FLASH->ACR & FLASH_ACR_LATENCY))
651 /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
652 __HAL_FLASH_SET_LATENCY(FLatency);
654 /* Check that the new number of wait states is taken into account to access the Flash
655 memory by reading the FLASH_ACR register */
656 if((FLASH->ACR & FLASH_ACR_LATENCY) != FLatency)
660 /*-------------------------- HCLK Configuration --------------------------*/
661 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
663 assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
664 MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
667 /*------------------------- SYSCLK Configuration ---------------------------*/
668 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_SYSCLK) == RCC_CLOCKTYPE_SYSCLK)
670 assert_param(IS_RCC_SYSCLKSOURCE(RCC_ClkInitStruct->SYSCLKSource));
672 /* HSE is selected as System Clock Source */
673 if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
675 /* Check the HSE ready flag */
676 if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)
681 /* PLL is selected as System Clock Source */
682 else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
684 /* Check the PLL ready flag */
685 if(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
690 /* HSI is selected as System Clock Source */
693 /* Check the HSI ready flag */
694 if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)
700 MODIFY_REG(RCC->CFGR, RCC_CFGR_SW, RCC_ClkInitStruct->SYSCLKSource);
703 tickstart = HAL_GetTick();
705 if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
707 while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_HSE)
709 if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
715 else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
717 while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_PLLCLK)
719 if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
727 while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_HSI)
729 if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
737 /* Decreasing the CPU frequency */
740 /*-------------------------- HCLK Configuration --------------------------*/
741 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
743 assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
744 MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
747 /*------------------------- SYSCLK Configuration -------------------------*/
748 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_SYSCLK) == RCC_CLOCKTYPE_SYSCLK)
750 assert_param(IS_RCC_SYSCLKSOURCE(RCC_ClkInitStruct->SYSCLKSource));
752 /* HSE is selected as System Clock Source */
753 if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
755 /* Check the HSE ready flag */
756 if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)
761 /* PLL is selected as System Clock Source */
762 else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
764 /* Check the PLL ready flag */
765 if(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
770 /* HSI is selected as System Clock Source */
773 /* Check the HSI ready flag */
774 if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)
780 MODIFY_REG(RCC->CFGR, RCC_CFGR_SW, RCC_ClkInitStruct->SYSCLKSource);
783 tickstart = HAL_GetTick();
785 if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
787 while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_HSE)
789 if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
795 else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
797 while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_PLLCLK)
799 if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
807 while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_HSI)
809 if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
817 /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
818 __HAL_FLASH_SET_LATENCY(FLatency);
820 /* Check that the new number of wait states is taken into account to access the Flash
821 memory by reading the FLASH_ACR register */
822 if((FLASH->ACR & FLASH_ACR_LATENCY) != FLatency)
828 /*-------------------------- PCLK1 Configuration ---------------------------*/
829 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
831 assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB1CLKDivider));
832 MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE1, RCC_ClkInitStruct->APB1CLKDivider);
835 /*-------------------------- PCLK2 Configuration ---------------------------*/
836 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2)
838 assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB2CLKDivider));
839 MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE2, ((RCC_ClkInitStruct->APB2CLKDivider) << 3));
842 /* Configure the source of time base considering new system clocks settings*/
843 HAL_InitTick (TICK_INT_PRIORITY);
851 #endif /* STM32F101x6 || STM32F101xB || STM32F101xE || (...) || STM32F105xC || STM32F107xC */
853 #if defined(STM32F105xC) || defined(STM32F107xC)
854 /** @addtogroup RCC_Exported_Functions_Group2
859 * @brief Returns the SYSCLK frequency
861 * @note The system frequency computed by this function is not the real
862 * frequency in the chip. It is calculated based on the predefined
863 * constant and the selected clock source:
864 * @note If SYSCLK source is HSI, function returns values based on HSI_VALUE(*)
865 * @note If SYSCLK source is HSE, function returns values based on HSE_VALUE
866 * divided by PREDIV factor(**)
867 * @note If SYSCLK source is PLL, function returns values based on HSE_VALUE
868 * divided by PREDIV factor(**) or HSI_VALUE(*) multiplied by the PLL factor.
869 * @note (*) HSI_VALUE is a constant defined in stm32f1xx_hal_conf.h file (default value
871 * @note (**) HSE_VALUE is a constant defined in stm32f1xx_hal_conf.h file (default value
872 * 8 MHz), user has to ensure that HSE_VALUE is same as the real
873 * frequency of the crystal used. Otherwise, this function may
876 * @note The result of this function could be not correct when using fractional
877 * value for HSE crystal.
879 * @note This function can be used by the user application to compute the
880 * baudrate for the communication peripherals or configure other parameters.
882 * @note Each time SYSCLK changes, this function must be called to update the
883 * right SYSCLK value. Otherwise, any configuration based on this function will be incorrect.
886 * @retval SYSCLK frequency
888 uint32_t HAL_RCC_GetSysClockFreq(void)
890 const uint8_t aPLLMULFactorTable[12] = {0, 0, 4, 5, 6, 7, 8, 9, 0, 0, 0, 13};
891 const uint8_t aPredivFactorTable[16] = { 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11, 12, 13, 14, 15, 16};
893 uint32_t tmp_reg = 0, prediv1 = 0, pllclk = 0, pllmul = 0;
894 uint32_t sysclockfreq = 0;
895 uint32_t prediv2 = 0, pll2mul = 0;
899 /* Get SYSCLK source -------------------------------------------------------*/
900 switch (tmp_reg & RCC_CFGR_SWS)
902 case RCC_CFGR_SWS_HSE: /* HSE used as system clock */
904 sysclockfreq = HSE_VALUE;
907 case RCC_CFGR_SWS_PLL: /* PLL used as system clock */
909 pllmul = aPLLMULFactorTable[(uint32_t)(tmp_reg & RCC_CFGR_PLLMULL) >> POSITION_VAL(RCC_CFGR_PLLMULL)];
911 if ((tmp_reg & RCC_CFGR_PLLSRC) != RCC_PLLSOURCE_HSI_DIV2)
913 prediv1 = aPredivFactorTable[(uint32_t)(RCC->CFGR2 & RCC_CFGR2_PREDIV1) >> POSITION_VAL(RCC_CFGR2_PREDIV1)];
914 if(HAL_IS_BIT_SET(RCC->CFGR2, RCC_CFGR2_PREDIV1SRC))
916 /* PLL2 selected as Prediv1 source */
917 /* PLLCLK = PLL2CLK / PREDIV1 * PLLMUL with PLL2CLK = HSE/PREDIV2 * PLL2MUL */
918 prediv2 = ((RCC->CFGR2 & RCC_CFGR2_PREDIV2) >> POSITION_VAL(RCC_CFGR2_PREDIV2)) + 1;
919 pll2mul = ((RCC->CFGR2 & RCC_CFGR2_PLL2MUL) >> POSITION_VAL(RCC_CFGR2_PLL2MUL)) + 2;
920 pllclk = (uint32_t)((((HSE_VALUE / prediv2) * pll2mul) / prediv1) * pllmul);
924 /* HSE used as PLL clock source : PLLCLK = HSE/PREDIV1 * PLLMUL */
925 pllclk = (uint32_t)((HSE_VALUE / prediv1) * pllmul);
928 /* If PLLMUL was set to 13 means that it was to cover the case PLLMUL 6.5 (avoid using float) */
929 /* In this case need to divide pllclk by 2 */
930 if (pllmul == aPLLMULFactorTable[(uint32_t)(RCC_CFGR_PLLMULL6_5) >> POSITION_VAL(RCC_CFGR_PLLMULL)])
937 /* HSI used as PLL clock source : PLLCLK = HSI/2 * PLLMUL */
938 pllclk = (uint32_t)((HSI_VALUE >> 1) * pllmul);
940 sysclockfreq = pllclk;
943 case RCC_CFGR_SWS_HSI: /* HSI used as system clock source */
944 default: /* HSI used as system clock */
946 sysclockfreq = HSI_VALUE;
955 * @brief Configures the RCC_OscInitStruct according to the internal
956 * RCC configuration registers.
957 * @param RCC_OscInitStruct: pointer to an RCC_OscInitTypeDef structure that
958 * will be configured.
961 void HAL_RCC_GetOscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
963 /* Check the parameters */
964 assert_param(RCC_OscInitStruct != NULL);
966 /* Set all possible values for the Oscillator type parameter ---------------*/
967 RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI \
968 | RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_LSI;
970 /* Get the Prediv1 source --------------------------------------------------*/
971 RCC_OscInitStruct->Prediv1Source = READ_BIT(RCC->CFGR2,RCC_CFGR2_PREDIV1SRC);
973 /* Get the HSE configuration -----------------------------------------------*/
974 if((RCC->CR &RCC_CR_HSEBYP) == RCC_CR_HSEBYP)
976 RCC_OscInitStruct->HSEState = RCC_HSE_BYPASS;
978 else if((RCC->CR &RCC_CR_HSEON) == RCC_CR_HSEON)
980 RCC_OscInitStruct->HSEState = RCC_HSE_ON;
984 RCC_OscInitStruct->HSEState = RCC_HSE_OFF;
987 RCC_OscInitStruct->HSEPredivValue = __HAL_RCC_HSE_GET_PREDIV();
989 /* Get the HSI configuration -----------------------------------------------*/
990 if((RCC->CR &RCC_CR_HSION) == RCC_CR_HSION)
992 RCC_OscInitStruct->HSIState = RCC_HSI_ON;
996 RCC_OscInitStruct->HSIState = RCC_HSI_OFF;
999 RCC_OscInitStruct->HSICalibrationValue = (uint32_t)((RCC->CR & RCC_CR_HSITRIM) >> POSITION_VAL(RCC_CR_HSITRIM));
1001 /* Get the LSE configuration -----------------------------------------------*/
1002 if((RCC->BDCR &RCC_BDCR_LSEBYP) == RCC_BDCR_LSEBYP)
1004 RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS;
1006 else if((RCC->BDCR &RCC_BDCR_LSEON) == RCC_BDCR_LSEON)
1008 RCC_OscInitStruct->LSEState = RCC_LSE_ON;
1012 RCC_OscInitStruct->LSEState = RCC_LSE_OFF;
1015 /* Get the LSI configuration -----------------------------------------------*/
1016 if((RCC->CSR &RCC_CSR_LSION) == RCC_CSR_LSION)
1018 RCC_OscInitStruct->LSIState = RCC_LSI_ON;
1022 RCC_OscInitStruct->LSIState = RCC_LSI_OFF;
1025 /* Get the PLL configuration -----------------------------------------------*/
1026 if((RCC->CR &RCC_CR_PLLON) == RCC_CR_PLLON)
1028 RCC_OscInitStruct->PLL.PLLState = RCC_PLL_ON;
1032 RCC_OscInitStruct->PLL.PLLState = RCC_PLL_OFF;
1034 RCC_OscInitStruct->PLL.PLLSource = (uint32_t)(RCC->CFGR & RCC_CFGR_PLLSRC);
1035 RCC_OscInitStruct->PLL.PLLMUL = (uint32_t)(RCC->CFGR & RCC_CFGR_PLLMULL);
1037 /* Get the PLL2 configuration -----------------------------------------------*/
1038 if((RCC->CR &RCC_CR_PLL2ON) == RCC_CR_PLL2ON)
1040 RCC_OscInitStruct->PLL2.PLL2State = RCC_PLL2_ON;
1044 RCC_OscInitStruct->PLL2.PLL2State = RCC_PLL2_OFF;
1046 RCC_OscInitStruct->PLL2.HSEPrediv2Value = __HAL_RCC_HSE_GET_PREDIV2();
1047 RCC_OscInitStruct->PLL2.PLL2MUL = (uint32_t)(RCC->CFGR2 & RCC_CFGR2_PLL2MUL);
1054 #endif /* STM32F105xC || STM32F107xC*/
1056 #if defined (STM32F100xB) || defined (STM32F100xE)
1057 /** @addtogroup RCC_Exported_Functions_Group2
1062 * @brief Returns the SYSCLK frequency
1064 * @note The system frequency computed by this function is not the real
1065 * frequency in the chip. It is calculated based on the predefined
1066 * constant and the selected clock source:
1067 * @note If SYSCLK source is HSI, function returns values based on HSI_VALUE(*)
1068 * @note If SYSCLK source is HSE, function returns values based on HSE_VALUE
1069 * divided by PREDIV factor(**)
1070 * @note If SYSCLK source is PLL, function returns values based on HSE_VALUE
1071 * divided by PREDIV factor(**) or HSI_VALUE(*) multiplied by the PLL factor.
1072 * @note (*) HSI_VALUE is a constant defined in stm32f1xx.h file (default value
1074 * @note (**) HSE_VALUE is a constant defined in stm32f1xx_hal_conf.h file (default value
1075 * 8 MHz), user has to ensure that HSE_VALUE is same as the real
1076 * frequency of the crystal used. Otherwise, this function may
1077 * have wrong result.
1079 * @note The result of this function could be not correct when using fractional
1080 * value for HSE crystal.
1082 * @note This function can be used by the user application to compute the
1083 * baudrate for the communication peripherals or configure other parameters.
1085 * @note Each time SYSCLK changes, this function must be called to update the
1086 * right SYSCLK value. Otherwise, any configuration based on this function will be incorrect.
1089 * @retval SYSCLK frequency
1091 uint32_t HAL_RCC_GetSysClockFreq(void)
1093 const uint8_t aPLLMULFactorTable[16] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 16};
1094 const uint8_t aPredivFactorTable[16] = { 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11, 12, 13, 14, 15, 16};
1095 uint32_t tmp_reg = 0, prediv1 = 0, pllclk = 0, pllmul = 0;
1096 uint32_t sysclockfreq = 0;
1098 tmp_reg = RCC->CFGR;
1100 /* Get SYSCLK source -------------------------------------------------------*/
1101 switch (tmp_reg & RCC_CFGR_SWS)
1103 case RCC_CFGR_SWS_HSE: /* HSE used as system clock */
1105 sysclockfreq = HSE_VALUE;
1108 case RCC_CFGR_SWS_PLL: /* PLL used as system clock */
1110 pllmul = aPLLMULFactorTable[(uint32_t)(tmp_reg & RCC_CFGR_PLLMULL) >> POSITION_VAL(RCC_CFGR_PLLMULL)];
1111 if ((tmp_reg & RCC_CFGR_PLLSRC) != RCC_PLLSOURCE_HSI_DIV2)
1113 prediv1 = aPredivFactorTable[(uint32_t)(RCC->CFGR2 & RCC_CFGR2_PREDIV1) >> POSITION_VAL(RCC_CFGR2_PREDIV1)];
1114 /* HSE used as PLL clock source : PLLCLK = HSE/PREDIV1 * PLLMUL */
1115 pllclk = (uint32_t)((HSE_VALUE / prediv1) * pllmul);
1119 /* HSI used as PLL clock source : PLLCLK = HSI/2 * PLLMUL */
1120 pllclk = (uint32_t)((HSI_VALUE >> 1) * pllmul);
1122 sysclockfreq = pllclk;
1125 case RCC_CFGR_SWS_HSI: /* HSI used as system clock source */
1126 default: /* HSI used as system clock */
1128 sysclockfreq = HSI_VALUE;
1132 return sysclockfreq;
1138 #endif /* STM32F100xB || STM32F100xE*/
1140 #if defined(STM32F101x6) || defined(STM32F101xB) || defined(STM32F101xE) || defined(STM32F101xG) || \
1141 defined(STM32F102x6) || defined(STM32F102xB) || defined(STM32F103x6) || defined(STM32F103xB) || \
1142 defined(STM32F103xE) || defined(STM32F103xG) || defined(STM32F105xC) || defined(STM32F107xC)
1143 /** @addtogroup RCC_Exported_Functions_Group2
1148 * @brief Configures the RCC_ClkInitStruct according to the internal
1149 * RCC configuration registers.
1150 * @param RCC_ClkInitStruct: pointer to an RCC_ClkInitTypeDef structure that
1151 * will be configured.
1152 * @param pFLatency: Pointer on the Flash Latency.
1155 void HAL_RCC_GetClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t *pFLatency)
1157 /* Check the parameters */
1158 assert_param(RCC_ClkInitStruct != NULL);
1159 assert_param(pFLatency != NULL);
1161 /* Set all possible values for the Clock type parameter --------------------*/
1162 RCC_ClkInitStruct->ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
1164 /* Get the SYSCLK configuration --------------------------------------------*/
1165 RCC_ClkInitStruct->SYSCLKSource = (uint32_t)(RCC->CFGR & RCC_CFGR_SW);
1167 /* Get the HCLK configuration ----------------------------------------------*/
1168 RCC_ClkInitStruct->AHBCLKDivider = (uint32_t)(RCC->CFGR & RCC_CFGR_HPRE);
1170 /* Get the APB1 configuration ----------------------------------------------*/
1171 RCC_ClkInitStruct->APB1CLKDivider = (uint32_t)(RCC->CFGR & RCC_CFGR_PPRE1);
1173 /* Get the APB2 configuration ----------------------------------------------*/
1174 RCC_ClkInitStruct->APB2CLKDivider = (uint32_t)((RCC->CFGR & RCC_CFGR_PPRE2) >> 3);
1176 /* Get the Flash Wait State (Latency) configuration ------------------------*/
1177 *pFLatency = (uint32_t)(FLASH->ACR & FLASH_ACR_LATENCY);
1183 #endif /* STM32F101x6 || STM32F101xB || STM32F101xE || (...) || STM32F105xC || STM32F107xC */
1193 /** @addtogroup RCCEx
1197 /** @defgroup RCCEx_Exported_Functions RCCEx Exported Functions
1201 /** @defgroup RCCEx_Exported_Functions_Group1 Peripheral Control functions
1202 * @brief Extended Peripheral Control functions
1205 ===============================================================================
1206 ##### Extended Peripheral Control functions #####
1207 ===============================================================================
1209 This subsection provides a set of functions allowing to control the RCC Clocks
1212 (@) Important note: Care must be taken when HAL_RCCEx_PeriphCLKConfig() is used to
1213 select the RTC clock source; in this case the Backup domain will be reset in
1214 order to modify the RTC Clock source, as consequence RTC registers (including
1215 the backup registers) and RCC_BDCR register are set to their reset values.
1222 * @brief Initializes the RCC extended peripherals clocks according to the specified parameters in the
1223 * RCC_PeriphCLKInitTypeDef.
1224 * @param PeriphClkInit: pointer to an RCC_PeriphCLKInitTypeDef structure that
1225 * contains the configuration information for the Extended Peripherals clocks(RTC clock).
1227 * @note Care must be taken when HAL_RCCEx_PeriphCLKConfig() is used to select
1228 * the RTC clock source; in this case the Backup domain will be reset in
1229 * order to modify the RTC Clock source, as consequence RTC registers (including
1230 * the backup registers) are set to their reset values.
1232 * @note In case of STM32F105xC or STM32F107xC devices, PLLI2S will be enabled if requested on
1233 * one of 2 I2S interfaces. When PLLI2S is enabled, you need to call HAL_RCCEx_DisablePLLI2S to
1234 * manually disable it.
1236 * @retval HAL status
1238 HAL_StatusTypeDef HAL_RCCEx_PeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
1240 uint32_t tickstart = 0, tmp_reg = 0;
1241 #if defined(STM32F105xC) || defined(STM32F107xC)
1242 uint32_t pllactive = 0;
1243 #endif /* STM32F105xC || STM32F107xC */
1245 /* Check the parameters */
1246 assert_param(IS_RCC_PERIPHCLOCK(PeriphClkInit->PeriphClockSelection));
1248 /*------------------------------- RTC/LCD Configuration ------------------------*/
1249 if ((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_RTC) == RCC_PERIPHCLK_RTC))
1251 /* Enable Power Controller clock */
1252 __HAL_RCC_PWR_CLK_ENABLE();
1254 /* Enable write access to Backup domain */
1255 SET_BIT(PWR->CR, PWR_CR_DBP);
1257 /* Wait for Backup domain Write protection disable */
1258 tickstart = HAL_GetTick();
1260 while((PWR->CR & PWR_CR_DBP) == RESET)
1262 if((HAL_GetTick() - tickstart ) > RCC_DBP_TIMEOUT_VALUE)
1268 tmp_reg = (RCC->BDCR & RCC_BDCR_RTCSEL);
1269 /* Reset the Backup domain only if the RTC Clock source selection is modified */
1270 if((tmp_reg != (PeriphClkInit->RTCClockSelection & RCC_BDCR_RTCSEL)))
1272 /* Store the content of BDCR register before the reset of Backup Domain */
1273 tmp_reg = (RCC->BDCR & ~(RCC_BDCR_RTCSEL));
1274 /* RTC Clock selection can be changed only if the Backup Domain is reset */
1275 __HAL_RCC_BACKUPRESET_FORCE();
1276 __HAL_RCC_BACKUPRESET_RELEASE();
1277 /* Restore the Content of BDCR register */
1278 RCC->BDCR = tmp_reg;
1281 /* If LSE is selected as RTC clock source, wait for LSE reactivation */
1282 if ((PeriphClkInit->RTCClockSelection == RCC_RTCCLKSOURCE_LSE))
1285 tickstart = HAL_GetTick();
1287 /* Wait till LSE is ready */
1288 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
1290 if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
1297 __HAL_RCC_RTC_CONFIG(PeriphClkInit->RTCClockSelection);
1300 /*------------------------------ ADC clock Configuration ------------------*/
1301 if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_ADC) == RCC_PERIPHCLK_ADC)
1303 /* Check the parameters */
1304 assert_param(IS_RCC_ADCPLLCLK_DIV(PeriphClkInit->AdcClockSelection));
1306 /* Configure the ADC clock source */
1307 __HAL_RCC_ADC_CONFIG(PeriphClkInit->AdcClockSelection);
1310 #if defined(STM32F105xC) || defined(STM32F107xC)
1311 /*------------------------------ I2S2 Configuration ------------------------*/
1312 if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S2) == RCC_PERIPHCLK_I2S2)
1314 /* Check the parameters */
1315 assert_param(IS_RCC_I2S2CLKSOURCE(PeriphClkInit->I2s2ClockSelection));
1317 /* Configure the I2S2 clock source */
1318 __HAL_RCC_I2S2_CONFIG(PeriphClkInit->I2s2ClockSelection);
1321 /*------------------------------ I2S3 Configuration ------------------------*/
1322 if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2S3) == RCC_PERIPHCLK_I2S3)
1324 /* Check the parameters */
1325 assert_param(IS_RCC_I2S3CLKSOURCE(PeriphClkInit->I2s3ClockSelection));
1327 /* Configure the I2S3 clock source */
1328 __HAL_RCC_I2S3_CONFIG(PeriphClkInit->I2s3ClockSelection);
1331 /*------------------------------ PLL I2S Configuration ----------------------*/
1332 /* Check that PLLI2S need to be enabled */
1333 if (HAL_IS_BIT_SET(RCC->CFGR2, RCC_CFGR2_I2S2SRC) || HAL_IS_BIT_SET(RCC->CFGR2, RCC_CFGR2_I2S3SRC))
1335 /* Update flag to indicate that PLL I2S should be active */
1339 /* Check if PLL I2S need to be enabled */
1342 /* Enable PLL I2S only if not active */
1343 if (HAL_IS_BIT_CLR(RCC->CR, RCC_CR_PLL3ON))
1345 /* Check the parameters */
1346 assert_param(IS_RCC_PLLI2S_MUL(PeriphClkInit->PLLI2S.PLLI2SMUL));
1347 assert_param(IS_RCC_HSE_PREDIV2(PeriphClkInit->PLLI2S.HSEPrediv2Value));
1349 /* Prediv2 can be written only when the PLL2 is disabled. */
1350 /* Return an error only if new value is different from the programmed value */
1351 if (HAL_IS_BIT_SET(RCC->CR,RCC_CR_PLL2ON) && \
1352 (__HAL_RCC_HSE_GET_PREDIV2() != PeriphClkInit->PLLI2S.HSEPrediv2Value))
1357 /* Configure the HSE prediv2 factor --------------------------------*/
1358 __HAL_RCC_HSE_PREDIV2_CONFIG(PeriphClkInit->PLLI2S.HSEPrediv2Value);
1360 /* Configure the main PLLI2S multiplication factors. */
1361 __HAL_RCC_PLLI2S_CONFIG(PeriphClkInit->PLLI2S.PLLI2SMUL);
1363 /* Enable the main PLLI2S. */
1364 __HAL_RCC_PLLI2S_ENABLE();
1367 tickstart = HAL_GetTick();
1369 /* Wait till PLLI2S is ready */
1370 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) == RESET)
1372 if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
1380 /* Return an error only if user wants to change the PLLI2SMUL whereas PLLI2S is active */
1381 if (READ_BIT(RCC->CFGR2, RCC_CFGR2_PLL3MUL) != PeriphClkInit->PLLI2S.PLLI2SMUL)
1387 #endif /* STM32F105xC || STM32F107xC */
1389 #if defined(STM32F102x6) || defined(STM32F102xB) || defined(STM32F103x6) || defined(STM32F103xB) || defined(STM32F103xE) || \
1390 defined(STM32F103xG) || defined(STM32F105xC) || defined(STM32F107xC)
1391 /*------------------------------ USB clock Configuration ------------------*/
1392 if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_USB) == RCC_PERIPHCLK_USB)
1394 /* Check the parameters */
1395 assert_param(IS_RCC_USBPLLCLK_DIV(PeriphClkInit->UsbClockSelection));
1397 /* Configure the USB clock source */
1398 __HAL_RCC_USB_CONFIG(PeriphClkInit->UsbClockSelection);
1400 #endif /* STM32F102x6 || STM32F102xB || STM32F103x6 || STM32F103xB || STM32F103xE || STM32F103xG || STM32F105xC || STM32F107xC */
1406 * @brief Get the PeriphClkInit according to the internal
1407 * RCC configuration registers.
1408 * @param PeriphClkInit: pointer to an RCC_PeriphCLKInitTypeDef structure that
1409 * returns the configuration information for the Extended Peripherals clocks(RTC, I2S, ADC clocks).
1412 void HAL_RCCEx_GetPeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
1414 uint32_t srcclk = 0;
1416 /* Set all possible values for the extended clock type parameter------------*/
1417 PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_RTC;
1419 /* Get the RTC configuration -----------------------------------------------*/
1420 srcclk = __HAL_RCC_GET_RTC_SOURCE();
1421 /* Source clock is LSE or LSI*/
1422 PeriphClkInit->RTCClockSelection = srcclk;
1424 /* Get the ADC clock configuration -----------------------------------------*/
1425 PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_ADC;
1426 PeriphClkInit->AdcClockSelection = __HAL_RCC_GET_ADC_SOURCE();
1428 #if defined(STM32F105xC) || defined(STM32F107xC)
1429 /* Get the I2S2 clock configuration -----------------------------------------*/
1430 PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_I2S2;
1431 PeriphClkInit->I2s2ClockSelection = __HAL_RCC_GET_I2S2_SOURCE();
1433 /* Get the I2S3 clock configuration -----------------------------------------*/
1434 PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_I2S3;
1435 PeriphClkInit->I2s3ClockSelection = __HAL_RCC_GET_I2S3_SOURCE();
1437 #endif /* STM32F105xC || STM32F107xC */
1439 #if defined(STM32F103xE) || defined(STM32F103xG)
1440 /* Get the I2S2 clock configuration -----------------------------------------*/
1441 PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_I2S2;
1442 PeriphClkInit->I2s2ClockSelection = RCC_I2S2CLKSOURCE_SYSCLK;
1444 /* Get the I2S3 clock configuration -----------------------------------------*/
1445 PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_I2S3;
1446 PeriphClkInit->I2s3ClockSelection = RCC_I2S3CLKSOURCE_SYSCLK;
1448 #endif /* STM32F103xE || STM32F103xG */
1450 #if defined(STM32F102x6) || defined(STM32F102xB) || defined(STM32F103x6) || defined(STM32F103xB) || defined(STM32F103xE) || \
1451 defined(STM32F103xG) || defined(STM32F105xC) || defined(STM32F107xC)
1452 /* Get the USB clock configuration -----------------------------------------*/
1453 PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_USB;
1454 PeriphClkInit->UsbClockSelection = __HAL_RCC_GET_USB_SOURCE();
1455 #endif /* STM32F102x6 || STM32F102xB || STM32F103x6 || STM32F103xB || STM32F103xE || STM32F103xG || STM32F105xC || STM32F107xC */
1459 * @brief Returns the peripheral clock frequency
1460 * @note Returns 0 if peripheral clock is unknown
1461 * @param PeriphClk: Peripheral clock identifier
1462 * This parameter can be one of the following values:
1463 * @arg RCC_PERIPHCLK_RTC: RTC peripheral clock
1464 * @arg RCC_PERIPHCLK_ADC: ADC peripheral clock
1465 * @arg RCC_PERIPHCLK_I2S2: I2S2 peripheral clock (STM32F103xE, STM32F103xG, STM32F105xC & STM32F107xC)
1466 * @arg RCC_PERIPHCLK_I2S3: I2S3 peripheral clock (STM32F103xE, STM32F103xG, STM32F105xC & STM32F107xC)
1467 * @arg RCC_PERIPHCLK_USB: USB peripheral clock (STM32F102xx, STM32F103xx, STM32F105xC & STM32F107xC)
1468 * @retval Frequency in Hz (0: means that no available frequency for the peripheral)
1470 uint32_t HAL_RCCEx_GetPeriphCLKFreq(uint32_t PeriphClk)
1472 #if defined(STM32F102x6) || defined(STM32F102xB) || defined(STM32F103x6) || defined(STM32F103xB) || defined(STM32F103xE) || \
1473 defined(STM32F103xG) || defined(STM32F105xC) || defined(STM32F107xC)
1474 #if defined(STM32F105xC) || defined(STM32F107xC)
1475 const uint8_t aPLLMULFactorTable[12] = {0, 0, 4, 5, 6, 7, 8, 9, 0, 0, 0, 13};
1476 const uint8_t aPredivFactorTable[16] = { 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11, 12, 13, 14, 15, 16};
1478 const uint8_t aPLLMULFactorTable[16] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 16};
1479 const uint8_t aPredivFactorTable[2] = { 1, 2};
1482 uint32_t tmp_reg = 0, frequency = 0;
1483 #if defined(STM32F102x6) || defined(STM32F102xB) || defined(STM32F103x6) || defined(STM32F103xB) || defined(STM32F103xE) || \
1484 defined(STM32F103xG) || defined(STM32F105xC) || defined(STM32F107xC)
1485 uint32_t prediv1 = 0, pllclk = 0, pllmul = 0;
1486 #endif /* STM32F102x6 || STM32F102xB || STM32F103x6 || STM32F103xB || STM32F103xE || STM32F103xG || STM32F105xC || STM32F107xC */
1487 #if defined(STM32F105xC) || defined(STM32F107xC)
1488 uint32_t pll2mul = 0, pll3mul = 0, prediv2 = 0;
1489 #endif /* STM32F105xC || STM32F107xC */
1491 /* Check the parameters */
1492 assert_param(IS_RCC_PERIPHCLOCK(PeriphClk));
1496 #if defined(STM32F102x6) || defined(STM32F102xB) || defined(STM32F103x6) || defined(STM32F103xB) || defined(STM32F103xE) || \
1497 defined(STM32F103xG) || defined(STM32F105xC) || defined(STM32F107xC)
1498 case RCC_PERIPHCLK_USB:
1500 /* Get RCC configuration ------------------------------------------------------*/
1501 tmp_reg = RCC->CFGR;
1503 /* Check if PLL is enabled */
1504 if (HAL_IS_BIT_SET(RCC->CR,RCC_CR_PLLON))
1506 pllmul = aPLLMULFactorTable[(uint32_t)(tmp_reg & RCC_CFGR_PLLMULL) >> POSITION_VAL(RCC_CFGR_PLLMULL)];
1507 if ((tmp_reg & RCC_CFGR_PLLSRC) != RCC_PLLSOURCE_HSI_DIV2)
1509 #if defined(STM32F105xC) || defined(STM32F107xC) || defined (STM32F100xB) || defined (STM32F100xE)
1510 prediv1 = aPredivFactorTable[(uint32_t)(RCC->CFGR2 & RCC_CFGR2_PREDIV1) >> POSITION_VAL(RCC_CFGR2_PREDIV1)];
1512 prediv1 = aPredivFactorTable[(uint32_t)(RCC->CFGR & RCC_CFGR_PLLXTPRE) >> POSITION_VAL(RCC_CFGR_PLLXTPRE)];
1513 #endif /* STM32F105xC || STM32F107xC || STM32F100xB || STM32F100xE */
1515 #if defined(STM32F105xC) || defined(STM32F107xC)
1516 if(HAL_IS_BIT_SET(RCC->CFGR2, RCC_CFGR2_PREDIV1SRC))
1518 /* PLL2 selected as Prediv1 source */
1519 /* PLLCLK = PLL2CLK / PREDIV1 * PLLMUL with PLL2CLK = HSE/PREDIV2 * PLL2MUL */
1520 prediv2 = ((RCC->CFGR2 & RCC_CFGR2_PREDIV2) >> POSITION_VAL(RCC_CFGR2_PREDIV2)) + 1;
1521 pll2mul = ((RCC->CFGR2 & RCC_CFGR2_PLL2MUL) >> POSITION_VAL(RCC_CFGR2_PLL2MUL)) + 2;
1522 pllclk = (uint32_t)((((HSE_VALUE / prediv2) * pll2mul) / prediv1) * pllmul);
1526 /* HSE used as PLL clock source : PLLCLK = HSE/PREDIV1 * PLLMUL */
1527 pllclk = (uint32_t)((HSE_VALUE / prediv1) * pllmul);
1530 /* If PLLMUL was set to 13 means that it was to cover the case PLLMUL 6.5 (avoid using float) */
1531 /* In this case need to divide pllclk by 2 */
1532 if (pllmul == aPLLMULFactorTable[(uint32_t)(RCC_CFGR_PLLMULL6_5) >> POSITION_VAL(RCC_CFGR_PLLMULL)])
1534 pllclk = pllclk / 2;
1537 if ((tmp_reg & RCC_CFGR_PLLSRC) != RCC_PLLSOURCE_HSI_DIV2)
1539 /* HSE used as PLL clock source : PLLCLK = HSE/PREDIV1 * PLLMUL */
1540 pllclk = (uint32_t)((HSE_VALUE / prediv1) * pllmul);
1542 #endif /* STM32F105xC || STM32F107xC */
1546 /* HSI used as PLL clock source : PLLCLK = HSI/2 * PLLMUL */
1547 pllclk = (uint32_t)((HSI_VALUE >> 1) * pllmul);
1550 /* Calcul of the USB frequency*/
1551 #if defined(STM32F105xC) || defined(STM32F107xC)
1552 /* USBCLK = PLLVCO = (2 x PLLCLK) / USB prescaler */
1553 if (__HAL_RCC_GET_USB_SOURCE() == RCC_USBPLLCLK_DIV2)
1555 /* Prescaler of 2 selected for USB */
1560 /* Prescaler of 3 selected for USB */
1561 frequency = (2 * pllclk) / 3;
1564 /* USBCLK = PLLCLK / USB prescaler */
1565 if (__HAL_RCC_GET_USB_SOURCE() == RCC_USBPLLCLK_DIV1)
1567 /* No prescaler selected for USB */
1572 /* Prescaler of 1.5 selected for USB */
1573 frequency = (pllclk * 2) / 3;
1579 #endif /* STM32F102x6 || STM32F102xB || STM32F103x6 || STM32F103xB || STM32F103xE || STM32F103xG || STM32F105xC || STM32F107xC */
1580 #if defined (STM32F103xE) || defined (STM32F103xG) || defined (STM32F105xC) || defined (STM32F107xC)
1581 case RCC_PERIPHCLK_I2S2:
1583 #if defined (STM32F103xE) || defined (STM32F103xG)
1584 /* SYSCLK used as source clock for I2S2 */
1585 frequency = HAL_RCC_GetSysClockFreq();
1587 if (__HAL_RCC_GET_I2S2_SOURCE() == RCC_I2S2CLKSOURCE_SYSCLK)
1589 /* SYSCLK used as source clock for I2S2 */
1590 frequency = HAL_RCC_GetSysClockFreq();
1594 /* Check if PLLI2S is enabled */
1595 if (HAL_IS_BIT_SET(RCC->CR, RCC_CR_PLL3ON))
1597 /* PLLI2SVCO = 2 * PLLI2SCLK = 2 * (HSE/PREDIV2 * PLL3MUL) */
1598 prediv2 = ((RCC->CFGR2 & RCC_CFGR2_PREDIV2) >> POSITION_VAL(RCC_CFGR2_PREDIV2)) + 1;
1599 pll3mul = ((RCC->CFGR2 & RCC_CFGR2_PLL3MUL) >> POSITION_VAL(RCC_CFGR2_PLL3MUL)) + 2;
1600 frequency = (uint32_t)(2 * ((HSE_VALUE / prediv2) * pll3mul));
1603 #endif /* STM32F103xE || STM32F103xG */
1606 case RCC_PERIPHCLK_I2S3:
1608 #if defined (STM32F103xE) || defined (STM32F103xG)
1609 /* SYSCLK used as source clock for I2S3 */
1610 frequency = HAL_RCC_GetSysClockFreq();
1612 if (__HAL_RCC_GET_I2S3_SOURCE() == RCC_I2S3CLKSOURCE_SYSCLK)
1614 /* SYSCLK used as source clock for I2S3 */
1615 frequency = HAL_RCC_GetSysClockFreq();
1619 /* Check if PLLI2S is enabled */
1620 if (HAL_IS_BIT_SET(RCC->CR, RCC_CR_PLL3ON))
1622 /* PLLI2SVCO = 2 * PLLI2SCLK = 2 * (HSE/PREDIV2 * PLL3MUL) */
1623 prediv2 = ((RCC->CFGR2 & RCC_CFGR2_PREDIV2) >> POSITION_VAL(RCC_CFGR2_PREDIV2)) + 1;
1624 pll3mul = ((RCC->CFGR2 & RCC_CFGR2_PLL3MUL) >> POSITION_VAL(RCC_CFGR2_PLL3MUL)) + 2;
1625 frequency = (uint32_t)(2 * ((HSE_VALUE / prediv2) * pll3mul));
1628 #endif /* STM32F103xE || STM32F103xG */
1631 #endif /* STM32F103xE || STM32F103xG || STM32F105xC || STM32F107xC */
1632 case RCC_PERIPHCLK_RTC:
1634 /* Get RCC BDCR configuration ------------------------------------------------------*/
1635 tmp_reg = RCC->BDCR;
1637 /* Check if LSE is ready if RTC clock selection is LSE */
1638 if (((tmp_reg & RCC_BDCR_RTCSEL) == RCC_RTCCLKSOURCE_LSE) && (HAL_IS_BIT_SET(tmp_reg, RCC_BDCR_LSERDY)))
1640 frequency = LSE_VALUE;
1642 /* Check if LSI is ready if RTC clock selection is LSI */
1643 else if (((tmp_reg & RCC_BDCR_RTCSEL) == RCC_RTCCLKSOURCE_LSI) && (HAL_IS_BIT_SET(RCC->CSR, RCC_CSR_LSIRDY)))
1645 frequency = LSI_VALUE;
1647 else if (((tmp_reg & RCC_BDCR_RTCSEL) == RCC_RTCCLKSOURCE_HSE_DIV128) && (HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSERDY)))
1649 frequency = HSE_VALUE / 128;
1651 /* Clock not enabled for RTC*/
1658 case RCC_PERIPHCLK_ADC:
1660 frequency = HAL_RCC_GetPCLK2Freq() / (((__HAL_RCC_GET_ADC_SOURCE() >> POSITION_VAL(RCC_CFGR_ADCPRE_DIV4)) + 1) * 2);
1675 #if defined(STM32F105xC) || defined(STM32F107xC)
1676 /** @defgroup RCCEx_Exported_Functions_Group2 PLLI2S Management function
1677 * @brief PLLI2S Management functions
1680 ===============================================================================
1681 ##### Extended PLLI2S Management functions #####
1682 ===============================================================================
1684 This subsection provides a set of functions allowing to control the PLLI2S
1685 activation or deactivation
1691 * @brief Enable PLLI2S
1692 * @param PLLI2SInit: pointer to an RCC_PLLI2SInitTypeDef structure that
1693 * contains the configuration information for the PLLI2S
1694 * @note The PLLI2S configuration not modified if used by I2S2 or I2S3 Interface.
1695 * @retval HAL status
1697 HAL_StatusTypeDef HAL_RCCEx_EnablePLLI2S(RCC_PLLI2SInitTypeDef *PLLI2SInit)
1699 uint32_t tickstart = 0;
1701 /* Check that PLL I2S has not been already enabled by I2S2 or I2S3*/
1702 if (HAL_IS_BIT_CLR(RCC->CFGR2, RCC_CFGR2_I2S2SRC) && HAL_IS_BIT_CLR(RCC->CFGR2, RCC_CFGR2_I2S3SRC))
1704 /* Check the parameters */
1705 assert_param(IS_RCC_PLLI2S_MUL(PLLI2SInit->PLLI2SMUL));
1706 assert_param(IS_RCC_HSE_PREDIV2(PLLI2SInit->HSEPrediv2Value));
1708 /* Prediv2 can be written only when the PLL2 is disabled. */
1709 /* Return an error only if new value is different from the programmed value */
1710 if (HAL_IS_BIT_SET(RCC->CR,RCC_CR_PLL2ON) && \
1711 (__HAL_RCC_HSE_GET_PREDIV2() != PLLI2SInit->HSEPrediv2Value))
1716 /* Disable the main PLLI2S. */
1717 __HAL_RCC_PLLI2S_DISABLE();
1720 tickstart = HAL_GetTick();
1722 /* Wait till PLLI2S is ready */
1723 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) != RESET)
1725 if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
1731 /* Configure the HSE prediv2 factor --------------------------------*/
1732 __HAL_RCC_HSE_PREDIV2_CONFIG(PLLI2SInit->HSEPrediv2Value);
1735 /* Configure the main PLLI2S multiplication factors. */
1736 __HAL_RCC_PLLI2S_CONFIG(PLLI2SInit->PLLI2SMUL);
1738 /* Enable the main PLLI2S. */
1739 __HAL_RCC_PLLI2S_ENABLE();
1742 tickstart = HAL_GetTick();
1744 /* Wait till PLLI2S is ready */
1745 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) == RESET)
1747 if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
1755 /* PLLI2S cannot be modified as already used by I2S2 or I2S3 */
1763 * @brief Disable PLLI2S
1764 * @note PLLI2S is not disabled if used by I2S2 or I2S3 Interface.
1765 * @retval HAL status
1767 HAL_StatusTypeDef HAL_RCCEx_DisablePLLI2S(void)
1769 uint32_t tickstart = 0;
1771 /* Disable PLL I2S as not requested by I2S2 or I2S3*/
1772 if (HAL_IS_BIT_CLR(RCC->CFGR2, RCC_CFGR2_I2S2SRC) && HAL_IS_BIT_CLR(RCC->CFGR2, RCC_CFGR2_I2S3SRC))
1774 /* Disable the main PLLI2S. */
1775 __HAL_RCC_PLLI2S_DISABLE();
1778 tickstart = HAL_GetTick();
1780 /* Wait till PLLI2S is ready */
1781 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) != RESET)
1783 if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
1791 /* PLLI2S is currently used by I2S2 or I2S3. Cannot be disabled.*/
1802 /** @defgroup RCCEx_Exported_Functions_Group3 PLL2 Management function
1803 * @brief PLL2 Management functions
1806 ===============================================================================
1807 ##### Extended PLL2 Management functions #####
1808 ===============================================================================
1810 This subsection provides a set of functions allowing to control the PLL2
1811 activation or deactivation
1817 * @brief Enable PLL2
1818 * @param PLL2Init: pointer to an RCC_PLL2InitTypeDef structure that
1819 * contains the configuration information for the PLL2
1820 * @note The PLL2 configuration not modified if used indirectly as system clock.
1821 * @retval HAL status
1823 HAL_StatusTypeDef HAL_RCCEx_EnablePLL2(RCC_PLL2InitTypeDef *PLL2Init)
1825 uint32_t tickstart = 0;
1827 /* This bit can not be cleared if the PLL2 clock is used indirectly as system
1828 clock (i.e. it is used as PLL clock entry that is used as system clock). */
1829 if((__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSE) && \
1830 (__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && \
1831 ((READ_BIT(RCC->CFGR2,RCC_CFGR2_PREDIV1SRC)) == RCC_CFGR2_PREDIV1SRC_PLL2))
1837 /* Check the parameters */
1838 assert_param(IS_RCC_PLL2_MUL(PLL2Init->PLL2MUL));
1839 assert_param(IS_RCC_HSE_PREDIV2(PLL2Init->HSEPrediv2Value));
1841 /* Prediv2 can be written only when the PLLI2S is disabled. */
1842 /* Return an error only if new value is different from the programmed value */
1843 if (HAL_IS_BIT_SET(RCC->CR,RCC_CR_PLL3ON) && \
1844 (__HAL_RCC_HSE_GET_PREDIV2() != PLL2Init->HSEPrediv2Value))
1849 /* Disable the main PLL2. */
1850 __HAL_RCC_PLL2_DISABLE();
1853 tickstart = HAL_GetTick();
1855 /* Wait till PLL2 is disabled */
1856 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLL2RDY) != RESET)
1858 if((HAL_GetTick() - tickstart ) > PLL2_TIMEOUT_VALUE)
1864 /* Configure the HSE prediv2 factor --------------------------------*/
1865 __HAL_RCC_HSE_PREDIV2_CONFIG(PLL2Init->HSEPrediv2Value);
1867 /* Configure the main PLL2 multiplication factors. */
1868 __HAL_RCC_PLL2_CONFIG(PLL2Init->PLL2MUL);
1870 /* Enable the main PLL2. */
1871 __HAL_RCC_PLL2_ENABLE();
1874 tickstart = HAL_GetTick();
1876 /* Wait till PLL2 is ready */
1877 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLL2RDY) == RESET)
1879 if((HAL_GetTick() - tickstart ) > PLL2_TIMEOUT_VALUE)
1890 * @brief Disable PLL2
1891 * @note PLL2 is not disabled if used indirectly as system clock.
1892 * @retval HAL status
1894 HAL_StatusTypeDef HAL_RCCEx_DisablePLL2(void)
1896 uint32_t tickstart = 0;
1898 /* This bit can not be cleared if the PLL2 clock is used indirectly as system
1899 clock (i.e. it is used as PLL clock entry that is used as system clock). */
1900 if((__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSE) && \
1901 (__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && \
1902 ((READ_BIT(RCC->CFGR2,RCC_CFGR2_PREDIV1SRC)) == RCC_CFGR2_PREDIV1SRC_PLL2))
1908 /* Disable the main PLL2. */
1909 __HAL_RCC_PLL2_DISABLE();
1912 tickstart = HAL_GetTick();
1914 /* Wait till PLL2 is disabled */
1915 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLL2RDY) != RESET)
1917 if((HAL_GetTick() - tickstart ) > PLL2_TIMEOUT_VALUE)
1930 #endif /* STM32F105xC || STM32F107xC */
1940 #endif /* HAL_RCC_MODULE_ENABLED */
1945 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/