Increment eeprom version
[betaflight.git] / lib / main / STM32F1 / Drivers / STM32F1xx_HAL_Driver / Src / stm32f1xx_hal_rcc.c
blob58bcf620c1b41254fdc0724ffb835c566390cac8
1 /**
2 ******************************************************************************
3 * @file stm32f1xx_hal_rcc.c
4 * @author MCD Application Team
5 * @version V1.1.1
6 * @date 12-May-2017
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
13 @verbatim
14 ==============================================================================
15 ##### RCC specific features #####
16 ==============================================================================
17 [..]
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) buses;
22 all peripherals mapped on these buses 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.
26 [..] Once the device started from reset, the user application has to:
27 (+) Configure the clock source to be used to drive the System clock
28 (if the application needs higher frequency/performance)
29 (+) Configure the System clock frequency and Flash settings
30 (+) Configure the AHB and APB buses prescalers
31 (+) Enable the clock for the peripheral(s) to be used
32 (+) Configure the clock source(s) for peripherals whose clocks are not
33 derived from the System clock (I2S, RTC, ADC, USB OTG FS)
35 ##### RCC Limitations #####
36 ==============================================================================
37 [..]
38 A delay between an RCC peripheral clock enable and the effective peripheral
39 enabling should be taken into account in order to manage the peripheral read/write
40 from/to registers.
41 (+) This delay depends on the peripheral mapping.
42 (++) AHB & APB peripherals, 1 dummy read is necessary
44 [..]
45 Workarounds:
46 (#) For AHB & APB peripherals, a dummy read to the peripheral register has been
47 inserted in each __HAL_RCC_PPP_CLK_ENABLE() macro.
49 @endverbatim
50 ******************************************************************************
51 * @attention
53 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
55 * Redistribution and use in source and binary forms, with or without modification,
56 * are permitted provided that the following conditions are met:
57 * 1. Redistributions of source code must retain the above copyright notice,
58 * this list of conditions and the following disclaimer.
59 * 2. Redistributions in binary form must reproduce the above copyright notice,
60 * this list of conditions and the following disclaimer in the documentation
61 * and/or other materials provided with the distribution.
62 * 3. Neither the name of STMicroelectronics nor the names of its contributors
63 * may be used to endorse or promote products derived from this software
64 * without specific prior written permission.
66 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
67 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
68 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
69 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
70 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
71 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
72 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
73 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
74 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
75 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
77 ******************************************************************************
80 /* Includes ------------------------------------------------------------------*/
81 #include "stm32f1xx_hal.h"
83 /** @addtogroup STM32F1xx_HAL_Driver
84 * @{
87 /** @defgroup RCC RCC
88 * @brief RCC HAL module driver
89 * @{
92 #ifdef HAL_RCC_MODULE_ENABLED
94 /* Private typedef -----------------------------------------------------------*/
95 /* Private define ------------------------------------------------------------*/
96 /** @defgroup RCC_Private_Constants RCC Private Constants
97 * @{
99 /**
100 * @}
102 /* Private macro -------------------------------------------------------------*/
103 /** @defgroup RCC_Private_Macros RCC Private Macros
104 * @{
107 #define MCO1_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
108 #define MCO1_GPIO_PORT GPIOA
109 #define MCO1_PIN GPIO_PIN_8
112 * @}
115 /* Private variables ---------------------------------------------------------*/
116 /** @defgroup RCC_Private_Variables RCC Private Variables
117 * @{
120 * @}
123 /* Private function prototypes -----------------------------------------------*/
124 static void RCC_Delay(uint32_t mdelay);
126 /* Exported functions --------------------------------------------------------*/
128 /** @defgroup RCC_Exported_Functions RCC Exported Functions
129 * @{
132 /** @defgroup RCC_Exported_Functions_Group1 Initialization and de-initialization functions
133 * @brief Initialization and Configuration functions
135 @verbatim
136 ===============================================================================
137 ##### Initialization and de-initialization functions #####
138 ===============================================================================
139 [..]
140 This section provides functions allowing to configure the internal/external oscillators
141 (HSE, HSI, LSE, LSI, PLL, CSS and MCO) and the System buses clocks (SYSCLK, AHB, APB1
142 and APB2).
144 [..] Internal/external clock and PLL configuration
145 (#) HSI (high-speed internal), 8 MHz factory-trimmed RC used directly or through
146 the PLL as System clock source.
147 (#) LSI (low-speed internal), ~40 KHz low consumption RC used as IWDG and/or RTC
148 clock source.
150 (#) 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
151 through the PLL as System clock source. Can be used also as RTC clock source.
153 (#) LSE (low-speed external), 32 KHz oscillator used as RTC clock source.
155 (#) PLL (clocked by HSI or HSE), featuring different output clocks:
156 (++) 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)
157 (++) The second output is used to generate the clock for the USB OTG FS (48 MHz)
159 (#) CSS (Clock security system), once enable using the macro __HAL_RCC_CSS_ENABLE()
160 and if a HSE clock failure occurs(HSE used directly or through PLL as System
161 clock source), the System clocks automatically switched to HSI and an interrupt
162 is generated if enabled. The interrupt is linked to the Cortex-M3 NMI
163 (Non-Maskable Interrupt) exception vector.
165 (#) MCO1 (microcontroller clock output), used to output SYSCLK, HSI,
166 HSE or PLL clock (divided by 2) on PA8 pin + PLL2CLK, PLL3CLK/2, PLL3CLK and XTI for STM32F105x/STM32F107x
168 [..] System, AHB and APB buses clocks configuration
169 (#) Several clock sources can be used to drive the System clock (SYSCLK): HSI,
170 HSE and PLL.
171 The AHB clock (HCLK) is derived from System clock through configurable
172 prescaler and used to clock the CPU, memory and peripherals mapped
173 on AHB bus (DMA, GPIO...). APB1 (PCLK1) and APB2 (PCLK2) clocks are derived
174 from AHB clock through configurable prescalers and used to clock
175 the peripherals mapped on these buses. You can use
176 "@ref HAL_RCC_GetSysClockFreq()" function to retrieve the frequencies of these clocks.
178 -@- All the peripheral clocks are derived from the System clock (SYSCLK) except:
179 (+@) RTC: RTC clock can be derived either from the LSI, LSE or HSE clock
180 divided by 128.
181 (+@) USB OTG FS and RTC: USB OTG FS require a frequency equal to 48 MHz
182 to work correctly. This clock is derived of the main PLL through PLL Multiplier.
183 (+@) I2S interface on STM32F105x/STM32F107x can be derived from PLL3CLK
184 (+@) IWDG clock which is always the LSI clock.
186 (#) For STM32F10xxx, the maximum frequency of the SYSCLK and HCLK/PCLK2 is 72 MHz, PCLK1 36 MHz.
187 For STM32F100xx, the maximum frequency of the SYSCLK and HCLK/PCLK1/PCLK2 is 24 MHz.
188 Depending on the SYSCLK frequency, the flash latency should be adapted accordingly.
189 @endverbatim
190 * @{
194 Additional consideration on the SYSCLK based on Latency settings:
195 +-----------------------------------------------+
196 | Latency | SYSCLK clock frequency (MHz) |
197 |---------------|-------------------------------|
198 |0WS(1CPU cycle)| 0 < SYSCLK <= 24 |
199 |---------------|-------------------------------|
200 |1WS(2CPU cycle)| 24 < SYSCLK <= 48 |
201 |---------------|-------------------------------|
202 |2WS(3CPU cycle)| 48 < SYSCLK <= 72 |
203 +-----------------------------------------------+
207 * @brief Resets the RCC clock configuration to the default reset state.
208 * @note The default reset state of the clock configuration is given below:
209 * - HSI ON and used as system clock source
210 * - HSE and PLL OFF
211 * - AHB, APB1 and APB2 prescaler set to 1.
212 * - CSS and MCO1 OFF
213 * - All interrupts disabled
214 * @note This function does not modify the configuration of the
215 * - Peripheral clocks
216 * - LSI, LSE and RTC clocks
217 * @retval None
219 void HAL_RCC_DeInit(void)
221 /* Switch SYSCLK to HSI */
222 CLEAR_BIT(RCC->CFGR, RCC_CFGR_SW);
224 /* Reset HSEON, CSSON, & PLLON bits */
225 CLEAR_BIT(RCC->CR, RCC_CR_HSEON | RCC_CR_CSSON | RCC_CR_PLLON);
227 /* Reset HSEBYP bit */
228 CLEAR_BIT(RCC->CR, RCC_CR_HSEBYP);
230 /* Reset CFGR register */
231 CLEAR_REG(RCC->CFGR);
233 /* Set HSITRIM bits to the reset value */
234 MODIFY_REG(RCC->CR, RCC_CR_HSITRIM, (0x10U << RCC_CR_HSITRIM_Pos));
236 #if defined(RCC_CFGR2_SUPPORT)
237 /* Reset CFGR2 register */
238 CLEAR_REG(RCC->CFGR2);
240 #endif /* RCC_CFGR2_SUPPORT */
241 /* Disable all interrupts */
242 CLEAR_REG(RCC->CIR);
244 /* Update the SystemCoreClock global variable */
245 SystemCoreClock = HSI_VALUE;
249 * @brief Initializes the RCC Oscillators according to the specified parameters in the
250 * RCC_OscInitTypeDef.
251 * @param RCC_OscInitStruct pointer to an RCC_OscInitTypeDef structure that
252 * contains the configuration information for the RCC Oscillators.
253 * @note The PLL is not disabled when used as system clock.
254 * @note The PLL is not disabled when USB OTG FS clock is enabled (specific to devices with USB FS)
255 * @note Transitions LSE Bypass to LSE On and LSE On to LSE Bypass are not
256 * supported by this macro. User should request a transition to LSE Off
257 * first and then LSE On or LSE Bypass.
258 * @note Transition HSE Bypass to HSE On and HSE On to HSE Bypass are not
259 * supported by this macro. User should request a transition to HSE Off
260 * first and then HSE On or HSE Bypass.
261 * @retval HAL status
263 HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
265 uint32_t tickstart = 0U;
267 /* Check the parameters */
268 assert_param(RCC_OscInitStruct != NULL);
269 assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType));
271 /*------------------------------- HSE Configuration ------------------------*/
272 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE)
274 /* Check the parameters */
275 assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState));
277 /* When the HSE is used as system clock or clock source for PLL in these cases it is not allowed to be disabled */
278 if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_HSE)
279 || ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && (__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSE)))
281 if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET) && (RCC_OscInitStruct->HSEState == RCC_HSE_OFF))
283 return HAL_ERROR;
286 else
288 /* Set the new HSE configuration ---------------------------------------*/
289 __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState);
292 /* Check the HSE State */
293 if(RCC_OscInitStruct->HSEState != RCC_HSE_OFF)
295 /* Get Start Tick */
296 tickstart = HAL_GetTick();
298 /* Wait till HSE is ready */
299 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)
301 if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
303 return HAL_TIMEOUT;
307 else
309 /* Get Start Tick */
310 tickstart = HAL_GetTick();
312 /* Wait till HSE is disabled */
313 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET)
315 if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
317 return HAL_TIMEOUT;
323 /*----------------------------- HSI Configuration --------------------------*/
324 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI)
326 /* Check the parameters */
327 assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState));
328 assert_param(IS_RCC_CALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue));
330 /* Check if HSI is used as system clock or as PLL source when PLL is selected as system clock */
331 if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_HSI)
332 || ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && (__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSI_DIV2)))
334 /* When HSI is used as system clock it will not disabled */
335 if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET) && (RCC_OscInitStruct->HSIState != RCC_HSI_ON))
337 return HAL_ERROR;
339 /* Otherwise, just the calibration is allowed */
340 else
342 /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
343 __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
346 else
348 /* Check the HSI State */
349 if(RCC_OscInitStruct->HSIState != RCC_HSI_OFF)
351 /* Enable the Internal High Speed oscillator (HSI). */
352 __HAL_RCC_HSI_ENABLE();
354 /* Get Start Tick */
355 tickstart = HAL_GetTick();
357 /* Wait till HSI is ready */
358 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)
360 if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
362 return HAL_TIMEOUT;
366 /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
367 __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
369 else
371 /* Disable the Internal High Speed oscillator (HSI). */
372 __HAL_RCC_HSI_DISABLE();
374 /* Get Start Tick */
375 tickstart = HAL_GetTick();
377 /* Wait till HSI is disabled */
378 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET)
380 if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
382 return HAL_TIMEOUT;
388 /*------------------------------ LSI Configuration -------------------------*/
389 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI)
391 /* Check the parameters */
392 assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState));
394 /* Check the LSI State */
395 if(RCC_OscInitStruct->LSIState != RCC_LSI_OFF)
397 /* Enable the Internal Low Speed oscillator (LSI). */
398 __HAL_RCC_LSI_ENABLE();
400 /* Get Start Tick */
401 tickstart = HAL_GetTick();
403 /* Wait till LSI is ready */
404 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) == RESET)
406 if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
408 return HAL_TIMEOUT;
411 /* To have a fully stabilized clock in the specified range, a software delay of 1ms
412 should be added.*/
413 RCC_Delay(1);
415 else
417 /* Disable the Internal Low Speed oscillator (LSI). */
418 __HAL_RCC_LSI_DISABLE();
420 /* Get Start Tick */
421 tickstart = HAL_GetTick();
423 /* Wait till LSI is disabled */
424 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) != RESET)
426 if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
428 return HAL_TIMEOUT;
433 /*------------------------------ LSE Configuration -------------------------*/
434 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE)
436 FlagStatus pwrclkchanged = RESET;
438 /* Check the parameters */
439 assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState));
441 /* Update LSE configuration in Backup Domain control register */
442 /* Requires to enable write access to Backup Domain of necessary */
443 if(__HAL_RCC_PWR_IS_CLK_DISABLED())
445 __HAL_RCC_PWR_CLK_ENABLE();
446 pwrclkchanged = SET;
449 if(HAL_IS_BIT_CLR(PWR->CR, PWR_CR_DBP))
451 /* Enable write access to Backup domain */
452 SET_BIT(PWR->CR, PWR_CR_DBP);
454 /* Wait for Backup domain Write protection disable */
455 tickstart = HAL_GetTick();
457 while(HAL_IS_BIT_CLR(PWR->CR, PWR_CR_DBP))
459 if((HAL_GetTick() - tickstart) > RCC_DBP_TIMEOUT_VALUE)
461 return HAL_TIMEOUT;
466 /* Set the new LSE configuration -----------------------------------------*/
467 __HAL_RCC_LSE_CONFIG(RCC_OscInitStruct->LSEState);
468 /* Check the LSE State */
469 if(RCC_OscInitStruct->LSEState != RCC_LSE_OFF)
471 /* Get Start Tick */
472 tickstart = HAL_GetTick();
474 /* Wait till LSE is ready */
475 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
477 if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
479 return HAL_TIMEOUT;
483 else
485 /* Get Start Tick */
486 tickstart = HAL_GetTick();
488 /* Wait till LSE is disabled */
489 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) != RESET)
491 if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
493 return HAL_TIMEOUT;
498 /* Require to disable power clock if necessary */
499 if(pwrclkchanged == SET)
501 __HAL_RCC_PWR_CLK_DISABLE();
505 #if defined(RCC_CR_PLL2ON)
506 /*-------------------------------- PLL2 Configuration -----------------------*/
507 /* Check the parameters */
508 assert_param(IS_RCC_PLL2(RCC_OscInitStruct->PLL2.PLL2State));
509 if ((RCC_OscInitStruct->PLL2.PLL2State) != RCC_PLL2_NONE)
511 /* This bit can not be cleared if the PLL2 clock is used indirectly as system
512 clock (i.e. it is used as PLL clock entry that is used as system clock). */
513 if((__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSE) && \
514 (__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && \
515 ((READ_BIT(RCC->CFGR2,RCC_CFGR2_PREDIV1SRC)) == RCC_CFGR2_PREDIV1SRC_PLL2))
517 return HAL_ERROR;
519 else
521 if((RCC_OscInitStruct->PLL2.PLL2State) == RCC_PLL2_ON)
523 /* Check the parameters */
524 assert_param(IS_RCC_PLL2_MUL(RCC_OscInitStruct->PLL2.PLL2MUL));
525 assert_param(IS_RCC_HSE_PREDIV2(RCC_OscInitStruct->PLL2.HSEPrediv2Value));
527 /* Prediv2 can be written only when the PLLI2S is disabled. */
528 /* Return an error only if new value is different from the programmed value */
529 if (HAL_IS_BIT_SET(RCC->CR,RCC_CR_PLL3ON) && \
530 (__HAL_RCC_HSE_GET_PREDIV2() != RCC_OscInitStruct->PLL2.HSEPrediv2Value))
532 return HAL_ERROR;
535 /* Disable the main PLL2. */
536 __HAL_RCC_PLL2_DISABLE();
538 /* Get Start Tick */
539 tickstart = HAL_GetTick();
541 /* Wait till PLL2 is disabled */
542 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLL2RDY) != RESET)
544 if((HAL_GetTick() - tickstart ) > PLL2_TIMEOUT_VALUE)
546 return HAL_TIMEOUT;
550 /* Configure the HSE prediv2 factor --------------------------------*/
551 __HAL_RCC_HSE_PREDIV2_CONFIG(RCC_OscInitStruct->PLL2.HSEPrediv2Value);
553 /* Configure the main PLL2 multiplication factors. */
554 __HAL_RCC_PLL2_CONFIG(RCC_OscInitStruct->PLL2.PLL2MUL);
556 /* Enable the main PLL2. */
557 __HAL_RCC_PLL2_ENABLE();
559 /* Get Start Tick */
560 tickstart = HAL_GetTick();
562 /* Wait till PLL2 is ready */
563 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLL2RDY) == RESET)
565 if((HAL_GetTick() - tickstart ) > PLL2_TIMEOUT_VALUE)
567 return HAL_TIMEOUT;
571 else
573 /* Set PREDIV1 source to HSE */
574 CLEAR_BIT(RCC->CFGR2, RCC_CFGR2_PREDIV1SRC);
576 /* Disable the main PLL2. */
577 __HAL_RCC_PLL2_DISABLE();
579 /* Get Start Tick */
580 tickstart = HAL_GetTick();
582 /* Wait till PLL2 is disabled */
583 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLL2RDY) != RESET)
585 if((HAL_GetTick() - tickstart ) > PLL2_TIMEOUT_VALUE)
587 return HAL_TIMEOUT;
594 #endif /* RCC_CR_PLL2ON */
595 /*-------------------------------- PLL Configuration -----------------------*/
596 /* Check the parameters */
597 assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState));
598 if ((RCC_OscInitStruct->PLL.PLLState) != RCC_PLL_NONE)
600 /* Check if the PLL is used as system clock or not */
601 if(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_PLLCLK)
603 if((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_ON)
605 /* Check the parameters */
606 assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource));
607 assert_param(IS_RCC_PLL_MUL(RCC_OscInitStruct->PLL.PLLMUL));
609 /* Disable the main PLL. */
610 __HAL_RCC_PLL_DISABLE();
612 /* Get Start Tick */
613 tickstart = HAL_GetTick();
615 /* Wait till PLL is disabled */
616 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
618 if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
620 return HAL_TIMEOUT;
624 /* Configure the HSE prediv factor --------------------------------*/
625 /* It can be written only when the PLL is disabled. Not used in PLL source is different than HSE */
626 if(RCC_OscInitStruct->PLL.PLLSource == RCC_PLLSOURCE_HSE)
628 /* Check the parameter */
629 assert_param(IS_RCC_HSE_PREDIV(RCC_OscInitStruct->HSEPredivValue));
630 #if defined(RCC_CFGR2_PREDIV1SRC)
631 assert_param(IS_RCC_PREDIV1_SOURCE(RCC_OscInitStruct->Prediv1Source));
633 /* Set PREDIV1 source */
634 SET_BIT(RCC->CFGR2, RCC_OscInitStruct->Prediv1Source);
635 #endif /* RCC_CFGR2_PREDIV1SRC */
637 /* Set PREDIV1 Value */
638 __HAL_RCC_HSE_PREDIV_CONFIG(RCC_OscInitStruct->HSEPredivValue);
641 /* Configure the main PLL clock source and multiplication factors. */
642 __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource,
643 RCC_OscInitStruct->PLL.PLLMUL);
644 /* Enable the main PLL. */
645 __HAL_RCC_PLL_ENABLE();
647 /* Get Start Tick */
648 tickstart = HAL_GetTick();
650 /* Wait till PLL is ready */
651 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
653 if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
655 return HAL_TIMEOUT;
659 else
661 /* Disable the main PLL. */
662 __HAL_RCC_PLL_DISABLE();
664 /* Get Start Tick */
665 tickstart = HAL_GetTick();
667 /* Wait till PLL is disabled */
668 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
670 if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
672 return HAL_TIMEOUT;
677 else
679 return HAL_ERROR;
683 return HAL_OK;
687 * @brief Initializes the CPU, AHB and APB buses clocks according to the specified
688 * parameters in the RCC_ClkInitStruct.
689 * @param RCC_ClkInitStruct pointer to an RCC_OscInitTypeDef structure that
690 * contains the configuration information for the RCC peripheral.
691 * @param FLatency FLASH Latency
692 * The value of this parameter depend on device used within the same series
693 * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency
694 * and updated by @ref HAL_RCC_GetHCLKFreq() function called within this function
696 * @note The HSI is used (enabled by hardware) as system clock source after
697 * start-up from Reset, wake-up from STOP and STANDBY mode, or in case
698 * of failure of the HSE used directly or indirectly as system clock
699 * (if the Clock Security System CSS is enabled).
701 * @note A switch from one clock source to another occurs only if the target
702 * clock source is ready (clock stable after start-up delay or PLL locked).
703 * If a clock source which is not yet ready is selected, the switch will
704 * occur when the clock source will be ready.
705 * You can use @ref HAL_RCC_GetClockConfig() function to know which clock is
706 * currently used as system clock source.
707 * @retval HAL status
709 HAL_StatusTypeDef HAL_RCC_ClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t FLatency)
711 uint32_t tickstart = 0U;
713 /* Check the parameters */
714 assert_param(RCC_ClkInitStruct != NULL);
715 assert_param(IS_RCC_CLOCKTYPE(RCC_ClkInitStruct->ClockType));
716 assert_param(IS_FLASH_LATENCY(FLatency));
718 /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
719 must be correctly programmed according to the frequency of the CPU clock
720 (HCLK) of the device. */
722 #if defined(FLASH_ACR_LATENCY)
723 /* Increasing the number of wait states because of higher CPU frequency */
724 if(FLatency > (FLASH->ACR & FLASH_ACR_LATENCY))
726 /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
727 __HAL_FLASH_SET_LATENCY(FLatency);
729 /* Check that the new number of wait states is taken into account to access the Flash
730 memory by reading the FLASH_ACR register */
731 if((FLASH->ACR & FLASH_ACR_LATENCY) != FLatency)
733 return HAL_ERROR;
737 #endif /* FLASH_ACR_LATENCY */
738 /*-------------------------- HCLK Configuration --------------------------*/
739 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
741 assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
742 MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
745 /*------------------------- SYSCLK Configuration ---------------------------*/
746 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_SYSCLK) == RCC_CLOCKTYPE_SYSCLK)
748 assert_param(IS_RCC_SYSCLKSOURCE(RCC_ClkInitStruct->SYSCLKSource));
750 /* HSE is selected as System Clock Source */
751 if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
753 /* Check the HSE ready flag */
754 if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)
756 return HAL_ERROR;
759 /* PLL is selected as System Clock Source */
760 else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
762 /* Check the PLL ready flag */
763 if(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
765 return HAL_ERROR;
768 /* HSI is selected as System Clock Source */
769 else
771 /* Check the HSI ready flag */
772 if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)
774 return HAL_ERROR;
777 __HAL_RCC_SYSCLK_CONFIG(RCC_ClkInitStruct->SYSCLKSource);
779 /* Get Start Tick */
780 tickstart = HAL_GetTick();
782 if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
784 while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_HSE)
786 if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
788 return HAL_TIMEOUT;
792 else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
794 while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_PLLCLK)
796 if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
798 return HAL_TIMEOUT;
802 else
804 while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_HSI)
806 if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
808 return HAL_TIMEOUT;
813 #if defined(FLASH_ACR_LATENCY)
814 /* Decreasing the number of wait states because of lower CPU frequency */
815 if(FLatency < (FLASH->ACR & FLASH_ACR_LATENCY))
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)
824 return HAL_ERROR;
827 #endif /* FLASH_ACR_LATENCY */
829 /*-------------------------- PCLK1 Configuration ---------------------------*/
830 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
832 assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB1CLKDivider));
833 MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE1, RCC_ClkInitStruct->APB1CLKDivider);
836 /*-------------------------- PCLK2 Configuration ---------------------------*/
837 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2)
839 assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB2CLKDivider));
840 MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE2, ((RCC_ClkInitStruct->APB2CLKDivider) << 3));
843 /* Update the SystemCoreClock global variable */
844 SystemCoreClock = HAL_RCC_GetSysClockFreq() >> AHBPrescTable[(RCC->CFGR & RCC_CFGR_HPRE)>> RCC_CFGR_HPRE_Pos];
846 /* Configure the source of time base considering new system clocks settings*/
847 HAL_InitTick (TICK_INT_PRIORITY);
849 return HAL_OK;
853 * @}
856 /** @defgroup RCC_Exported_Functions_Group2 Peripheral Control functions
857 * @brief RCC clocks control functions
859 @verbatim
860 ===============================================================================
861 ##### Peripheral Control functions #####
862 ===============================================================================
863 [..]
864 This subsection provides a set of functions allowing to control the RCC Clocks
865 frequencies.
867 @endverbatim
868 * @{
872 * @brief Selects the clock source to output on MCO pin.
873 * @note MCO pin should be configured in alternate function mode.
874 * @param RCC_MCOx specifies the output direction for the clock source.
875 * This parameter can be one of the following values:
876 * @arg @ref RCC_MCO1 Clock source to output on MCO1 pin(PA8).
877 * @param RCC_MCOSource specifies the clock source to output.
878 * This parameter can be one of the following values:
879 * @arg @ref RCC_MCO1SOURCE_NOCLOCK No clock selected as MCO clock
880 * @arg @ref RCC_MCO1SOURCE_SYSCLK System clock selected as MCO clock
881 * @arg @ref RCC_MCO1SOURCE_HSI HSI selected as MCO clock
882 * @arg @ref RCC_MCO1SOURCE_HSE HSE selected as MCO clock
883 @if STM32F105xC
884 * @arg @ref RCC_MCO1SOURCE_PLLCLK PLL clock divided by 2 selected as MCO source
885 * @arg @ref RCC_MCO1SOURCE_PLL2CLK PLL2 clock selected as MCO source
886 * @arg @ref RCC_MCO1SOURCE_PLL3CLK_DIV2 PLL3 clock divided by 2 selected as MCO source
887 * @arg @ref RCC_MCO1SOURCE_EXT_HSE XT1 external 3-25 MHz oscillator clock selected as MCO source
888 * @arg @ref RCC_MCO1SOURCE_PLL3CLK PLL3 clock selected as MCO source
889 @endif
890 @if STM32F107xC
891 * @arg @ref RCC_MCO1SOURCE_PLLCLK PLL clock divided by 2 selected as MCO source
892 * @arg @ref RCC_MCO1SOURCE_PLL2CLK PLL2 clock selected as MCO source
893 * @arg @ref RCC_MCO1SOURCE_PLL3CLK_DIV2 PLL3 clock divided by 2 selected as MCO source
894 * @arg @ref RCC_MCO1SOURCE_EXT_HSE XT1 external 3-25 MHz oscillator clock selected as MCO source
895 * @arg @ref RCC_MCO1SOURCE_PLL3CLK PLL3 clock selected as MCO source
896 @endif
897 * @param RCC_MCODiv specifies the MCO DIV.
898 * This parameter can be one of the following values:
899 * @arg @ref RCC_MCODIV_1 no division applied to MCO clock
900 * @retval None
902 void HAL_RCC_MCOConfig(uint32_t RCC_MCOx, uint32_t RCC_MCOSource, uint32_t RCC_MCODiv)
904 GPIO_InitTypeDef gpio = {0U};
906 /* Check the parameters */
907 assert_param(IS_RCC_MCO(RCC_MCOx));
908 assert_param(IS_RCC_MCODIV(RCC_MCODiv));
909 assert_param(IS_RCC_MCO1SOURCE(RCC_MCOSource));
911 /* Prevent unused argument(s) compilation warning */
912 UNUSED(RCC_MCOx);
913 UNUSED(RCC_MCODiv);
915 /* Configure the MCO1 pin in alternate function mode */
916 gpio.Mode = GPIO_MODE_AF_PP;
917 gpio.Speed = GPIO_SPEED_FREQ_HIGH;
918 gpio.Pull = GPIO_NOPULL;
919 gpio.Pin = MCO1_PIN;
921 /* MCO1 Clock Enable */
922 MCO1_CLK_ENABLE();
924 HAL_GPIO_Init(MCO1_GPIO_PORT, &gpio);
926 /* Configure the MCO clock source */
927 __HAL_RCC_MCO1_CONFIG(RCC_MCOSource, RCC_MCODiv);
931 * @brief Enables the Clock Security System.
932 * @note If a failure is detected on the HSE oscillator clock, this oscillator
933 * is automatically disabled and an interrupt is generated to inform the
934 * software about the failure (Clock Security System Interrupt, CSSI),
935 * allowing the MCU to perform rescue operations. The CSSI is linked to
936 * the Cortex-M3 NMI (Non-Maskable Interrupt) exception vector.
937 * @retval None
939 void HAL_RCC_EnableCSS(void)
941 *(__IO uint32_t *) RCC_CR_CSSON_BB = (uint32_t)ENABLE;
945 * @brief Disables the Clock Security System.
946 * @retval None
948 void HAL_RCC_DisableCSS(void)
950 *(__IO uint32_t *) RCC_CR_CSSON_BB = (uint32_t)DISABLE;
954 * @brief Returns the SYSCLK frequency
955 * @note The system frequency computed by this function is not the real
956 * frequency in the chip. It is calculated based on the predefined
957 * constant and the selected clock source:
958 * @note If SYSCLK source is HSI, function returns values based on HSI_VALUE(*)
959 * @note If SYSCLK source is HSE, function returns a value based on HSE_VALUE
960 * divided by PREDIV factor(**)
961 * @note If SYSCLK source is PLL, function returns a value based on HSE_VALUE
962 * divided by PREDIV factor(**) or HSI_VALUE(*) multiplied by the PLL factor.
963 * @note (*) HSI_VALUE is a constant defined in stm32f1xx_hal_conf.h file (default value
964 * 8 MHz) but the real value may vary depending on the variations
965 * in voltage and temperature.
966 * @note (**) HSE_VALUE is a constant defined in stm32f1xx_hal_conf.h file (default value
967 * 8 MHz), user has to ensure that HSE_VALUE is same as the real
968 * frequency of the crystal used. Otherwise, this function may
969 * have wrong result.
971 * @note The result of this function could be not correct when using fractional
972 * value for HSE crystal.
974 * @note This function can be used by the user application to compute the
975 * baud-rate for the communication peripherals or configure other parameters.
977 * @note Each time SYSCLK changes, this function must be called to update the
978 * right SYSCLK value. Otherwise, any configuration based on this function will be incorrect.
980 * @retval SYSCLK frequency
982 uint32_t HAL_RCC_GetSysClockFreq(void)
984 #if defined(RCC_CFGR2_PREDIV1SRC)
985 const uint8_t aPLLMULFactorTable[14] = {0, 0, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 13};
986 const uint8_t aPredivFactorTable[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
987 #else
988 const uint8_t aPLLMULFactorTable[16] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 16};
989 #if defined(RCC_CFGR2_PREDIV1)
990 const uint8_t aPredivFactorTable[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
991 #else
992 const uint8_t aPredivFactorTable[2] = {1, 2};
993 #endif /*RCC_CFGR2_PREDIV1*/
995 #endif
996 uint32_t tmpreg = 0U, prediv = 0U, pllclk = 0U, pllmul = 0U;
997 uint32_t sysclockfreq = 0U;
998 #if defined(RCC_CFGR2_PREDIV1SRC)
999 uint32_t prediv2 = 0U, pll2mul = 0U;
1000 #endif /*RCC_CFGR2_PREDIV1SRC*/
1002 tmpreg = RCC->CFGR;
1004 /* Get SYSCLK source -------------------------------------------------------*/
1005 switch (tmpreg & RCC_CFGR_SWS)
1007 case RCC_SYSCLKSOURCE_STATUS_HSE: /* HSE used as system clock */
1009 sysclockfreq = HSE_VALUE;
1010 break;
1012 case RCC_SYSCLKSOURCE_STATUS_PLLCLK: /* PLL used as system clock */
1014 pllmul = aPLLMULFactorTable[(uint32_t)(tmpreg & RCC_CFGR_PLLMULL) >> RCC_CFGR_PLLMULL_Pos];
1015 if ((tmpreg & RCC_CFGR_PLLSRC) != RCC_PLLSOURCE_HSI_DIV2)
1017 #if defined(RCC_CFGR2_PREDIV1)
1018 prediv = aPredivFactorTable[(uint32_t)(RCC->CFGR2 & RCC_CFGR2_PREDIV1) >> RCC_CFGR2_PREDIV1_Pos];
1019 #else
1020 prediv = aPredivFactorTable[(uint32_t)(RCC->CFGR & RCC_CFGR_PLLXTPRE) >> RCC_CFGR_PLLXTPRE_Pos];
1021 #endif /*RCC_CFGR2_PREDIV1*/
1022 #if defined(RCC_CFGR2_PREDIV1SRC)
1024 if(HAL_IS_BIT_SET(RCC->CFGR2, RCC_CFGR2_PREDIV1SRC))
1026 /* PLL2 selected as Prediv1 source */
1027 /* PLLCLK = PLL2CLK / PREDIV1 * PLLMUL with PLL2CLK = HSE/PREDIV2 * PLL2MUL */
1028 prediv2 = ((RCC->CFGR2 & RCC_CFGR2_PREDIV2) >> RCC_CFGR2_PREDIV2_Pos) + 1;
1029 pll2mul = ((RCC->CFGR2 & RCC_CFGR2_PLL2MUL) >> RCC_CFGR2_PLL2MUL_Pos) + 2;
1030 pllclk = (uint32_t)((((HSE_VALUE / prediv2) * pll2mul) / prediv) * pllmul);
1032 else
1034 /* HSE used as PLL clock source : PLLCLK = HSE/PREDIV1 * PLLMUL */
1035 pllclk = (uint32_t)((HSE_VALUE / prediv) * pllmul);
1038 /* If PLLMUL was set to 13 means that it was to cover the case PLLMUL 6.5 (avoid using float) */
1039 /* In this case need to divide pllclk by 2 */
1040 if (pllmul == aPLLMULFactorTable[(uint32_t)(RCC_CFGR_PLLMULL6_5) >> RCC_CFGR_PLLMULL_Pos])
1042 pllclk = pllclk / 2;
1044 #else
1045 /* HSE used as PLL clock source : PLLCLK = HSE/PREDIV1 * PLLMUL */
1046 pllclk = (uint32_t)((HSE_VALUE / prediv) * pllmul);
1047 #endif /*RCC_CFGR2_PREDIV1SRC*/
1049 else
1051 /* HSI used as PLL clock source : PLLCLK = HSI/2 * PLLMUL */
1052 pllclk = (uint32_t)((HSI_VALUE >> 1) * pllmul);
1054 sysclockfreq = pllclk;
1055 break;
1057 case RCC_SYSCLKSOURCE_STATUS_HSI: /* HSI used as system clock source */
1058 default: /* HSI used as system clock */
1060 sysclockfreq = HSI_VALUE;
1061 break;
1064 return sysclockfreq;
1068 * @brief Returns the HCLK frequency
1069 * @note Each time HCLK changes, this function must be called to update the
1070 * right HCLK value. Otherwise, any configuration based on this function will be incorrect.
1072 * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency
1073 * and updated within this function
1074 * @retval HCLK frequency
1076 uint32_t HAL_RCC_GetHCLKFreq(void)
1078 return SystemCoreClock;
1082 * @brief Returns the PCLK1 frequency
1083 * @note Each time PCLK1 changes, this function must be called to update the
1084 * right PCLK1 value. Otherwise, any configuration based on this function will be incorrect.
1085 * @retval PCLK1 frequency
1087 uint32_t HAL_RCC_GetPCLK1Freq(void)
1089 /* Get HCLK source and Compute PCLK1 frequency ---------------------------*/
1090 return (HAL_RCC_GetHCLKFreq() >> APBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE1) >> RCC_CFGR_PPRE1_Pos]);
1094 * @brief Returns the PCLK2 frequency
1095 * @note Each time PCLK2 changes, this function must be called to update the
1096 * right PCLK2 value. Otherwise, any configuration based on this function will be incorrect.
1097 * @retval PCLK2 frequency
1099 uint32_t HAL_RCC_GetPCLK2Freq(void)
1101 /* Get HCLK source and Compute PCLK2 frequency ---------------------------*/
1102 return (HAL_RCC_GetHCLKFreq()>> APBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE2) >> RCC_CFGR_PPRE2_Pos]);
1106 * @brief Configures the RCC_OscInitStruct according to the internal
1107 * RCC configuration registers.
1108 * @param RCC_OscInitStruct pointer to an RCC_OscInitTypeDef structure that
1109 * will be configured.
1110 * @retval None
1112 void HAL_RCC_GetOscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
1114 /* Check the parameters */
1115 assert_param(RCC_OscInitStruct != NULL);
1117 /* Set all possible values for the Oscillator type parameter ---------------*/
1118 RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI \
1119 | RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_LSI;
1121 #if defined(RCC_CFGR2_PREDIV1SRC)
1122 /* Get the Prediv1 source --------------------------------------------------*/
1123 RCC_OscInitStruct->Prediv1Source = READ_BIT(RCC->CFGR2,RCC_CFGR2_PREDIV1SRC);
1124 #endif /* RCC_CFGR2_PREDIV1SRC */
1126 /* Get the HSE configuration -----------------------------------------------*/
1127 if((RCC->CR &RCC_CR_HSEBYP) == RCC_CR_HSEBYP)
1129 RCC_OscInitStruct->HSEState = RCC_HSE_BYPASS;
1131 else if((RCC->CR &RCC_CR_HSEON) == RCC_CR_HSEON)
1133 RCC_OscInitStruct->HSEState = RCC_HSE_ON;
1135 else
1137 RCC_OscInitStruct->HSEState = RCC_HSE_OFF;
1139 RCC_OscInitStruct->HSEPredivValue = __HAL_RCC_HSE_GET_PREDIV();
1141 /* Get the HSI configuration -----------------------------------------------*/
1142 if((RCC->CR &RCC_CR_HSION) == RCC_CR_HSION)
1144 RCC_OscInitStruct->HSIState = RCC_HSI_ON;
1146 else
1148 RCC_OscInitStruct->HSIState = RCC_HSI_OFF;
1151 RCC_OscInitStruct->HSICalibrationValue = (uint32_t)((RCC->CR & RCC_CR_HSITRIM) >> RCC_CR_HSITRIM_Pos);
1153 /* Get the LSE configuration -----------------------------------------------*/
1154 if((RCC->BDCR &RCC_BDCR_LSEBYP) == RCC_BDCR_LSEBYP)
1156 RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS;
1158 else if((RCC->BDCR &RCC_BDCR_LSEON) == RCC_BDCR_LSEON)
1160 RCC_OscInitStruct->LSEState = RCC_LSE_ON;
1162 else
1164 RCC_OscInitStruct->LSEState = RCC_LSE_OFF;
1167 /* Get the LSI configuration -----------------------------------------------*/
1168 if((RCC->CSR &RCC_CSR_LSION) == RCC_CSR_LSION)
1170 RCC_OscInitStruct->LSIState = RCC_LSI_ON;
1172 else
1174 RCC_OscInitStruct->LSIState = RCC_LSI_OFF;
1178 /* Get the PLL configuration -----------------------------------------------*/
1179 if((RCC->CR &RCC_CR_PLLON) == RCC_CR_PLLON)
1181 RCC_OscInitStruct->PLL.PLLState = RCC_PLL_ON;
1183 else
1185 RCC_OscInitStruct->PLL.PLLState = RCC_PLL_OFF;
1187 RCC_OscInitStruct->PLL.PLLSource = (uint32_t)(RCC->CFGR & RCC_CFGR_PLLSRC);
1188 RCC_OscInitStruct->PLL.PLLMUL = (uint32_t)(RCC->CFGR & RCC_CFGR_PLLMULL);
1189 #if defined(RCC_CR_PLL2ON)
1190 /* Get the PLL2 configuration -----------------------------------------------*/
1191 if((RCC->CR &RCC_CR_PLL2ON) == RCC_CR_PLL2ON)
1193 RCC_OscInitStruct->PLL2.PLL2State = RCC_PLL2_ON;
1195 else
1197 RCC_OscInitStruct->PLL2.PLL2State = RCC_PLL2_OFF;
1199 RCC_OscInitStruct->PLL2.HSEPrediv2Value = __HAL_RCC_HSE_GET_PREDIV2();
1200 RCC_OscInitStruct->PLL2.PLL2MUL = (uint32_t)(RCC->CFGR2 & RCC_CFGR2_PLL2MUL);
1201 #endif /* RCC_CR_PLL2ON */
1205 * @brief Get the RCC_ClkInitStruct according to the internal
1206 * RCC configuration registers.
1207 * @param RCC_ClkInitStruct pointer to an RCC_ClkInitTypeDef structure that
1208 * contains the current clock configuration.
1209 * @param pFLatency Pointer on the Flash Latency.
1210 * @retval None
1212 void HAL_RCC_GetClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t *pFLatency)
1214 /* Check the parameters */
1215 assert_param(RCC_ClkInitStruct != NULL);
1216 assert_param(pFLatency != NULL);
1218 /* Set all possible values for the Clock type parameter --------------------*/
1219 RCC_ClkInitStruct->ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
1221 /* Get the SYSCLK configuration --------------------------------------------*/
1222 RCC_ClkInitStruct->SYSCLKSource = (uint32_t)(RCC->CFGR & RCC_CFGR_SW);
1224 /* Get the HCLK configuration ----------------------------------------------*/
1225 RCC_ClkInitStruct->AHBCLKDivider = (uint32_t)(RCC->CFGR & RCC_CFGR_HPRE);
1227 /* Get the APB1 configuration ----------------------------------------------*/
1228 RCC_ClkInitStruct->APB1CLKDivider = (uint32_t)(RCC->CFGR & RCC_CFGR_PPRE1);
1230 /* Get the APB2 configuration ----------------------------------------------*/
1231 RCC_ClkInitStruct->APB2CLKDivider = (uint32_t)((RCC->CFGR & RCC_CFGR_PPRE2) >> 3);
1233 #if defined(FLASH_ACR_LATENCY)
1234 /* Get the Flash Wait State (Latency) configuration ------------------------*/
1235 *pFLatency = (uint32_t)(FLASH->ACR & FLASH_ACR_LATENCY);
1236 #else
1237 /* For VALUE lines devices, only LATENCY_0 can be set*/
1238 *pFLatency = (uint32_t)FLASH_LATENCY_0;
1239 #endif
1243 * @brief This function handles the RCC CSS interrupt request.
1244 * @note This API should be called under the NMI_Handler().
1245 * @retval None
1247 void HAL_RCC_NMI_IRQHandler(void)
1249 /* Check RCC CSSF flag */
1250 if(__HAL_RCC_GET_IT(RCC_IT_CSS))
1252 /* RCC Clock Security System interrupt user callback */
1253 HAL_RCC_CSSCallback();
1255 /* Clear RCC CSS pending bit */
1256 __HAL_RCC_CLEAR_IT(RCC_IT_CSS);
1261 * @brief This function provides delay (in milliseconds) based on CPU cycles method.
1262 * @param mdelay: specifies the delay time length, in milliseconds.
1263 * @retval None
1265 static void RCC_Delay(uint32_t mdelay)
1267 __IO uint32_t Delay = mdelay * (SystemCoreClock / 8U / 1000U);
1270 __NOP();
1272 while (Delay --);
1276 * @brief RCC Clock Security System interrupt callback
1277 * @retval none
1279 __weak void HAL_RCC_CSSCallback(void)
1281 /* NOTE : This function Should not be modified, when the callback is needed,
1282 the HAL_RCC_CSSCallback could be implemented in the user file
1287 * @}
1291 * @}
1294 #endif /* HAL_RCC_MODULE_ENABLED */
1296 * @}
1300 * @}
1303 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/