2 ******************************************************************************
\r
3 * @file stm32f4xx_hal_rcc.c
\r
4 * @author MCD Application Team
\r
6 * @date 18-February-2014
\r
7 * @brief RCC HAL module driver.
\r
8 * This file provides firmware functions to manage the following
\r
9 * functionalities of the Reset and Clock Control (RCC) peripheral:
\r
10 * + Initialization and de-initialization functions
\r
11 * + Peripheral Control functions
\r
14 ==============================================================================
\r
15 ##### RCC specific features #####
\r
16 ==============================================================================
\r
18 After reset the device is running from Internal High Speed oscillator
\r
19 (HSI 16MHz) with Flash 0 wait state, Flash prefetch buffer, D-Cache
\r
20 and I-Cache are disabled, and all peripherals are off except internal
\r
21 SRAM, Flash and JTAG.
\r
22 (+) There is no prescaler on High speed (AHB) and Low speed (APB) busses;
\r
23 all peripherals mapped on these busses are running at HSI speed.
\r
24 (+) The clock for all peripherals is switched off, except the SRAM and FLASH.
\r
25 (+) All GPIOs are in input floating state, except the JTAG pins which
\r
26 are assigned to be used for debug purpose.
\r
29 Once the device started from reset, the user application has to:
\r
30 (+) Configure the clock source to be used to drive the System clock
\r
31 (if the application needs higher frequency/performance)
\r
32 (+) Configure the System clock frequency and Flash settings
\r
33 (+) Configure the AHB and APB busses prescalers
\r
34 (+) Enable the clock for the peripheral(s) to be used
\r
35 (+) Configure the clock source(s) for peripherals which clocks are not
\r
36 derived from the System clock (I2S, RTC, ADC, USB OTG FS/SDIO/RNG)
\r
39 ******************************************************************************
\r
42 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
\r
44 * Redistribution and use in source and binary forms, with or without modification,
\r
45 * are permitted provided that the following conditions are met:
\r
46 * 1. Redistributions of source code must retain the above copyright notice,
\r
47 * this list of conditions and the following disclaimer.
\r
48 * 2. Redistributions in binary form must reproduce the above copyright notice,
\r
49 * this list of conditions and the following disclaimer in the documentation
\r
50 * and/or other materials provided with the distribution.
\r
51 * 3. Neither the name of STMicroelectronics nor the names of its contributors
\r
52 * may be used to endorse or promote products derived from this software
\r
53 * without specific prior written permission.
\r
55 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
\r
56 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
\r
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
\r
58 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
\r
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
\r
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
\r
61 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
\r
62 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
\r
63 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
\r
64 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
66 ******************************************************************************
\r
69 /* Includes ------------------------------------------------------------------*/
\r
70 #include "stm32f4xx_hal.h"
\r
72 /** @addtogroup STM32F4xx_HAL_Driver
\r
77 * @brief RCC HAL module driver
\r
81 #ifdef HAL_RCC_MODULE_ENABLED
\r
83 /* Private typedef -----------------------------------------------------------*/
\r
84 /* Private define ------------------------------------------------------------*/
\r
85 #define HSE_TIMEOUT_VALUE HSE_STARTUP_TIMEOUT
\r
86 #define HSI_TIMEOUT_VALUE ((uint32_t)100) /* 100 ms */
\r
87 #define LSI_TIMEOUT_VALUE ((uint32_t)100) /* 100 ms */
\r
88 #define PLL_TIMEOUT_VALUE ((uint32_t)100) /* 100 ms */
\r
89 #define CLOCKSWITCH_TIMEOUT_VALUE ((uint32_t)5000) /* 5 s */
\r
91 /* Private macro -------------------------------------------------------------*/
\r
92 #define __MCO1_CLK_ENABLE() __GPIOA_CLK_ENABLE()
\r
93 #define MCO1_GPIO_PORT GPIOA
\r
94 #define MCO1_PIN GPIO_PIN_8
\r
96 #define __MCO2_CLK_ENABLE() __GPIOC_CLK_ENABLE()
\r
97 #define MCO2_GPIO_PORT GPIOC
\r
98 #define MCO2_PIN GPIO_PIN_9
\r
100 /* Private variables ---------------------------------------------------------*/
\r
101 const uint8_t APBAHBPrescTable[16] = {0, 0, 0, 0, 1, 2, 3, 4, 1, 2, 3, 4, 6, 7, 8, 9};
\r
103 /* Private function prototypes -----------------------------------------------*/
\r
104 /* Private functions ---------------------------------------------------------*/
\r
106 /** @defgroup RCC_Private_Functions
\r
110 /** @defgroup RCC_Group1 Initialization and de-initialization functions
\r
111 * @brief Initialization and Configuration functions
\r
114 ===============================================================================
\r
115 ##### Initialization and de-initialization functions #####
\r
116 ===============================================================================
\r
118 This section provide functions allowing to configure the internal/external oscillators
\r
119 (HSE, HSI, LSE, LSI, PLL, CSS and MCO) and the System busses clocks (SYSCLK, AHB, APB1
\r
122 [..] Internal/external clock and PLL configuration
\r
123 (#) HSI (high-speed internal), 16 MHz factory-trimmed RC used directly or through
\r
124 the PLL as System clock source.
\r
126 (#) LSI (low-speed internal), 32 KHz low consumption RC used as IWDG and/or RTC
\r
129 (#) HSE (high-speed external), 4 to 26 MHz crystal oscillator used directly or
\r
130 through the PLL as System clock source. Can be used also as RTC clock source.
\r
132 (#) LSE (low-speed external), 32 KHz oscillator used as RTC clock source.
\r
134 (#) PLL (clocked by HSI or HSE), featuring two different output clocks:
\r
135 (++) The first output is used to generate the high speed system clock (up to 168 MHz)
\r
136 (++) The second output is used to generate the clock for the USB OTG FS (48 MHz),
\r
137 the random analog generator (<=48 MHz) and the SDIO (<= 48 MHz).
\r
139 (#) CSS (Clock security system), once enable using the macro __HAL_RCC_CSS_ENABLE()
\r
140 and if a HSE clock failure occurs(HSE used directly or through PLL as System
\r
141 clock source), the System clockis automatically switched to HSI and an interrupt
\r
142 is generated if enabled. The interrupt is linked to the Cortex-M4 NMI
\r
143 (Non-Maskable Interrupt) exception vector.
\r
145 (#) MCO1 (microcontroller clock output), used to output HSI, LSE, HSE or PLL
\r
146 clock (through a configurable prescaler) on PA8 pin.
\r
148 (#) MCO2 (microcontroller clock output), used to output HSE, PLL, SYSCLK or PLLI2S
\r
149 clock (through a configurable prescaler) on PC9 pin.
\r
151 [..] System, AHB and APB busses clocks configuration
\r
152 (#) Several clock sources can be used to drive the System clock (SYSCLK): HSI,
\r
154 The AHB clock (HCLK) is derived from System clock through configurable
\r
155 prescaler and used to clock the CPU, memory and peripherals mapped
\r
156 on AHB bus (DMA, GPIO...). APB1 (PCLK1) and APB2 (PCLK2) clocks are derived
\r
157 from AHB clock through configurable prescalers and used to clock
\r
158 the peripherals mapped on these busses. You can use
\r
159 "HAL_RCC_GetSysClockFreq()" function to retrieve the frequencies of these clocks.
\r
161 -@- All the peripheral clocks are derived from the System clock (SYSCLK) except:
\r
162 (+@) I2S: the I2S clock can be derived either from a specific PLL (PLLI2S) or
\r
163 from an external clock mapped on the I2S_CKIN pin.
\r
164 You have to use __HAL_RCC_PLLI2S_CONFIG() macro to configure this clock.
\r
165 (+@) SAI: the SAI clock can be derived either from a specific PLL (PLLI2S) or (PLLSAI) or
\r
166 from an external clock mapped on the I2S_CKIN pin.
\r
167 You have to use __HAL_RCC_PLLI2S_CONFIG() macro to configure this clock.
\r
168 (+@) RTC: the RTC clock can be derived either from the LSI, LSE or HSE clock
\r
169 divided by 2 to 31. You have to use __HAL_RCC_RTC_CONFIG() and __HAL_RCC_RTC_ENABLE()
\r
170 macros to configure this clock.
\r
171 (+@) USB OTG FS, SDIO and RTC: USB OTG FS require a frequency equal to 48 MHz
\r
172 to work correctly, while the SDIO require a frequency equal or lower than
\r
173 to 48. This clock is derived of the main PLL through PLLQ divider.
\r
174 (+@) IWDG clock which is always the LSI clock.
\r
176 (#) For the STM32F405xx/07xx and STM32F415xx/17xx devices, the maximum
\r
177 frequency of the SYSCLK and HCLK is 168 MHz, PCLK2 84 MHz and PCLK1 42 MHz.
\r
178 Depending on the device voltage range, the maximum frequency should
\r
179 be adapted accordingly:
\r
180 +-------------------------------------------------------------------------------------+
\r
181 | Latency | HCLK clock frequency (MHz) |
\r
182 | |---------------------------------------------------------------------|
\r
183 | | voltage range | voltage range | voltage range | voltage range |
\r
184 | | 2.7 V - 3.6 V | 2.4 V - 2.7 V | 2.1 V - 2.4 V | 1.8 V - 2.1 V |
\r
185 |---------------|----------------|----------------|-----------------|-----------------|
\r
186 |0WS(1CPU cycle)|0 < HCLK <= 30 |0 < HCLK <= 24 |0 < HCLK <= 22 |0 < HCLK <= 20 |
\r
187 |---------------|----------------|----------------|-----------------|-----------------|
\r
188 |1WS(2CPU cycle)|30 < HCLK <= 60 |24 < HCLK <= 48 |22 < HCLK <= 44 |20 < HCLK <= 40 |
\r
189 |---------------|----------------|----------------|-----------------|-----------------|
\r
190 |2WS(3CPU cycle)|60 < HCLK <= 90 |48 < HCLK <= 72 |44 < HCLK <= 66 |40 < HCLK <= 60 |
\r
191 |---------------|----------------|----------------|-----------------|-----------------|
\r
192 |3WS(4CPU cycle)|90 < HCLK <= 120|72 < HCLK <= 96 |66 < HCLK <= 88 |60 < HCLK <= 80 |
\r
193 |---------------|----------------|----------------|-----------------|-----------------|
\r
194 |4WS(5CPU cycle)|120< HCLK <= 150|96 < HCLK <= 120|88 < HCLK <= 110 |80 < HCLK <= 100 |
\r
195 |---------------|----------------|----------------|-----------------|-----------------|
\r
196 |5WS(6CPU cycle)|150< HCLK <= 168|120< HCLK <= 144|110 < HCLK <= 132|100 < HCLK <= 120|
\r
197 |---------------|----------------|----------------|-----------------|-----------------|
\r
198 |6WS(7CPU cycle)| NA |144< HCLK <= 168|132 < HCLK <= 154|120 < HCLK <= 140|
\r
199 |---------------|----------------|----------------|-----------------|-----------------|
\r
200 |7WS(8CPU cycle)| NA | NA |154 < HCLK <= 168|140 < HCLK <= 160|
\r
201 +-------------------------------------------------------------------------------------+
\r
202 (#) For the STM32F42xxx and STM32F43xxx devices, the maximum frequency
\r
203 of the SYSCLK and HCLK is 180 MHz, PCLK2 90 MHz and PCLK1 45 MHz.
\r
204 Depending on the device voltage range, the maximum frequency should
\r
205 be adapted accordingly:
\r
206 +-------------------------------------------------------------------------------------+
\r
207 | Latency | HCLK clock frequency (MHz) |
\r
208 | |---------------------------------------------------------------------|
\r
209 | | voltage range | voltage range | voltage range | voltage range |
\r
210 | | 2.7 V - 3.6 V | 2.4 V - 2.7 V | 2.1 V - 2.4 V | 1.8 V - 2.1 V |
\r
211 |---------------|----------------|----------------|-----------------|-----------------|
\r
212 |0WS(1CPU cycle)|0 < HCLK <= 30 |0 < HCLK <= 24 |0 < HCLK <= 22 |0 < HCLK <= 20 |
\r
213 |---------------|----------------|----------------|-----------------|-----------------|
\r
214 |1WS(2CPU cycle)|30 < HCLK <= 60 |24 < HCLK <= 48 |22 < HCLK <= 44 |20 < HCLK <= 40 |
\r
215 |---------------|----------------|----------------|-----------------|-----------------|
\r
216 |2WS(3CPU cycle)|60 < HCLK <= 90 |48 < HCLK <= 72 |44 < HCLK <= 66 |40 < HCLK <= 60 |
\r
217 |---------------|----------------|----------------|-----------------|-----------------|
\r
218 |3WS(4CPU cycle)|90 < HCLK <= 120|72 < HCLK <= 96 |66 < HCLK <= 88 |60 < HCLK <= 80 |
\r
219 |---------------|----------------|----------------|-----------------|-----------------|
\r
220 |4WS(5CPU cycle)|120< HCLK <= 150|96 < HCLK <= 120|88 < HCLK <= 110 |80 < HCLK <= 100 |
\r
221 |---------------|----------------|----------------|-----------------|-----------------|
\r
222 |5WS(6CPU cycle)|150< HCLK <= 180|120< HCLK <= 144|110 < HCLK <= 132|100 < HCLK <= 120|
\r
223 |---------------|----------------|----------------|-----------------|-----------------|
\r
224 |6WS(7CPU cycle)| NA |144< HCLK <= 168|132 < HCLK <= 154|120 < HCLK <= 140|
\r
225 |---------------|----------------|----------------|-----------------|-----------------|
\r
226 |7WS(8CPU cycle)| NA |168< HCLK <= 180|154 < HCLK <= 176|140 < HCLK <= 160|
\r
227 |-------------------------------------------------------------------------------------|
\r
228 |8WS(9CPU cycle)| NA | NA |176 < HCLK <= 180|160 < HCLK <= 180|
\r
229 +-------------------------------------------------------------------------------------+
\r
230 (#) For the STM32F401xx, the maximum frequency of the SYSCLK and HCLK is 84 MHz,
\r
231 PCLK2 84 MHz and PCLK1 42 MHz.
\r
232 Depending on the device voltage range, the maximum frequency should
\r
233 be adapted accordingly:
\r
234 +-------------------------------------------------------------------------------------+
\r
235 | Latency | HCLK clock frequency (MHz) |
\r
236 | |---------------------------------------------------------------------|
\r
237 | | voltage range | voltage range | voltage range | voltage range |
\r
238 | | 2.7 V - 3.6 V | 2.4 V - 2.7 V | 2.1 V - 2.4 V | 1.8 V - 2.1 V |
\r
239 |---------------|----------------|----------------|-----------------|-----------------|
\r
240 |0WS(1CPU cycle)|0 < HCLK <= 30 |0 < HCLK <= 24 |0 < HCLK <= 22 |0 < HCLK <= 20 |
\r
241 |---------------|----------------|----------------|-----------------|-----------------|
\r
242 |1WS(2CPU cycle)|30 < HCLK <= 60 |24 < HCLK <= 48 |22 < HCLK <= 44 |20 < HCLK <= 40 |
\r
243 |---------------|----------------|----------------|-----------------|-----------------|
\r
244 |2WS(3CPU cycle)|60 < HCLK <= 84 |48 < HCLK <= 72 |44 < HCLK <= 66 |40 < HCLK <= 60 |
\r
245 |---------------|----------------|----------------|-----------------|-----------------|
\r
246 |3WS(4CPU cycle)| NA |72 < HCLK <= 84 |66 < HCLK <= 84 |60 < HCLK <= 80 |
\r
247 |---------------|----------------|----------------|-----------------|-----------------|
\r
248 |4WS(5CPU cycle)| NA | NA | NA |80 < HCLK <= 84 |
\r
249 +-------------------------------------------------------------------------------------+
\r
255 * @brief Resets the RCC clock configuration to the default reset state.
\r
256 * @note The default reset state of the clock configuration is given below:
\r
257 * - HSI ON and used as system clock source
\r
258 * - HSE, PLL and PLLI2S OFF
\r
259 * - AHB, APB1 and APB2 prescaler set to 1.
\r
260 * - CSS, MCO1 and MCO2 OFF
\r
261 * - All interrupts disabled
\r
262 * @note This function doesn't modify the configuration of the
\r
263 * - Peripheral clocks
\r
264 * - LSI, LSE and RTC clocks
\r
268 void HAL_RCC_DeInit(void)
\r
270 /* Set HSION bit */
\r
271 SET_BIT(RCC->CR, RCC_CR_HSION | RCC_CR_HSITRIM_4);
\r
273 /* Reset CFGR register */
\r
274 CLEAR_REG(RCC->CFGR);
\r
276 /* Reset HSEON, CSSON, PLLON, PLLI2S */
\r
277 CLEAR_BIT(RCC->CR, RCC_CR_HSEON | RCC_CR_CSSON | RCC_CR_PLLON| RCC_CR_PLLI2SON);
\r
279 /* Reset PLLCFGR register */
\r
280 CLEAR_REG(RCC->PLLCFGR);
\r
281 SET_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLM_4 | RCC_PLLCFGR_PLLN_6 | RCC_PLLCFGR_PLLN_7 | RCC_PLLCFGR_PLLQ_2);
\r
283 /* Reset PLLI2SCFGR register */
\r
284 CLEAR_REG(RCC->PLLI2SCFGR);
\r
285 SET_BIT(RCC->PLLI2SCFGR, RCC_PLLI2SCFGR_PLLI2SN_6 | RCC_PLLI2SCFGR_PLLI2SN_7 | RCC_PLLI2SCFGR_PLLI2SR_1);
\r
287 /* Reset HSEBYP bit */
\r
288 CLEAR_BIT(RCC->CR, RCC_CR_HSEBYP);
\r
290 /* Disable all interrupts */
\r
291 CLEAR_REG(RCC->CIR);
\r
295 * @brief Initializes the RCC Oscillators according to the specified parameters in the
\r
296 * RCC_OscInitTypeDef.
\r
297 * @param RCC_OscInitStruct: pointer to an RCC_OscInitTypeDef structure that
\r
298 * contains the configuration information for the RCC Oscillators.
\r
299 * @note The PLL is not disabled when used as system clock.
\r
300 * @retval HAL status
\r
302 HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
\r
305 uint32_t timeout = 0;
\r
307 /* Check the parameters */
\r
308 assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType));
\r
309 /*------------------------------- HSE Configuration ------------------------*/
\r
310 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE)
\r
312 /* Check the parameters */
\r
313 assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState));
\r
314 /* When the HSE is used as system clock or clock source for PLL in these cases HSE will not disabled */
\r
315 if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSE) || ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL) && ((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLCFGR_PLLSRC_HSE)))
\r
317 if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET) && (RCC_OscInitStruct->HSEState != RCC_HSE_ON))
\r
324 /* Reset HSEON and HSEBYP bits before configuring the HSE --------------*/
\r
325 __HAL_RCC_HSE_CONFIG(RCC_HSE_OFF);
\r
328 timeout = HAL_GetTick() + HSE_TIMEOUT_VALUE;
\r
330 /* Wait till HSE is disabled */
\r
331 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET)
\r
333 if(HAL_GetTick() >= timeout)
\r
335 return HAL_TIMEOUT;
\r
339 /* Set the new HSE configuration ---------------------------------------*/
\r
340 __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState);
\r
342 /* Check the HSE State */
\r
343 if((RCC_OscInitStruct->HSEState) == RCC_HSE_ON)
\r
346 timeout = HAL_GetTick() + HSE_TIMEOUT_VALUE;
\r
348 /* Wait till HSE is ready */
\r
349 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)
\r
351 if(HAL_GetTick() >= timeout)
\r
353 return HAL_TIMEOUT;
\r
360 timeout = HAL_GetTick() + HSE_TIMEOUT_VALUE;
\r
362 /* Wait till HSE is bypassed or disabled */
\r
363 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET)
\r
365 if(HAL_GetTick() >= timeout)
\r
367 return HAL_TIMEOUT;
\r
373 /*----------------------------- HSI Configuration --------------------------*/
\r
374 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI)
\r
376 /* Check the parameters */
\r
377 assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState));
\r
378 assert_param(IS_RCC_CALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue));
\r
380 /* When the HSI is used as system clock it will not disabled */
\r
381 if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSI) || ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL) && ((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLCFGR_PLLSRC_HSI)))
\r
383 if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET) && (RCC_OscInitStruct->HSIState != RCC_HSI_ON))
\r
390 /* Check the HSI State */
\r
391 if((RCC_OscInitStruct->HSIState)!= RCC_HSI_OFF)
\r
393 /* Enable the Internal High Speed oscillator (HSI). */
\r
394 __HAL_RCC_HSI_ENABLE();
\r
397 timeout = HAL_GetTick() + HSI_TIMEOUT_VALUE;
\r
399 /* Wait till HSI is ready */
\r
400 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)
\r
402 if(HAL_GetTick() >= timeout)
\r
404 return HAL_TIMEOUT;
\r
408 /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
\r
409 __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
\r
413 /* Disable the Internal High Speed oscillator (HSI). */
\r
414 __HAL_RCC_HSI_DISABLE();
\r
417 timeout = HAL_GetTick() + HSI_TIMEOUT_VALUE;
\r
419 /* Wait till HSI is ready */
\r
420 while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET)
\r
422 if(HAL_GetTick() >= timeout)
\r
424 return HAL_TIMEOUT;
\r
430 /*------------------------------ LSI Configuration -------------------------*/
\r
431 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI)
\r
433 /* Check the parameters */
\r
434 assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState));
\r
436 /* Check the LSI State */
\r
437 if((RCC_OscInitStruct->LSIState)!= RCC_LSI_OFF)
\r
439 /* Enable the Internal Low Speed oscillator (LSI). */
\r
440 __HAL_RCC_LSI_ENABLE();
\r
443 timeout = HAL_GetTick() + LSI_TIMEOUT_VALUE;
\r
445 /* Wait till LSI is ready */
\r
446 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) == RESET)
\r
448 if(HAL_GetTick() >= timeout)
\r
450 return HAL_TIMEOUT;
\r
456 /* Disable the Internal Low Speed oscillator (LSI). */
\r
457 __HAL_RCC_LSI_DISABLE();
\r
460 timeout = HAL_GetTick() + LSI_TIMEOUT_VALUE;
\r
462 /* Wait till LSI is ready */
\r
463 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) != RESET)
\r
465 if(HAL_GetTick() >= timeout)
\r
467 return HAL_TIMEOUT;
\r
472 /*------------------------------ LSE Configuration -------------------------*/
\r
473 if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE)
\r
475 /* Check the parameters */
\r
476 assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState));
\r
478 /* Enable Power Clock*/
\r
479 __PWR_CLK_ENABLE();
\r
481 /* Enable write access to Backup domain */
\r
482 PWR->CR |= PWR_CR_DBP;
\r
484 /* Wait for Backup domain Write protection disable */
\r
485 timeout = HAL_GetTick() + DBP_TIMEOUT_VALUE;
\r
487 while((PWR->CR & PWR_CR_DBP) == RESET)
\r
489 if(HAL_GetTick() >= timeout)
\r
491 return HAL_TIMEOUT;
\r
495 /* Reset LSEON and LSEBYP bits before configuring the LSE ----------------*/
\r
496 __HAL_RCC_LSE_CONFIG(RCC_LSE_OFF);
\r
499 timeout = HAL_GetTick() + LSE_TIMEOUT_VALUE;
\r
501 /* Wait till LSE is ready */
\r
502 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) != RESET)
\r
504 if(HAL_GetTick() >= timeout)
\r
506 return HAL_TIMEOUT;
\r
510 /* Set the new LSE configuration -----------------------------------------*/
\r
511 __HAL_RCC_LSE_CONFIG(RCC_OscInitStruct->LSEState);
\r
512 /* Check the LSE State */
\r
513 if((RCC_OscInitStruct->LSEState) == RCC_LSE_ON)
\r
516 timeout = HAL_GetTick() + LSE_TIMEOUT_VALUE;
\r
518 /* Wait till LSE is ready */
\r
519 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
\r
521 if(HAL_GetTick() >= timeout)
\r
523 return HAL_TIMEOUT;
\r
530 timeout = HAL_GetTick() + LSE_TIMEOUT_VALUE;
\r
532 /* Wait till LSE is ready */
\r
533 while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) != RESET)
\r
535 if(HAL_GetTick() >= timeout)
\r
537 return HAL_TIMEOUT;
\r
542 /*-------------------------------- PLL Configuration -----------------------*/
\r
543 /* Check the parameters */
\r
544 assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState));
\r
545 if ((RCC_OscInitStruct->PLL.PLLState) != RCC_PLL_NONE)
\r
547 /* Check if the PLL is used as system clock or not */
\r
548 if(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_PLL)
\r
550 if((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_ON)
\r
552 /* Check the parameters */
\r
553 assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource));
\r
554 assert_param(IS_RCC_PLLM_VALUE(RCC_OscInitStruct->PLL.PLLM));
\r
555 assert_param(IS_RCC_PLLN_VALUE(RCC_OscInitStruct->PLL.PLLN));
\r
556 assert_param(IS_RCC_PLLP_VALUE(RCC_OscInitStruct->PLL.PLLP));
\r
557 assert_param(IS_RCC_PLLQ_VALUE(RCC_OscInitStruct->PLL.PLLQ));
\r
559 /* Disable the main PLL. */
\r
560 __HAL_RCC_PLL_DISABLE();
\r
563 timeout = HAL_GetTick() + PLL_TIMEOUT_VALUE;
\r
565 /* Wait till PLL is ready */
\r
566 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
\r
568 if(HAL_GetTick() >= timeout)
\r
570 return HAL_TIMEOUT;
\r
574 /* Configure the main PLL clock source, multiplication and division factors. */
\r
575 __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource,
\r
576 RCC_OscInitStruct->PLL.PLLM,
\r
577 RCC_OscInitStruct->PLL.PLLN,
\r
578 RCC_OscInitStruct->PLL.PLLP,
\r
579 RCC_OscInitStruct->PLL.PLLQ);
\r
580 /* Enable the main PLL. */
\r
581 __HAL_RCC_PLL_ENABLE();
\r
584 timeout = HAL_GetTick() + PLL_TIMEOUT_VALUE;
\r
586 /* Wait till PLL is ready */
\r
587 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
\r
589 if(HAL_GetTick() >= timeout)
\r
591 return HAL_TIMEOUT;
\r
597 /* Disable the main PLL. */
\r
598 __HAL_RCC_PLL_DISABLE();
\r
600 timeout = HAL_GetTick() + PLL_TIMEOUT_VALUE;
\r
602 /* Wait till PLL is ready */
\r
603 while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
\r
605 if(HAL_GetTick() >= timeout)
\r
607 return HAL_TIMEOUT;
\r
621 * @brief Initializes the CPU, AHB and APB busses clocks according to the specified
\r
622 * parameters in the RCC_ClkInitStruct.
\r
623 * @param RCC_ClkInitStruct: pointer to an RCC_OscInitTypeDef structure that
\r
624 * contains the configuration information for the RCC peripheral.
\r
625 * @param FLatency: FLASH Latency, this parameter depend on device selected
\r
627 * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency
\r
628 * and updated by HAL_RCC_GetHCLKFreq() function called within this function
\r
630 * @note The HSI is used (enabled by hardware) as system clock source after
\r
631 * startup from Reset, wake-up from STOP and STANDBY mode, or in case
\r
632 * of failure of the HSE used directly or indirectly as system clock
\r
633 * (if the Clock Security System CSS is enabled).
\r
635 * @note A switch from one clock source to another occurs only if the target
\r
636 * clock source is ready (clock stable after startup delay or PLL locked).
\r
637 * If a clock source which is not yet ready is selected, the switch will
\r
638 * occur when the clock source will be ready.
\r
640 * @note Depending on the device voltage range, the software has to set correctly
\r
641 * HPRE[3:0] bits to ensure that HCLK not exceed the maximum allowed frequency
\r
642 * (for more details refer to section above "Initialization/de-initialization functions")
\r
645 HAL_StatusTypeDef HAL_RCC_ClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t FLatency)
\r
648 uint32_t timeout = 0;
\r
650 /* Check the parameters */
\r
651 assert_param(IS_RCC_CLOCKTYPE(RCC_ClkInitStruct->ClockType));
\r
652 assert_param(IS_FLASH_LATENCY(FLatency));
\r
654 /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
\r
655 must be correctly programmed according to the frequency of the CPU clock
\r
656 (HCLK) and the supply voltage of the device. */
\r
658 /* Increasing the CPU frequency */
\r
659 if(FLatency > (FLASH->ACR & FLASH_ACR_LATENCY))
\r
661 /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
\r
662 __HAL_FLASH_SET_LATENCY(FLatency);
\r
664 /* Check that the new number of wait states is taken into account to access the Flash
\r
665 memory by reading the FLASH_ACR register */
\r
666 if((FLASH->ACR & FLASH_ACR_LATENCY) != FLatency)
\r
671 /*------------------------- SYSCLK Configuration ---------------------------*/
\r
672 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_SYSCLK) == RCC_CLOCKTYPE_SYSCLK)
\r
674 assert_param(IS_RCC_SYSCLKSOURCE(RCC_ClkInitStruct->SYSCLKSource));
\r
676 /* HSE is selected as System Clock Source */
\r
677 if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
\r
679 /* Check the HSE ready flag */
\r
680 if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)
\r
685 /* PLL is selected as System Clock Source */
\r
686 else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
\r
688 /* Check the PLL ready flag */
\r
689 if(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
\r
694 /* HSI is selected as System Clock Source */
\r
697 /* Check the HSI ready flag */
\r
698 if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)
\r
703 MODIFY_REG(RCC->CFGR, RCC_CFGR_SW, RCC_ClkInitStruct->SYSCLKSource);
\r
706 timeout = HAL_GetTick() + CLOCKSWITCH_TIMEOUT_VALUE;
\r
708 if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
\r
710 while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_HSE)
\r
712 if(HAL_GetTick() >= timeout)
\r
714 return HAL_TIMEOUT;
\r
718 else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
\r
720 while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_PLL)
\r
722 if(HAL_GetTick() >= timeout)
\r
724 return HAL_TIMEOUT;
\r
730 while(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_HSI)
\r
732 if(HAL_GetTick() >= timeout)
\r
734 return HAL_TIMEOUT;
\r
740 /* Decreasing the CPU frequency */
\r
743 /*------------------------- SYSCLK Configuration ---------------------------*/
\r
744 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_SYSCLK) == RCC_CLOCKTYPE_SYSCLK)
\r
746 assert_param(IS_RCC_SYSCLKSOURCE(RCC_ClkInitStruct->SYSCLKSource));
\r
748 /* HSE is selected as System Clock Source */
\r
749 if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
\r
751 /* Check the HSE ready flag */
\r
752 if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)
\r
757 /* PLL is selected as System Clock Source */
\r
758 else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
\r
760 /* Check the PLL ready flag */
\r
761 if(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
\r
766 /* HSI is selected as System Clock Source */
\r
769 /* Check the HSI ready flag */
\r
770 if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)
\r
775 MODIFY_REG(RCC->CFGR, RCC_CFGR_SW, RCC_ClkInitStruct->SYSCLKSource);
\r
778 timeout = HAL_GetTick() + CLOCKSWITCH_TIMEOUT_VALUE;
\r
780 if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
\r
782 while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_HSE)
\r
784 if(HAL_GetTick() >= timeout)
\r
786 return HAL_TIMEOUT;
\r
790 else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
\r
792 while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_PLL)
\r
794 if(HAL_GetTick() >= timeout)
\r
796 return HAL_TIMEOUT;
\r
802 while(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_HSI)
\r
804 if(HAL_GetTick() >= timeout)
\r
806 return HAL_TIMEOUT;
\r
812 /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
\r
813 __HAL_FLASH_SET_LATENCY(FLatency);
\r
815 /* Check that the new number of wait states is taken into account to access the Flash
\r
816 memory by reading the FLASH_ACR register */
\r
817 if((FLASH->ACR & FLASH_ACR_LATENCY) != FLatency)
\r
823 /*-------------------------- HCLK Configuration ----------------------------*/
\r
824 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
\r
826 assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
\r
827 MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
\r
830 /*-------------------------- PCLK1 Configuration ---------------------------*/
\r
831 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
\r
833 assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB1CLKDivider));
\r
834 MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE1, RCC_ClkInitStruct->APB1CLKDivider);
\r
837 /*-------------------------- PCLK2 Configuration ---------------------------*/
\r
838 if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2)
\r
840 assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB2CLKDivider));
\r
841 MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE2, ((RCC_ClkInitStruct->APB2CLKDivider) << 3));
\r
844 /* Setup SysTick Timer for 1 msec interrupts.
\r
845 ------------------------------------------
\r
846 The SysTick_Config() function is a CMSIS function which configure:
\r
847 - The SysTick Reload register with value passed as function parameter.
\r
848 - Configure the SysTick IRQ priority to the lowest value (0x0F).
\r
849 - Reset the SysTick Counter register.
\r
850 - Configure the SysTick Counter clock source to be Core Clock Source (HCLK).
\r
851 - Enable the SysTick Interrupt.
\r
852 - Start the SysTick Counter.*/
\r
853 SysTick_Config(HAL_RCC_GetHCLKFreq() / 1000);
\r
862 /** @defgroup RCC_Group2 Peripheral Control functions
\r
863 * @brief RCC clocks control functions
\r
866 ===============================================================================
\r
867 ##### Peripheral Control functions #####
\r
868 ===============================================================================
\r
870 This subsection provides a set of functions allowing to control the RCC Clocks
\r
878 * @brief Selects the clock source to output on MCO1 pin(PA8) or on MCO2 pin(PC9).
\r
879 * @note PA8/PC9 should be configured in alternate function mode.
\r
880 * @param RCC_MCOx: specifies the output direction for the clock source.
\r
881 * This parameter can be one of the following values:
\r
882 * @arg RCC_MCO1: Clock source to output on MCO1 pin(PA8).
\r
883 * @arg RCC_MCO2: Clock source to output on MCO2 pin(PC9).
\r
884 * @param RCC_MCOSource: specifies the clock source to output.
\r
885 * This parameter can be one of the following values:
\r
886 * @arg RCC_MCO1SOURCE_HSI: HSI clock selected as MCO1 source
\r
887 * @arg RCC_MCO1SOURCE_LSE: LSE clock selected as MCO1 source
\r
888 * @arg RCC_MCO1SOURCE_HSE: HSE clock selected as MCO1 source
\r
889 * @arg RCC_MCO1SOURCE_PLLCLK: main PLL clock selected as MCO1 source
\r
890 * @arg RCC_MCO2SOURCE_SYSCLK: System clock (SYSCLK) selected as MCO2 source
\r
891 * @arg RCC_MCO2SOURCE_PLLI2SCLK: PLLI2S clock selected as MCO2 source
\r
892 * @arg RCC_MCO2SOURCE_HSE: HSE clock selected as MCO2 source
\r
893 * @arg RCC_MCO2SOURCE_PLLCLK: main PLL clock selected as MCO2 source
\r
894 * @param RCC_MCODiv: specifies the MCOx prescaler.
\r
895 * This parameter can be one of the following values:
\r
896 * @arg RCC_MCODIV_1: no division applied to MCOx clock
\r
897 * @arg RCC_MCODIV_2: division by 2 applied to MCOx clock
\r
898 * @arg RCC_MCODIV_3: division by 3 applied to MCOx clock
\r
899 * @arg RCC_MCODIV_4: division by 4 applied to MCOx clock
\r
900 * @arg RCC_MCODIV_5: division by 5 applied to MCOx clock
\r
903 void HAL_RCC_MCOConfig(uint32_t RCC_MCOx, uint32_t RCC_MCOSource, uint32_t RCC_MCODiv)
\r
905 GPIO_InitTypeDef GPIO_InitStruct;
\r
906 /* Check the parameters */
\r
907 assert_param(IS_RCC_MCO(RCC_MCOx));
\r
908 assert_param(IS_RCC_MCODIV(RCC_MCODiv));
\r
910 if(RCC_MCOx == RCC_MCO1)
\r
912 assert_param(IS_RCC_MCO1SOURCE(RCC_MCOSource));
\r
914 /* MCO1 Clock Enable */
\r
915 __MCO1_CLK_ENABLE();
\r
917 /* Configue the MCO1 pin in alternate function mode */
\r
918 GPIO_InitStruct.Pin = MCO1_PIN;
\r
919 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
\r
920 GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
\r
921 GPIO_InitStruct.Pull = GPIO_NOPULL;
\r
922 GPIO_InitStruct.Alternate = GPIO_AF0_MCO;
\r
923 HAL_GPIO_Init(MCO1_GPIO_PORT, &GPIO_InitStruct);
\r
925 /* Mask MCO1 and MCO1PRE[2:0] bits then Select MCO1 clock source and prescaler */
\r
926 MODIFY_REG(RCC->CFGR, (RCC_CFGR_MCO1 | RCC_CFGR_MCO1PRE), (RCC_MCOSource | RCC_MCODiv));
\r
930 assert_param(IS_RCC_MCO2SOURCE(RCC_MCOSource));
\r
932 /* MCO2 Clock Enable */
\r
933 __MCO2_CLK_ENABLE();
\r
935 /* Configue the MCO2 pin in alternate function mode */
\r
936 GPIO_InitStruct.Pin = MCO2_PIN;
\r
937 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
\r
938 GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
\r
939 GPIO_InitStruct.Pull = GPIO_NOPULL;
\r
940 GPIO_InitStruct.Alternate = GPIO_AF0_MCO;
\r
941 HAL_GPIO_Init(MCO2_GPIO_PORT, &GPIO_InitStruct);
\r
943 /* Mask MCO2 and MCO2PRE[2:0] bits then Select MCO2 clock source and prescaler */
\r
944 MODIFY_REG(RCC->CFGR, (RCC_CFGR_MCO2 | RCC_CFGR_MCO2PRE), (RCC_MCOSource | (RCC_MCODiv << 3)));
\r
949 * @brief Enables the Clock Security System.
\r
950 * @note If a failure is detected on the HSE oscillator clock, this oscillator
\r
951 * is automatically disabled and an interrupt is generated to inform the
\r
952 * software about the failure (Clock Security System Interrupt, CSSI),
\r
953 * allowing the MCU to perform rescue operations. The CSSI is linked to
\r
954 * the Cortex-M4 NMI (Non-Maskable Interrupt) exception vector.
\r
958 void HAL_RCC_EnableCSS(void)
\r
960 *(__IO uint32_t *) CR_CSSON_BB = (uint32_t)ENABLE;
\r
964 * @brief Disables the Clock Security System.
\r
968 void HAL_RCC_DisableCSS(void)
\r
970 *(__IO uint32_t *) CR_CSSON_BB = (uint32_t)DISABLE;
\r
974 * @brief Returns the SYSCLK frequency
\r
976 * @note The system frequency computed by this function is not the real
\r
977 * frequency in the chip. It is calculated based on the predefined
\r
978 * constant and the selected clock source:
\r
979 * @note If SYSCLK source is HSI, function returns values based on HSI_VALUE(*)
\r
980 * @note If SYSCLK source is HSE, function returns values based on HSE_VALUE(**)
\r
981 * @note If SYSCLK source is PLL, function returns values based on HSE_VALUE(**)
\r
982 * or HSI_VALUE(*) multiplied/divided by the PLL factors.
\r
983 * @note (*) HSI_VALUE is a constant defined in stm32f4xx_hal_conf.h file (default value
\r
984 * 16 MHz) but the real value may vary depending on the variations
\r
985 * in voltage and temperature.
\r
986 * @note (**) HSE_VALUE is a constant defined in stm32f4xx_hal_conf.h file (default value
\r
987 * 25 MHz), user has to ensure that HSE_VALUE is same as the real
\r
988 * frequency of the crystal used. Otherwise, this function may
\r
989 * have wrong result.
\r
991 * @note The result of this function could be not correct when using fractional
\r
992 * value for HSE crystal.
\r
994 * @note This function can be used by the user application to compute the
\r
995 * baudrate for the communication peripherals or configure other parameters.
\r
997 * @note Each time SYSCLK changes, this function must be called to update the
\r
998 * right SYSCLK value. Otherwise, any configuration based on this function will be incorrect.
\r
1002 * @retval SYSCLK frequency
\r
1004 uint32_t HAL_RCC_GetSysClockFreq(void)
\r
1006 uint32_t pllm = 0, pllvco = 0, pllp = 0;
\r
1007 uint32_t sysclockfreq = 0;
\r
1009 /* Get SYSCLK source -------------------------------------------------------*/
\r
1010 switch (RCC->CFGR & RCC_CFGR_SWS)
\r
1012 case RCC_CFGR_SWS_HSI: /* HSI used as system clock source */
\r
1014 sysclockfreq = HSI_VALUE;
\r
1017 case RCC_CFGR_SWS_HSE: /* HSE used as system clock source */
\r
1019 sysclockfreq = HSE_VALUE;
\r
1022 case RCC_CFGR_SWS_PLL: /* PLL used as system clock source */
\r
1024 /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM) * PLLN
\r
1025 SYSCLK = PLL_VCO / PLLP */
\r
1026 pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
\r
1027 if (__RCC_PLLSRC() != 0)
\r
1029 /* HSE used as PLL clock source */
\r
1030 pllvco = ((HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> POSITION_VAL(RCC_PLLCFGR_PLLN)));
\r
1034 /* HSI used as PLL clock source */
\r
1035 pllvco = ((HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> POSITION_VAL(RCC_PLLCFGR_PLLN)));
\r
1037 pllp = ((((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >> POSITION_VAL(RCC_PLLCFGR_PLLP)) + 1 ) *2);
\r
1039 sysclockfreq = pllvco/pllp;
\r
1044 sysclockfreq = HSI_VALUE;
\r
1048 return sysclockfreq;
\r
1052 * @brief Returns the HCLK frequency
\r
1053 * @note Each time HCLK changes, this function must be called to update the
\r
1054 * right HCLK value. Otherwise, any configuration based on this function will be incorrect.
\r
1056 * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency
\r
1057 * and updated within this function
\r
1059 * @retval HCLK frequency
\r
1061 uint32_t HAL_RCC_GetHCLKFreq(void)
\r
1063 SystemCoreClock = HAL_RCC_GetSysClockFreq() >> APBAHBPrescTable[(RCC->CFGR & RCC_CFGR_HPRE)>> POSITION_VAL(RCC_CFGR_HPRE)];
\r
1064 return SystemCoreClock;
\r
1068 * @brief Returns the PCLK1 frequency
\r
1069 * @note Each time PCLK1 changes, this function must be called to update the
\r
1070 * right PCLK1 value. Otherwise, any configuration based on this function will be incorrect.
\r
1072 * @retval PCLK1 frequency
\r
1074 uint32_t HAL_RCC_GetPCLK1Freq(void)
\r
1076 /* Get HCLK source and Compute PCLK1 frequency ---------------------------*/
\r
1077 return (HAL_RCC_GetHCLKFreq() >> APBAHBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE1)>> POSITION_VAL(RCC_CFGR_PPRE1)]);
\r
1081 * @brief Returns the PCLK2 frequency
\r
1082 * @note Each time PCLK2 changes, this function must be called to update the
\r
1083 * right PCLK2 value. Otherwise, any configuration based on this function will be incorrect.
\r
1085 * @retval PCLK2 frequency
\r
1087 uint32_t HAL_RCC_GetPCLK2Freq(void)
\r
1089 /* Get HCLK source and Compute PCLK2 frequency ---------------------------*/
\r
1090 return (HAL_RCC_GetHCLKFreq()>> APBAHBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE2)>> POSITION_VAL(RCC_CFGR_PPRE2)]);
\r
1094 * @brief Configures the RCC_OscInitStruct according to the internal
\r
1095 * RCC configuration registers.
\r
1096 * @param RCC_OscInitStruct: pointer to an RCC_OscInitTypeDef structure that
\r
1097 * will be configured.
\r
1100 void HAL_RCC_GetOscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
\r
1102 /* Set all possible values for the Oscillator type parameter ---------------*/
\r
1103 RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_LSI;
\r
1105 /* Get the HSE configuration -----------------------------------------------*/
\r
1106 if((RCC->CR &RCC_CR_HSEBYP) == RCC_CR_HSEBYP)
\r
1108 RCC_OscInitStruct->HSEState = RCC_HSE_BYPASS;
\r
1110 else if((RCC->CR &RCC_CR_HSEON) == RCC_CR_HSEON)
\r
1112 RCC_OscInitStruct->HSEState = RCC_HSE_ON;
\r
1116 RCC_OscInitStruct->HSEState = RCC_HSE_OFF;
\r
1119 /* Get the HSI configuration -----------------------------------------------*/
\r
1120 if((RCC->CR &RCC_CR_HSION) == RCC_CR_HSION)
\r
1122 RCC_OscInitStruct->HSIState = RCC_HSI_ON;
\r
1126 RCC_OscInitStruct->HSIState = RCC_HSI_OFF;
\r
1129 RCC_OscInitStruct->HSICalibrationValue = (uint32_t)((RCC->CR &RCC_CR_HSITRIM) >> POSITION_VAL(RCC_CR_HSITRIM));
\r
1131 /* Get the LSE configuration -----------------------------------------------*/
\r
1132 if((RCC->BDCR &RCC_BDCR_LSEBYP) == RCC_BDCR_LSEBYP)
\r
1134 RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS;
\r
1136 else if((RCC->BDCR &RCC_BDCR_LSEON) == RCC_BDCR_LSEON)
\r
1138 RCC_OscInitStruct->LSEState = RCC_LSE_ON;
\r
1142 RCC_OscInitStruct->LSEState = RCC_LSE_OFF;
\r
1145 /* Get the LSI configuration -----------------------------------------------*/
\r
1146 if((RCC->CSR &RCC_CSR_LSION) == RCC_CSR_LSION)
\r
1148 RCC_OscInitStruct->LSIState = RCC_LSI_ON;
\r
1152 RCC_OscInitStruct->LSIState = RCC_LSI_OFF;
\r
1155 /* Get the PLL configuration -----------------------------------------------*/
\r
1156 if((RCC->CR &RCC_CR_PLLON) == RCC_CR_PLLON)
\r
1158 RCC_OscInitStruct->PLL.PLLState = RCC_PLL_ON;
\r
1162 RCC_OscInitStruct->PLL.PLLState = RCC_PLL_OFF;
\r
1164 RCC_OscInitStruct->PLL.PLLSource = (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC);
\r
1165 RCC_OscInitStruct->PLL.PLLM = (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM);
\r
1166 RCC_OscInitStruct->PLL.PLLN = (uint32_t)((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> POSITION_VAL(RCC_PLLCFGR_PLLN));
\r
1167 RCC_OscInitStruct->PLL.PLLP = (uint32_t)((((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) + RCC_PLLCFGR_PLLP_0) << 1) >> POSITION_VAL(RCC_PLLCFGR_PLLP));
\r
1168 RCC_OscInitStruct->PLL.PLLQ = (uint32_t)((RCC->PLLCFGR & RCC_PLLCFGR_PLLQ) >> POSITION_VAL(RCC_PLLCFGR_PLLQ));
\r
1172 * @brief Configures the RCC_ClkInitStruct according to the internal
\r
1173 * RCC configuration registers.
\r
1174 * @param RCC_OscInitStruct: pointer to an RCC_ClkInitTypeDef structure that
\r
1175 * will be configured.
\r
1176 * @param pFLatency: Pointer on the Flash Latency.
\r
1179 void HAL_RCC_GetClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t *pFLatency)
\r
1181 /* Set all possible values for the Clock type parameter --------------------*/
\r
1182 RCC_ClkInitStruct->ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
\r
1184 /* Get the SYSCLK configuration --------------------------------------------*/
\r
1185 RCC_ClkInitStruct->SYSCLKSource = (uint32_t)(RCC->CFGR & RCC_CFGR_SW);
\r
1187 /* Get the HCLK configuration ----------------------------------------------*/
\r
1188 RCC_ClkInitStruct->AHBCLKDivider = (uint32_t)(RCC->CFGR & RCC_CFGR_HPRE);
\r
1190 /* Get the APB1 configuration ----------------------------------------------*/
\r
1191 RCC_ClkInitStruct->APB1CLKDivider = (uint32_t)(RCC->CFGR & RCC_CFGR_PPRE1);
\r
1193 /* Get the APB2 configuration ----------------------------------------------*/
\r
1194 RCC_ClkInitStruct->APB2CLKDivider = (uint32_t)((RCC->CFGR & RCC_CFGR_PPRE2) >> 3);
\r
1196 /* Get the Flash Wait State (Latency) configuration ------------------------*/
\r
1197 *pFLatency = (uint32_t)(FLASH->ACR & FLASH_ACR_LATENCY);
\r
1201 * @brief This function handles the RCC CSS interrupt request.
\r
1202 * @note This API should be called under the NMI_Handler().
\r
1206 void HAL_RCC_NMI_IRQHandler(void)
\r
1208 /* Check RCC CSSF flag */
\r
1209 if(__HAL_RCC_GET_IT(RCC_IT_CSS))
\r
1211 /* RCC Clock Security System interrupt user callback */
\r
1212 HAL_RCC_CCSCallback();
\r
1214 /* Clear RCC CSS pending bit */
\r
1215 __HAL_RCC_CLEAR_IT(RCC_IT_CSS);
\r
1220 * @brief RCC Clock Security System interrupt callback
\r
1224 __weak void HAL_RCC_CCSCallback(void)
\r
1226 /* NOTE : This function Should not be modified, when the callback is needed,
\r
1227 the HAL_RCC_CCSCallback could be implemented in the user file
\r
1239 #endif /* HAL_RCC_MODULE_ENABLED */
\r
1248 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
\r