2 ******************************************************************************
\r
3 * @file stm32l4xx_hal_rcc.c
\r
4 * @author MCD Application Team
\r
5 * @brief RCC HAL module driver.
\r
6 * This file provides firmware functions to manage the following
\r
7 * functionalities of the Reset and Clock Control (RCC) peripheral:
\r
8 * + Initialization and de-initialization functions
\r
9 * + Peripheral Control functions
\r
12 ==============================================================================
\r
13 ##### RCC specific features #####
\r
14 ==============================================================================
\r
16 After reset the device is running from Multiple Speed Internal oscillator
\r
17 (4 MHz) with Flash 0 wait state. Flash prefetch buffer, D-Cache
\r
18 and I-Cache are disabled, and all peripherals are off except internal
\r
19 SRAM, Flash and JTAG.
\r
21 (+) There is no prescaler on High speed (AHBs) and Low speed (APBs) busses:
\r
22 all peripherals mapped on these busses are running at MSI speed.
\r
23 (+) The clock for all peripherals is switched off, except the SRAM and FLASH.
\r
24 (+) All GPIOs are in analog mode, except the JTAG pins which
\r
25 are assigned to be used for debug purpose.
\r
28 Once the device started from reset, the user application has to:
\r
29 (+) Configure the clock source to be used to drive the System clock
\r
30 (if the application needs higher frequency/performance)
\r
31 (+) Configure the System clock frequency and Flash settings
\r
32 (+) Configure the AHB and APB busses prescalers
\r
33 (+) Enable the clock for the peripheral(s) to be used
\r
34 (+) Configure the clock source(s) for peripherals which clocks are not
\r
35 derived from the System clock (SAIx, RTC, ADC, USB OTG FS/SDMMC1/RNG)
\r
38 ******************************************************************************
\r
41 * <h2><center>© Copyright (c) 2017 STMicroelectronics.
\r
42 * All rights reserved.</center></h2>
\r
44 * This software component is licensed by ST under BSD 3-Clause license,
\r
45 * the "License"; You may not use this file except in compliance with the
\r
46 * License. You may obtain a copy of the License at:
\r
47 * opensource.org/licenses/BSD-3-Clause
\r
49 ******************************************************************************
\r
52 /* Includes ------------------------------------------------------------------*/
\r
53 #include "stm32l4xx_hal.h"
\r
55 /** @addtogroup STM32L4xx_HAL_Driver
\r
59 /** @defgroup RCC RCC
\r
60 * @brief RCC HAL module driver
\r
64 #ifdef HAL_RCC_MODULE_ENABLED
\r
66 /* Private typedef -----------------------------------------------------------*/
\r
67 /* Private define ------------------------------------------------------------*/
\r
68 /** @defgroup RCC_Private_Constants RCC Private Constants
\r
71 #define HSE_TIMEOUT_VALUE HSE_STARTUP_TIMEOUT
\r
72 #define HSI_TIMEOUT_VALUE 2U /* 2 ms (minimum Tick + 1) */
\r
73 #define MSI_TIMEOUT_VALUE 2U /* 2 ms (minimum Tick + 1) */
\r
74 #if defined(RCC_CSR_LSIPREDIV)
\r
75 #define LSI_TIMEOUT_VALUE 17U /* 17 ms (16 ms starting time + 1) */
\r
77 #define LSI_TIMEOUT_VALUE 2U /* 2 ms (minimum Tick + 1) */
\r
78 #endif /* RCC_CSR_LSIPREDIV */
\r
79 #define HSI48_TIMEOUT_VALUE 2U /* 2 ms (minimum Tick + 1) */
\r
80 #define PLL_TIMEOUT_VALUE 2U /* 2 ms (minimum Tick + 1) */
\r
81 #define CLOCKSWITCH_TIMEOUT_VALUE 5000U /* 5 s */
\r
86 /* Private macro -------------------------------------------------------------*/
\r
87 /** @defgroup RCC_Private_Macros RCC Private Macros
\r
90 #define __MCO1_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
\r
91 #define MCO1_GPIO_PORT GPIOA
\r
92 #define MCO1_PIN GPIO_PIN_8
\r
94 #define RCC_PLL_OSCSOURCE_CONFIG(__HAL_RCC_PLLSOURCE__) \
\r
95 (MODIFY_REG(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC, (__HAL_RCC_PLLSOURCE__)))
\r
100 /* Private variables ---------------------------------------------------------*/
\r
102 /* Private function prototypes -----------------------------------------------*/
\r
103 /** @defgroup RCC_Private_Functions RCC Private Functions
\r
106 static HAL_StatusTypeDef RCC_SetFlashLatencyFromMSIRange(uint32_t msirange);
\r
107 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
\r
108 static uint32_t RCC_GetSysClockFreqFromPLLSource(void);
\r
114 /* Exported functions --------------------------------------------------------*/
\r
116 /** @defgroup RCC_Exported_Functions RCC Exported Functions
\r
120 /** @defgroup RCC_Exported_Functions_Group1 Initialization and de-initialization functions
\r
121 * @brief Initialization and Configuration functions
\r
124 ===============================================================================
\r
125 ##### Initialization and de-initialization functions #####
\r
126 ===============================================================================
\r
128 This section provides functions allowing to configure the internal and external oscillators
\r
129 (HSE, HSI, LSE, MSI, LSI, PLL, CSS and MCO) and the System busses clocks (SYSCLK, AHB, APB1
\r
132 [..] Internal/external clock and PLL configuration
\r
133 (+) HSI (high-speed internal): 16 MHz factory-trimmed RC used directly or through
\r
134 the PLL as System clock source.
\r
136 (+) MSI (Mutiple Speed Internal): Its frequency is software trimmable from 100KHZ to 48MHZ.
\r
137 It can be used to generate the clock for the USB OTG FS (48 MHz).
\r
138 The number of flash wait states is automatically adjusted when MSI range is updated with
\r
139 HAL_RCC_OscConfig() and the MSI is used as System clock source.
\r
141 (+) LSI (low-speed internal): 32 KHz low consumption RC used as IWDG and/or RTC
\r
144 (+) HSE (high-speed external): 4 to 48 MHz crystal oscillator used directly or
\r
145 through the PLL as System clock source. Can be used also optionally as RTC clock source.
\r
147 (+) LSE (low-speed external): 32.768 KHz oscillator used optionally as RTC clock source.
\r
149 (+) PLL (clocked by HSI, HSE or MSI) providing up to three independent output clocks:
\r
150 (++) The first output is used to generate the high speed system clock (up to 80MHz).
\r
151 (++) The second output is used to generate the clock for the USB OTG FS (48 MHz),
\r
152 the random analog generator (<=48 MHz) and the SDMMC1 (<= 48 MHz).
\r
153 (++) The third output is used to generate an accurate clock to achieve
\r
154 high-quality audio performance on SAI interface.
\r
156 (+) PLLSAI1 (clocked by HSI, HSE or MSI) providing up to three independent output clocks:
\r
157 (++) The first output is used to generate SAR ADC1 clock.
\r
158 (++) The second output is used to generate the clock for the USB OTG FS (48 MHz),
\r
159 the random analog generator (<=48 MHz) and the SDMMC1 (<= 48 MHz).
\r
160 (++) The Third output is used to generate an accurate clock to achieve
\r
161 high-quality audio performance on SAI interface.
\r
163 (+) PLLSAI2 (clocked by HSI, HSE or MSI) providing up to two independent output clocks:
\r
164 (++) The first output is used to generate SAR ADC2 clock.
\r
165 (++) The second output is used to generate an accurate clock to achieve
\r
166 high-quality audio performance on SAI interface.
\r
168 (+) CSS (Clock security system): once enabled, if a HSE clock failure occurs
\r
169 (HSE used directly or through PLL as System clock source), the System clock
\r
170 is automatically switched to HSI and an interrupt is generated if enabled.
\r
171 The interrupt is linked to the Cortex-M4 NMI (Non-Maskable Interrupt)
\r
174 (+) MCO (microcontroller clock output): used to output MSI, LSI, HSI, LSE, HSE or
\r
175 main PLL clock (through a configurable prescaler) on PA8 pin.
\r
177 [..] System, AHB and APB busses clocks configuration
\r
178 (+) Several clock sources can be used to drive the System clock (SYSCLK): MSI, HSI,
\r
180 The AHB clock (HCLK) is derived from System clock through configurable
\r
181 prescaler and used to clock the CPU, memory and peripherals mapped
\r
182 on AHB bus (DMA, GPIO...). APB1 (PCLK1) and APB2 (PCLK2) clocks are derived
\r
183 from AHB clock through configurable prescalers and used to clock
\r
184 the peripherals mapped on these busses. You can use
\r
185 "HAL_RCC_GetSysClockFreq()" function to retrieve the frequencies of these clocks.
\r
187 -@- All the peripheral clocks are derived from the System clock (SYSCLK) except:
\r
189 (+@) SAI: the SAI clock can be derived either from a specific PLL (PLLSAI1) or (PLLSAI2) or
\r
190 from an external clock mapped on the SAI_CKIN pin.
\r
191 You have to use HAL_RCCEx_PeriphCLKConfig() function to configure this clock.
\r
192 (+@) RTC: the RTC clock can be derived either from the LSI, LSE or HSE clock
\r
193 divided by 2 to 31.
\r
194 You have to use __HAL_RCC_RTC_ENABLE() and HAL_RCCEx_PeriphCLKConfig() function
\r
195 to configure this clock.
\r
196 (+@) USB OTG FS, SDMMC1 and RNG: USB OTG FS requires a frequency equal to 48 MHz
\r
197 to work correctly, while the SDMMC1 and RNG peripherals require a frequency
\r
198 equal or lower than to 48 MHz. This clock is derived of the main PLL or PLLSAI1
\r
199 through PLLQ divider. You have to enable the peripheral clock and use
\r
200 HAL_RCCEx_PeriphCLKConfig() function to configure this clock.
\r
201 (+@) IWDG clock which is always the LSI clock.
\r
204 (+) The maximum frequency of the SYSCLK, HCLK, PCLK1 and PCLK2 is 80 MHz.
\r
205 The clock source frequency should be adapted depending on the device voltage range
\r
206 as listed in the Reference Manual "Clock source frequency versus voltage scaling" chapter.
\r
210 Table 1. HCLK clock frequency for STM32L4Rx/STM32L4Sx devices
\r
211 +--------------------------------------------------------+
\r
212 | Latency | HCLK clock frequency (MHz) |
\r
213 | |--------------------------------------|
\r
214 | | voltage range 1 | voltage range 2 |
\r
215 | | 1.2 V | 1.0 V |
\r
216 |-----------------|-------------------|------------------|
\r
217 |0WS(1 CPU cycles)| 0 < HCLK <= 20 | 0 < HCLK <= 8 |
\r
218 |-----------------|-------------------|------------------|
\r
219 |1WS(2 CPU cycles)| 20 < HCLK <= 40 | 8 < HCLK <= 16 |
\r
220 |-----------------|-------------------|------------------|
\r
221 |2WS(3 CPU cycles)| 40 < HCLK <= 60 | 16 < HCLK <= 26 |
\r
222 |-----------------|-------------------|------------------|
\r
223 |3WS(4 CPU cycles)| 60 < HCLK <= 80 | 16 < HCLK <= 26 |
\r
224 |-----------------|-------------------|------------------|
\r
225 |4WS(5 CPU cycles)| 80 < HCLK <= 100 | 16 < HCLK <= 26 |
\r
226 |-----------------|-------------------|------------------|
\r
227 |5WS(6 CPU cycles)| 100 < HCLK <= 120 | 16 < HCLK <= 26 |
\r
228 +--------------------------------------------------------+
\r
230 Table 2. HCLK clock frequency for other STM32L4 devices
\r
231 +-------------------------------------------------------+
\r
232 | Latency | HCLK clock frequency (MHz) |
\r
233 | |-------------------------------------|
\r
234 | | voltage range 1 | voltage range 2 |
\r
235 | | 1.2 V | 1.0 V |
\r
236 |-----------------|------------------|------------------|
\r
237 |0WS(1 CPU cycles)| 0 < HCLK <= 16 | 0 < HCLK <= 6 |
\r
238 |-----------------|------------------|------------------|
\r
239 |1WS(2 CPU cycles)| 16 < HCLK <= 32 | 6 < HCLK <= 12 |
\r
240 |-----------------|------------------|------------------|
\r
241 |2WS(3 CPU cycles)| 32 < HCLK <= 48 | 12 < HCLK <= 18 |
\r
242 |-----------------|------------------|------------------|
\r
243 |3WS(4 CPU cycles)| 48 < HCLK <= 64 | 18 < HCLK <= 26 |
\r
244 |-----------------|------------------|------------------|
\r
245 |4WS(5 CPU cycles)| 64 < HCLK <= 80 | 18 < HCLK <= 26 |
\r
246 +-------------------------------------------------------+
\r
251 * @brief Reset the RCC clock configuration to the default reset state.
\r
252 * @note The default reset state of the clock configuration is given below:
\r
253 * - MSI ON and used as system clock source
\r
254 * - HSE, HSI, PLL, PLLSAI1 and PLLSAI2 OFF
\r
255 * - AHB, APB1 and APB2 prescalers set to 1.
\r
257 * - All interrupts disabled
\r
258 * - All interrupt and reset flags cleared
\r
259 * @note This function does not modify the configuration of the
\r
260 * - Peripheral clock sources
\r
261 * - LSI, LSE and RTC clocks (Backup domain)
\r
262 * @retval HAL status
\r
264 HAL_StatusTypeDef HAL_RCC_DeInit(void)
\r
266 uint32_t tickstart;
\r
268 /* Reset to default System clock */
\r
269 /* Set MSION bit */
\r
270 SET_BIT(RCC->CR, RCC_CR_MSION);
\r
272 /* Insure MSIRDY bit is set before writing default MSIRANGE value */
\r
273 /* Get start tick */
\r
274 tickstart = HAL_GetTick();
\r
276 /* Wait till MSI is ready */
\r
277 while(READ_BIT(RCC->CR, RCC_CR_MSIRDY) == 0U)
\r
279 if((HAL_GetTick() - tickstart) > MSI_TIMEOUT_VALUE)
\r
281 return HAL_TIMEOUT;
\r
285 /* Set MSIRANGE default value */
\r
286 MODIFY_REG(RCC->CR, RCC_CR_MSIRANGE, RCC_MSIRANGE_6);
\r
288 /* Reset CFGR register (MSI is selected as system clock source) */
\r
289 CLEAR_REG(RCC->CFGR);
\r
291 /* Update the SystemCoreClock global variable for MSI as system clock source */
\r
292 SystemCoreClock = MSI_VALUE;
\r
294 /* Configure the source of time base considering new system clock settings */
\r
295 if(HAL_InitTick(uwTickPrio) != HAL_OK)
\r
300 /* Insure MSI selected as system clock source */
\r
301 /* Get start tick */
\r
302 tickstart = HAL_GetTick();
\r
304 /* Wait till system clock source is ready */
\r
305 while(READ_BIT(RCC->CFGR, RCC_CFGR_SWS) != RCC_CFGR_SWS_MSI)
\r
307 if((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
\r
309 return HAL_TIMEOUT;
\r
313 /* Reset HSION, HSIKERON, HSIASFS, HSEON, HSECSSON, PLLON, PLLSAIxON bits */
\r
314 #if defined(RCC_PLLSAI2_SUPPORT)
\r
316 CLEAR_BIT(RCC->CR, RCC_CR_HSEON | RCC_CR_HSION | RCC_CR_HSIKERON| RCC_CR_HSIASFS | RCC_CR_PLLON | RCC_CR_PLLSAI1ON | RCC_CR_PLLSAI2ON);
\r
318 #elif defined(RCC_PLLSAI1_SUPPORT)
\r
320 CLEAR_BIT(RCC->CR, RCC_CR_HSEON | RCC_CR_HSION | RCC_CR_HSIKERON| RCC_CR_HSIASFS | RCC_CR_PLLON | RCC_CR_PLLSAI1ON);
\r
324 CLEAR_BIT(RCC->CR, RCC_CR_HSEON | RCC_CR_HSION | RCC_CR_HSIKERON| RCC_CR_HSIASFS | RCC_CR_PLLON);
\r
326 #endif /* RCC_PLLSAI2_SUPPORT */
\r
328 /* Insure PLLRDY, PLLSAI1RDY and PLLSAI2RDY (if present) are reset */
\r
329 /* Get start tick */
\r
330 tickstart = HAL_GetTick();
\r
332 #if defined(RCC_PLLSAI2_SUPPORT)
\r
334 while(READ_BIT(RCC->CR, RCC_CR_PLLRDY | RCC_CR_PLLSAI1RDY | RCC_CR_PLLSAI2RDY) != 0U)
\r
336 #elif defined(RCC_PLLSAI1_SUPPORT)
\r
338 while(READ_BIT(RCC->CR, RCC_CR_PLLRDY | RCC_CR_PLLSAI1RDY) != 0U)
\r
342 while(READ_BIT(RCC->CR, RCC_CR_PLLRDY) != 0U)
\r
346 if((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
\r
348 return HAL_TIMEOUT;
\r
352 /* Reset PLLCFGR register */
\r
353 CLEAR_REG(RCC->PLLCFGR);
\r
354 SET_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN_4 );
\r
356 #if defined(RCC_PLLSAI1_SUPPORT)
\r
358 /* Reset PLLSAI1CFGR register */
\r
359 CLEAR_REG(RCC->PLLSAI1CFGR);
\r
360 SET_BIT(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1N_4 );
\r
362 #endif /* RCC_PLLSAI1_SUPPORT */
\r
364 #if defined(RCC_PLLSAI2_SUPPORT)
\r
366 /* Reset PLLSAI2CFGR register */
\r
367 CLEAR_REG(RCC->PLLSAI2CFGR);
\r
368 SET_BIT(RCC->PLLSAI2CFGR, RCC_PLLSAI2CFGR_PLLSAI2N_4 );
\r
370 #endif /* RCC_PLLSAI2_SUPPORT */
\r
372 /* Reset HSEBYP bit */
\r
373 CLEAR_BIT(RCC->CR, RCC_CR_HSEBYP);
\r
375 /* Disable all interrupts */
\r
376 CLEAR_REG(RCC->CIER);
\r
378 /* Clear all interrupt flags */
\r
379 WRITE_REG(RCC->CICR, 0xFFFFFFFFU);
\r
381 /* Clear all reset flags */
\r
382 SET_BIT(RCC->CSR, RCC_CSR_RMVF);
\r
388 * @brief Initialize the RCC Oscillators according to the specified parameters in the
\r
389 * RCC_OscInitTypeDef.
\r
390 * @param RCC_OscInitStruct pointer to an RCC_OscInitTypeDef structure that
\r
391 * contains the configuration information for the RCC Oscillators.
\r
392 * @note The PLL is not disabled when used as system clock.
\r
393 * @note Transitions LSE Bypass to LSE On and LSE On to LSE Bypass are not
\r
394 * supported by this macro. User should request a transition to LSE Off
\r
395 * first and then LSE On or LSE Bypass.
\r
396 * @note Transition HSE Bypass to HSE On and HSE On to HSE Bypass are not
\r
397 * supported by this macro. User should request a transition to HSE Off
\r
398 * first and then HSE On or HSE Bypass.
\r
399 * @retval HAL status
\r
401 HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
\r
403 uint32_t tickstart;
\r
404 HAL_StatusTypeDef status;
\r
405 uint32_t sysclk_source, pll_config;
\r
407 /* Check Null pointer */
\r
408 if(RCC_OscInitStruct == NULL)
\r
413 /* Check the parameters */
\r
414 assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType));
\r
416 sysclk_source = __HAL_RCC_GET_SYSCLK_SOURCE();
\r
417 pll_config = __HAL_RCC_GET_PLL_OSCSOURCE();
\r
419 /*----------------------------- MSI Configuration --------------------------*/
\r
420 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_MSI) == RCC_OSCILLATORTYPE_MSI)
\r
422 /* Check the parameters */
\r
423 assert_param(IS_RCC_MSI(RCC_OscInitStruct->MSIState));
\r
424 assert_param(IS_RCC_MSICALIBRATION_VALUE(RCC_OscInitStruct->MSICalibrationValue));
\r
425 assert_param(IS_RCC_MSI_CLOCK_RANGE(RCC_OscInitStruct->MSIClockRange));
\r
427 /* Check if MSI is used as system clock or as PLL source when PLL is selected as system clock */
\r
428 if((sysclk_source == RCC_CFGR_SWS_MSI) ||
\r
429 ((sysclk_source == RCC_CFGR_SWS_PLL) && (pll_config == RCC_PLLSOURCE_MSI)))
\r
431 if((READ_BIT(RCC->CR, RCC_CR_MSIRDY) != 0U) && (RCC_OscInitStruct->MSIState == RCC_MSI_OFF))
\r
436 /* Otherwise, just the calibration and MSI range change are allowed */
\r
439 /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
\r
440 must be correctly programmed according to the frequency of the CPU clock
\r
441 (HCLK) and the supply voltage of the device. */
\r
442 if(RCC_OscInitStruct->MSIClockRange > __HAL_RCC_GET_MSI_RANGE())
\r
444 /* First increase number of wait states update if necessary */
\r
445 if(RCC_SetFlashLatencyFromMSIRange(RCC_OscInitStruct->MSIClockRange) != HAL_OK)
\r
450 /* Selects the Multiple Speed oscillator (MSI) clock range .*/
\r
451 __HAL_RCC_MSI_RANGE_CONFIG(RCC_OscInitStruct->MSIClockRange);
\r
452 /* Adjusts the Multiple Speed oscillator (MSI) calibration value.*/
\r
453 __HAL_RCC_MSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->MSICalibrationValue);
\r
457 /* Else, keep current flash latency while decreasing applies */
\r
458 /* Selects the Multiple Speed oscillator (MSI) clock range .*/
\r
459 __HAL_RCC_MSI_RANGE_CONFIG(RCC_OscInitStruct->MSIClockRange);
\r
460 /* Adjusts the Multiple Speed oscillator (MSI) calibration value.*/
\r
461 __HAL_RCC_MSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->MSICalibrationValue);
\r
463 /* Decrease number of wait states update if necessary */
\r
464 if(RCC_SetFlashLatencyFromMSIRange(RCC_OscInitStruct->MSIClockRange) != HAL_OK)
\r
470 /* Update the SystemCoreClock global variable */
\r
471 SystemCoreClock = HAL_RCC_GetSysClockFreq() >> (AHBPrescTable[READ_BIT(RCC->CFGR, RCC_CFGR_HPRE) >> RCC_CFGR_HPRE_Pos] & 0x1FU);
\r
473 /* Configure the source of time base considering new system clocks settings*/
\r
474 status = HAL_InitTick(uwTickPrio);
\r
475 if(status != HAL_OK)
\r
483 /* Check the MSI State */
\r
484 if(RCC_OscInitStruct->MSIState != RCC_MSI_OFF)
\r
486 /* Enable the Internal High Speed oscillator (MSI). */
\r
487 __HAL_RCC_MSI_ENABLE();
\r
490 tickstart = HAL_GetTick();
\r
492 /* Wait till MSI is ready */
\r
493 while(READ_BIT(RCC->CR, RCC_CR_MSIRDY) == 0U)
\r
495 if((HAL_GetTick() - tickstart) > MSI_TIMEOUT_VALUE)
\r
497 return HAL_TIMEOUT;
\r
500 /* Selects the Multiple Speed oscillator (MSI) clock range .*/
\r
501 __HAL_RCC_MSI_RANGE_CONFIG(RCC_OscInitStruct->MSIClockRange);
\r
502 /* Adjusts the Multiple Speed oscillator (MSI) calibration value.*/
\r
503 __HAL_RCC_MSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->MSICalibrationValue);
\r
508 /* Disable the Internal High Speed oscillator (MSI). */
\r
509 __HAL_RCC_MSI_DISABLE();
\r
512 tickstart = HAL_GetTick();
\r
514 /* Wait till MSI is ready */
\r
515 while(READ_BIT(RCC->CR, RCC_CR_MSIRDY) != 0U)
\r
517 if((HAL_GetTick() - tickstart) > MSI_TIMEOUT_VALUE)
\r
519 return HAL_TIMEOUT;
\r
525 /*------------------------------- HSE Configuration ------------------------*/
\r
526 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE)
\r
528 /* Check the parameters */
\r
529 assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState));
\r
531 /* When the HSE is used as system clock or clock source for PLL in these cases it is not allowed to be disabled */
\r
532 if((sysclk_source == RCC_CFGR_SWS_HSE) ||
\r
533 ((sysclk_source == RCC_CFGR_SWS_PLL) && (pll_config == RCC_PLLSOURCE_HSE)))
\r
535 if((READ_BIT(RCC->CR, RCC_CR_HSERDY) != 0U) && (RCC_OscInitStruct->HSEState == RCC_HSE_OFF))
\r
542 /* Set the new HSE configuration ---------------------------------------*/
\r
543 __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState);
\r
545 /* Check the HSE State */
\r
546 if(RCC_OscInitStruct->HSEState != RCC_HSE_OFF)
\r
548 /* Get Start Tick*/
\r
549 tickstart = HAL_GetTick();
\r
551 /* Wait till HSE is ready */
\r
552 while(READ_BIT(RCC->CR, RCC_CR_HSERDY) == 0U)
\r
554 if((HAL_GetTick() - tickstart) > HSE_TIMEOUT_VALUE)
\r
556 return HAL_TIMEOUT;
\r
562 /* Get Start Tick*/
\r
563 tickstart = HAL_GetTick();
\r
565 /* Wait till HSE is disabled */
\r
566 while(READ_BIT(RCC->CR, RCC_CR_HSERDY) != 0U)
\r
568 if((HAL_GetTick() - tickstart) > HSE_TIMEOUT_VALUE)
\r
570 return HAL_TIMEOUT;
\r
576 /*----------------------------- HSI Configuration --------------------------*/
\r
577 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI)
\r
579 /* Check the parameters */
\r
580 assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState));
\r
581 assert_param(IS_RCC_HSI_CALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue));
\r
583 /* Check if HSI is used as system clock or as PLL source when PLL is selected as system clock */
\r
584 if((sysclk_source == RCC_CFGR_SWS_HSI) ||
\r
585 ((sysclk_source == RCC_CFGR_SWS_PLL) && (pll_config == RCC_PLLSOURCE_HSI)))
\r
587 /* When HSI is used as system clock it will not be disabled */
\r
588 if((READ_BIT(RCC->CR, RCC_CR_HSIRDY) != 0U) && (RCC_OscInitStruct->HSIState == RCC_HSI_OFF))
\r
592 /* Otherwise, just the calibration is allowed */
\r
595 /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
\r
596 __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
\r
601 /* Check the HSI State */
\r
602 if(RCC_OscInitStruct->HSIState != RCC_HSI_OFF)
\r
604 /* Enable the Internal High Speed oscillator (HSI). */
\r
605 __HAL_RCC_HSI_ENABLE();
\r
607 /* Get Start Tick*/
\r
608 tickstart = HAL_GetTick();
\r
610 /* Wait till HSI is ready */
\r
611 while(READ_BIT(RCC->CR, RCC_CR_HSIRDY) == 0U)
\r
613 if((HAL_GetTick() - tickstart) > HSI_TIMEOUT_VALUE)
\r
615 return HAL_TIMEOUT;
\r
619 /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
\r
620 __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
\r
624 /* Disable the Internal High Speed oscillator (HSI). */
\r
625 __HAL_RCC_HSI_DISABLE();
\r
627 /* Get Start Tick*/
\r
628 tickstart = HAL_GetTick();
\r
630 /* Wait till HSI is disabled */
\r
631 while(READ_BIT(RCC->CR, RCC_CR_HSIRDY) != 0U)
\r
633 if((HAL_GetTick() - tickstart) > HSI_TIMEOUT_VALUE)
\r
635 return HAL_TIMEOUT;
\r
641 /*------------------------------ LSI Configuration -------------------------*/
\r
642 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI)
\r
644 /* Check the parameters */
\r
645 assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState));
\r
647 /* Check the LSI State */
\r
648 if(RCC_OscInitStruct->LSIState != RCC_LSI_OFF)
\r
650 #if defined(RCC_CSR_LSIPREDIV)
\r
651 uint32_t csr_temp = RCC->CSR;
\r
653 /* Check LSI division factor */
\r
654 assert_param(IS_RCC_LSIDIV(RCC_OscInitStruct->LSIDiv));
\r
656 if (RCC_OscInitStruct->LSIDiv != (csr_temp & RCC_CSR_LSIPREDIV))
\r
658 if (((csr_temp & RCC_CSR_LSIRDY) == RCC_CSR_LSIRDY) && \
\r
659 ((csr_temp & RCC_CSR_LSION) != RCC_CSR_LSION))
\r
661 /* If LSIRDY is set while LSION is not enabled,
\r
662 LSIPREDIV can't be updated */
\r
666 /* Turn off LSI before changing RCC_CSR_LSIPREDIV */
\r
667 if ((csr_temp & RCC_CSR_LSION) == RCC_CSR_LSION)
\r
669 __HAL_RCC_LSI_DISABLE();
\r
671 /* Get Start Tick*/
\r
672 tickstart = HAL_GetTick();
\r
674 /* Wait till LSI is disabled */
\r
675 while(READ_BIT(RCC->CSR, RCC_CSR_LSIRDY) != 0U)
\r
677 if((HAL_GetTick() - tickstart) > LSI_TIMEOUT_VALUE)
\r
679 return HAL_TIMEOUT;
\r
684 /* Set LSI division factor */
\r
685 MODIFY_REG(RCC->CSR, RCC_CSR_LSIPREDIV, RCC_OscInitStruct->LSIDiv);
\r
687 #endif /* RCC_CSR_LSIPREDIV */
\r
689 /* Enable the Internal Low Speed oscillator (LSI). */
\r
690 __HAL_RCC_LSI_ENABLE();
\r
692 /* Get Start Tick*/
\r
693 tickstart = HAL_GetTick();
\r
695 /* Wait till LSI is ready */
\r
696 while(READ_BIT(RCC->CSR, RCC_CSR_LSIRDY) == 0U)
\r
698 if((HAL_GetTick() - tickstart) > LSI_TIMEOUT_VALUE)
\r
700 return HAL_TIMEOUT;
\r
706 /* Disable the Internal Low Speed oscillator (LSI). */
\r
707 __HAL_RCC_LSI_DISABLE();
\r
709 /* Get Start Tick*/
\r
710 tickstart = HAL_GetTick();
\r
712 /* Wait till LSI is disabled */
\r
713 while(READ_BIT(RCC->CSR, RCC_CSR_LSIRDY) != 0U)
\r
715 if((HAL_GetTick() - tickstart) > LSI_TIMEOUT_VALUE)
\r
717 return HAL_TIMEOUT;
\r
722 /*------------------------------ LSE Configuration -------------------------*/
\r
723 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE)
\r
725 FlagStatus pwrclkchanged = RESET;
\r
727 /* Check the parameters */
\r
728 assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState));
\r
730 /* Update LSE configuration in Backup Domain control register */
\r
731 /* Requires to enable write access to Backup Domain of necessary */
\r
732 if(HAL_IS_BIT_CLR(RCC->APB1ENR1, RCC_APB1ENR1_PWREN))
\r
734 __HAL_RCC_PWR_CLK_ENABLE();
\r
735 pwrclkchanged = SET;
\r
738 if(HAL_IS_BIT_CLR(PWR->CR1, PWR_CR1_DBP))
\r
740 /* Enable write access to Backup domain */
\r
741 SET_BIT(PWR->CR1, PWR_CR1_DBP);
\r
743 /* Wait for Backup domain Write protection disable */
\r
744 tickstart = HAL_GetTick();
\r
746 while(HAL_IS_BIT_CLR(PWR->CR1, PWR_CR1_DBP))
\r
748 if((HAL_GetTick() - tickstart) > RCC_DBP_TIMEOUT_VALUE)
\r
750 return HAL_TIMEOUT;
\r
755 /* Set the new LSE configuration -----------------------------------------*/
\r
756 #if defined(RCC_BDCR_LSESYSDIS)
\r
757 if((RCC_OscInitStruct->LSEState & RCC_BDCR_LSEON) != 0U)
\r
759 /* Set LSESYSDIS bit according to LSE propagation option (enabled or disabled) */
\r
760 MODIFY_REG(RCC->BDCR, RCC_BDCR_LSESYSDIS, (RCC_OscInitStruct->LSEState & RCC_BDCR_LSESYSDIS));
\r
762 if((RCC_OscInitStruct->LSEState & RCC_BDCR_LSEBYP) != 0U)
\r
764 /* LSE oscillator bypass enable */
\r
765 SET_BIT(RCC->BDCR, RCC_BDCR_LSEBYP);
\r
766 SET_BIT(RCC->BDCR, RCC_BDCR_LSEON);
\r
770 /* LSE oscillator enable */
\r
771 SET_BIT(RCC->BDCR, RCC_BDCR_LSEON);
\r
776 CLEAR_BIT(RCC->BDCR, RCC_BDCR_LSEON);
\r
777 CLEAR_BIT(RCC->BDCR, RCC_BDCR_LSEBYP);
\r
780 __HAL_RCC_LSE_CONFIG(RCC_OscInitStruct->LSEState);
\r
781 #endif /* RCC_BDCR_LSESYSDIS */
\r
783 /* Check the LSE State */
\r
784 if(RCC_OscInitStruct->LSEState != RCC_LSE_OFF)
\r
786 /* Get Start Tick*/
\r
787 tickstart = HAL_GetTick();
\r
789 /* Wait till LSE is ready */
\r
790 while(READ_BIT(RCC->BDCR, RCC_BDCR_LSERDY) == 0U)
\r
792 if((HAL_GetTick() - tickstart) > RCC_LSE_TIMEOUT_VALUE)
\r
794 return HAL_TIMEOUT;
\r
800 /* Get Start Tick*/
\r
801 tickstart = HAL_GetTick();
\r
803 /* Wait till LSE is disabled */
\r
804 while(READ_BIT(RCC->BDCR, RCC_BDCR_LSERDY) != 0U)
\r
806 if((HAL_GetTick() - tickstart) > RCC_LSE_TIMEOUT_VALUE)
\r
808 return HAL_TIMEOUT;
\r
812 #if defined(RCC_BDCR_LSESYSDIS)
\r
813 /* By default, stop disabling LSE propagation */
\r
814 CLEAR_BIT(RCC->BDCR, RCC_BDCR_LSESYSDIS);
\r
815 #endif /* RCC_BDCR_LSESYSDIS */
\r
818 /* Restore clock configuration if changed */
\r
819 if(pwrclkchanged == SET)
\r
821 __HAL_RCC_PWR_CLK_DISABLE();
\r
824 #if defined(RCC_HSI48_SUPPORT)
\r
825 /*------------------------------ HSI48 Configuration -----------------------*/
\r
826 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI48) == RCC_OSCILLATORTYPE_HSI48)
\r
828 /* Check the parameters */
\r
829 assert_param(IS_RCC_HSI48(RCC_OscInitStruct->HSI48State));
\r
831 /* Check the LSI State */
\r
832 if(RCC_OscInitStruct->HSI48State != RCC_HSI48_OFF)
\r
834 /* Enable the Internal Low Speed oscillator (HSI48). */
\r
835 __HAL_RCC_HSI48_ENABLE();
\r
837 /* Get Start Tick*/
\r
838 tickstart = HAL_GetTick();
\r
840 /* Wait till HSI48 is ready */
\r
841 while(READ_BIT(RCC->CRRCR, RCC_CRRCR_HSI48RDY) == 0U)
\r
843 if((HAL_GetTick() - tickstart) > HSI48_TIMEOUT_VALUE)
\r
845 return HAL_TIMEOUT;
\r
851 /* Disable the Internal Low Speed oscillator (HSI48). */
\r
852 __HAL_RCC_HSI48_DISABLE();
\r
854 /* Get Start Tick*/
\r
855 tickstart = HAL_GetTick();
\r
857 /* Wait till HSI48 is disabled */
\r
858 while(READ_BIT(RCC->CRRCR, RCC_CRRCR_HSI48RDY) != 0U)
\r
860 if((HAL_GetTick() - tickstart) > HSI48_TIMEOUT_VALUE)
\r
862 return HAL_TIMEOUT;
\r
867 #endif /* RCC_HSI48_SUPPORT */
\r
868 /*-------------------------------- PLL Configuration -----------------------*/
\r
869 /* Check the parameters */
\r
870 assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState));
\r
872 if(RCC_OscInitStruct->PLL.PLLState != RCC_PLL_NONE)
\r
874 /* Check if the PLL is used as system clock or not */
\r
875 if(sysclk_source != RCC_CFGR_SWS_PLL)
\r
877 if(RCC_OscInitStruct->PLL.PLLState == RCC_PLL_ON)
\r
879 /* Check the parameters */
\r
880 assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource));
\r
881 assert_param(IS_RCC_PLLM_VALUE(RCC_OscInitStruct->PLL.PLLM));
\r
882 assert_param(IS_RCC_PLLN_VALUE(RCC_OscInitStruct->PLL.PLLN));
\r
883 #if defined(RCC_PLLP_SUPPORT)
\r
884 assert_param(IS_RCC_PLLP_VALUE(RCC_OscInitStruct->PLL.PLLP));
\r
885 #endif /* RCC_PLLP_SUPPORT */
\r
886 assert_param(IS_RCC_PLLQ_VALUE(RCC_OscInitStruct->PLL.PLLQ));
\r
887 assert_param(IS_RCC_PLLR_VALUE(RCC_OscInitStruct->PLL.PLLR));
\r
889 /* Disable the main PLL. */
\r
890 __HAL_RCC_PLL_DISABLE();
\r
892 /* Get Start Tick*/
\r
893 tickstart = HAL_GetTick();
\r
895 /* Wait till PLL is ready */
\r
896 while(READ_BIT(RCC->CR, RCC_CR_PLLRDY) != 0U)
\r
898 if((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
\r
900 return HAL_TIMEOUT;
\r
904 /* Configure the main PLL clock source, multiplication and division factors. */
\r
905 __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource,
\r
906 RCC_OscInitStruct->PLL.PLLM,
\r
907 RCC_OscInitStruct->PLL.PLLN,
\r
908 #if defined(RCC_PLLP_SUPPORT)
\r
909 RCC_OscInitStruct->PLL.PLLP,
\r
911 RCC_OscInitStruct->PLL.PLLQ,
\r
912 RCC_OscInitStruct->PLL.PLLR);
\r
914 /* Enable the main PLL. */
\r
915 __HAL_RCC_PLL_ENABLE();
\r
917 /* Enable PLL System Clock output. */
\r
918 __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL_SYSCLK);
\r
920 /* Get Start Tick*/
\r
921 tickstart = HAL_GetTick();
\r
923 /* Wait till PLL is ready */
\r
924 while(READ_BIT(RCC->CR, RCC_CR_PLLRDY) == 0U)
\r
926 if((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
\r
928 return HAL_TIMEOUT;
\r
934 /* Disable the main PLL. */
\r
935 __HAL_RCC_PLL_DISABLE();
\r
937 /* Disable all PLL outputs to save power if no PLLs on */
\r
938 #if defined(RCC_PLLSAI1_SUPPORT) && defined(RCC_CR_PLLSAI2RDY)
\r
939 if(READ_BIT(RCC->CR, (RCC_CR_PLLSAI1RDY | RCC_CR_PLLSAI2RDY)) == 0U)
\r
941 MODIFY_REG(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC, RCC_PLLSOURCE_NONE);
\r
943 #elif defined(RCC_PLLSAI1_SUPPORT)
\r
944 if(READ_BIT(RCC->CR, RCC_CR_PLLSAI1RDY) == 0U)
\r
946 MODIFY_REG(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC, RCC_PLLSOURCE_NONE);
\r
949 MODIFY_REG(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC, RCC_PLLSOURCE_NONE);
\r
950 #endif /* RCC_PLLSAI1_SUPPORT && RCC_CR_PLLSAI2RDY */
\r
952 #if defined(RCC_PLLSAI2_SUPPORT)
\r
953 __HAL_RCC_PLLCLKOUT_DISABLE(RCC_PLL_SYSCLK | RCC_PLL_48M1CLK | RCC_PLL_SAI3CLK);
\r
954 #elif defined(RCC_PLLSAI1_SUPPORT)
\r
955 __HAL_RCC_PLLCLKOUT_DISABLE(RCC_PLL_SYSCLK | RCC_PLL_48M1CLK | RCC_PLL_SAI2CLK);
\r
957 __HAL_RCC_PLLCLKOUT_DISABLE(RCC_PLL_SYSCLK | RCC_PLL_48M1CLK);
\r
958 #endif /* RCC_PLLSAI2_SUPPORT */
\r
960 /* Get Start Tick*/
\r
961 tickstart = HAL_GetTick();
\r
963 /* Wait till PLL is disabled */
\r
964 while(READ_BIT(RCC->CR, RCC_CR_PLLRDY) != 0U)
\r
966 if((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
\r
968 return HAL_TIMEOUT;
\r
975 /* Check if there is a request to disable the PLL used as System clock source */
\r
976 if((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_OFF)
\r
982 pll_config = RCC->PLLCFGR;
\r
983 /* Do not return HAL_ERROR if request repeats the current configuration */
\r
984 if((READ_BIT(pll_config, RCC_PLLCFGR_PLLSRC) != RCC_OscInitStruct->PLL.PLLSource) ||
\r
985 (READ_BIT(pll_config, RCC_PLLCFGR_PLLM) != ((RCC_OscInitStruct->PLL.PLLM - 1U) << RCC_PLLCFGR_PLLM_Pos)) ||
\r
986 (READ_BIT(pll_config, RCC_PLLCFGR_PLLN) != (RCC_OscInitStruct->PLL.PLLN << RCC_PLLCFGR_PLLN_Pos)) ||
\r
987 #if defined(RCC_PLLP_SUPPORT)
\r
988 #if defined(RCC_PLLP_DIV_2_31_SUPPORT)
\r
989 (READ_BIT(pll_config, RCC_PLLCFGR_PLLPDIV) != (RCC_OscInitStruct->PLL.PLLP << RCC_PLLCFGR_PLLPDIV_Pos)) ||
\r
991 (READ_BIT(pll_config, RCC_PLLCFGR_PLLP) != ((RCC_OscInitStruct->PLL.PLLP == RCC_PLLP_DIV7) ? 0U : 1U)) ||
\r
994 (READ_BIT(pll_config, RCC_PLLCFGR_PLLQ) != ((((RCC_OscInitStruct->PLL.PLLQ) >> 1U) - 1U) << RCC_PLLCFGR_PLLQ_Pos)) ||
\r
995 (READ_BIT(pll_config, RCC_PLLCFGR_PLLR) != ((((RCC_OscInitStruct->PLL.PLLR) >> 1U) - 1U) << RCC_PLLCFGR_PLLR_Pos)))
\r
1006 * @brief Initialize the CPU, AHB and APB busses clocks according to the specified
\r
1007 * parameters in the RCC_ClkInitStruct.
\r
1008 * @param RCC_ClkInitStruct pointer to an RCC_OscInitTypeDef structure that
\r
1009 * contains the configuration information for the RCC peripheral.
\r
1010 * @param FLatency FLASH Latency
\r
1011 * This parameter can be one of the following values:
\r
1012 * @arg FLASH_LATENCY_0 FLASH 0 Latency cycle
\r
1013 * @arg FLASH_LATENCY_1 FLASH 1 Latency cycle
\r
1014 * @arg FLASH_LATENCY_2 FLASH 2 Latency cycles
\r
1015 * @arg FLASH_LATENCY_3 FLASH 3 Latency cycles
\r
1016 * @arg FLASH_LATENCY_4 FLASH 4 Latency cycles
\r
1018 * @arg FLASH_LATENCY_5 FLASH 5 Latency cycles
\r
1019 * @arg FLASH_LATENCY_6 FLASH 6 Latency cycles
\r
1020 * @arg FLASH_LATENCY_7 FLASH 7 Latency cycles
\r
1021 * @arg FLASH_LATENCY_8 FLASH 8 Latency cycles
\r
1022 * @arg FLASH_LATENCY_9 FLASH 9 Latency cycles
\r
1023 * @arg FLASH_LATENCY_10 FLASH 10 Latency cycles
\r
1024 * @arg FLASH_LATENCY_11 FLASH 11 Latency cycles
\r
1025 * @arg FLASH_LATENCY_12 FLASH 12 Latency cycles
\r
1026 * @arg FLASH_LATENCY_13 FLASH 13 Latency cycles
\r
1027 * @arg FLASH_LATENCY_14 FLASH 14 Latency cycles
\r
1028 * @arg FLASH_LATENCY_15 FLASH 15 Latency cycles
\r
1031 * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency
\r
1032 * and updated by HAL_RCC_GetHCLKFreq() function called within this function
\r
1034 * @note The MSI is used by default as system clock source after
\r
1035 * startup from Reset, wake-up from STANDBY mode. After restart from Reset,
\r
1036 * the MSI frequency is set to its default value 4 MHz.
\r
1038 * @note The HSI can be selected as system clock source after
\r
1039 * from STOP modes or in case of failure of the HSE used directly or indirectly
\r
1040 * as system clock (if the Clock Security System CSS is enabled).
\r
1042 * @note A switch from one clock source to another occurs only if the target
\r
1043 * clock source is ready (clock stable after startup delay or PLL locked).
\r
1044 * If a clock source which is not yet ready is selected, the switch will
\r
1045 * occur when the clock source is ready.
\r
1047 * @note You can use HAL_RCC_GetClockConfig() function to know which clock is
\r
1048 * currently used as system clock source.
\r
1050 * @note Depending on the device voltage range, the software has to set correctly
\r
1051 * HPRE[3:0] bits to ensure that HCLK not exceed the maximum allowed frequency
\r
1052 * (for more details refer to section above "Initialization/de-initialization functions")
\r
1055 HAL_StatusTypeDef HAL_RCC_ClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t FLatency)
\r
1057 uint32_t tickstart;
\r
1058 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
\r
1059 uint32_t hpre = RCC_SYSCLK_DIV1;
\r
1061 HAL_StatusTypeDef status;
\r
1063 /* Check Null pointer */
\r
1064 if(RCC_ClkInitStruct == NULL)
\r
1069 /* Check the parameters */
\r
1070 assert_param(IS_RCC_CLOCKTYPE(RCC_ClkInitStruct->ClockType));
\r
1071 assert_param(IS_FLASH_LATENCY(FLatency));
\r
1073 /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
\r
1074 must be correctly programmed according to the frequency of the CPU clock
\r
1075 (HCLK) and the supply voltage of the device. */
\r
1077 /* Increasing the number of wait states because of higher CPU frequency */
\r
1078 if(FLatency > __HAL_FLASH_GET_LATENCY())
\r
1080 /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
\r
1081 __HAL_FLASH_SET_LATENCY(FLatency);
\r
1083 /* Check that the new number of wait states is taken into account to access the Flash
\r
1084 memory by reading the FLASH_ACR register */
\r
1085 if(__HAL_FLASH_GET_LATENCY() != FLatency)
\r
1091 /*------------------------- SYSCLK Configuration ---------------------------*/
\r
1092 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_SYSCLK) == RCC_CLOCKTYPE_SYSCLK)
\r
1094 assert_param(IS_RCC_SYSCLKSOURCE(RCC_ClkInitStruct->SYSCLKSource));
\r
1096 /* PLL is selected as System Clock Source */
\r
1097 if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
\r
1099 /* Check the PLL ready flag */
\r
1100 if(READ_BIT(RCC->CR, RCC_CR_PLLRDY) == 0U)
\r
1104 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
\r
1105 /* Undershoot management when selection PLL as SYSCLK source and frequency above 80Mhz */
\r
1106 /* Compute target PLL output frequency */
\r
1107 if(RCC_GetSysClockFreqFromPLLSource() > 80000000U)
\r
1109 if(READ_BIT(RCC->CFGR, RCC_CFGR_HPRE) == RCC_SYSCLK_DIV1)
\r
1111 /* Intermediate step with HCLK prescaler 2 necessary before to go over 80Mhz */
\r
1112 MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_SYSCLK_DIV2);
\r
1113 hpre = RCC_SYSCLK_DIV2;
\r
1115 else if((((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK) && (RCC_ClkInitStruct->AHBCLKDivider == RCC_SYSCLK_DIV1))
\r
1117 /* Intermediate step with HCLK prescaler 2 necessary before to go over 80Mhz */
\r
1118 MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_SYSCLK_DIV2);
\r
1119 hpre = RCC_SYSCLK_DIV2;
\r
1123 /* nothing to do */
\r
1130 /* HSE is selected as System Clock Source */
\r
1131 if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
\r
1133 /* Check the HSE ready flag */
\r
1134 if(READ_BIT(RCC->CR, RCC_CR_HSERDY) == 0U)
\r
1139 /* MSI is selected as System Clock Source */
\r
1140 else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_MSI)
\r
1142 /* Check the MSI ready flag */
\r
1143 if(READ_BIT(RCC->CR, RCC_CR_MSIRDY) == 0U)
\r
1148 /* HSI is selected as System Clock Source */
\r
1151 /* Check the HSI ready flag */
\r
1152 if(READ_BIT(RCC->CR, RCC_CR_HSIRDY) == 0U)
\r
1157 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
\r
1158 /* Overshoot management when going down from PLL as SYSCLK source and frequency above 80Mhz */
\r
1159 if(HAL_RCC_GetSysClockFreq() > 80000000U)
\r
1161 /* Intermediate step with HCLK prescaler 2 necessary before to go under 80Mhz */
\r
1162 MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_SYSCLK_DIV2);
\r
1163 hpre = RCC_SYSCLK_DIV2;
\r
1169 MODIFY_REG(RCC->CFGR, RCC_CFGR_SW, RCC_ClkInitStruct->SYSCLKSource);
\r
1171 /* Get Start Tick*/
\r
1172 tickstart = HAL_GetTick();
\r
1174 while(__HAL_RCC_GET_SYSCLK_SOURCE() != (RCC_ClkInitStruct->SYSCLKSource << RCC_CFGR_SWS_Pos))
\r
1176 if((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
\r
1178 return HAL_TIMEOUT;
\r
1183 /*-------------------------- HCLK Configuration --------------------------*/
\r
1184 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
\r
1186 assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
\r
1187 MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
\r
1189 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
\r
1192 /* Is intermediate HCLK prescaler 2 applied internally, complete with HCLK prescaler 1 */
\r
1193 if(hpre == RCC_SYSCLK_DIV2)
\r
1195 MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_SYSCLK_DIV1);
\r
1200 /* Decreasing the number of wait states because of lower CPU frequency */
\r
1201 if(FLatency < __HAL_FLASH_GET_LATENCY())
\r
1203 /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
\r
1204 __HAL_FLASH_SET_LATENCY(FLatency);
\r
1206 /* Check that the new number of wait states is taken into account to access the Flash
\r
1207 memory by reading the FLASH_ACR register */
\r
1208 if(__HAL_FLASH_GET_LATENCY() != FLatency)
\r
1214 /*-------------------------- PCLK1 Configuration ---------------------------*/
\r
1215 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
\r
1217 assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB1CLKDivider));
\r
1218 MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE1, RCC_ClkInitStruct->APB1CLKDivider);
\r
1221 /*-------------------------- PCLK2 Configuration ---------------------------*/
\r
1222 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2)
\r
1224 assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB2CLKDivider));
\r
1225 MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE2, ((RCC_ClkInitStruct->APB2CLKDivider) << 3U));
\r
1228 /* Update the SystemCoreClock global variable */
\r
1229 SystemCoreClock = HAL_RCC_GetSysClockFreq() >> (AHBPrescTable[READ_BIT(RCC->CFGR, RCC_CFGR_HPRE) >> RCC_CFGR_HPRE_Pos] & 0x1FU);
\r
1231 /* Configure the source of time base considering new system clocks settings*/
\r
1232 status = HAL_InitTick(uwTickPrio);
\r
1241 /** @defgroup RCC_Exported_Functions_Group2 Peripheral Control functions
\r
1242 * @brief RCC clocks control functions
\r
1245 ===============================================================================
\r
1246 ##### Peripheral Control functions #####
\r
1247 ===============================================================================
\r
1249 This subsection provides a set of functions allowing to:
\r
1251 (+) Ouput clock to MCO pin.
\r
1252 (+) Retrieve current clock frequencies.
\r
1253 (+) Enable the Clock Security System.
\r
1260 * @brief Select the clock source to output on MCO pin(PA8).
\r
1261 * @note PA8 should be configured in alternate function mode.
\r
1262 * @param RCC_MCOx specifies the output direction for the clock source.
\r
1263 * For STM32L4xx family this parameter can have only one value:
\r
1264 * @arg @ref RCC_MCO1 Clock source to output on MCO1 pin(PA8).
\r
1265 * @param RCC_MCOSource specifies the clock source to output.
\r
1266 * This parameter can be one of the following values:
\r
1267 * @arg @ref RCC_MCO1SOURCE_NOCLOCK MCO output disabled, no clock on MCO
\r
1268 * @arg @ref RCC_MCO1SOURCE_SYSCLK system clock selected as MCO source
\r
1269 * @arg @ref RCC_MCO1SOURCE_MSI MSI clock selected as MCO source
\r
1270 * @arg @ref RCC_MCO1SOURCE_HSI HSI clock selected as MCO source
\r
1271 * @arg @ref RCC_MCO1SOURCE_HSE HSE clock selected as MCO sourcee
\r
1272 * @arg @ref RCC_MCO1SOURCE_PLLCLK main PLL clock selected as MCO source
\r
1273 * @arg @ref RCC_MCO1SOURCE_LSI LSI clock selected as MCO source
\r
1274 * @arg @ref RCC_MCO1SOURCE_LSE LSE clock selected as MCO source
\r
1276 * @arg @ref RCC_MCO1SOURCE_HSI48 HSI48 clock selected as MCO source for devices with HSI48
\r
1278 * @param RCC_MCODiv specifies the MCO prescaler.
\r
1279 * This parameter can be one of the following values:
\r
1280 * @arg @ref RCC_MCODIV_1 no division applied to MCO clock
\r
1281 * @arg @ref RCC_MCODIV_2 division by 2 applied to MCO clock
\r
1282 * @arg @ref RCC_MCODIV_4 division by 4 applied to MCO clock
\r
1283 * @arg @ref RCC_MCODIV_8 division by 8 applied to MCO clock
\r
1284 * @arg @ref RCC_MCODIV_16 division by 16 applied to MCO clock
\r
1287 void HAL_RCC_MCOConfig( uint32_t RCC_MCOx, uint32_t RCC_MCOSource, uint32_t RCC_MCODiv)
\r
1289 GPIO_InitTypeDef GPIO_InitStruct;
\r
1291 /* Check the parameters */
\r
1292 assert_param(IS_RCC_MCO(RCC_MCOx));
\r
1293 assert_param(IS_RCC_MCODIV(RCC_MCODiv));
\r
1294 assert_param(IS_RCC_MCO1SOURCE(RCC_MCOSource));
\r
1296 /* Prevent unused argument(s) compilation warning if no assert_param check */
\r
1299 /* MCO Clock Enable */
\r
1300 __MCO1_CLK_ENABLE();
\r
1302 /* Configue the MCO1 pin in alternate function mode */
\r
1303 GPIO_InitStruct.Pin = MCO1_PIN;
\r
1304 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
\r
1305 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
\r
1306 GPIO_InitStruct.Pull = GPIO_NOPULL;
\r
1307 GPIO_InitStruct.Alternate = GPIO_AF0_MCO;
\r
1308 HAL_GPIO_Init(MCO1_GPIO_PORT, &GPIO_InitStruct);
\r
1310 /* Mask MCOSEL[] and MCOPRE[] bits then set MCO1 clock source and prescaler */
\r
1311 MODIFY_REG(RCC->CFGR, (RCC_CFGR_MCOSEL | RCC_CFGR_MCOPRE), (RCC_MCOSource | RCC_MCODiv ));
\r
1315 * @brief Return the SYSCLK frequency.
\r
1317 * @note The system frequency computed by this function is not the real
\r
1318 * frequency in the chip. It is calculated based on the predefined
\r
1319 * constant and the selected clock source:
\r
1320 * @note If SYSCLK source is MSI, function returns values based on MSI
\r
1321 * Value as defined by the MSI range.
\r
1322 * @note If SYSCLK source is HSI, function returns values based on HSI_VALUE(*)
\r
1323 * @note If SYSCLK source is HSE, function returns values based on HSE_VALUE(**)
\r
1324 * @note If SYSCLK source is PLL, function returns values based on HSE_VALUE(**),
\r
1325 * HSI_VALUE(*) or MSI Value multiplied/divided by the PLL factors.
\r
1326 * @note (*) HSI_VALUE is a constant defined in stm32l4xx_hal_conf.h file (default value
\r
1327 * 16 MHz) but the real value may vary depending on the variations
\r
1328 * in voltage and temperature.
\r
1329 * @note (**) HSE_VALUE is a constant defined in stm32l4xx_hal_conf.h file (default value
\r
1330 * 8 MHz), user has to ensure that HSE_VALUE is same as the real
\r
1331 * frequency of the crystal used. Otherwise, this function may
\r
1332 * have wrong result.
\r
1334 * @note The result of this function could be not correct when using fractional
\r
1335 * value for HSE crystal.
\r
1337 * @note This function can be used by the user application to compute the
\r
1338 * baudrate for the communication peripherals or configure other parameters.
\r
1340 * @note Each time SYSCLK changes, this function must be called to update the
\r
1341 * right SYSCLK value. Otherwise, any configuration based on this function will be incorrect.
\r
1344 * @retval SYSCLK frequency
\r
1346 uint32_t HAL_RCC_GetSysClockFreq(void)
\r
1348 uint32_t msirange = 0U, sysclockfreq = 0U;
\r
1349 uint32_t pllvco, pllsource, pllr, pllm; /* no init needed */
\r
1350 uint32_t sysclk_source, pll_oscsource;
\r
1352 sysclk_source = __HAL_RCC_GET_SYSCLK_SOURCE();
\r
1353 pll_oscsource = __HAL_RCC_GET_PLL_OSCSOURCE();
\r
1355 if((sysclk_source == RCC_CFGR_SWS_MSI) ||
\r
1356 ((sysclk_source == RCC_CFGR_SWS_PLL) && (pll_oscsource == RCC_PLLSOURCE_MSI)))
\r
1358 /* MSI or PLL with MSI source used as system clock source */
\r
1360 /* Get SYSCLK source */
\r
1361 if(READ_BIT(RCC->CR, RCC_CR_MSIRGSEL) == 0U)
\r
1362 { /* MSISRANGE from RCC_CSR applies */
\r
1363 msirange = READ_BIT(RCC->CSR, RCC_CSR_MSISRANGE) >> RCC_CSR_MSISRANGE_Pos;
\r
1366 { /* MSIRANGE from RCC_CR applies */
\r
1367 msirange = READ_BIT(RCC->CR, RCC_CR_MSIRANGE) >> RCC_CR_MSIRANGE_Pos;
\r
1369 /*MSI frequency range in HZ*/
\r
1370 msirange = MSIRangeTable[msirange];
\r
1372 if(sysclk_source == RCC_CFGR_SWS_MSI)
\r
1374 /* MSI used as system clock source */
\r
1375 sysclockfreq = msirange;
\r
1378 else if(sysclk_source == RCC_CFGR_SWS_HSI)
\r
1380 /* HSI used as system clock source */
\r
1381 sysclockfreq = HSI_VALUE;
\r
1383 else if(sysclk_source == RCC_CFGR_SWS_HSE)
\r
1385 /* HSE used as system clock source */
\r
1386 sysclockfreq = HSE_VALUE;
\r
1390 /* unexpected case: sysclockfreq at 0 */
\r
1393 if(sysclk_source == RCC_CFGR_SWS_PLL)
\r
1395 /* PLL used as system clock source */
\r
1397 /* PLL_VCO = (HSE_VALUE or HSI_VALUE or MSI_VALUE) * PLLN / PLLM
\r
1398 SYSCLK = PLL_VCO / PLLR
\r
1400 pllsource = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC);
\r
1402 switch (pllsource)
\r
1404 case RCC_PLLSOURCE_HSI: /* HSI used as PLL clock source */
\r
1405 pllvco = HSI_VALUE;
\r
1408 case RCC_PLLSOURCE_HSE: /* HSE used as PLL clock source */
\r
1409 pllvco = HSE_VALUE;
\r
1412 case RCC_PLLSOURCE_MSI: /* MSI used as PLL clock source */
\r
1414 pllvco = msirange;
\r
1417 pllm = (READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1U ;
\r
1418 pllvco = (pllvco * (READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos)) / pllm;
\r
1419 pllr = ((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLR) >> RCC_PLLCFGR_PLLR_Pos) + 1U ) * 2U;
\r
1420 sysclockfreq = pllvco / pllr;
\r
1423 return sysclockfreq;
\r
1427 * @brief Return the HCLK frequency.
\r
1428 * @note Each time HCLK changes, this function must be called to update the
\r
1429 * right HCLK value. Otherwise, any configuration based on this function will be incorrect.
\r
1431 * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency.
\r
1432 * @retval HCLK frequency in Hz
\r
1434 uint32_t HAL_RCC_GetHCLKFreq(void)
\r
1436 return SystemCoreClock;
\r
1440 * @brief Return the PCLK1 frequency.
\r
1441 * @note Each time PCLK1 changes, this function must be called to update the
\r
1442 * right PCLK1 value. Otherwise, any configuration based on this function will be incorrect.
\r
1443 * @retval PCLK1 frequency in Hz
\r
1445 uint32_t HAL_RCC_GetPCLK1Freq(void)
\r
1447 /* Get HCLK source and Compute PCLK1 frequency ---------------------------*/
\r
1448 return (HAL_RCC_GetHCLKFreq() >> (APBPrescTable[READ_BIT(RCC->CFGR, RCC_CFGR_PPRE1) >> RCC_CFGR_PPRE1_Pos] & 0x1FU));
\r
1452 * @brief Return the PCLK2 frequency.
\r
1453 * @note Each time PCLK2 changes, this function must be called to update the
\r
1454 * right PCLK2 value. Otherwise, any configuration based on this function will be incorrect.
\r
1455 * @retval PCLK2 frequency in Hz
\r
1457 uint32_t HAL_RCC_GetPCLK2Freq(void)
\r
1459 /* Get HCLK source and Compute PCLK2 frequency ---------------------------*/
\r
1460 return (HAL_RCC_GetHCLKFreq()>> (APBPrescTable[READ_BIT(RCC->CFGR, RCC_CFGR_PPRE2) >> RCC_CFGR_PPRE2_Pos] & 0x1FU));
\r
1464 * @brief Configure the RCC_OscInitStruct according to the internal
\r
1465 * RCC configuration registers.
\r
1466 * @param RCC_OscInitStruct pointer to an RCC_OscInitTypeDef structure that
\r
1467 * will be configured.
\r
1470 void HAL_RCC_GetOscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
\r
1472 /* Check the parameters */
\r
1473 assert_param(RCC_OscInitStruct != (void *)NULL);
\r
1475 /* Set all possible values for the Oscillator type parameter ---------------*/
\r
1476 #if defined(RCC_HSI48_SUPPORT)
\r
1477 RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_MSI | \
\r
1478 RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_HSI48;
\r
1480 RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_MSI | \
\r
1481 RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_LSI;
\r
1482 #endif /* RCC_HSI48_SUPPORT */
\r
1484 /* Get the HSE configuration -----------------------------------------------*/
\r
1485 if(READ_BIT(RCC->CR, RCC_CR_HSEBYP) == RCC_CR_HSEBYP)
\r
1487 RCC_OscInitStruct->HSEState = RCC_HSE_BYPASS;
\r
1489 else if(READ_BIT(RCC->CR, RCC_CR_HSEON) == RCC_CR_HSEON)
\r
1491 RCC_OscInitStruct->HSEState = RCC_HSE_ON;
\r
1495 RCC_OscInitStruct->HSEState = RCC_HSE_OFF;
\r
1498 /* Get the MSI configuration -----------------------------------------------*/
\r
1499 if(READ_BIT(RCC->CR, RCC_CR_MSION) == RCC_CR_MSION)
\r
1501 RCC_OscInitStruct->MSIState = RCC_MSI_ON;
\r
1505 RCC_OscInitStruct->MSIState = RCC_MSI_OFF;
\r
1508 RCC_OscInitStruct->MSICalibrationValue = READ_BIT(RCC->ICSCR, RCC_ICSCR_MSITRIM) >> RCC_ICSCR_MSITRIM_Pos;
\r
1509 RCC_OscInitStruct->MSIClockRange = READ_BIT(RCC->CR, RCC_CR_MSIRANGE);
\r
1511 /* Get the HSI configuration -----------------------------------------------*/
\r
1512 if(READ_BIT(RCC->CR, RCC_CR_HSION) == RCC_CR_HSION)
\r
1514 RCC_OscInitStruct->HSIState = RCC_HSI_ON;
\r
1518 RCC_OscInitStruct->HSIState = RCC_HSI_OFF;
\r
1521 RCC_OscInitStruct->HSICalibrationValue = READ_BIT(RCC->ICSCR, RCC_ICSCR_HSITRIM) >> RCC_ICSCR_HSITRIM_Pos;
\r
1523 /* Get the LSE configuration -----------------------------------------------*/
\r
1524 if(READ_BIT(RCC->BDCR, RCC_BDCR_LSEBYP) == RCC_BDCR_LSEBYP)
\r
1526 #if defined(RCC_BDCR_LSESYSDIS)
\r
1527 if((RCC->BDCR & RCC_BDCR_LSESYSDIS) == RCC_BDCR_LSESYSDIS)
\r
1529 RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS_RTC_ONLY;
\r
1532 #endif /* RCC_BDCR_LSESYSDIS */
\r
1534 RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS;
\r
1537 else if(READ_BIT(RCC->BDCR, RCC_BDCR_LSEON) == RCC_BDCR_LSEON)
\r
1539 #if defined(RCC_BDCR_LSESYSDIS)
\r
1540 if((RCC->BDCR & RCC_BDCR_LSESYSDIS) == RCC_BDCR_LSESYSDIS)
\r
1542 RCC_OscInitStruct->LSEState = RCC_LSE_ON_RTC_ONLY;
\r
1545 #endif /* RCC_BDCR_LSESYSDIS */
\r
1547 RCC_OscInitStruct->LSEState = RCC_LSE_ON;
\r
1552 RCC_OscInitStruct->LSEState = RCC_LSE_OFF;
\r
1555 /* Get the LSI configuration -----------------------------------------------*/
\r
1556 if(READ_BIT(RCC->CSR, RCC_CSR_LSION) == RCC_CSR_LSION)
\r
1558 RCC_OscInitStruct->LSIState = RCC_LSI_ON;
\r
1562 RCC_OscInitStruct->LSIState = RCC_LSI_OFF;
\r
1564 #if defined(RCC_CSR_LSIPREDIV)
\r
1566 /* Get the LSI configuration -----------------------------------------------*/
\r
1567 if((RCC->CSR & RCC_CSR_LSIPREDIV) == RCC_CSR_LSIPREDIV)
\r
1569 RCC_OscInitStruct->LSIDiv = RCC_LSI_DIV128;
\r
1573 RCC_OscInitStruct->LSIDiv = RCC_LSI_DIV1;
\r
1575 #endif /* RCC_CSR_LSIPREDIV */
\r
1577 #if defined(RCC_HSI48_SUPPORT)
\r
1578 /* Get the HSI48 configuration ---------------------------------------------*/
\r
1579 if(READ_BIT(RCC->CRRCR, RCC_CRRCR_HSI48ON) == RCC_CRRCR_HSI48ON)
\r
1581 RCC_OscInitStruct->HSI48State = RCC_HSI48_ON;
\r
1585 RCC_OscInitStruct->HSI48State = RCC_HSI48_OFF;
\r
1588 RCC_OscInitStruct->HSI48State = RCC_HSI48_OFF;
\r
1589 #endif /* RCC_HSI48_SUPPORT */
\r
1591 /* Get the PLL configuration -----------------------------------------------*/
\r
1592 if(READ_BIT(RCC->CR, RCC_CR_PLLON) == RCC_CR_PLLON)
\r
1594 RCC_OscInitStruct->PLL.PLLState = RCC_PLL_ON;
\r
1598 RCC_OscInitStruct->PLL.PLLState = RCC_PLL_OFF;
\r
1600 RCC_OscInitStruct->PLL.PLLSource = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC);
\r
1601 RCC_OscInitStruct->PLL.PLLM = (READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1U;
\r
1602 RCC_OscInitStruct->PLL.PLLN = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos;
\r
1603 RCC_OscInitStruct->PLL.PLLQ = (((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLQ) >> RCC_PLLCFGR_PLLQ_Pos) + 1U) << 1U);
\r
1604 RCC_OscInitStruct->PLL.PLLR = (((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLR) >> RCC_PLLCFGR_PLLR_Pos) + 1U) << 1U);
\r
1605 #if defined(RCC_PLLP_SUPPORT)
\r
1606 #if defined(RCC_PLLP_DIV_2_31_SUPPORT)
\r
1607 RCC_OscInitStruct->PLL.PLLP = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLPDIV) >> RCC_PLLCFGR_PLLPDIV_Pos;
\r
1609 if(READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLP) != 0U)
\r
1611 RCC_OscInitStruct->PLL.PLLP = RCC_PLLP_DIV17;
\r
1615 RCC_OscInitStruct->PLL.PLLP = RCC_PLLP_DIV7;
\r
1617 #endif /* RCC_PLLP_DIV_2_31_SUPPORT */
\r
1618 #endif /* RCC_PLLP_SUPPORT */
\r
1622 * @brief Configure the RCC_ClkInitStruct according to the internal
\r
1623 * RCC configuration registers.
\r
1624 * @param RCC_ClkInitStruct pointer to an RCC_ClkInitTypeDef structure that
\r
1625 * will be configured.
\r
1626 * @param pFLatency Pointer on the Flash Latency.
\r
1629 void HAL_RCC_GetClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t *pFLatency)
\r
1631 /* Check the parameters */
\r
1632 assert_param(RCC_ClkInitStruct != (void *)NULL);
\r
1633 assert_param(pFLatency != (void *)NULL);
\r
1635 /* Set all possible values for the Clock type parameter --------------------*/
\r
1636 RCC_ClkInitStruct->ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
\r
1638 /* Get the SYSCLK configuration --------------------------------------------*/
\r
1639 RCC_ClkInitStruct->SYSCLKSource = READ_BIT(RCC->CFGR, RCC_CFGR_SW);
\r
1641 /* Get the HCLK configuration ----------------------------------------------*/
\r
1642 RCC_ClkInitStruct->AHBCLKDivider = READ_BIT(RCC->CFGR, RCC_CFGR_HPRE);
\r
1644 /* Get the APB1 configuration ----------------------------------------------*/
\r
1645 RCC_ClkInitStruct->APB1CLKDivider = READ_BIT(RCC->CFGR, RCC_CFGR_PPRE1);
\r
1647 /* Get the APB2 configuration ----------------------------------------------*/
\r
1648 RCC_ClkInitStruct->APB2CLKDivider = (READ_BIT(RCC->CFGR, RCC_CFGR_PPRE2) >> 3U);
\r
1650 /* Get the Flash Wait State (Latency) configuration ------------------------*/
\r
1651 *pFLatency = __HAL_FLASH_GET_LATENCY();
\r
1655 * @brief Enable the Clock Security System.
\r
1656 * @note If a failure is detected on the HSE oscillator clock, this oscillator
\r
1657 * is automatically disabled and an interrupt is generated to inform the
\r
1658 * software about the failure (Clock Security System Interrupt, CSSI),
\r
1659 * allowing the MCU to perform rescue operations. The CSSI is linked to
\r
1660 * the Cortex-M4 NMI (Non-Maskable Interrupt) exception vector.
\r
1661 * @note The Clock Security System can only be cleared by reset.
\r
1664 void HAL_RCC_EnableCSS(void)
\r
1666 SET_BIT(RCC->CR, RCC_CR_CSSON) ;
\r
1670 * @brief Handle the RCC Clock Security System interrupt request.
\r
1671 * @note This API should be called under the NMI_Handler().
\r
1674 void HAL_RCC_NMI_IRQHandler(void)
\r
1676 /* Check RCC CSSF interrupt flag */
\r
1677 if(__HAL_RCC_GET_IT(RCC_IT_CSS))
\r
1679 /* RCC Clock Security System interrupt user callback */
\r
1680 HAL_RCC_CSSCallback();
\r
1682 /* Clear RCC CSS pending bit */
\r
1683 __HAL_RCC_CLEAR_IT(RCC_IT_CSS);
\r
1688 * @brief RCC Clock Security System interrupt callback.
\r
1691 __weak void HAL_RCC_CSSCallback(void)
\r
1693 /* NOTE : This function should not be modified, when the callback is needed,
\r
1694 the HAL_RCC_CSSCallback should be implemented in the user file
\r
1706 /* Private function prototypes -----------------------------------------------*/
\r
1707 /** @addtogroup RCC_Private_Functions
\r
1711 * @brief Update number of Flash wait states in line with MSI range and current
\r
1713 * @param msirange MSI range value from RCC_MSIRANGE_0 to RCC_MSIRANGE_11
\r
1714 * @retval HAL status
\r
1716 static HAL_StatusTypeDef RCC_SetFlashLatencyFromMSIRange(uint32_t msirange)
\r
1719 uint32_t latency = FLASH_LATENCY_0; /* default value 0WS */
\r
1721 if(__HAL_RCC_PWR_IS_CLK_ENABLED())
\r
1723 vos = HAL_PWREx_GetVoltageRange();
\r
1727 __HAL_RCC_PWR_CLK_ENABLE();
\r
1728 vos = HAL_PWREx_GetVoltageRange();
\r
1729 __HAL_RCC_PWR_CLK_DISABLE();
\r
1732 if(vos == PWR_REGULATOR_VOLTAGE_SCALE1)
\r
1734 if(msirange > RCC_MSIRANGE_8)
\r
1737 if(msirange > RCC_MSIRANGE_10)
\r
1740 latency = FLASH_LATENCY_2; /* 2WS */
\r
1744 /* MSI 24Mhz or 32Mhz */
\r
1745 latency = FLASH_LATENCY_1; /* 1WS */
\r
1748 /* else MSI <= 16Mhz default FLASH_LATENCY_0 0WS */
\r
1752 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
\r
1753 if(msirange >= RCC_MSIRANGE_8)
\r
1755 /* MSI >= 16Mhz */
\r
1756 latency = FLASH_LATENCY_2; /* 2WS */
\r
1760 if(msirange == RCC_MSIRANGE_7)
\r
1763 latency = FLASH_LATENCY_1; /* 1WS */
\r
1765 /* else MSI < 8Mhz default FLASH_LATENCY_0 0WS */
\r
1768 if(msirange > RCC_MSIRANGE_8)
\r
1771 latency = FLASH_LATENCY_3; /* 3WS */
\r
1775 if(msirange == RCC_MSIRANGE_8)
\r
1778 latency = FLASH_LATENCY_2; /* 2WS */
\r
1780 else if(msirange == RCC_MSIRANGE_7)
\r
1783 latency = FLASH_LATENCY_1; /* 1WS */
\r
1785 /* else MSI < 8Mhz default FLASH_LATENCY_0 0WS */
\r
1790 __HAL_FLASH_SET_LATENCY(latency);
\r
1792 /* Check that the new number of wait states is taken into account to access the Flash
\r
1793 memory by reading the FLASH_ACR register */
\r
1794 if(__HAL_FLASH_GET_LATENCY() != latency)
\r
1802 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
\r
1804 * @brief Compute SYSCLK frequency based on PLL SYSCLK source.
\r
1805 * @retval SYSCLK frequency
\r
1807 static uint32_t RCC_GetSysClockFreqFromPLLSource(void)
\r
1809 uint32_t msirange = 0U;
\r
1810 uint32_t pllvco, pllsource, pllr, pllm, sysclockfreq; /* no init needed */
\r
1812 if(__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_MSI)
\r
1814 /* Get MSI range source */
\r
1815 if(READ_BIT(RCC->CR, RCC_CR_MSIRGSEL) == 0U)
\r
1816 { /* MSISRANGE from RCC_CSR applies */
\r
1817 msirange = READ_BIT(RCC->CSR, RCC_CSR_MSISRANGE) >> RCC_CSR_MSISRANGE_Pos;
\r
1820 { /* MSIRANGE from RCC_CR applies */
\r
1821 msirange = READ_BIT(RCC->CR, RCC_CR_MSIRANGE) >> RCC_CR_MSIRANGE_Pos;
\r
1823 /*MSI frequency range in HZ*/
\r
1824 msirange = MSIRangeTable[msirange];
\r
1827 /* PLL_VCO = (HSE_VALUE or HSI_VALUE or MSI_VALUE) * PLLN / PLLM
\r
1828 SYSCLK = PLL_VCO / PLLR
\r
1830 pllsource = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC);
\r
1832 switch (pllsource)
\r
1834 case RCC_PLLSOURCE_HSI: /* HSI used as PLL clock source */
\r
1835 pllvco = HSI_VALUE;
\r
1838 case RCC_PLLSOURCE_HSE: /* HSE used as PLL clock source */
\r
1839 pllvco = HSE_VALUE;
\r
1842 case RCC_PLLSOURCE_MSI: /* MSI used as PLL clock source */
\r
1844 pllvco = msirange;
\r
1847 pllm = (READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1U ;
\r
1848 pllvco = (pllvco * (READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos)) / pllm;
\r
1849 pllr = ((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLR) >> RCC_PLLCFGR_PLLR_Pos) + 1U ) * 2U;
\r
1850 sysclockfreq = pllvco / pllr;
\r
1852 return sysclockfreq;
\r
1854 #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
\r
1860 #endif /* HAL_RCC_MODULE_ENABLED */
\r
1869 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
\r