2 ******************************************************************************
3 * @file stm32f7xx_hal_rcc.c
4 * @author MCD Application Team
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 16MHz) with Flash 0 wait state, Flash prefetch buffer, D-Cache
20 and I-Cache are disabled, and all peripherals are off except internal
22 (+) There is no prescaler on High speed (AHB) and Low speed (APB) busses;
23 all peripherals mapped on these busses are running at HSI speed.
24 (+) The clock for all peripherals is switched off, except the SRAM and FLASH.
25 (+) All GPIOs are in input floating state, except the JTAG pins which
26 are assigned to be used for debug purpose.
29 Once the device started from reset, the user application has to:
30 (+) Configure the clock source to be used to drive the System clock
31 (if the application needs higher frequency/performance)
32 (+) Configure the System clock frequency and Flash settings
33 (+) Configure the AHB and APB busses prescalers
34 (+) Enable the clock for the peripheral(s) to be used
35 (+) Configure the clock source(s) for peripherals which clocks are not
36 derived from the System clock (I2S, RTC, ADC, USB OTG FS/SDIO/RNG)
38 ##### RCC Limitations #####
39 ==============================================================================
41 A delay between an RCC peripheral clock enable and the effective peripheral
42 enabling should be taken into account in order to manage the peripheral read/write
44 (+) This delay depends on the peripheral mapping.
45 (+) If peripheral is mapped on AHB: the delay is 2 AHB clock cycle
46 after the clock enable bit is set on the hardware register
47 (+) If peripheral is mapped on APB: the delay is 2 APB clock cycle
48 after the clock enable bit is set on the hardware register
51 Implemented Workaround:
52 (+) For AHB & APB peripherals, a dummy read to the peripheral register has been
53 inserted in each __HAL_RCC_PPP_CLK_ENABLE() macro.
56 ******************************************************************************
59 * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
61 * Redistribution and use in source and binary forms, with or without modification,
62 * are permitted provided that the following conditions are met:
63 * 1. Redistributions of source code must retain the above copyright notice,
64 * this list of conditions and the following disclaimer.
65 * 2. Redistributions in binary form must reproduce the above copyright notice,
66 * this list of conditions and the following disclaimer in the documentation
67 * and/or other materials provided with the distribution.
68 * 3. Neither the name of STMicroelectronics nor the names of its contributors
69 * may be used to endorse or promote products derived from this software
70 * without specific prior written permission.
72 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
73 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
74 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
75 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
76 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
77 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
78 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
79 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
80 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
81 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
83 ******************************************************************************
86 /* Includes ------------------------------------------------------------------*/
87 #include "stm32f7xx_hal.h"
89 /** @addtogroup STM32F7xx_HAL_Driver
94 * @brief RCC HAL module driver
98 #ifdef HAL_RCC_MODULE_ENABLED
100 /* Private typedef -----------------------------------------------------------*/
101 /* Private define ------------------------------------------------------------*/
102 /* Private macro -------------------------------------------------------------*/
103 /** @defgroup RCC_Private_Macros RCC Private Macros
107 #define MCO1_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
108 #define MCO1_GPIO_PORT GPIOA
109 #define MCO1_PIN GPIO_PIN_8
111 #define MCO2_CLK_ENABLE() __HAL_RCC_GPIOC_CLK_ENABLE()
112 #define MCO2_GPIO_PORT GPIOC
113 #define MCO2_PIN GPIO_PIN_9
118 /* Private variables ---------------------------------------------------------*/
119 /** @defgroup RCC_Private_Variables RCC Private Variables
127 /* Private function prototypes -----------------------------------------------*/
128 /* Exported 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 provides functions allowing to configure the internal/external oscillators
143 (HSE, HSI, LSE, LSI, PLL, CSS and MCO) and the System buses clocks (SYSCLK, AHB, APB1
146 [..] Internal/external clock and PLL configuration
147 (#) HSI (high-speed internal), 16 MHz factory-trimmed RC used directly or through
148 the PLL as System clock source.
150 (#) LSI (low-speed internal), 32 KHz low consumption RC used as IWDG and/or RTC
153 (#) HSE (high-speed external), 4 to 26 MHz 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 216 MHz)
160 (++) The second output is used to generate the clock for the USB OTG FS (48 MHz),
161 the random analog generator (<=48 MHz) and the SDIO (<= 48 MHz).
163 (#) CSS (Clock security system), once enable using the function HAL_RCC_EnableCSS()
164 and if a HSE clock failure occurs(HSE used directly or through PLL as System
165 clock source), the System clock is automatically switched to HSI and an interrupt
166 is generated if enabled. The interrupt is linked to the Cortex-M7 NMI
167 (Non-Maskable Interrupt) exception vector.
169 (#) MCO1 (microcontroller clock output), used to output HSI, LSE, HSE or PLL
170 clock (through a configurable prescaler) on PA8 pin.
172 (#) MCO2 (microcontroller clock output), used to output HSE, PLL, SYSCLK or PLLI2S
173 clock (through a configurable prescaler) on PC9 pin.
175 [..] System, AHB and APB busses clocks configuration
176 (#) Several clock sources can be used to drive the System clock (SYSCLK): HSI,
178 The AHB clock (HCLK) is derived from System clock through configurable
179 prescaler and used to clock the CPU, memory and peripherals mapped
180 on AHB bus (DMA, GPIO...). APB1 (PCLK1) and APB2 (PCLK2) clocks are derived
181 from AHB clock through configurable prescalers and used to clock
182 the peripherals mapped on these busses. You can use
183 "HAL_RCC_GetSysClockFreq()" function to retrieve the frequencies of these clocks.
185 -@- All the peripheral clocks are derived from the System clock (SYSCLK) except:
186 (+@) I2S: the I2S clock can be derived either from a specific PLL (PLLI2S) or
187 from an external clock mapped on the I2S_CKIN pin.
188 You have to use __HAL_RCC_PLLI2S_CONFIG() macro to configure this clock.
189 (+@) SAI: the SAI clock can be derived either from a specific PLL (PLLI2S) or (PLLSAI) or
190 from an external clock mapped on the I2S_CKIN pin.
191 You have to use __HAL_RCC_PLLI2S_CONFIG() macro to configure this clock.
192 (+@) RTC: the RTC clock can be derived either from the LSI, LSE or HSE clock
193 divided by 2 to 31. You have to use __HAL_RCC_RTC_CONFIG() and __HAL_RCC_RTC_ENABLE()
194 macros to configure this clock.
195 (+@) USB OTG FS, SDIO and RTC: USB OTG FS require a frequency equal to 48 MHz
196 to work correctly, while the SDIO require a frequency equal or lower than
197 to 48. This clock is derived of the main PLL through PLLQ divider.
198 (+@) IWDG clock which is always the LSI clock.
204 * @brief Resets the RCC clock configuration to the default reset state.
205 * @note The default reset state of the clock configuration is given below:
206 * - HSI ON and used as system clock source
207 * - HSE, PLL and PLLI2S OFF
208 * - AHB, APB1 and APB2 prescaler set to 1.
209 * - CSS, MCO1 and MCO2 OFF
210 * - All interrupts disabled
211 * @note This function doesn't modify the configuration of the
212 * - Peripheral clocks
213 * - LSI, LSE and RTC clocks
216 void HAL_RCC_DeInit(void)
219 SET_BIT(RCC
->CR
, RCC_CR_HSION
| RCC_CR_HSITRIM_4
);
221 /* Reset CFGR register */
222 CLEAR_REG(RCC
->CFGR
);
224 /* Reset HSEON, CSSON, PLLON, PLLI2S */
225 CLEAR_BIT(RCC
->CR
, RCC_CR_HSEON
| RCC_CR_CSSON
| RCC_CR_PLLON
| RCC_CR_PLLI2SON
);
227 /* Reset PLLCFGR register */
228 CLEAR_REG(RCC
->PLLCFGR
);
229 SET_BIT(RCC
->PLLCFGR
, RCC_PLLCFGR_PLLM_4
| RCC_PLLCFGR_PLLN_6
| RCC_PLLCFGR_PLLN_7
| RCC_PLLCFGR_PLLQ_2
| ((uint32_t)0x20000000U
));
231 /* Reset PLLI2SCFGR register */
232 CLEAR_REG(RCC
->PLLI2SCFGR
);
233 SET_BIT(RCC
->PLLI2SCFGR
, RCC_PLLI2SCFGR_PLLI2SN_6
| RCC_PLLI2SCFGR_PLLI2SN_7
| RCC_PLLI2SCFGR_PLLI2SR_1
);
235 /* Reset HSEBYP bit */
236 CLEAR_BIT(RCC
->CR
, RCC_CR_HSEBYP
);
238 /* Disable all interrupts */
241 /* Update the SystemCoreClock global variable */
242 SystemCoreClock
= HSI_VALUE
;
246 * @brief Initializes the RCC Oscillators according to the specified parameters in the
247 * RCC_OscInitTypeDef.
248 * @param RCC_OscInitStruct: pointer to an RCC_OscInitTypeDef structure that
249 * contains the configuration information for the RCC Oscillators.
250 * @note The PLL is not disabled when used as system clock.
251 * @note Transitions LSE Bypass to LSE On and LSE On to LSE Bypass are not
252 * supported by this function. User should request a transition to LSE Off
253 * first and then LSE On or LSE Bypass.
254 * @note Transition HSE Bypass to HSE On and HSE On to HSE Bypass are not
255 * supported by this function. User should request a transition to HSE Off
256 * first and then HSE On or HSE Bypass.
259 __weak HAL_StatusTypeDef
HAL_RCC_OscConfig(RCC_OscInitTypeDef
*RCC_OscInitStruct
)
261 uint32_t tickstart
= 0;
263 /* Check the parameters */
264 assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct
->OscillatorType
));
266 /*------------------------------- HSE Configuration ------------------------*/
267 if(((RCC_OscInitStruct
->OscillatorType
) & RCC_OSCILLATORTYPE_HSE
) == RCC_OSCILLATORTYPE_HSE
)
269 /* Check the parameters */
270 assert_param(IS_RCC_HSE(RCC_OscInitStruct
->HSEState
));
271 /* When the HSE is used as system clock or clock source for PLL, It can not be disabled */
272 if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_HSE
)
273 || ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_PLLCLK
) && ((RCC
->PLLCFGR
& RCC_PLLCFGR_PLLSRC
) == RCC_PLLCFGR_PLLSRC_HSE
)))
275 if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY
) != RESET
) && (RCC_OscInitStruct
->HSEState
== RCC_HSE_OFF
))
282 /* Set the new HSE configuration ---------------------------------------*/
283 __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct
->HSEState
);
285 /* Check the HSE State */
286 if(RCC_OscInitStruct
->HSEState
!= RCC_HSE_OFF
)
289 tickstart
= HAL_GetTick();
291 /* Wait till HSE is ready */
292 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY
) == RESET
)
294 if((HAL_GetTick() - tickstart
) > HSE_TIMEOUT_VALUE
)
303 tickstart
= HAL_GetTick();
305 /* Wait till HSE is bypassed or disabled */
306 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY
) != RESET
)
308 if((HAL_GetTick() - tickstart
) > HSE_TIMEOUT_VALUE
)
316 /*----------------------------- HSI Configuration --------------------------*/
317 if(((RCC_OscInitStruct
->OscillatorType
) & RCC_OSCILLATORTYPE_HSI
) == RCC_OSCILLATORTYPE_HSI
)
319 /* Check the parameters */
320 assert_param(IS_RCC_HSI(RCC_OscInitStruct
->HSIState
));
321 assert_param(IS_RCC_CALIBRATION_VALUE(RCC_OscInitStruct
->HSICalibrationValue
));
323 /* Check if HSI is used as system clock or as PLL source when PLL is selected as system clock */
324 if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_HSI
)
325 || ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_PLLCLK
) && ((RCC
->PLLCFGR
& RCC_PLLCFGR_PLLSRC
) == RCC_PLLCFGR_PLLSRC_HSI
)))
327 /* When HSI is used as system clock it will not disabled */
328 if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY
) != RESET
) && (RCC_OscInitStruct
->HSIState
!= RCC_HSI_ON
))
332 /* Otherwise, just the calibration is allowed */
335 /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
336 __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct
->HSICalibrationValue
);
341 /* Check the HSI State */
342 if((RCC_OscInitStruct
->HSIState
)!= RCC_HSI_OFF
)
344 /* Enable the Internal High Speed oscillator (HSI). */
345 __HAL_RCC_HSI_ENABLE();
348 tickstart
= HAL_GetTick();
350 /* Wait till HSI is ready */
351 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY
) == RESET
)
353 if((HAL_GetTick() - tickstart
) > HSI_TIMEOUT_VALUE
)
359 /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
360 __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct
->HSICalibrationValue
);
364 /* Disable the Internal High Speed oscillator (HSI). */
365 __HAL_RCC_HSI_DISABLE();
368 tickstart
= HAL_GetTick();
370 /* Wait till HSI is ready */
371 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY
) != RESET
)
373 if((HAL_GetTick() - tickstart
) > HSI_TIMEOUT_VALUE
)
381 /*------------------------------ LSI Configuration -------------------------*/
382 if(((RCC_OscInitStruct
->OscillatorType
) & RCC_OSCILLATORTYPE_LSI
) == RCC_OSCILLATORTYPE_LSI
)
384 /* Check the parameters */
385 assert_param(IS_RCC_LSI(RCC_OscInitStruct
->LSIState
));
387 /* Check the LSI State */
388 if((RCC_OscInitStruct
->LSIState
)!= RCC_LSI_OFF
)
390 /* Enable the Internal Low Speed oscillator (LSI). */
391 __HAL_RCC_LSI_ENABLE();
394 tickstart
= HAL_GetTick();
396 /* Wait till LSI is ready */
397 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY
) == RESET
)
399 if((HAL_GetTick() - tickstart
) > LSI_TIMEOUT_VALUE
)
407 /* Disable the Internal Low Speed oscillator (LSI). */
408 __HAL_RCC_LSI_DISABLE();
411 tickstart
= HAL_GetTick();
413 /* Wait till LSI is ready */
414 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY
) != RESET
)
416 if((HAL_GetTick() - tickstart
) > LSI_TIMEOUT_VALUE
)
423 /*------------------------------ LSE Configuration -------------------------*/
424 if(((RCC_OscInitStruct
->OscillatorType
) & RCC_OSCILLATORTYPE_LSE
) == RCC_OSCILLATORTYPE_LSE
)
426 /* Check the parameters */
427 assert_param(IS_RCC_LSE(RCC_OscInitStruct
->LSEState
));
429 /* Enable Power Clock*/
430 __HAL_RCC_PWR_CLK_ENABLE();
432 /* Enable write access to Backup domain */
433 PWR
->CR1
|= PWR_CR1_DBP
;
435 /* Wait for Backup domain Write protection disable */
436 tickstart
= HAL_GetTick();
438 while((PWR
->CR1
& PWR_CR1_DBP
) == RESET
)
440 if((HAL_GetTick() - tickstart
) > RCC_DBP_TIMEOUT_VALUE
)
446 /* Set the new LSE configuration -----------------------------------------*/
447 __HAL_RCC_LSE_CONFIG(RCC_OscInitStruct
->LSEState
);
448 /* Check the LSE State */
449 if((RCC_OscInitStruct
->LSEState
) != RCC_LSE_OFF
)
452 tickstart
= HAL_GetTick();
454 /* Wait till LSE is ready */
455 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY
) == RESET
)
457 if((HAL_GetTick() - tickstart
) > RCC_LSE_TIMEOUT_VALUE
)
466 tickstart
= HAL_GetTick();
468 /* Wait till LSE is ready */
469 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY
) != RESET
)
471 if((HAL_GetTick() - tickstart
) > RCC_LSE_TIMEOUT_VALUE
)
478 /*-------------------------------- PLL Configuration -----------------------*/
479 /* Check the parameters */
480 assert_param(IS_RCC_PLL(RCC_OscInitStruct
->PLL
.PLLState
));
481 if ((RCC_OscInitStruct
->PLL
.PLLState
) != RCC_PLL_NONE
)
483 /* Check if the PLL is used as system clock or not */
484 if(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_PLLCLK
)
486 if((RCC_OscInitStruct
->PLL
.PLLState
) == RCC_PLL_ON
)
488 /* Check the parameters */
489 assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct
->PLL
.PLLSource
));
490 assert_param(IS_RCC_PLLM_VALUE(RCC_OscInitStruct
->PLL
.PLLM
));
491 assert_param(IS_RCC_PLLN_VALUE(RCC_OscInitStruct
->PLL
.PLLN
));
492 assert_param(IS_RCC_PLLP_VALUE(RCC_OscInitStruct
->PLL
.PLLP
));
493 assert_param(IS_RCC_PLLQ_VALUE(RCC_OscInitStruct
->PLL
.PLLQ
));
495 /* Disable the main PLL. */
496 __HAL_RCC_PLL_DISABLE();
499 tickstart
= HAL_GetTick();
501 /* Wait till PLL is ready */
502 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY
) != RESET
)
504 if((HAL_GetTick() - tickstart
) > PLL_TIMEOUT_VALUE
)
510 /* Configure the main PLL clock source, multiplication and division factors. */
511 WRITE_REG(RCC
->PLLCFGR
, (RCC_OscInitStruct
->PLL
.PLLSource
| \
512 RCC_OscInitStruct
->PLL
.PLLM
| \
513 (RCC_OscInitStruct
->PLL
.PLLN
<< POSITION_VAL(RCC_PLLCFGR_PLLN
)) | \
514 (((RCC_OscInitStruct
->PLL
.PLLP
>> 1) -1) << POSITION_VAL(RCC_PLLCFGR_PLLP
)) | \
515 (RCC_OscInitStruct
->PLL
.PLLQ
<< POSITION_VAL(RCC_PLLCFGR_PLLQ
))));
516 /* Enable the main PLL. */
517 __HAL_RCC_PLL_ENABLE();
520 tickstart
= HAL_GetTick();
522 /* Wait till PLL is ready */
523 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY
) == RESET
)
525 if((HAL_GetTick() - tickstart
) > PLL_TIMEOUT_VALUE
)
533 /* Disable the main PLL. */
534 __HAL_RCC_PLL_DISABLE();
537 tickstart
= HAL_GetTick();
539 /* Wait till PLL is ready */
540 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY
) != RESET
)
542 if((HAL_GetTick() - tickstart
) > PLL_TIMEOUT_VALUE
)
558 * @brief Initializes the CPU, AHB and APB busses clocks according to the specified
559 * parameters in the RCC_ClkInitStruct.
560 * @param RCC_ClkInitStruct: pointer to an RCC_OscInitTypeDef structure that
561 * contains the configuration information for the RCC peripheral.
562 * @param FLatency: FLASH Latency, this parameter depend on device selected
564 * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency
565 * and updated by HAL_RCC_GetHCLKFreq() function called within this function
567 * @note The HSI is used (enabled by hardware) as system clock source after
568 * startup from Reset, wake-up from STOP and STANDBY mode, or in case
569 * of failure of the HSE used directly or indirectly as system clock
570 * (if the Clock Security System CSS is enabled).
572 * @note A switch from one clock source to another occurs only if the target
573 * clock source is ready (clock stable after startup delay or PLL locked).
574 * If a clock source which is not yet ready is selected, the switch will
575 * occur when the clock source will be ready.
576 * You can use HAL_RCC_GetClockConfig() function to know which clock is
577 * currently used as system clock source.
578 * @note Depending on the device voltage range, the software has to set correctly
579 * HPRE[3:0] bits to ensure that HCLK not exceed the maximum allowed frequency
580 * (for more details refer to section above "Initialization/de-initialization functions")
583 HAL_StatusTypeDef
HAL_RCC_ClockConfig(RCC_ClkInitTypeDef
*RCC_ClkInitStruct
, uint32_t FLatency
)
585 uint32_t tickstart
= 0;
587 /* Check the parameters */
588 assert_param(IS_RCC_CLOCKTYPE(RCC_ClkInitStruct
->ClockType
));
589 assert_param(IS_FLASH_LATENCY(FLatency
));
591 /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
592 must be correctly programmed according to the frequency of the CPU clock
593 (HCLK) and the supply voltage of the device. */
595 /* Increasing the CPU frequency */
596 if(FLatency
> (FLASH
->ACR
& FLASH_ACR_LATENCY
))
598 /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
599 __HAL_FLASH_SET_LATENCY(FLatency
);
601 /* Check that the new number of wait states is taken into account to access the Flash
602 memory by reading the FLASH_ACR register */
603 if((FLASH
->ACR
& FLASH_ACR_LATENCY
) != FLatency
)
609 /*-------------------------- HCLK Configuration --------------------------*/
610 if(((RCC_ClkInitStruct
->ClockType
) & RCC_CLOCKTYPE_HCLK
) == RCC_CLOCKTYPE_HCLK
)
612 assert_param(IS_RCC_HCLK(RCC_ClkInitStruct
->AHBCLKDivider
));
613 MODIFY_REG(RCC
->CFGR
, RCC_CFGR_HPRE
, RCC_ClkInitStruct
->AHBCLKDivider
);
616 /*------------------------- SYSCLK Configuration ---------------------------*/
617 if(((RCC_ClkInitStruct
->ClockType
) & RCC_CLOCKTYPE_SYSCLK
) == RCC_CLOCKTYPE_SYSCLK
)
619 assert_param(IS_RCC_SYSCLKSOURCE(RCC_ClkInitStruct
->SYSCLKSource
));
621 /* HSE is selected as System Clock Source */
622 if(RCC_ClkInitStruct
->SYSCLKSource
== RCC_SYSCLKSOURCE_HSE
)
624 /* Check the HSE ready flag */
625 if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY
) == RESET
)
630 /* PLL is selected as System Clock Source */
631 else if(RCC_ClkInitStruct
->SYSCLKSource
== RCC_SYSCLKSOURCE_PLLCLK
)
633 /* Check the PLL ready flag */
634 if(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY
) == RESET
)
639 /* HSI is selected as System Clock Source */
642 /* Check the HSI ready flag */
643 if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY
) == RESET
)
649 __HAL_RCC_SYSCLK_CONFIG(RCC_ClkInitStruct
->SYSCLKSource
);
651 tickstart
= HAL_GetTick();
653 if(RCC_ClkInitStruct
->SYSCLKSource
== RCC_SYSCLKSOURCE_HSE
)
655 while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_HSE
)
657 if((HAL_GetTick() - tickstart
) > CLOCKSWITCH_TIMEOUT_VALUE
)
663 else if(RCC_ClkInitStruct
->SYSCLKSource
== RCC_SYSCLKSOURCE_PLLCLK
)
665 while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_PLLCLK
)
667 if((HAL_GetTick() - tickstart
) > CLOCKSWITCH_TIMEOUT_VALUE
)
675 while(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_HSI
)
677 if((HAL_GetTick() - tickstart
) > CLOCKSWITCH_TIMEOUT_VALUE
)
685 /* Decreasing the number of wait states because of lower CPU frequency */
686 if(FLatency
< (FLASH
->ACR
& FLASH_ACR_LATENCY
))
688 /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
689 __HAL_FLASH_SET_LATENCY(FLatency
);
691 /* Check that the new number of wait states is taken into account to access the Flash
692 memory by reading the FLASH_ACR register */
693 if((FLASH
->ACR
& FLASH_ACR_LATENCY
) != FLatency
)
699 /*-------------------------- PCLK1 Configuration ---------------------------*/
700 if(((RCC_ClkInitStruct
->ClockType
) & RCC_CLOCKTYPE_PCLK1
) == RCC_CLOCKTYPE_PCLK1
)
702 assert_param(IS_RCC_PCLK(RCC_ClkInitStruct
->APB1CLKDivider
));
703 MODIFY_REG(RCC
->CFGR
, RCC_CFGR_PPRE1
, RCC_ClkInitStruct
->APB1CLKDivider
);
706 /*-------------------------- PCLK2 Configuration ---------------------------*/
707 if(((RCC_ClkInitStruct
->ClockType
) & RCC_CLOCKTYPE_PCLK2
) == RCC_CLOCKTYPE_PCLK2
)
709 assert_param(IS_RCC_PCLK(RCC_ClkInitStruct
->APB2CLKDivider
));
710 MODIFY_REG(RCC
->CFGR
, RCC_CFGR_PPRE2
, ((RCC_ClkInitStruct
->APB2CLKDivider
) << 3));
713 /* Update the SystemCoreClock global variable */
714 SystemCoreClock
= HAL_RCC_GetSysClockFreq() >> AHBPrescTable
[(RCC
->CFGR
& RCC_CFGR_HPRE
)>> POSITION_VAL(RCC_CFGR_HPRE
)];
716 /* Configure the source of time base considering new system clocks settings*/
717 HAL_InitTick (TICK_INT_PRIORITY
);
726 /** @defgroup RCC_Exported_Functions_Group2 Peripheral Control functions
727 * @brief RCC clocks control functions
730 ===============================================================================
731 ##### Peripheral Control functions #####
732 ===============================================================================
734 This subsection provides a set of functions allowing to control the RCC Clocks
742 * @brief Selects the clock source to output on MCO1 pin(PA8) or on MCO2 pin(PC9).
743 * @note PA8/PC9 should be configured in alternate function mode.
744 * @param RCC_MCOx: specifies the output direction for the clock source.
745 * This parameter can be one of the following values:
746 * @arg RCC_MCO1: Clock source to output on MCO1 pin(PA8).
747 * @arg RCC_MCO2: Clock source to output on MCO2 pin(PC9).
748 * @param RCC_MCOSource: specifies the clock source to output.
749 * This parameter can be one of the following values:
750 * @arg RCC_MCO1SOURCE_HSI: HSI clock selected as MCO1 source
751 * @arg RCC_MCO1SOURCE_LSE: LSE clock selected as MCO1 source
752 * @arg RCC_MCO1SOURCE_HSE: HSE clock selected as MCO1 source
753 * @arg RCC_MCO1SOURCE_PLLCLK: main PLL clock selected as MCO1 source
754 * @arg RCC_MCO2SOURCE_SYSCLK: System clock (SYSCLK) selected as MCO2 source
755 * @arg RCC_MCO2SOURCE_PLLI2SCLK: PLLI2S clock selected as MCO2 source
756 * @arg RCC_MCO2SOURCE_HSE: HSE clock selected as MCO2 source
757 * @arg RCC_MCO2SOURCE_PLLCLK: main PLL clock selected as MCO2 source
758 * @param RCC_MCODiv: specifies the MCOx prescaler.
759 * This parameter can be one of the following values:
760 * @arg RCC_MCODIV_1: no division applied to MCOx clock
761 * @arg RCC_MCODIV_2: division by 2 applied to MCOx clock
762 * @arg RCC_MCODIV_3: division by 3 applied to MCOx clock
763 * @arg RCC_MCODIV_4: division by 4 applied to MCOx clock
764 * @arg RCC_MCODIV_5: division by 5 applied to MCOx clock
767 void HAL_RCC_MCOConfig(uint32_t RCC_MCOx
, uint32_t RCC_MCOSource
, uint32_t RCC_MCODiv
)
769 GPIO_InitTypeDef GPIO_InitStruct
;
770 /* Check the parameters */
771 assert_param(IS_RCC_MCO(RCC_MCOx
));
772 assert_param(IS_RCC_MCODIV(RCC_MCODiv
));
774 if(RCC_MCOx
== RCC_MCO1
)
776 assert_param(IS_RCC_MCO1SOURCE(RCC_MCOSource
));
778 /* MCO1 Clock Enable */
781 /* Configure the MCO1 pin in alternate function mode */
782 GPIO_InitStruct
.Pin
= MCO1_PIN
;
783 GPIO_InitStruct
.Mode
= GPIO_MODE_AF_PP
;
784 GPIO_InitStruct
.Speed
= GPIO_SPEED_HIGH
;
785 GPIO_InitStruct
.Pull
= GPIO_NOPULL
;
786 GPIO_InitStruct
.Alternate
= GPIO_AF0_MCO
;
787 HAL_GPIO_Init(MCO1_GPIO_PORT
, &GPIO_InitStruct
);
789 /* Mask MCO1 and MCO1PRE[2:0] bits then Select MCO1 clock source and prescaler */
790 MODIFY_REG(RCC
->CFGR
, (RCC_CFGR_MCO1
| RCC_CFGR_MCO1PRE
), (RCC_MCOSource
| RCC_MCODiv
));
794 assert_param(IS_RCC_MCO2SOURCE(RCC_MCOSource
));
796 /* MCO2 Clock Enable */
799 /* Configure the MCO2 pin in alternate function mode */
800 GPIO_InitStruct
.Pin
= MCO2_PIN
;
801 GPIO_InitStruct
.Mode
= GPIO_MODE_AF_PP
;
802 GPIO_InitStruct
.Speed
= GPIO_SPEED_HIGH
;
803 GPIO_InitStruct
.Pull
= GPIO_NOPULL
;
804 GPIO_InitStruct
.Alternate
= GPIO_AF0_MCO
;
805 HAL_GPIO_Init(MCO2_GPIO_PORT
, &GPIO_InitStruct
);
807 /* Mask MCO2 and MCO2PRE[2:0] bits then Select MCO2 clock source and prescaler */
808 MODIFY_REG(RCC
->CFGR
, (RCC_CFGR_MCO2
| RCC_CFGR_MCO2PRE
), (RCC_MCOSource
| (RCC_MCODiv
<< 3)));
813 * @brief Enables the Clock Security System.
814 * @note If a failure is detected on the HSE oscillator clock, this oscillator
815 * is automatically disabled and an interrupt is generated to inform the
816 * software about the failure (Clock Security System Interrupt, CSSI),
817 * allowing the MCU to perform rescue operations. The CSSI is linked to
818 * the Cortex-M7 NMI (Non-Maskable Interrupt) exception vector.
821 void HAL_RCC_EnableCSS(void)
823 SET_BIT(RCC
->CR
, RCC_CR_CSSON
);
827 * @brief Disables the Clock Security System.
830 void HAL_RCC_DisableCSS(void)
832 CLEAR_BIT(RCC
->CR
, RCC_CR_CSSON
);
836 * @brief Returns the SYSCLK frequency
838 * @note The system frequency computed by this function is not the real
839 * frequency in the chip. It is calculated based on the predefined
840 * constant and the selected clock source:
841 * @note If SYSCLK source is HSI, function returns values based on HSI_VALUE(*)
842 * @note If SYSCLK source is HSE, function returns values based on HSE_VALUE(**)
843 * @note If SYSCLK source is PLL, function returns values based on HSE_VALUE(**)
844 * or HSI_VALUE(*) multiplied/divided by the PLL factors.
845 * @note (*) HSI_VALUE is a constant defined in stm32f7xx_hal_conf.h file (default value
846 * 16 MHz) but the real value may vary depending on the variations
847 * in voltage and temperature.
848 * @note (**) HSE_VALUE is a constant defined in stm32f7xx_hal_conf.h file (default value
849 * 25 MHz), user has to ensure that HSE_VALUE is same as the real
850 * frequency of the crystal used. Otherwise, this function may
853 * @note The result of this function could be not correct when using fractional
854 * value for HSE crystal.
856 * @note This function can be used by the user application to compute the
857 * baudrate for the communication peripherals or configure other parameters.
859 * @note Each time SYSCLK changes, this function must be called to update the
860 * right SYSCLK value. Otherwise, any configuration based on this function will be incorrect.
863 * @retval SYSCLK frequency
865 uint32_t HAL_RCC_GetSysClockFreq(void)
867 uint32_t pllm
= 0, pllvco
= 0, pllp
= 0;
868 uint32_t sysclockfreq
= 0;
870 /* Get SYSCLK source -------------------------------------------------------*/
871 switch (RCC
->CFGR
& RCC_CFGR_SWS
)
873 case RCC_SYSCLKSOURCE_STATUS_HSI
: /* HSI used as system clock source */
875 sysclockfreq
= HSI_VALUE
;
878 case RCC_SYSCLKSOURCE_STATUS_HSE
: /* HSE used as system clock source */
880 sysclockfreq
= HSE_VALUE
;
883 case RCC_SYSCLKSOURCE_STATUS_PLLCLK
: /* PLL used as system clock source */
885 /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM) * PLLN
886 SYSCLK = PLL_VCO / PLLP */
887 pllm
= RCC
->PLLCFGR
& RCC_PLLCFGR_PLLM
;
888 if (__HAL_RCC_GET_PLL_OSCSOURCE() != RCC_PLLCFGR_PLLSRC_HSI
)
890 /* HSE used as PLL clock source */
891 pllvco
= ((HSE_VALUE
/ pllm
) * ((RCC
->PLLCFGR
& RCC_PLLCFGR_PLLN
) >> POSITION_VAL(RCC_PLLCFGR_PLLN
)));
895 /* HSI used as PLL clock source */
896 pllvco
= ((HSI_VALUE
/ pllm
) * ((RCC
->PLLCFGR
& RCC_PLLCFGR_PLLN
) >> POSITION_VAL(RCC_PLLCFGR_PLLN
)));
898 pllp
= ((((RCC
->PLLCFGR
& RCC_PLLCFGR_PLLP
) >> POSITION_VAL(RCC_PLLCFGR_PLLP
)) + 1 ) *2);
900 sysclockfreq
= pllvco
/pllp
;
905 sysclockfreq
= HSI_VALUE
;
913 * @brief Returns the HCLK frequency
914 * @note Each time HCLK changes, this function must be called to update the
915 * right HCLK value. Otherwise, any configuration based on this function will be incorrect.
916 * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency.
917 * @retval HCLK frequency
919 uint32_t HAL_RCC_GetHCLKFreq(void)
921 return SystemCoreClock
;
925 * @brief Returns the PCLK1 frequency
926 * @note Each time PCLK1 changes, this function must be called to update the
927 * right PCLK1 value. Otherwise, any configuration based on this function will be incorrect.
928 * @retval PCLK1 frequency
930 uint32_t HAL_RCC_GetPCLK1Freq(void)
932 /* Get HCLK source and Compute PCLK1 frequency ---------------------------*/
933 return (HAL_RCC_GetHCLKFreq() >> APBPrescTable
[(RCC
->CFGR
& RCC_CFGR_PPRE1
)>> POSITION_VAL(RCC_CFGR_PPRE1
)]);
937 * @brief Returns the PCLK2 frequency
938 * @note Each time PCLK2 changes, this function must be called to update the
939 * right PCLK2 value. Otherwise, any configuration based on this function will be incorrect.
940 * @retval PCLK2 frequency
942 uint32_t HAL_RCC_GetPCLK2Freq(void)
944 /* Get HCLK source and Compute PCLK2 frequency ---------------------------*/
945 return (HAL_RCC_GetHCLKFreq()>> APBPrescTable
[(RCC
->CFGR
& RCC_CFGR_PPRE2
)>> POSITION_VAL(RCC_CFGR_PPRE2
)]);
949 * @brief Configures the RCC_OscInitStruct according to the internal
950 * RCC configuration registers.
951 * @param RCC_OscInitStruct: pointer to an RCC_OscInitTypeDef structure that
952 * will be configured.
955 void HAL_RCC_GetOscConfig(RCC_OscInitTypeDef
*RCC_OscInitStruct
)
957 /* Set all possible values for the Oscillator type parameter ---------------*/
958 RCC_OscInitStruct
->OscillatorType
= RCC_OSCILLATORTYPE_HSE
| RCC_OSCILLATORTYPE_HSI
| RCC_OSCILLATORTYPE_LSE
| RCC_OSCILLATORTYPE_LSI
;
960 /* Get the HSE configuration -----------------------------------------------*/
961 if((RCC
->CR
&RCC_CR_HSEBYP
) == RCC_CR_HSEBYP
)
963 RCC_OscInitStruct
->HSEState
= RCC_HSE_BYPASS
;
965 else if((RCC
->CR
&RCC_CR_HSEON
) == RCC_CR_HSEON
)
967 RCC_OscInitStruct
->HSEState
= RCC_HSE_ON
;
971 RCC_OscInitStruct
->HSEState
= RCC_HSE_OFF
;
974 /* Get the HSI configuration -----------------------------------------------*/
975 if((RCC
->CR
&RCC_CR_HSION
) == RCC_CR_HSION
)
977 RCC_OscInitStruct
->HSIState
= RCC_HSI_ON
;
981 RCC_OscInitStruct
->HSIState
= RCC_HSI_OFF
;
984 RCC_OscInitStruct
->HSICalibrationValue
= (uint32_t)((RCC
->CR
&RCC_CR_HSITRIM
) >> POSITION_VAL(RCC_CR_HSITRIM
));
986 /* Get the LSE configuration -----------------------------------------------*/
987 if((RCC
->BDCR
&RCC_BDCR_LSEBYP
) == RCC_BDCR_LSEBYP
)
989 RCC_OscInitStruct
->LSEState
= RCC_LSE_BYPASS
;
991 else if((RCC
->BDCR
&RCC_BDCR_LSEON
) == RCC_BDCR_LSEON
)
993 RCC_OscInitStruct
->LSEState
= RCC_LSE_ON
;
997 RCC_OscInitStruct
->LSEState
= RCC_LSE_OFF
;
1000 /* Get the LSI configuration -----------------------------------------------*/
1001 if((RCC
->CSR
&RCC_CSR_LSION
) == RCC_CSR_LSION
)
1003 RCC_OscInitStruct
->LSIState
= RCC_LSI_ON
;
1007 RCC_OscInitStruct
->LSIState
= RCC_LSI_OFF
;
1010 /* Get the PLL configuration -----------------------------------------------*/
1011 if((RCC
->CR
&RCC_CR_PLLON
) == RCC_CR_PLLON
)
1013 RCC_OscInitStruct
->PLL
.PLLState
= RCC_PLL_ON
;
1017 RCC_OscInitStruct
->PLL
.PLLState
= RCC_PLL_OFF
;
1019 RCC_OscInitStruct
->PLL
.PLLSource
= (uint32_t)(RCC
->PLLCFGR
& RCC_PLLCFGR_PLLSRC
);
1020 RCC_OscInitStruct
->PLL
.PLLM
= (uint32_t)(RCC
->PLLCFGR
& RCC_PLLCFGR_PLLM
);
1021 RCC_OscInitStruct
->PLL
.PLLN
= (uint32_t)((RCC
->PLLCFGR
& RCC_PLLCFGR_PLLN
) >> POSITION_VAL(RCC_PLLCFGR_PLLN
));
1022 RCC_OscInitStruct
->PLL
.PLLP
= (uint32_t)((((RCC
->PLLCFGR
& RCC_PLLCFGR_PLLP
) + RCC_PLLCFGR_PLLP_0
) << 1) >> POSITION_VAL(RCC_PLLCFGR_PLLP
));
1023 RCC_OscInitStruct
->PLL
.PLLQ
= (uint32_t)((RCC
->PLLCFGR
& RCC_PLLCFGR_PLLQ
) >> POSITION_VAL(RCC_PLLCFGR_PLLQ
));
1027 * @brief Configures the RCC_ClkInitStruct according to the internal
1028 * RCC configuration registers.
1029 * @param RCC_ClkInitStruct: pointer to an RCC_ClkInitTypeDef structure that
1030 * will be configured.
1031 * @param pFLatency: Pointer on the Flash Latency.
1034 void HAL_RCC_GetClockConfig(RCC_ClkInitTypeDef
*RCC_ClkInitStruct
, uint32_t *pFLatency
)
1036 /* Set all possible values for the Clock type parameter --------------------*/
1037 RCC_ClkInitStruct
->ClockType
= RCC_CLOCKTYPE_SYSCLK
| RCC_CLOCKTYPE_HCLK
| RCC_CLOCKTYPE_PCLK1
| RCC_CLOCKTYPE_PCLK2
;
1039 /* Get the SYSCLK configuration --------------------------------------------*/
1040 RCC_ClkInitStruct
->SYSCLKSource
= (uint32_t)(RCC
->CFGR
& RCC_CFGR_SW
);
1042 /* Get the HCLK configuration ----------------------------------------------*/
1043 RCC_ClkInitStruct
->AHBCLKDivider
= (uint32_t)(RCC
->CFGR
& RCC_CFGR_HPRE
);
1045 /* Get the APB1 configuration ----------------------------------------------*/
1046 RCC_ClkInitStruct
->APB1CLKDivider
= (uint32_t)(RCC
->CFGR
& RCC_CFGR_PPRE1
);
1048 /* Get the APB2 configuration ----------------------------------------------*/
1049 RCC_ClkInitStruct
->APB2CLKDivider
= (uint32_t)((RCC
->CFGR
& RCC_CFGR_PPRE2
) >> 3);
1051 /* Get the Flash Wait State (Latency) configuration ------------------------*/
1052 *pFLatency
= (uint32_t)(FLASH
->ACR
& FLASH_ACR_LATENCY
);
1056 * @brief This function handles the RCC CSS interrupt request.
1057 * @note This API should be called under the NMI_Handler().
1060 void HAL_RCC_NMI_IRQHandler(void)
1062 /* Check RCC CSSF flag */
1063 if(__HAL_RCC_GET_IT(RCC_IT_CSS
))
1065 /* RCC Clock Security System interrupt user callback */
1066 HAL_RCC_CSSCallback();
1068 /* Clear RCC CSS pending bit */
1069 __HAL_RCC_CLEAR_IT(RCC_IT_CSS
);
1074 * @brief RCC Clock Security System interrupt callback
1077 __weak
void HAL_RCC_CSSCallback(void)
1079 /* NOTE : This function Should not be modified, when the callback is needed,
1080 the HAL_RCC_CSSCallback could be implemented in the user file
1092 #endif /* HAL_RCC_MODULE_ENABLED */
1101 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/