2 ******************************************************************************
3 * @file stm32f1xx_hal_rcc.c
4 * @author MCD Application Team
6 * @date 15-December-2014
7 * @brief RCC HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the Reset and Clock Control (RCC) peripheral:
10 * + Initialization and de-initialization functions
11 * + Peripheral Control functions
14 ==============================================================================
15 ##### RCC specific features #####
16 ==============================================================================
18 After reset the device is running from Internal High Speed oscillator
19 (HSI 8MHz) with Flash 0 wait state, Flash prefetch buffer is enabled,
20 and all peripherals are off except internal SRAM, Flash and JTAG.
21 (+) There is no prescaler on High speed (AHB) and Low speed (APB) busses;
22 all peripherals mapped on these busses are running at HSI speed.
23 (+) The clock for all peripherals is switched off, except the SRAM and FLASH.
24 (+) All GPIOs are in input floating state, except the JTAG pins which
25 are assigned to be used for debug purpose.
28 Once the device started from reset, the user application has to:
29 (+) Configure the clock source to be used to drive the System clock
30 (if the application needs higher frequency/performance)
31 (+) Configure the System clock frequency and Flash settings
32 (+) Configure the AHB and APB busses prescalers
33 (+) Enable the clock for the peripheral(s) to be used
34 (+) Configure the clock source(s) for peripherals whose clocks are not
35 derived from the System clock (I2S, RTC, ADC, USB OTG FS)
37 ##### RCC Limitations #####
38 ==============================================================================
40 A delay between an RCC peripheral clock enable and the effective peripheral
41 enabling should be taken into account in order to manage the peripheral read/write
43 (+) This delay depends on the peripheral mapping.
44 (++) AHB & APB peripherals, 1 dummy read is necessary
48 (#) For AHB & APB peripherals, a dummy read to the peripheral register has been
49 inserted in each __HAL_RCC_PPP_CLK_ENABLE() macro.
52 ******************************************************************************
55 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
57 * Redistribution and use in source and binary forms, with or without modification,
58 * are permitted provided that the following conditions are met:
59 * 1. Redistributions of source code must retain the above copyright notice,
60 * this list of conditions and the following disclaimer.
61 * 2. Redistributions in binary form must reproduce the above copyright notice,
62 * this list of conditions and the following disclaimer in the documentation
63 * and/or other materials provided with the distribution.
64 * 3. Neither the name of STMicroelectronics nor the names of its contributors
65 * may be used to endorse or promote products derived from this software
66 * without specific prior written permission.
68 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
69 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
70 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
71 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
72 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
73 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
74 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
75 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
76 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
77 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
79 ******************************************************************************
82 /* Includes ------------------------------------------------------------------*/
83 #include "stm32f1xx_hal.h"
85 /** @addtogroup STM32F1xx_HAL_Driver
90 * @brief RCC HAL module driver
94 #ifdef HAL_RCC_MODULE_ENABLED
96 /* Private typedef -----------------------------------------------------------*/
97 /* Private define ------------------------------------------------------------*/
98 /** @defgroup RCC_Private_Constants RCC Private Constants
104 /* Private macro -------------------------------------------------------------*/
105 /** @defgroup RCC_Private_Macros RCC Private Macros
109 #define MCO1_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
110 #define MCO1_GPIO_PORT GPIOA
111 #define MCO1_PIN GPIO_PIN_8
117 /* Private variables ---------------------------------------------------------*/
118 /** @defgroup RCC_Private_Variables RCC Private Variables
121 const uint8_t aAPBAHBPrescTable[16] = {0, 0, 0, 0, 1, 2, 3, 4, 1, 2, 3, 4, 6, 7, 8, 9};
127 /* Private function prototypes -----------------------------------------------*/
128 /* Private functions ---------------------------------------------------------*/
130 /** @defgroup RCC_Exported_Functions RCC Exported Functions
134 /** @defgroup RCC_Exported_Functions_Group1 Initialization and de-initialization functions
135 * @brief Initialization and Configuration functions
138 ===============================================================================
139 ##### Initialization and de-initialization functions #####
140 ===============================================================================
142 This section provide functions allowing to configure the internal/external oscillators
143 (HSE, HSI, LSE, LSI, PLL, CSS and MCO) and the System busses clocks (SYSCLK, AHB, APB1
146 [..] Internal/external clock and PLL configuration
147 (#) HSI (high-speed internal), 8 MHz factory-trimmed RC used directly or through
148 the PLL as System clock source.
150 (#) LSI (low-speed internal), 40 KHz low consumption RC used as IWDG and/or RTC
153 (#) HSE (high-speed external), 4 to 24 MHz (STM32F100xx) or 4 to 16 MHz (STM32F101x/STM32F102x/STM32F103x) or 3 to 25 MHz (STM32F105x/STM32F107x) crystal oscillator used directly or
154 through the PLL as System clock source. Can be used also as RTC clock source.
156 (#) LSE (low-speed external), 32 KHz oscillator used as RTC clock source.
158 (#) PLL (clocked by HSI or HSE), featuring two different output clocks:
159 (++) The first output is used to generate the high speed system clock (up to 72 MHz for STM32F10xxx or up to 24 MHz for STM32F100xx)
160 (++) The second output is used to generate the clock for the USB OTG FS (48 MHz)
162 (#) CSS (Clock security system), once enable using the macro __HAL_RCC_CSS_ENABLE()
163 and if a HSE clock failure occurs(HSE used directly or through PLL as System
164 clock source), the System clockis automatically switched to HSI and an interrupt
165 is generated if enabled. The interrupt is linked to the Cortex-M3 NMI
166 (Non-Maskable Interrupt) exception vector.
168 (#) MCO1 (microcontroller clock output), used to output SYSCLK, HSI,
169 HSE or PLL clock (divided by 2) on PA8 pin + PLL2CLK, PLL3CLK/2, PLL3CLK and XTI for STM32F105x/STM32F107x
171 [..] System, AHB and APB busses clocks configuration
172 (#) Several clock sources can be used to drive the System clock (SYSCLK): HSI,
174 The AHB clock (HCLK) is derived from System clock through configurable
175 prescaler and used to clock the CPU, memory and peripherals mapped
176 on AHB bus (DMA, GPIO...). APB1 (PCLK1) and APB2 (PCLK2) clocks are derived
177 from AHB clock through configurable prescalers and used to clock
178 the peripherals mapped on these busses. You can use
179 "HAL_RCC_GetSysClockFreq()" function to retrieve the frequencies of these clocks.
181 -@- All the peripheral clocks are derived from the System clock (SYSCLK) except:
182 (+@) RTC: RTC clock can be derived either from the LSI, LSE or HSE clock
184 (+@) USB OTG FS and RTC: USB OTG FS require a frequency equal to 48 MHz
185 to work correctly. This clock is derived of the main PLL through PLL Multiplier.
186 (+@) I2S interface on STM32F105x/STM32F107x can be derived from PLL3CLK
187 (+@) IWDG clock which is always the LSI clock.
189 (#) For STM32F10xxx, the maximum frequency of the SYSCLK and HCLK/PCLK2 is 72 MHz, PCLK1 36 MHz.
190 For STM32F100xx, the maximum frequency of the SYSCLK and HCLK/PCLK1/PCLK2 is 24 MHz.
191 Depending on the SYSCLK frequency, the flash latency should be adapted accordingly:
192 +-----------------------------------------------+
193 | Latency | SYSCLK clock frequency (MHz) |
194 |---------------|-------------------------------|
195 |0WS(1CPU cycle)| 0 < SYSCLK <= 24 |
196 |---------------|-------------------------------|
197 |1WS(2CPU cycle)| 24 < SYSCLK <= 48 |
198 |---------------|-------------------------------|
199 |2WS(3CPU cycle)| 48 < SYSCLK <= 72 |
200 +-----------------------------------------------+
206 * @brief Resets the RCC clock configuration to the default reset state.
207 * @note The default reset state of the clock configuration is given below:
208 * - HSI ON and used as system clock source
210 * - AHB, APB1 and APB2 prescaler set to 1.
212 * - All interrupts disabled
213 * @note This function doesn't modify the configuration of the
214 * - Peripheral clocks
215 * - LSI, LSE and RTC clocks
218 __weak void HAL_RCC_DeInit(void)
220 /* Switch SYSCLK to HSI */
221 CLEAR_BIT(RCC->CFGR, RCC_CFGR_SW);
223 /* Reset HSEON, CSSON, & PLLON bits */
224 CLEAR_BIT(RCC->CR, RCC_CR_HSEON | RCC_CR_CSSON | RCC_CR_PLLON);
226 /* Reset HSEBYP bit */
227 CLEAR_BIT(RCC->CR, RCC_CR_HSEBYP);
229 /* Reset CFGR register */
230 CLEAR_REG(RCC->CFGR);
232 /* Set HSITRIM bits to the reset value */
233 MODIFY_REG(RCC->CR, RCC_CR_HSITRIM, ((uint32_t)0x10 << POSITION_VAL(RCC_CR_HSITRIM)));
235 /* Disable all interrupts */
240 * @brief Initializes the RCC Oscillators according to the specified parameters in the
241 * RCC_OscInitTypeDef.
242 * @param RCC_OscInitStruct: pointer to an RCC_OscInitTypeDef structure that
243 * contains the configuration information for the RCC Oscillators.
244 * @note The PLL is not disabled when used as system clock.
245 * @note The PLL is not disabled when USB OTG FS clock is enabled (specific to devices with USB FS)
248 __weak HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
250 uint32_t tickstart = 0;
252 /* Check the parameters */
253 assert_param(RCC_OscInitStruct != NULL);
254 assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType));
256 /*------------------------------- HSE Configuration ------------------------*/
257 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE)
259 /* Check the parameters */
260 assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState));
262 /* When the HSE is used as system clock or clock source for PLL in these cases it is not allowed to be disabled */
263 if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_HSE)
264 || ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && (__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSE)))
266 if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET) && (RCC_OscInitStruct->HSEState != RCC_HSE_ON) && (RCC_OscInitStruct->HSEState != RCC_HSE_BYPASS))
273 /* Reset HSEON and HSEBYP bits before configuring the HSE --------------*/
274 __HAL_RCC_HSE_CONFIG(RCC_HSE_OFF);
277 tickstart = HAL_GetTick();
279 /* Wait till HSE is disabled */
280 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET)
282 if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
288 /* Set the new HSE configuration ---------------------------------------*/
289 __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState);
291 /* Check the HSE State */
292 if(RCC_OscInitStruct->HSEState != RCC_HSE_OFF)
295 tickstart = HAL_GetTick();
297 /* Wait till HSE is ready */
298 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)
300 if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
309 tickstart = HAL_GetTick();
311 /* Wait till HSE is disabled */
312 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET)
314 if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
322 /*----------------------------- HSI Configuration --------------------------*/
323 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI)
325 /* Check the parameters */
326 assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState));
327 assert_param(IS_RCC_CALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue));
329 /* Check if HSI is used as system clock or as PLL source when PLL is selected as system clock */
330 if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_HSI)
331 || ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && (__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSI_DIV2)))
333 /* When HSI is used as system clock it will not disabled */
334 if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET) && (RCC_OscInitStruct->HSIState != RCC_HSI_ON))
338 /* Otherwise, just the calibration is allowed */
341 /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
342 __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
347 /* Check the HSI State */
348 if((RCC_OscInitStruct->HSIState)!= RCC_HSI_OFF)
350 /* Enable the Internal High Speed oscillator (HSI). */
351 __HAL_RCC_HSI_ENABLE();
354 tickstart = HAL_GetTick();
356 /* Wait till HSI is ready */
357 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)
359 if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
365 /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
366 __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
370 /* Disable the Internal High Speed oscillator (HSI). */
371 __HAL_RCC_HSI_DISABLE();
374 tickstart = HAL_GetTick();
376 /* Wait till HSI is disabled */
377 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET)
379 if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
387 /*------------------------------ LSI Configuration -------------------------*/
388 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI)
390 /* Check the parameters */
391 assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState));
393 /* Check the LSI State */
394 if((RCC_OscInitStruct->LSIState)!= RCC_LSI_OFF)
396 /* Enable the Internal Low Speed oscillator (LSI). */
397 __HAL_RCC_LSI_ENABLE();
400 tickstart = HAL_GetTick();
402 /* Wait till LSI is ready */
403 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) == RESET)
405 if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
410 /* To have a fully stabilized clock in the specified range, a software temporization of 1ms
416 /* Disable the Internal Low Speed oscillator (LSI). */
417 __HAL_RCC_LSI_DISABLE();
420 tickstart = HAL_GetTick();
422 /* Wait till LSI is disabled */
423 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) != RESET)
425 if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
432 /*------------------------------ LSE Configuration -------------------------*/
433 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE)
435 /* Check the parameters */
436 assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState));
438 /* Enable Power Clock*/
439 __HAL_RCC_PWR_CLK_ENABLE();
441 /* Enable write access to Backup domain */
442 SET_BIT(PWR->CR, PWR_CR_DBP);
444 /* Wait for Backup domain Write protection disable */
445 tickstart = HAL_GetTick();
447 while((PWR->CR & PWR_CR_DBP) == RESET)
449 if((HAL_GetTick() - tickstart ) > RCC_DBP_TIMEOUT_VALUE)
455 /* Reset LSEON and LSEBYP bits before configuring the LSE ----------------*/
456 __HAL_RCC_LSE_CONFIG(RCC_LSE_OFF);
459 tickstart = HAL_GetTick();
461 /* Wait till LSE is ready */
462 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) != RESET)
464 if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
470 /* Set the new LSE configuration -----------------------------------------*/
471 __HAL_RCC_LSE_CONFIG(RCC_OscInitStruct->LSEState);
472 /* Check the LSE State */
473 if((RCC_OscInitStruct->LSEState) == RCC_LSE_ON)
476 tickstart = HAL_GetTick();
478 /* Wait till LSE is ready */
479 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
481 if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
490 tickstart = HAL_GetTick();
492 /* Wait till LSE is disabled */
493 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) != RESET)
495 if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
503 /*-------------------------------- PLL Configuration -----------------------*/
504 /* Check the parameters */
505 assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState));
506 if ((RCC_OscInitStruct->PLL.PLLState) != RCC_PLL_NONE)
508 /* Check if the PLL is used as system clock or not */
509 if(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_PLLCLK)
511 if((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_ON)
513 /* Check the parameters */
514 assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource));
515 assert_param(IS_RCC_PLL_MUL(RCC_OscInitStruct->PLL.PLLMUL));
517 /* Disable the main PLL. */
518 __HAL_RCC_PLL_DISABLE();
521 tickstart = HAL_GetTick();
523 /* Wait till PLL is ready */
524 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
526 if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
532 /* Configure the HSE prediv1 factor --------------------------------*/
533 /* It can be written only when the PLL is disabled. Not used in PLL source is different than HSE */
534 if(RCC_OscInitStruct->PLL.PLLSource == RCC_PLLSOURCE_HSE)
536 /* Check the parameters */
537 assert_param(IS_RCC_HSE_PREDIV(RCC_OscInitStruct->HSEPredivValue));
539 __HAL_RCC_HSE_PREDIV_CONFIG(RCC_OscInitStruct->HSEPredivValue);
542 /* Configure the main PLL clock source and multiplication factors. */
543 __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource,
544 RCC_OscInitStruct->PLL.PLLMUL);
545 /* Enable the main PLL. */
546 __HAL_RCC_PLL_ENABLE();
549 tickstart = HAL_GetTick();
551 /* Wait till PLL is ready */
552 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
554 if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
562 /* Disable the main PLL. */
563 __HAL_RCC_PLL_DISABLE();
566 tickstart = HAL_GetTick();
568 /* Wait till PLL is disabled */
569 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
571 if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
588 * @brief Initializes the CPU, AHB and APB busses clocks according to the specified
589 * parameters in the RCC_ClkInitStruct.
590 * @param RCC_ClkInitStruct: pointer to an RCC_OscInitTypeDef structure that
591 * contains the configuration information for the RCC peripheral.
592 * @param FLatency: FLASH Latency
593 * This parameter can be one of the following values:
594 * @arg FLASH_LATENCY_0: FLASH 0 Latency cycle
595 * @arg FLASH_LATENCY_1: FLASH 1 Latency cycle
596 * @arg FLASH_LATENCY_2: FLASH 2 Latency cycle
598 * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency
599 * and updated by HAL_RCC_GetHCLKFreq() function called within this function
601 * @note The HSI is used (enabled by hardware) as system clock source after
602 * startup from Reset, wake-up from STOP and STANDBY mode, or in case
603 * of failure of the HSE used directly or indirectly as system clock
604 * (if the Clock Security System CSS is enabled).
606 * @note A switch from one clock source to another occurs only if the target
607 * clock source is ready (clock stable after startup delay or PLL locked).
608 * If a clock source which is not yet ready is selected, the switch will
609 * occur when the clock source will be ready.
610 * You can use HAL_RCC_GetClockConfig() function to know which clock is
611 * currently used as system clock source.
614 __weak HAL_StatusTypeDef HAL_RCC_ClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t FLatency)
616 uint32_t tickstart = 0;
618 /* Check the parameters */
619 assert_param(RCC_ClkInitStruct != NULL);
620 assert_param(IS_RCC_CLOCKTYPE(RCC_ClkInitStruct->ClockType));
621 assert_param(IS_FLASH_LATENCY(FLatency));
623 /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
624 must be correctly programmed according to the frequency of the CPU clock
625 (HCLK) of the device. */
627 /*-------------------------- HCLK Configuration --------------------------*/
628 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
630 assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
631 MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
634 /*------------------------- SYSCLK Configuration ---------------------------*/
635 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_SYSCLK) == RCC_CLOCKTYPE_SYSCLK)
637 assert_param(IS_RCC_SYSCLKSOURCE(RCC_ClkInitStruct->SYSCLKSource));
639 /* HSE is selected as System Clock Source */
640 if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
642 /* Check the HSE ready flag */
643 if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)
648 /* PLL is selected as System Clock Source */
649 else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
651 /* Check the PLL ready flag */
652 if(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
657 /* HSI is selected as System Clock Source */
660 /* Check the HSI ready flag */
661 if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)
667 MODIFY_REG(RCC->CFGR, RCC_CFGR_SW, RCC_ClkInitStruct->SYSCLKSource);
670 tickstart = HAL_GetTick();
672 if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
674 while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_HSE)
676 if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
682 else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
684 while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_PLLCLK)
686 if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
694 while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_HSI)
696 if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
704 /*-------------------------- PCLK1 Configuration ---------------------------*/
705 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
707 assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB1CLKDivider));
708 MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE1, RCC_ClkInitStruct->APB1CLKDivider);
711 /*-------------------------- PCLK2 Configuration ---------------------------*/
712 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2)
714 assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB2CLKDivider));
715 MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE2, ((RCC_ClkInitStruct->APB2CLKDivider) << 3));
718 /* Configure the source of time base considering new system clocks settings*/
719 HAL_InitTick (TICK_INT_PRIORITY);
728 /** @defgroup RCC_Exported_Functions_Group2 Peripheral Control functions
729 * @brief RCC clocks control functions
732 ===============================================================================
733 ##### Peripheral Control functions #####
734 ===============================================================================
736 This subsection provides a set of functions allowing to control the RCC Clocks
744 * @brief Selects the clock source to output on MCO pin.
745 * @note MCO pin should be configured in alternate function mode.
746 * @param RCC_MCOx: specifies the output direction for the clock source.
747 * This parameter can be one of the following values:
748 * @arg RCC_MCO: Clock source to output on MCO1 pin(PA8).
749 * @param RCC_MCOSource: specifies the clock source to output.
750 * This parameter can be one of the following values:
751 * @arg RCC_MCO1SOURCE_NOCLOCK: No clock selected
752 * @arg RCC_MCO1SOURCE_SYSCLK: System clock selected as MCO source
753 * @arg RCC_MCO1SOURCE_HSI: HSI oscillator clock selected
754 * @arg RCC_MCO1SOURCE_HSE: HSE oscillator clock selected
755 * @arg RCC_MCO1SOURCE_PLLCLK: PLL clock divided by 2 selected as MCO source
756 * @arg RCC_MCO1SOURCE_PLL2CLK: PLL2 clock selected as MCO source (only for connectivity line devices)
757 * @arg RCC_MCO1SOURCE_PLL3CLK_DIV2: PLL3 clock divided by 2 selected as MCO source (only for connectivity line devices)
758 * @arg RCC_MCO1SOURCE_EXT_HSE: XT1 external 3-25 MHz oscillator clock selected as MCO source (only for connectivity line devices)
759 * @arg RCC_MCO1SOURCE_PLL3CLK: PLL3 clock selected as MCO source (only for connectivity line devices)
760 * @param RCC_MCODiv: specifies the MCO DIV.
761 * This parameter can be one of the following values:
762 * @arg RCC_MCODIV_1: no division applied to MCO clock
765 void HAL_RCC_MCOConfig(uint32_t RCC_MCOx, uint32_t RCC_MCOSource, uint32_t RCC_MCODiv)
767 GPIO_InitTypeDef gpio;
769 /* Check the parameters */
770 assert_param(IS_RCC_MCO(RCC_MCOx));
771 assert_param(IS_RCC_MCODIV(RCC_MCODiv));
772 assert_param(IS_RCC_MCO1SOURCE(RCC_MCOSource));
774 /* MCO Clock Enable */
777 /* Configure the MCO1 pin in alternate function mode */
779 gpio.Mode = GPIO_MODE_AF_PP;
780 gpio.Speed = GPIO_SPEED_HIGH;
781 gpio.Pull = GPIO_NOPULL;
782 HAL_GPIO_Init(MCO1_GPIO_PORT, &gpio);
784 /* Mask MCO and MCOPRE[2:0] bits then Select MCO clock source and prescaler */
785 MODIFY_REG(RCC->CFGR, RCC_CFGR_MCO, RCC_MCOSource);
789 * @brief Enables the Clock Security System.
790 * @note If a failure is detected on the HSE oscillator clock, this oscillator
791 * is automatically disabled and an interrupt is generated to inform the
792 * software about the failure (Clock Security System Interrupt, CSSI),
793 * allowing the MCU to perform rescue operations. The CSSI is linked to
794 * the Cortex-M3 NMI (Non-Maskable Interrupt) exception vector.
797 void HAL_RCC_EnableCSS(void)
799 *(__IO uint32_t *) RCC_CR_CSSON_BB = (uint32_t)ENABLE;
803 * @brief Disables the Clock Security System.
806 void HAL_RCC_DisableCSS(void)
808 *(__IO uint32_t *) RCC_CR_CSSON_BB = (uint32_t)DISABLE;
812 * @brief Returns the SYSCLK frequency
814 * @note The system frequency computed by this function is not the real
815 * frequency in the chip. It is calculated based on the predefined
816 * constant and the selected clock source:
817 * @note If SYSCLK source is HSI, function returns values based on HSI_VALUE(*)
818 * @note If SYSCLK source is HSE, function returns values based on HSE_VALUE
819 * divided by PREDIV factor(**)
820 * @note If SYSCLK source is PLL, function returns values based on HSE_VALUE
821 * divided by PREDIV factor(**) or HSI_VALUE(*) multiplied by the PLL factor.
822 * @note (*) HSI_VALUE is a constant defined in stm32f1xx_hal_conf.h file (default value
824 * @note (**) HSE_VALUE is a constant defined in stm32f1xx_hal_conf.h file (default value
825 * 8 MHz), user has to ensure that HSE_VALUE is same as the real
826 * frequency of the crystal used. Otherwise, this function may
829 * @note The result of this function could be not correct when using fractional
830 * value for HSE crystal.
832 * @note This function can be used by the user application to compute the
833 * baudrate for the communication peripherals or configure other parameters.
835 * @note Each time SYSCLK changes, this function must be called to update the
836 * right SYSCLK value. Otherwise, any configuration based on this function will be incorrect.
839 * @retval SYSCLK frequency
841 __weak uint32_t HAL_RCC_GetSysClockFreq(void)
843 const uint8_t aPLLMULFactorTable[16] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 16};
844 const uint8_t aPredivFactorTable[2] = { 1, 2};
846 uint32_t tmpreg = 0, prediv1 = 0, pllclk = 0, pllmul = 0;
847 uint32_t sysclockfreq = 0;
851 /* Get SYSCLK source -------------------------------------------------------*/
852 switch (tmpreg & RCC_CFGR_SWS)
854 case RCC_CFGR_SWS_HSE: /* HSE used as system clock */
856 sysclockfreq = HSE_VALUE;
859 case RCC_CFGR_SWS_PLL: /* PLL used as system clock */
861 pllmul = aPLLMULFactorTable[(uint32_t)(tmpreg & RCC_CFGR_PLLMULL) >> POSITION_VAL(RCC_CFGR_PLLMULL)];
862 if ((tmpreg & RCC_CFGR_PLLSRC) != RCC_PLLSOURCE_HSI_DIV2)
864 prediv1 = aPredivFactorTable[(uint32_t)(RCC->CFGR & RCC_CFGR_PLLXTPRE) >> POSITION_VAL(RCC_CFGR_PLLXTPRE)];
865 /* HSE used as PLL clock source : PLLCLK = HSE/PREDIV1 * PLLMUL */
866 pllclk = (uint32_t)((HSE_VALUE / prediv1) * pllmul);
870 /* HSI used as PLL clock source : PLLCLK = HSI/2 * PLLMUL */
871 pllclk = (uint32_t)((HSI_VALUE >> 1) * pllmul);
873 sysclockfreq = pllclk;
876 case RCC_CFGR_SWS_HSI: /* HSI used as system clock source */
877 default: /* HSI used as system clock */
879 sysclockfreq = HSI_VALUE;
887 * @brief Returns the HCLK frequency
888 * @note Each time HCLK changes, this function must be called to update the
889 * right HCLK value. Otherwise, any configuration based on this function will be incorrect.
891 * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency
892 * and updated within this function
893 * @retval HCLK frequency
895 uint32_t HAL_RCC_GetHCLKFreq(void)
897 SystemCoreClock = HAL_RCC_GetSysClockFreq() >> aAPBAHBPrescTable[(RCC->CFGR & RCC_CFGR_HPRE)>> POSITION_VAL(RCC_CFGR_HPRE)];
898 return SystemCoreClock;
902 * @brief Returns the PCLK1 frequency
903 * @note Each time PCLK1 changes, this function must be called to update the
904 * right PCLK1 value. Otherwise, any configuration based on this function will be incorrect.
905 * @retval PCLK1 frequency
907 uint32_t HAL_RCC_GetPCLK1Freq(void)
909 /* Get HCLK source and Compute PCLK1 frequency ---------------------------*/
910 return (HAL_RCC_GetHCLKFreq() >> aAPBAHBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE1)>> POSITION_VAL(RCC_CFGR_PPRE1)]);
914 * @brief Returns the PCLK2 frequency
915 * @note Each time PCLK2 changes, this function must be called to update the
916 * right PCLK2 value. Otherwise, any configuration based on this function will be incorrect.
917 * @retval PCLK2 frequency
919 uint32_t HAL_RCC_GetPCLK2Freq(void)
921 /* Get HCLK source and Compute PCLK2 frequency ---------------------------*/
922 return (HAL_RCC_GetHCLKFreq()>> aAPBAHBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE2)>> POSITION_VAL(RCC_CFGR_PPRE2)]);
926 * @brief Configures the RCC_OscInitStruct according to the internal
927 * RCC configuration registers.
928 * @param RCC_OscInitStruct: pointer to an RCC_OscInitTypeDef structure that
929 * will be configured.
932 __weak void HAL_RCC_GetOscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
934 /* Check the parameters */
935 assert_param(RCC_OscInitStruct != NULL);
937 /* Set all possible values for the Oscillator type parameter ---------------*/
938 RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI \
939 | RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_LSI;
941 /* Get the HSE configuration -----------------------------------------------*/
942 if((RCC->CR &RCC_CR_HSEBYP) == RCC_CR_HSEBYP)
944 RCC_OscInitStruct->HSEState = RCC_HSE_BYPASS;
946 else if((RCC->CR &RCC_CR_HSEON) == RCC_CR_HSEON)
948 RCC_OscInitStruct->HSEState = RCC_HSE_ON;
952 RCC_OscInitStruct->HSEState = RCC_HSE_OFF;
955 RCC_OscInitStruct->HSEPredivValue = __HAL_RCC_HSE_GET_PREDIV();
957 /* Get the HSI configuration -----------------------------------------------*/
958 if((RCC->CR &RCC_CR_HSION) == RCC_CR_HSION)
960 RCC_OscInitStruct->HSIState = RCC_HSI_ON;
964 RCC_OscInitStruct->HSIState = RCC_HSI_OFF;
967 RCC_OscInitStruct->HSICalibrationValue = (uint32_t)((RCC->CR & RCC_CR_HSITRIM) >> POSITION_VAL(RCC_CR_HSITRIM));
969 /* Get the LSE configuration -----------------------------------------------*/
970 if((RCC->BDCR &RCC_BDCR_LSEBYP) == RCC_BDCR_LSEBYP)
972 RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS;
974 else if((RCC->BDCR &RCC_BDCR_LSEON) == RCC_BDCR_LSEON)
976 RCC_OscInitStruct->LSEState = RCC_LSE_ON;
980 RCC_OscInitStruct->LSEState = RCC_LSE_OFF;
983 /* Get the LSI configuration -----------------------------------------------*/
984 if((RCC->CSR &RCC_CSR_LSION) == RCC_CSR_LSION)
986 RCC_OscInitStruct->LSIState = RCC_LSI_ON;
990 RCC_OscInitStruct->LSIState = RCC_LSI_OFF;
993 /* Get the PLL configuration -----------------------------------------------*/
994 if((RCC->CR &RCC_CR_PLLON) == RCC_CR_PLLON)
996 RCC_OscInitStruct->PLL.PLLState = RCC_PLL_ON;
1000 RCC_OscInitStruct->PLL.PLLState = RCC_PLL_OFF;
1002 RCC_OscInitStruct->PLL.PLLSource = (uint32_t)(RCC->CFGR & RCC_CFGR_PLLSRC);
1003 RCC_OscInitStruct->PLL.PLLMUL = (uint32_t)(RCC->CFGR & RCC_CFGR_PLLMULL);
1007 * @brief Configures the RCC_ClkInitStruct according to the internal
1008 * RCC configuration registers.
1009 * @param RCC_ClkInitStruct: pointer to an RCC_ClkInitTypeDef structure that
1010 * will be configured.
1011 * @param pFLatency: Pointer on the Flash Latency.
1014 __weak void HAL_RCC_GetClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t *pFLatency)
1016 /* Check the parameters */
1017 assert_param(RCC_ClkInitStruct != NULL);
1018 assert_param(pFLatency != NULL);
1020 /* Set all possible values for the Clock type parameter --------------------*/
1021 RCC_ClkInitStruct->ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
1023 /* Get the SYSCLK configuration --------------------------------------------*/
1024 RCC_ClkInitStruct->SYSCLKSource = (uint32_t)(RCC->CFGR & RCC_CFGR_SW);
1026 /* Get the HCLK configuration ----------------------------------------------*/
1027 RCC_ClkInitStruct->AHBCLKDivider = (uint32_t)(RCC->CFGR & RCC_CFGR_HPRE);
1029 /* Get the APB1 configuration ----------------------------------------------*/
1030 RCC_ClkInitStruct->APB1CLKDivider = (uint32_t)(RCC->CFGR & RCC_CFGR_PPRE1);
1032 /* Get the APB2 configuration ----------------------------------------------*/
1033 RCC_ClkInitStruct->APB2CLKDivider = (uint32_t)((RCC->CFGR & RCC_CFGR_PPRE2) >> 3);
1035 /* For VALUE lines devices, only LATENCY_0 can be set*/
1036 *pFLatency = (uint32_t)FLASH_LATENCY_0;
1040 * @brief This function handles the RCC CSS interrupt request.
1041 * @note This API should be called under the NMI_Handler().
1044 void HAL_RCC_NMI_IRQHandler(void)
1046 /* Check RCC CSSF flag */
1047 if(__HAL_RCC_GET_IT(RCC_IT_CSS))
1049 /* RCC Clock Security System interrupt user callback */
1050 HAL_RCC_CSSCallback();
1052 /* Clear RCC CSS pending bit */
1053 __HAL_RCC_CLEAR_IT(RCC_IT_CSS);
1058 * @brief RCC Clock Security System interrupt callback
1061 __weak void HAL_RCC_CSSCallback(void)
1063 /* NOTE : This function Should not be modified, when the callback is needed,
1064 the HAL_RCC_CSSCallback could be implemented in the user file
1076 #endif /* HAL_RCC_MODULE_ENABLED */
1085 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/