Rev 2 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2 | Rev 28 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | /** |
1 | /** |
2 | ****************************************************************************** |
2 | ****************************************************************************** |
3 | * @file stm32l1xx_hal_rcc.c |
3 | * @file stm32l1xx_hal_rcc.c |
4 | * @author MCD Application Team |
4 | * @author MCD Application Team |
5 | * @version V1.2.0 |
- | |
6 | * @date 01-July-2016 |
- | |
7 | * @brief RCC HAL module driver. |
5 | * @brief RCC HAL module driver. |
8 | * This file provides firmware functions to manage the following |
6 | * This file provides firmware functions to manage the following |
9 | * functionalities of the Reset and Clock Control (RCC) peripheral: |
7 | * functionalities of the Reset and Clock Control (RCC) peripheral: |
10 | * + Initialization and de-initialization functions |
8 | * + Initialization and de-initialization functions |
11 | * + Peripheral Control functions |
9 | * + Peripheral Control functions |
12 | * |
10 | * |
13 | @verbatim |
11 | @verbatim |
14 | ============================================================================== |
12 | ============================================================================== |
15 | ##### RCC specific features ##### |
13 | ##### RCC specific features ##### |
16 | ============================================================================== |
14 | ============================================================================== |
17 | [..] |
15 | [..] |
18 | After reset the device is running from multispeed internal oscillator clock |
16 | After reset the device is running from multispeed internal oscillator clock |
19 | (MSI 2.097MHz) with Flash 0 wait state and Flash prefetch buffer is disabled, |
17 | (MSI 2.097MHz) with Flash 0 wait state and Flash prefetch buffer is disabled, |
20 | and all peripherals are off except internal SRAM, Flash and JTAG. |
18 | and all peripherals are off except internal SRAM, Flash and JTAG. |
21 | (+) There is no prescaler on High speed (AHB) and Low speed (APB) buses; |
19 | (+) There is no prescaler on High speed (AHB) and Low speed (APB) buses; |
22 | all peripherals mapped on these buses are running at MSI speed. |
20 | all peripherals mapped on these buses are running at MSI speed. |
23 | (+) The clock for all peripherals is switched off, except the SRAM and FLASH. |
21 | (+) The clock for all peripherals is switched off, except the SRAM and FLASH. |
24 | (+) All GPIOs are in input floating state, except the JTAG pins which |
22 | (+) All GPIOs are in input floating state, except the JTAG pins which |
25 | are assigned to be used for debug purpose. |
23 | are assigned to be used for debug purpose. |
26 | [..] Once the device started from reset, the user application has to: |
24 | [..] Once the device started from reset, the user application has to: |
27 | (+) Configure the clock source to be used to drive the System clock |
25 | (+) Configure the clock source to be used to drive the System clock |
28 | (if the application needs higher frequency/performance) |
26 | (if the application needs higher frequency/performance) |
29 | (+) Configure the System clock frequency and Flash settings |
27 | (+) Configure the System clock frequency and Flash settings |
30 | (+) Configure the AHB and APB buses prescalers |
28 | (+) Configure the AHB and APB buses prescalers |
31 | (+) Enable the clock for the peripheral(s) to be used |
29 | (+) Enable the clock for the peripheral(s) to be used |
32 | (+) Configure the clock source(s) for peripherals whose clocks are not |
30 | (+) Configure the clock source(s) for peripherals whose clocks are not |
33 | derived from the System clock (I2S, RTC, ADC, USB OTG FS/SDIO/RNG) |
31 | derived from the System clock (I2S, RTC, ADC, USB OTG FS/SDIO/RNG) |
34 | (*) SDIO only for STM32L1xxxD devices |
32 | (*) SDIO only for STM32L1xxxD devices |
35 | 33 | ||
36 | ##### RCC Limitations ##### |
34 | ##### RCC Limitations ##### |
37 | ============================================================================== |
35 | ============================================================================== |
38 | [..] |
36 | [..] |
39 | A delay between an RCC peripheral clock enable and the effective peripheral |
37 | A delay between an RCC peripheral clock enable and the effective peripheral |
40 | enabling should be taken into account in order to manage the peripheral read/write |
38 | enabling should be taken into account in order to manage the peripheral read/write |
41 | from/to registers. |
39 | from/to registers. |
42 | (+) This delay depends on the peripheral mapping. |
40 | (+) This delay depends on the peripheral mapping. |
43 | (++) AHB & APB peripherals, 1 dummy read is necessary |
41 | (++) AHB & APB peripherals, 1 dummy read is necessary |
44 | 42 | ||
45 | [..] |
43 | [..] |
46 | Workarounds: |
44 | Workarounds: |
47 | (#) For AHB & APB peripherals, a dummy read to the peripheral register has been |
45 | (#) For AHB & APB peripherals, a dummy read to the peripheral register has been |
48 | inserted in each __HAL_RCC_PPP_CLK_ENABLE() macro. |
46 | inserted in each __HAL_RCC_PPP_CLK_ENABLE() macro. |
49 | 47 | ||
50 | @endverbatim |
48 | @endverbatim |
51 | ****************************************************************************** |
49 | ****************************************************************************** |
52 | * @attention |
50 | * @attention |
53 | * |
51 | * |
54 | * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
52 | * <h2><center>© Copyright(c) 2017 STMicroelectronics. |
55 | * |
- | |
56 | * Redistribution and use in source and binary forms, with or without modification, |
- | |
57 | * are permitted provided that the following conditions are met: |
- | |
58 | * 1. Redistributions of source code must retain the above copyright notice, |
- | |
59 | * this list of conditions and the following disclaimer. |
- | |
60 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
- | |
61 | * this list of conditions and the following disclaimer in the documentation |
- | |
62 | * and/or other materials provided with the distribution. |
- | |
63 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
- | |
64 | * may be used to endorse or promote products derived from this software |
- | |
65 | * without specific prior written permission. |
53 | * All rights reserved.</center></h2> |
66 | * |
54 | * |
67 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
55 | * This software component is licensed by ST under BSD 3-Clause license, |
68 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
- | |
69 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
- | |
70 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
- | |
71 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
- | |
72 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
56 | * the "License"; You may not use this file except in compliance with the |
73 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
57 | * License. You may obtain a copy of the License at: |
74 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
- | |
75 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
- | |
76 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
58 | * opensource.org/licenses/BSD-3-Clause |
77 | * |
59 | * |
78 | ****************************************************************************** |
60 | ****************************************************************************** |
79 | */ |
61 | */ |
80 | 62 | ||
81 | /* Includes ------------------------------------------------------------------*/ |
63 | /* Includes ------------------------------------------------------------------*/ |
82 | #include "stm32l1xx_hal.h" |
64 | #include "stm32l1xx_hal.h" |
83 | 65 | ||
84 | /** @addtogroup STM32L1xx_HAL_Driver |
66 | /** @addtogroup STM32L1xx_HAL_Driver |
Line 92... | Line 74... | ||
92 | 74 | ||
93 | #ifdef HAL_RCC_MODULE_ENABLED |
75 | #ifdef HAL_RCC_MODULE_ENABLED |
94 | 76 | ||
95 | /* Private typedef -----------------------------------------------------------*/ |
77 | /* Private typedef -----------------------------------------------------------*/ |
96 | /* Private define ------------------------------------------------------------*/ |
78 | /* Private define ------------------------------------------------------------*/ |
97 | /** @defgroup RCC_Private_Constants RCC Private Constants |
- | |
98 | * @{ |
- | |
99 | */ |
- | |
100 | /* Bits position in in the CFGR register */ |
- | |
101 | #define RCC_CFGR_PLLMUL_BITNUMBER POSITION_VAL(RCC_CFGR_PLLMUL) |
- | |
102 | #define RCC_CFGR_PLLDIV_BITNUMBER POSITION_VAL(RCC_CFGR_PLLDIV) |
- | |
103 | #define RCC_CFGR_HPRE_BITNUMBER POSITION_VAL(RCC_CFGR_HPRE) |
- | |
104 | #define RCC_CFGR_PPRE1_BITNUMBER POSITION_VAL(RCC_CFGR_PPRE1) |
- | |
105 | #define RCC_CFGR_PPRE2_BITNUMBER POSITION_VAL(RCC_CFGR_PPRE2) |
- | |
106 | /* Bits position in in the ICSCR register */ |
- | |
107 | #define RCC_ICSCR_MSIRANGE_BITNUMBER POSITION_VAL(RCC_ICSCR_MSIRANGE) |
- | |
108 | #define RCC_ICSCR_MSITRIM_BITNUMBER POSITION_VAL(RCC_ICSCR_MSITRIM) |
- | |
109 | /** |
- | |
110 | * @} |
- | |
111 | */ |
- | |
112 | /* Private macro -------------------------------------------------------------*/ |
79 | /* Private macro -------------------------------------------------------------*/ |
113 | /** @defgroup RCC_Private_Macros RCC Private Macros |
80 | /** @defgroup RCC_Private_Macros RCC Private Macros |
114 | * @{ |
81 | * @{ |
115 | */ |
82 | */ |
116 | 83 | ||
Line 144... | Line 111... | ||
144 | 111 | ||
145 | /** @defgroup RCC_Exported_Functions RCC Exported Functions |
112 | /** @defgroup RCC_Exported_Functions RCC Exported Functions |
146 | * @{ |
113 | * @{ |
147 | */ |
114 | */ |
148 | 115 | ||
149 | /** @defgroup RCC_Exported_Functions_Group1 Initialization and de-initialization functions |
116 | /** @defgroup RCC_Exported_Functions_Group1 Initialization and de-initialization functions |
150 | * @brief Initialization and Configuration functions |
117 | * @brief Initialization and Configuration functions |
151 | * |
118 | * |
152 | @verbatim |
119 | @verbatim |
153 | =============================================================================== |
120 | =============================================================================== |
154 | ##### Initialization and de-initialization functions ##### |
121 | ##### Initialization and de-initialization functions ##### |
155 | =============================================================================== |
122 | =============================================================================== |
156 | [..] |
123 | [..] |
157 | This section provides functions allowing to configure the internal/external oscillators |
124 | This section provides functions allowing to configure the internal/external oscillators |
158 | (MSI, HSE, HSI, LSE, LSI, PLL, CSS and MCO) and the System buses clocks (SYSCLK, AHB, APB1 |
125 | (MSI, HSE, HSI, LSE, LSI, PLL, CSS and MCO) and the System buses clocks (SYSCLK, AHB, APB1 |
159 | and APB2). |
126 | and APB2). |
160 | 127 | ||
161 | [..] Internal/external clock and PLL configuration |
128 | [..] Internal/external clock and PLL configuration |
162 | (#) MSI (Multispeed internal), Seven frequency ranges are available: 65.536 kHz, |
129 | (#) MSI (Multispeed internal), Seven frequency ranges are available: 65.536 kHz, |
163 | 131.072 kHz, 262.144 kHz, 524.288 kHz, 1.048 MHz, 2.097 MHz (default value) and 4.194 MHz. |
130 | 131.072 kHz, 262.144 kHz, 524.288 kHz, 1.048 MHz, 2.097 MHz (default value) and 4.194 MHz. |
164 | 131 | ||
165 | (#) HSI (high-speed internal), 16 MHz factory-trimmed RC used directly or through |
132 | (#) HSI (high-speed internal), 16 MHz factory-trimmed RC used directly or through |
166 | the PLL as System clock source. |
133 | the PLL as System clock source. |
167 | (#) LSI (low-speed internal), ~37 KHz low consumption RC used as IWDG and/or RTC |
134 | (#) LSI (low-speed internal), ~37 KHz low consumption RC used as IWDG and/or RTC |
168 | clock source. |
135 | clock source. |
169 | 136 | ||
170 | (#) HSE (high-speed external), 1 to 24 MHz crystal oscillator used directly or |
137 | (#) HSE (high-speed external), 1 to 24 MHz crystal oscillator used directly or |
171 | through the PLL as System clock source. Can be used also as RTC clock source. |
138 | through the PLL as System clock source. Can be used also as RTC clock source. |
172 | 139 | ||
173 | (#) LSE (low-speed external), 32 KHz oscillator used as RTC clock source. |
140 | (#) LSE (low-speed external), 32 KHz oscillator used as RTC clock source. |
174 | 141 | ||
175 | (#) PLL (clocked by HSI or HSE), featuring different output clocks: |
142 | (#) PLL (clocked by HSI or HSE), featuring different output clocks: |
176 | (++) The first output is used to generate the high speed system clock (up to 32 MHz) |
143 | (++) The first output is used to generate the high speed system clock (up to 32 MHz) |
177 | (++) The second output is used to generate the clock for the USB OTG FS (48 MHz) |
144 | (++) The second output is used to generate the clock for the USB OTG FS (48 MHz) |
178 | 145 | ||
179 | (#) CSS (Clock security system), once enable using the macro __HAL_RCC_CSS_ENABLE() |
146 | (#) CSS (Clock security system), once enable using the macro __HAL_RCC_CSS_ENABLE() |
180 | and if a HSE clock failure occurs(HSE used directly or through PLL as System |
147 | and if a HSE clock failure occurs(HSE used directly or through PLL as System |
181 | clock source), the System clocks automatically switched to MSI and an interrupt |
148 | clock source), the System clocks automatically switched to MSI and an interrupt |
182 | is generated if enabled. The interrupt is linked to the Cortex-M3 NMI |
149 | is generated if enabled. The interrupt is linked to the Cortex-M3 NMI |
183 | (Non-Maskable Interrupt) exception vector. |
150 | (Non-Maskable Interrupt) exception vector. |
184 | 151 | ||
185 | (#) MCO1 (microcontroller clock output), used to output SYSCLK, HSI, LSI, MSI, LSE, |
152 | (#) MCO1 (microcontroller clock output), used to output SYSCLK, HSI, LSI, MSI, LSE, |
186 | HSE or PLL clock (through a configurable prescaler) on PA8 pin. |
153 | HSE or PLL clock (through a configurable prescaler) on PA8 pin. |
187 | 154 | ||
188 | [..] System, AHB and APB buses clocks configuration |
155 | [..] System, AHB and APB buses clocks configuration |
189 | (#) Several clock sources can be used to drive the System clock (SYSCLK): MSI, HSI, |
156 | (#) Several clock sources can be used to drive the System clock (SYSCLK): MSI, HSI, |
190 | HSE and PLL. |
157 | HSE and PLL. |
Line 196... | Line 163... | ||
196 | "@ref HAL_RCC_GetSysClockFreq()" function to retrieve the frequencies of these clocks. |
163 | "@ref HAL_RCC_GetSysClockFreq()" function to retrieve the frequencies of these clocks. |
197 | 164 | ||
198 | -@- All the peripheral clocks are derived from the System clock (SYSCLK) except: |
165 | -@- All the peripheral clocks are derived from the System clock (SYSCLK) except: |
199 | (+@) RTC: RTC clock can be derived either from the LSI, LSE or HSE clock |
166 | (+@) RTC: RTC clock can be derived either from the LSI, LSE or HSE clock |
200 | divided by 2 to 16. You have to use @ref __HAL_RCC_RTC_CONFIG() and @ref __HAL_RCC_RTC_ENABLE() |
167 | divided by 2 to 16. You have to use @ref __HAL_RCC_RTC_CONFIG() and @ref __HAL_RCC_RTC_ENABLE() |
201 | macros to configure this clock. |
168 | macros to configure this clock. |
202 | (+@) LCD: LCD clock can be derived either from the LSI, LSE or HSE clock |
169 | (+@) LCD: LCD clock can be derived either from the LSI, LSE or HSE clock |
203 | divided by 2 to 16. You have to use @ref __HAL_RCC_LCD_CONFIG() |
170 | divided by 2 to 16. You have to use @ref __HAL_RCC_LCD_CONFIG() |
204 | macros to configure this clock. |
171 | macros to configure this clock. |
205 | (+@) USB OTG FS: USB OTG FS require a frequency equal to 48 MHz |
172 | (+@) USB OTG FS: USB OTG FS require a frequency equal to 48 MHz |
206 | to work correctly. This clock is derived of the main PLL through PLL Multiplier. |
173 | to work correctly. This clock is derived of the main PLL through PLL Multiplier. |
207 | 174 | ||
208 | (+@) IWDG clock which is always the LSI clock. |
175 | (+@) IWDG clock which is always the LSI clock. |
209 | 176 | ||
210 | (#) The maximum frequency of the SYSCLK and HCLK is 32 MHz, PCLK2 32 MHz |
177 | (#) The maximum frequency of the SYSCLK and HCLK is 32 MHz, PCLK2 32 MHz |
211 | and PCLK1 32 MHz. Depending on the device voltage range, the maximum |
178 | and PCLK1 32 MHz. Depending on the device voltage range, the maximum |
212 | frequency should be adapted accordingly. |
179 | frequency should be adapted accordingly. |
213 | @endverbatim |
180 | @endverbatim |
214 | * @{ |
181 | * @{ |
215 | */ |
182 | */ |
216 | 183 | ||
217 | /* |
184 | /* |
218 | Additional consideration on the HCLK based on Latency settings: |
185 | Additional consideration on the HCLK based on Latency settings: |
219 | +----------------------------------------------------------------------+ |
186 | +----------------------------------------------------------------------+ |
220 | | Latency | HCLK clock frequency (MHz) | |
187 | | Latency | HCLK clock frequency (MHz) | |
221 | | |------------------------------------------------------| |
188 | | |------------------------------------------------------| |
222 | | | voltage range 1 | voltage range 2 | voltage range 3 | |
189 | | | voltage range 1 | voltage range 2 | voltage range 3 | |
223 | | | 1.8 V | 1.5 V | 1.2 V | |
190 | | | 1.8 V | 1.5 V | 1.2 V | |
224 | |---------------|------------------|-----------------|-----------------| |
191 | |---------------|------------------|-----------------|-----------------| |
225 | |0WS(1CPU cycle)| 0 < HCLK <= 16 | 0 < HCLK <= 8 | 0 < HCLK <= 2 | |
192 | |0WS(1CPU cycle)| 0 < HCLK <= 16 | 0 < HCLK <= 8 | 0 < HCLK <= 2 | |
226 | |---------------|------------------|-----------------|-----------------| |
193 | |---------------|------------------|-----------------|-----------------| |
227 | |1WS(2CPU cycle)| 16 < HCLK <= 32 | 8 < HCLK <= 16 | 2 < HCLK <= 4 | |
194 | |1WS(2CPU cycle)| 16 < HCLK <= 32 | 8 < HCLK <= 16 | 2 < HCLK <= 4 | |
228 | +----------------------------------------------------------------------+ |
195 | +----------------------------------------------------------------------+ |
229 | 196 | ||
230 | The following table gives the different clock source frequencies depending on the product |
197 | The following table gives the different clock source frequencies depending on the product |
231 | voltage range: |
198 | voltage range: |
232 | +------------------------------------------------------------------------------------------+ |
199 | +------------------------------------------------------------------------------------------+ |
233 | | Product voltage | Clock frequency | |
200 | | Product voltage | Clock frequency | |
234 | | |------------------|-----------------------------|-----------------------| |
201 | | |------------------|-----------------------------|-----------------------| |
235 | | range | MSI | HSI | HSE | PLL | |
202 | | range | MSI | HSI | HSE | PLL | |
236 | |-----------------|---------|--------|-----------------------------|-----------------------| |
203 | |-----------------|---------|--------|-----------------------------|-----------------------| |
237 | | Range 1 (1.8 V) | 4.2 MHz | 16 MHz | HSE 32 MHz (external clock) | 32 MHz | |
204 | | Range 1 (1.8 V) | 4.2 MHz | 16 MHz | HSE 32 MHz (external clock) | 32 MHz | |
238 | | | | | or 24 MHz (crystal) | (PLLVCO max = 96 MHz) | |
205 | | | | | or 24 MHz (crystal) | (PLLVCO max = 96 MHz) | |
239 | |-----------------|---------|--------|-----------------------------|-----------------------| |
206 | |-----------------|---------|--------|-----------------------------|-----------------------| |
240 | | Range 2 (1.5 V) | 4.2 MHz | 16 MHz | 16 MHz | 16 MHz | |
207 | | Range 2 (1.5 V) | 4.2 MHz | 16 MHz | 16 MHz | 16 MHz | |
241 | | | | | | (PLLVCO max = 48 MHz) | |
208 | | | | | | (PLLVCO max = 48 MHz) | |
242 | |-----------------|---------|--------|-----------------------------|-----------------------| |
209 | |-----------------|---------|--------|-----------------------------|-----------------------| |
243 | | Range 3 (1.2 V) | 4.2 MHz | NA | 8 MHz | 4 MHz | |
210 | | Range 3 (1.2 V) | 4.2 MHz | NA | 8 MHz | 4 MHz | |
244 | | | | | | (PLLVCO max = 24 MHz) | |
211 | | | | | | (PLLVCO max = 24 MHz) | |
245 | +------------------------------------------------------------------------------------------+ |
212 | +------------------------------------------------------------------------------------------+ |
246 | */ |
213 | */ |
247 | 214 | ||
248 | /** |
215 | /** |
249 | * @brief Resets the RCC clock configuration to the default reset state. |
216 | * @brief Resets the RCC clock configuration to the default reset state. |
250 | * @note The default reset state of the clock configuration is given below: |
217 | * @note The default reset state of the clock configuration is given below: |
Line 254... | Line 221... | ||
254 | * - CSS and MCO1 OFF |
221 | * - CSS and MCO1 OFF |
255 | * - All interrupts disabled |
222 | * - All interrupts disabled |
256 | * @note This function does not modify the configuration of the |
223 | * @note This function does not modify the configuration of the |
257 | * - Peripheral clocks |
224 | * - Peripheral clocks |
258 | * - LSI, LSE and RTC clocks |
225 | * - LSI, LSE and RTC clocks |
259 | * @retval None |
226 | * @retval HAL status |
260 | */ |
227 | */ |
261 | void HAL_RCC_DeInit(void) |
228 | HAL_StatusTypeDef HAL_RCC_DeInit(void) |
262 | { |
229 | { |
- | 230 | uint32_t tickstart; |
|
- | 231 | HAL_StatusTypeDef status; |
|
- | 232 | ||
- | 233 | /* Set MSIClockRange, HSITRIM and MSITRIM bits to the reset values */ |
|
- | 234 | MODIFY_REG(RCC->ICSCR, (RCC_ICSCR_MSITRIM | RCC_ICSCR_HSITRIM | RCC_ICSCR_MSIRANGE), \ |
|
- | 235 | ((RCC_MSICALIBRATION_DEFAULT << RCC_ICSCR_MSITRIM_Pos) | (RCC_HSICALIBRATION_DEFAULT << RCC_ICSCR_HSITRIM_Pos) | RCC_ICSCR_MSIRANGE_5)); |
|
- | 236 | ||
263 | /* Set MSION bit */ |
237 | /* Set MSION bit */ |
264 | SET_BIT(RCC->CR, RCC_CR_MSION); |
238 | SET_BIT(RCC->CR, RCC_CR_MSION); |
- | 239 | ||
- | 240 | /* Get Start Tick*/ |
|
- | 241 | tickstart = HAL_GetTick(); |
|
- | 242 | ||
- | 243 | /* Wait till MSI is ready */ |
|
- | 244 | while (READ_BIT(RCC->CR, RCC_CR_MSIRDY) == 0U) |
|
265 | 245 | { |
|
- | 246 | if ((HAL_GetTick() - tickstart) > MSI_TIMEOUT_VALUE) |
|
- | 247 | { |
|
- | 248 | return HAL_TIMEOUT; |
|
- | 249 | } |
|
- | 250 | } |
|
- | 251 | ||
266 | /* Switch SYSCLK to MSI*/ |
252 | /* Switch SYSCLK to MSI*/ |
267 | CLEAR_BIT(RCC->CFGR, RCC_CFGR_SW); |
253 | CLEAR_BIT(RCC->CFGR, RCC_CFGR_SW); |
268 | 254 | ||
- | 255 | /* Wait till MSI as SYSCLK status is ready */ |
|
- | 256 | while (READ_BIT(RCC->CFGR, RCC_CFGR_SWS) != 0U) |
|
- | 257 | { |
|
- | 258 | if ((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE) |
|
- | 259 | { |
|
- | 260 | return HAL_TIMEOUT; |
|
- | 261 | } |
|
- | 262 | } |
|
- | 263 | ||
- | 264 | /* Update the SystemCoreClock global variable */ |
|
- | 265 | SystemCoreClock = MSI_VALUE; |
|
- | 266 | ||
- | 267 | /* Configure the source of time base considering new system clock settings */ |
|
- | 268 | status = HAL_InitTick(uwTickPrio); |
|
- | 269 | if(status != HAL_OK) |
|
- | 270 | { |
|
- | 271 | return status; |
|
- | 272 | } |
|
- | 273 | ||
269 | /* Reset HSION, HSEON, CSSON, HSEBYP & PLLON bits */ |
274 | /* Reset HSION, HSEON, CSSON & PLLON bits */ |
270 | CLEAR_BIT(RCC->CR, RCC_CR_HSION | RCC_CR_HSEON | RCC_CR_CSSON | RCC_CR_PLLON | RCC_CR_HSEBYP); |
275 | CLEAR_BIT(RCC->CR, RCC_CR_HSION | RCC_CR_HSEON | RCC_CR_CSSON | RCC_CR_PLLON); |
- | 276 | /* Reset HSEBYP bit */ |
|
- | 277 | CLEAR_BIT(RCC->CR, RCC_CR_HSEBYP); |
|
- | 278 | ||
- | 279 | /* Get Start Tick*/ |
|
- | 280 | tickstart = HAL_GetTick(); |
|
- | 281 | ||
- | 282 | /* Wait till PLL is not ready */ |
|
- | 283 | while (READ_BIT(RCC->CR, RCC_CR_PLLRDY) != 0U) |
|
- | 284 | { |
|
- | 285 | if ((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE) |
|
- | 286 | { |
|
- | 287 | return HAL_TIMEOUT; |
|
- | 288 | } |
|
- | 289 | } |
|
- | 290 | ||
271 | /* Reset CFGR register */ |
291 | /* Reset CFGR register */ |
272 | CLEAR_REG(RCC->CFGR); |
292 | CLEAR_REG(RCC->CFGR); |
273 | - | ||
274 | /* Set MSIClockRange & MSITRIM[4:0] bits to the reset value */ |
- | |
275 | MODIFY_REG(RCC->ICSCR, (RCC_ICSCR_MSIRANGE | RCC_ICSCR_MSITRIM), (((uint32_t)0 << RCC_ICSCR_MSITRIM_BITNUMBER) | RCC_ICSCR_MSIRANGE_5)); |
- | |
276 | 293 | ||
277 | /* Set HSITRIM bits to the reset value */ |
- | |
278 | MODIFY_REG(RCC->ICSCR, RCC_ICSCR_HSITRIM, ((uint32_t)0x10 << POSITION_VAL(RCC_ICSCR_HSITRIM))); |
- | |
279 | - | ||
280 | /* Disable all interrupts */ |
294 | /* Disable all interrupts */ |
281 | CLEAR_REG(RCC->CIR); |
295 | CLEAR_REG(RCC->CIR); |
282 | 296 | ||
- | 297 | /* Clear all flags */ |
|
- | 298 | #if defined(RCC_LSECSS_SUPPORT) |
|
- | 299 | WRITE_REG(RCC->CIR, RCC_CIR_LSIRDYC | RCC_CIR_LSERDYC | RCC_CIR_HSIRDYC | RCC_CIR_HSERDYC | RCC_CIR_PLLRDYC | RCC_CIR_MSIRDYC | RCC_CIR_LSECSSC | RCC_CIR_CSSC); |
|
- | 300 | #else |
|
- | 301 | WRITE_REG(RCC->CIR, RCC_CIR_LSIRDYC | RCC_CIR_LSERDYC | RCC_CIR_HSIRDYC | RCC_CIR_HSERDYC | RCC_CIR_PLLRDYC | RCC_CIR_MSIRDYC | RCC_CIR_CSSC); |
|
- | 302 | #endif |
|
- | 303 | ||
283 | /* Update the SystemCoreClock global variable */ |
304 | /* Clear all reset flags */ |
284 | SystemCoreClock = MSI_VALUE; |
305 | SET_BIT(RCC->CSR, RCC_CSR_RMVF); |
- | 306 | ||
- | 307 | return HAL_OK; |
|
285 | } |
308 | } |
286 | 309 | ||
287 | /** |
310 | /** |
288 | * @brief Initializes the RCC Oscillators according to the specified parameters in the |
311 | * @brief Initializes the RCC Oscillators according to the specified parameters in the |
289 | * RCC_OscInitTypeDef. |
312 | * RCC_OscInitTypeDef. |
Line 298... | Line 321... | ||
298 | * first and then HSE On or HSE Bypass. |
321 | * first and then HSE On or HSE Bypass. |
299 | * @retval HAL status |
322 | * @retval HAL status |
300 | */ |
323 | */ |
301 | HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct) |
324 | HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct) |
302 | { |
325 | { |
303 | uint32_t tickstart = 0U; |
326 | uint32_t tickstart; |
- | 327 | HAL_StatusTypeDef status; |
|
- | 328 | uint32_t sysclk_source, pll_config; |
|
304 | 329 | ||
305 | /* Check the parameters */ |
330 | /* Check the parameters */ |
306 | assert_param(RCC_OscInitStruct != NULL); |
331 | if(RCC_OscInitStruct == NULL) |
- | 332 | { |
|
- | 333 | return HAL_ERROR; |
|
- | 334 | } |
|
- | 335 | ||
307 | assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType)); |
336 | assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType)); |
308 | 337 | ||
- | 338 | sysclk_source = __HAL_RCC_GET_SYSCLK_SOURCE(); |
|
- | 339 | pll_config = __HAL_RCC_GET_PLL_OSCSOURCE(); |
|
- | 340 | ||
309 | /*------------------------------- HSE Configuration ------------------------*/ |
341 | /*------------------------------- HSE Configuration ------------------------*/ |
310 | if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE) |
342 | if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE) |
311 | { |
343 | { |
312 | /* Check the parameters */ |
344 | /* Check the parameters */ |
313 | assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState)); |
345 | assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState)); |
314 | 346 | ||
315 | /* When the HSE is used as system clock or clock source for PLL in these cases it is not allowed to be disabled */ |
347 | /* When the HSE is used as system clock or clock source for PLL in these cases it is not allowed to be disabled */ |
316 | if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_HSE) |
348 | if((sysclk_source == RCC_SYSCLKSOURCE_STATUS_HSE) |
317 | || ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && (__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSE))) |
349 | || ((sysclk_source == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && (pll_config == RCC_PLLSOURCE_HSE))) |
318 | { |
350 | { |
319 | if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET) && (RCC_OscInitStruct->HSEState == RCC_HSE_OFF)) |
351 | if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != 0U) && (RCC_OscInitStruct->HSEState == RCC_HSE_OFF)) |
320 | { |
352 | { |
321 | return HAL_ERROR; |
353 | return HAL_ERROR; |
322 | } |
354 | } |
323 | } |
355 | } |
324 | else |
356 | else |
325 | { |
357 | { |
326 | /* Set the new HSE configuration ---------------------------------------*/ |
358 | /* Set the new HSE configuration ---------------------------------------*/ |
327 | __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState); |
359 | __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState); |
328 | - | ||
329 | 360 | ||
330 | /* Check the HSE State */ |
361 | /* Check the HSE State */ |
331 | if(RCC_OscInitStruct->HSEState != RCC_HSE_OFF) |
362 | if(RCC_OscInitStruct->HSEState != RCC_HSE_OFF) |
332 | { |
363 | { |
333 | /* Get Start Tick */ |
364 | /* Get Start Tick */ |
334 | tickstart = HAL_GetTick(); |
365 | tickstart = HAL_GetTick(); |
335 | 366 | ||
336 | /* Wait till HSE is ready */ |
367 | /* Wait till HSE is ready */ |
337 | while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET) |
368 | while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == 0U) |
338 | { |
369 | { |
339 | if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE) |
370 | if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE) |
340 | { |
371 | { |
341 | return HAL_TIMEOUT; |
372 | return HAL_TIMEOUT; |
342 | } |
373 | } |
Line 344... | Line 375... | ||
344 | } |
375 | } |
345 | else |
376 | else |
346 | { |
377 | { |
347 | /* Get Start Tick */ |
378 | /* Get Start Tick */ |
348 | tickstart = HAL_GetTick(); |
379 | tickstart = HAL_GetTick(); |
349 | 380 | ||
350 | /* Wait till HSE is disabled */ |
381 | /* Wait till HSE is disabled */ |
351 | while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET) |
382 | while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != 0U) |
352 | { |
383 | { |
353 | if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE) |
384 | if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE) |
354 | { |
385 | { |
355 | return HAL_TIMEOUT; |
386 | return HAL_TIMEOUT; |
356 | } |
387 | } |
357 | } |
388 | } |
358 | } |
389 | } |
359 | } |
390 | } |
360 | } |
391 | } |
361 | /*----------------------------- HSI Configuration --------------------------*/ |
392 | /*----------------------------- HSI Configuration --------------------------*/ |
362 | if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI) |
393 | if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI) |
363 | { |
394 | { |
364 | /* Check the parameters */ |
395 | /* Check the parameters */ |
365 | assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState)); |
396 | assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState)); |
366 | assert_param(IS_RCC_CALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue)); |
397 | assert_param(IS_RCC_CALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue)); |
367 | 398 | ||
368 | /* Check if HSI is used as system clock or as PLL source when PLL is selected as system clock */ |
399 | /* Check if HSI is used as system clock or as PLL source when PLL is selected as system clock */ |
369 | if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_HSI) |
400 | if((sysclk_source == RCC_SYSCLKSOURCE_STATUS_HSI) |
370 | || ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && (__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSI))) |
401 | || ((sysclk_source == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && (pll_config == RCC_PLLSOURCE_HSI))) |
371 | { |
402 | { |
372 | /* When HSI is used as system clock it will not disabled */ |
403 | /* When HSI is used as system clock it will not disabled */ |
373 | if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET) && (RCC_OscInitStruct->HSIState != RCC_HSI_ON)) |
404 | if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != 0U) && (RCC_OscInitStruct->HSIState != RCC_HSI_ON)) |
374 | { |
405 | { |
375 | return HAL_ERROR; |
406 | return HAL_ERROR; |
376 | } |
407 | } |
377 | /* Otherwise, just the calibration is allowed */ |
408 | /* Otherwise, just the calibration is allowed */ |
378 | else |
409 | else |
Line 384... | Line 415... | ||
384 | else |
415 | else |
385 | { |
416 | { |
386 | /* Check the HSI State */ |
417 | /* Check the HSI State */ |
387 | if(RCC_OscInitStruct->HSIState != RCC_HSI_OFF) |
418 | if(RCC_OscInitStruct->HSIState != RCC_HSI_OFF) |
388 | { |
419 | { |
389 | /* Enable the Internal High Speed oscillator (HSI). */ |
420 | /* Enable the Internal High Speed oscillator (HSI). */ |
390 | __HAL_RCC_HSI_ENABLE(); |
421 | __HAL_RCC_HSI_ENABLE(); |
391 | 422 | ||
392 | /* Get Start Tick */ |
423 | /* Get Start Tick */ |
393 | tickstart = HAL_GetTick(); |
424 | tickstart = HAL_GetTick(); |
394 | 425 | ||
395 | /* Wait till HSI is ready */ |
426 | /* Wait till HSI is ready */ |
396 | while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET) |
427 | while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == 0U) |
397 | { |
428 | { |
398 | if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE) |
429 | if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE) |
399 | { |
430 | { |
400 | return HAL_TIMEOUT; |
431 | return HAL_TIMEOUT; |
401 | } |
432 | } |
402 | } |
433 | } |
403 | - | ||
- | 434 | ||
404 | /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/ |
435 | /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/ |
405 | __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue); |
436 | __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue); |
406 | } |
437 | } |
407 | else |
438 | else |
408 | { |
439 | { |
409 | /* Disable the Internal High Speed oscillator (HSI). */ |
440 | /* Disable the Internal High Speed oscillator (HSI). */ |
410 | __HAL_RCC_HSI_DISABLE(); |
441 | __HAL_RCC_HSI_DISABLE(); |
411 | 442 | ||
412 | /* Get Start Tick */ |
443 | /* Get Start Tick */ |
413 | tickstart = HAL_GetTick(); |
444 | tickstart = HAL_GetTick(); |
414 | 445 | ||
415 | /* Wait till HSI is disabled */ |
446 | /* Wait till HSI is disabled */ |
416 | while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET) |
447 | while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != 0U) |
417 | { |
448 | { |
418 | if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE) |
449 | if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE) |
419 | { |
450 | { |
420 | return HAL_TIMEOUT; |
451 | return HAL_TIMEOUT; |
421 | } |
452 | } |
422 | } |
453 | } |
423 | } |
454 | } |
424 | } |
455 | } |
425 | } |
456 | } |
426 | /*----------------------------- MSI Configuration --------------------------*/ |
457 | /*----------------------------- MSI Configuration --------------------------*/ |
427 | if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_MSI) == RCC_OSCILLATORTYPE_MSI) |
458 | if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_MSI) == RCC_OSCILLATORTYPE_MSI) |
428 | { |
459 | { |
429 | /* When the MSI is used as system clock it will not be disabled */ |
460 | /* When the MSI is used as system clock it will not be disabled */ |
430 | if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_MSI) ) |
461 | if(sysclk_source == RCC_CFGR_SWS_MSI) |
431 | { |
462 | { |
432 | if((__HAL_RCC_GET_FLAG(RCC_FLAG_MSIRDY) != RESET) && (RCC_OscInitStruct->MSIState == RCC_MSI_OFF)) |
463 | if((__HAL_RCC_GET_FLAG(RCC_FLAG_MSIRDY) != 0U) && (RCC_OscInitStruct->MSIState == RCC_MSI_OFF)) |
433 | { |
464 | { |
434 | return HAL_ERROR; |
465 | return HAL_ERROR; |
435 | } |
466 | } |
436 | /* Otherwise, just the calibration and MSI range change are allowed */ |
467 | /* Otherwise, just the calibration and MSI range change are allowed */ |
437 | else |
468 | else |
Line 466... | Line 497... | ||
466 | 497 | ||
467 | /* Decrease number of wait states update if necessary */ |
498 | /* Decrease number of wait states update if necessary */ |
468 | if(RCC_SetFlashLatencyFromMSIRange(RCC_OscInitStruct->MSIClockRange) != HAL_OK) |
499 | if(RCC_SetFlashLatencyFromMSIRange(RCC_OscInitStruct->MSIClockRange) != HAL_OK) |
469 | { |
500 | { |
470 | return HAL_ERROR; |
501 | return HAL_ERROR; |
471 | } |
502 | } |
472 | } |
503 | } |
473 | 504 | ||
474 | /* Update the SystemCoreClock global variable */ |
505 | /* Update the SystemCoreClock global variable */ |
475 | SystemCoreClock = (32768U * (1U << ((RCC_OscInitStruct->MSIClockRange >> RCC_ICSCR_MSIRANGE_BITNUMBER) + 1U))) |
506 | SystemCoreClock = (32768U * (1UL << ((RCC_OscInitStruct->MSIClockRange >> RCC_ICSCR_MSIRANGE_Pos) + 1U))) |
476 | >> AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> RCC_CFGR_HPRE_BITNUMBER)]; |
507 | >> AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> RCC_CFGR_HPRE_Pos)]; |
477 | 508 | ||
478 | /* Configure the source of time base considering new system clocks settings*/ |
509 | /* Configure the source of time base considering new system clocks settings*/ |
479 | HAL_InitTick (TICK_INT_PRIORITY); |
510 | status = HAL_InitTick(uwTickPrio); |
- | 511 | if(status != HAL_OK) |
|
- | 512 | { |
|
- | 513 | return status; |
|
- | 514 | } |
|
480 | } |
515 | } |
481 | } |
516 | } |
482 | else |
517 | else |
483 | { |
518 | { |
484 | /* Check MSI State */ |
519 | /* Check MSI State */ |
Line 492... | Line 527... | ||
492 | 527 | ||
493 | /* Get Start Tick */ |
528 | /* Get Start Tick */ |
494 | tickstart = HAL_GetTick(); |
529 | tickstart = HAL_GetTick(); |
495 | 530 | ||
496 | /* Wait till MSI is ready */ |
531 | /* Wait till MSI is ready */ |
497 | while(__HAL_RCC_GET_FLAG(RCC_FLAG_MSIRDY) == RESET) |
532 | while(__HAL_RCC_GET_FLAG(RCC_FLAG_MSIRDY) == 0U) |
498 | { |
533 | { |
499 | if((HAL_GetTick() - tickstart) > MSI_TIMEOUT_VALUE) |
534 | if((HAL_GetTick() - tickstart) > MSI_TIMEOUT_VALUE) |
500 | { |
535 | { |
501 | return HAL_TIMEOUT; |
536 | return HAL_TIMEOUT; |
502 | } |
537 | } |
Line 518... | Line 553... | ||
518 | 553 | ||
519 | /* Get Start Tick */ |
554 | /* Get Start Tick */ |
520 | tickstart = HAL_GetTick(); |
555 | tickstart = HAL_GetTick(); |
521 | 556 | ||
522 | /* Wait till MSI is ready */ |
557 | /* Wait till MSI is ready */ |
523 | while(__HAL_RCC_GET_FLAG(RCC_FLAG_MSIRDY) != RESET) |
558 | while(__HAL_RCC_GET_FLAG(RCC_FLAG_MSIRDY) != 0U) |
524 | { |
559 | { |
525 | if((HAL_GetTick() - tickstart) > MSI_TIMEOUT_VALUE) |
560 | if((HAL_GetTick() - tickstart) > MSI_TIMEOUT_VALUE) |
526 | { |
561 | { |
527 | return HAL_TIMEOUT; |
562 | return HAL_TIMEOUT; |
528 | } |
563 | } |
529 | } |
564 | } |
530 | } |
565 | } |
531 | } |
566 | } |
532 | } |
567 | } |
533 | /*------------------------------ LSI Configuration -------------------------*/ |
568 | /*------------------------------ LSI Configuration -------------------------*/ |
534 | if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI) |
569 | if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI) |
535 | { |
570 | { |
536 | /* Check the parameters */ |
571 | /* Check the parameters */ |
537 | assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState)); |
572 | assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState)); |
538 | 573 | ||
539 | /* Check the LSI State */ |
574 | /* Check the LSI State */ |
540 | if(RCC_OscInitStruct->LSIState != RCC_LSI_OFF) |
575 | if(RCC_OscInitStruct->LSIState != RCC_LSI_OFF) |
541 | { |
576 | { |
542 | /* Enable the Internal Low Speed oscillator (LSI). */ |
577 | /* Enable the Internal Low Speed oscillator (LSI). */ |
543 | __HAL_RCC_LSI_ENABLE(); |
578 | __HAL_RCC_LSI_ENABLE(); |
544 | 579 | ||
545 | /* Get Start Tick */ |
580 | /* Get Start Tick */ |
546 | tickstart = HAL_GetTick(); |
581 | tickstart = HAL_GetTick(); |
547 | 582 | ||
548 | /* Wait till LSI is ready */ |
583 | /* Wait till LSI is ready */ |
549 | while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) == RESET) |
584 | while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) == 0U) |
550 | { |
585 | { |
551 | if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE) |
586 | if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE) |
552 | { |
587 | { |
553 | return HAL_TIMEOUT; |
588 | return HAL_TIMEOUT; |
554 | } |
589 | } |
Line 556... | Line 591... | ||
556 | } |
591 | } |
557 | else |
592 | else |
558 | { |
593 | { |
559 | /* Disable the Internal Low Speed oscillator (LSI). */ |
594 | /* Disable the Internal Low Speed oscillator (LSI). */ |
560 | __HAL_RCC_LSI_DISABLE(); |
595 | __HAL_RCC_LSI_DISABLE(); |
561 | 596 | ||
562 | /* Get Start Tick */ |
597 | /* Get Start Tick */ |
563 | tickstart = HAL_GetTick(); |
598 | tickstart = HAL_GetTick(); |
564 | 599 | ||
565 | /* Wait till LSI is disabled */ |
600 | /* Wait till LSI is disabled */ |
566 | while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) != RESET) |
601 | while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) != 0U) |
567 | { |
602 | { |
568 | if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE) |
603 | if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE) |
569 | { |
604 | { |
570 | return HAL_TIMEOUT; |
605 | return HAL_TIMEOUT; |
571 | } |
606 | } |
572 | } |
607 | } |
573 | } |
608 | } |
574 | } |
609 | } |
575 | /*------------------------------ LSE Configuration -------------------------*/ |
610 | /*------------------------------ LSE Configuration -------------------------*/ |
576 | if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE) |
611 | if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE) |
577 | { |
612 | { |
578 | FlagStatus pwrclkchanged = RESET; |
613 | FlagStatus pwrclkchanged = RESET; |
579 | 614 | ||
580 | /* Check the parameters */ |
615 | /* Check the parameters */ |
581 | assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState)); |
616 | assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState)); |
582 | 617 | ||
583 | /* Update LSE configuration in Backup Domain control register */ |
618 | /* Update LSE configuration in Backup Domain control register */ |
584 | /* Requires to enable write access to Backup Domain of necessary */ |
619 | /* Requires to enable write access to Backup Domain of necessary */ |
585 | if(__HAL_RCC_PWR_IS_CLK_DISABLED()) |
620 | if(__HAL_RCC_PWR_IS_CLK_DISABLED()) |
586 | { |
621 | { |
587 | __HAL_RCC_PWR_CLK_ENABLE(); |
622 | __HAL_RCC_PWR_CLK_ENABLE(); |
588 | pwrclkchanged = SET; |
623 | pwrclkchanged = SET; |
589 | } |
624 | } |
590 | 625 | ||
591 | if(HAL_IS_BIT_CLR(PWR->CR, PWR_CR_DBP)) |
626 | if(HAL_IS_BIT_CLR(PWR->CR, PWR_CR_DBP)) |
592 | { |
627 | { |
593 | /* Enable write access to Backup domain */ |
628 | /* Enable write access to Backup domain */ |
594 | SET_BIT(PWR->CR, PWR_CR_DBP); |
629 | SET_BIT(PWR->CR, PWR_CR_DBP); |
595 | 630 | ||
596 | /* Wait for Backup domain Write protection disable */ |
631 | /* Wait for Backup domain Write protection disable */ |
597 | tickstart = HAL_GetTick(); |
632 | tickstart = HAL_GetTick(); |
598 | 633 | ||
599 | while(HAL_IS_BIT_CLR(PWR->CR, PWR_CR_DBP)) |
634 | while(HAL_IS_BIT_CLR(PWR->CR, PWR_CR_DBP)) |
600 | { |
635 | { |
Line 610... | Line 645... | ||
610 | /* Check the LSE State */ |
645 | /* Check the LSE State */ |
611 | if(RCC_OscInitStruct->LSEState != RCC_LSE_OFF) |
646 | if(RCC_OscInitStruct->LSEState != RCC_LSE_OFF) |
612 | { |
647 | { |
613 | /* Get Start Tick */ |
648 | /* Get Start Tick */ |
614 | tickstart = HAL_GetTick(); |
649 | tickstart = HAL_GetTick(); |
615 | 650 | ||
616 | /* Wait till LSE is ready */ |
651 | /* Wait till LSE is ready */ |
617 | while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET) |
652 | while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == 0U) |
618 | { |
653 | { |
619 | if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE) |
654 | if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE) |
620 | { |
655 | { |
621 | return HAL_TIMEOUT; |
656 | return HAL_TIMEOUT; |
622 | } |
657 | } |
Line 624... | Line 659... | ||
624 | } |
659 | } |
625 | else |
660 | else |
626 | { |
661 | { |
627 | /* Get Start Tick */ |
662 | /* Get Start Tick */ |
628 | tickstart = HAL_GetTick(); |
663 | tickstart = HAL_GetTick(); |
629 | 664 | ||
630 | /* Wait till LSE is disabled */ |
665 | /* Wait till LSE is disabled */ |
631 | while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) != RESET) |
666 | while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) != 0U) |
632 | { |
667 | { |
633 | if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE) |
668 | if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE) |
634 | { |
669 | { |
635 | return HAL_TIMEOUT; |
670 | return HAL_TIMEOUT; |
636 | } |
671 | } |
Line 648... | Line 683... | ||
648 | /* Check the parameters */ |
683 | /* Check the parameters */ |
649 | assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState)); |
684 | assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState)); |
650 | if ((RCC_OscInitStruct->PLL.PLLState) != RCC_PLL_NONE) |
685 | if ((RCC_OscInitStruct->PLL.PLLState) != RCC_PLL_NONE) |
651 | { |
686 | { |
652 | /* Check if the PLL is used as system clock or not */ |
687 | /* Check if the PLL is used as system clock or not */ |
653 | if(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_PLLCLK) |
688 | if(sysclk_source != RCC_SYSCLKSOURCE_STATUS_PLLCLK) |
654 | { |
689 | { |
655 | if((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_ON) |
690 | if((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_ON) |
656 | { |
691 | { |
657 | /* Check the parameters */ |
692 | /* Check the parameters */ |
658 | assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource)); |
693 | assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource)); |
659 | assert_param(IS_RCC_PLL_MUL(RCC_OscInitStruct->PLL.PLLMUL)); |
694 | assert_param(IS_RCC_PLL_MUL(RCC_OscInitStruct->PLL.PLLMUL)); |
660 | assert_param(IS_RCC_PLL_DIV(RCC_OscInitStruct->PLL.PLLDIV)); |
695 | assert_param(IS_RCC_PLL_DIV(RCC_OscInitStruct->PLL.PLLDIV)); |
661 | 696 | ||
662 | /* Disable the main PLL. */ |
697 | /* Disable the main PLL. */ |
663 | __HAL_RCC_PLL_DISABLE(); |
698 | __HAL_RCC_PLL_DISABLE(); |
664 | 699 | ||
665 | /* Get Start Tick */ |
700 | /* Get Start Tick */ |
666 | tickstart = HAL_GetTick(); |
701 | tickstart = HAL_GetTick(); |
667 | 702 | ||
668 | /* Wait till PLL is disabled */ |
703 | /* Wait till PLL is disabled */ |
669 | while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET) |
704 | while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != 0U) |
670 | { |
705 | { |
671 | if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE) |
706 | if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE) |
672 | { |
707 | { |
673 | return HAL_TIMEOUT; |
708 | return HAL_TIMEOUT; |
674 | } |
709 | } |
Line 678... | Line 713... | ||
678 | __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource, |
713 | __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource, |
679 | RCC_OscInitStruct->PLL.PLLMUL, |
714 | RCC_OscInitStruct->PLL.PLLMUL, |
680 | RCC_OscInitStruct->PLL.PLLDIV); |
715 | RCC_OscInitStruct->PLL.PLLDIV); |
681 | /* Enable the main PLL. */ |
716 | /* Enable the main PLL. */ |
682 | __HAL_RCC_PLL_ENABLE(); |
717 | __HAL_RCC_PLL_ENABLE(); |
683 | 718 | ||
684 | /* Get Start Tick */ |
719 | /* Get Start Tick */ |
685 | tickstart = HAL_GetTick(); |
720 | tickstart = HAL_GetTick(); |
686 | 721 | ||
687 | /* Wait till PLL is ready */ |
722 | /* Wait till PLL is ready */ |
688 | while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET) |
723 | while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == 0U) |
689 | { |
724 | { |
690 | if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE) |
725 | if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE) |
691 | { |
726 | { |
692 | return HAL_TIMEOUT; |
727 | return HAL_TIMEOUT; |
693 | } |
728 | } |
Line 695... | Line 730... | ||
695 | } |
730 | } |
696 | else |
731 | else |
697 | { |
732 | { |
698 | /* Disable the main PLL. */ |
733 | /* Disable the main PLL. */ |
699 | __HAL_RCC_PLL_DISABLE(); |
734 | __HAL_RCC_PLL_DISABLE(); |
700 | 735 | ||
701 | /* Get Start Tick */ |
736 | /* Get Start Tick */ |
702 | tickstart = HAL_GetTick(); |
737 | tickstart = HAL_GetTick(); |
703 | 738 | ||
704 | /* Wait till PLL is disabled */ |
739 | /* Wait till PLL is disabled */ |
705 | while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET) |
740 | while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != 0U) |
706 | { |
741 | { |
707 | if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE) |
742 | if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE) |
708 | { |
743 | { |
709 | return HAL_TIMEOUT; |
744 | return HAL_TIMEOUT; |
710 | } |
745 | } |
711 | } |
746 | } |
712 | } |
747 | } |
713 | } |
748 | } |
714 | else |
749 | else |
715 | { |
750 | { |
- | 751 | /* Check if there is a request to disable the PLL used as System clock source */ |
|
- | 752 | if((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_OFF) |
|
- | 753 | { |
|
716 | return HAL_ERROR; |
754 | return HAL_ERROR; |
- | 755 | } |
|
- | 756 | else |
|
- | 757 | { |
|
- | 758 | /* Do not return HAL_ERROR if request repeats the current configuration */ |
|
- | 759 | pll_config = RCC->CFGR; |
|
- | 760 | if((READ_BIT(pll_config, RCC_CFGR_PLLSRC) != RCC_OscInitStruct->PLL.PLLSource) || |
|
- | 761 | (READ_BIT(pll_config, RCC_CFGR_PLLMUL) != RCC_OscInitStruct->PLL.PLLMUL) || |
|
- | 762 | (READ_BIT(pll_config, RCC_CFGR_PLLDIV) != RCC_OscInitStruct->PLL.PLLDIV)) |
|
- | 763 | { |
|
- | 764 | return HAL_ERROR; |
|
- | 765 | } |
|
- | 766 | } |
|
717 | } |
767 | } |
718 | } |
768 | } |
719 | 769 | ||
720 | return HAL_OK; |
770 | return HAL_OK; |
721 | } |
771 | } |
722 | 772 | ||
723 | /** |
773 | /** |
724 | * @brief Initializes the CPU, AHB and APB buses clocks according to the specified |
774 | * @brief Initializes the CPU, AHB and APB buses clocks according to the specified |
725 | * parameters in the RCC_ClkInitStruct. |
775 | * parameters in the RCC_ClkInitStruct. |
726 | * @param RCC_ClkInitStruct pointer to an RCC_OscInitTypeDef structure that |
776 | * @param RCC_ClkInitStruct pointer to an RCC_OscInitTypeDef structure that |
727 | * contains the configuration information for the RCC peripheral. |
777 | * contains the configuration information for the RCC peripheral. |
728 | * @param FLatency FLASH Latency |
778 | * @param FLatency FLASH Latency |
729 | * The value of this parameter depend on device used within the same series |
779 | * The value of this parameter depend on device used within the same series |
730 | * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency |
780 | * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency |
731 | * and updated by @ref HAL_RCC_GetHCLKFreq() function called within this function |
781 | * and updated by @ref HAL_RCC_GetHCLKFreq() function called within this function |
732 | * |
782 | * |
733 | * @note The MSI is used (enabled by hardware) as system clock source after |
783 | * @note The MSI is used (enabled by hardware) as system clock source after |
734 | * start-up from Reset, wake-up from STOP and STANDBY mode, or in case |
784 | * start-up from Reset, wake-up from STOP and STANDBY mode, or in case |
735 | * of failure of the HSE used directly or indirectly as system clock |
785 | * of failure of the HSE used directly or indirectly as system clock |
736 | * (if the Clock Security System CSS is enabled). |
786 | * (if the Clock Security System CSS is enabled). |
737 | * |
787 | * |
738 | * @note A switch from one clock source to another occurs only if the target |
788 | * @note A switch from one clock source to another occurs only if the target |
739 | * clock source is ready (clock stable after start-up delay or PLL locked). |
789 | * clock source is ready (clock stable after start-up delay or PLL locked). |
740 | * If a clock source which is not yet ready is selected, the switch will |
790 | * If a clock source which is not yet ready is selected, the switch will |
741 | * occur when the clock source will be ready. |
791 | * occur when the clock source will be ready. |
742 | * You can use @ref HAL_RCC_GetClockConfig() function to know which clock is |
792 | * You can use @ref HAL_RCC_GetClockConfig() function to know which clock is |
743 | * currently used as system clock source. |
793 | * currently used as system clock source. |
744 | * @note Depending on the device voltage range, the software has to set correctly |
794 | * @note Depending on the device voltage range, the software has to set correctly |
745 | * HPRE[3:0] bits to ensure that HCLK not exceed the maximum allowed frequency |
795 | * HPRE[3:0] bits to ensure that HCLK not exceed the maximum allowed frequency |
746 | * (for more details refer to section above "Initialization/de-initialization functions") |
796 | * (for more details refer to section above "Initialization/de-initialization functions") |
747 | * @retval HAL status |
797 | * @retval HAL status |
748 | */ |
798 | */ |
749 | HAL_StatusTypeDef HAL_RCC_ClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t FLatency) |
799 | HAL_StatusTypeDef HAL_RCC_ClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t FLatency) |
750 | { |
800 | { |
751 | uint32_t tickstart = 0U; |
801 | uint32_t tickstart; |
- | 802 | HAL_StatusTypeDef status; |
|
752 | 803 | ||
753 | /* Check the parameters */ |
804 | /* Check the parameters */ |
754 | assert_param(RCC_ClkInitStruct != NULL); |
805 | if(RCC_ClkInitStruct == NULL) |
- | 806 | { |
|
755 | assert_param(IS_RCC_CLOCKTYPE(RCC_ClkInitStruct->ClockType)); |
807 | return HAL_ERROR; |
- | 808 | } |
|
- | 809 | ||
756 | assert_param(IS_FLASH_LATENCY(FLatency)); |
810 | assert_param(IS_FLASH_LATENCY(FLatency)); |
757 | 811 | ||
758 | /* To correctly read data from FLASH memory, the number of wait states (LATENCY) |
812 | /* To correctly read data from FLASH memory, the number of wait states (LATENCY) |
759 | must be correctly programmed according to the frequency of the CPU clock |
813 | must be correctly programmed according to the frequency of the CPU clock |
760 | (HCLK) and the supply voltage of the device. */ |
814 | (HCLK) and the supply voltage of the device. */ |
761 | 815 | ||
762 | /* Increasing the number of wait states because of higher CPU frequency */ |
816 | /* Increasing the number of wait states because of higher CPU frequency */ |
763 | if(FLatency > (FLASH->ACR & FLASH_ACR_LATENCY)) |
817 | if(FLatency > __HAL_FLASH_GET_LATENCY()) |
764 | { |
818 | { |
765 | /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */ |
819 | /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */ |
766 | __HAL_FLASH_SET_LATENCY(FLatency); |
820 | __HAL_FLASH_SET_LATENCY(FLatency); |
767 | 821 | ||
768 | /* Check that the new number of wait states is taken into account to access the Flash |
822 | /* Check that the new number of wait states is taken into account to access the Flash |
769 | memory by reading the FLASH_ACR register */ |
823 | memory by reading the FLASH_ACR register */ |
770 | if((FLASH->ACR & FLASH_ACR_LATENCY) != FLatency) |
824 | if(__HAL_FLASH_GET_LATENCY() != FLatency) |
771 | { |
825 | { |
772 | return HAL_ERROR; |
826 | return HAL_ERROR; |
773 | } |
827 | } |
774 | } |
828 | } |
775 | 829 | ||
Line 778... | Line 832... | ||
778 | { |
832 | { |
779 | assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider)); |
833 | assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider)); |
780 | MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider); |
834 | MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider); |
781 | } |
835 | } |
782 | 836 | ||
783 | /*------------------------- SYSCLK Configuration ---------------------------*/ |
837 | /*------------------------- SYSCLK Configuration ---------------------------*/ |
784 | if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_SYSCLK) == RCC_CLOCKTYPE_SYSCLK) |
838 | if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_SYSCLK) == RCC_CLOCKTYPE_SYSCLK) |
785 | { |
839 | { |
786 | assert_param(IS_RCC_SYSCLKSOURCE(RCC_ClkInitStruct->SYSCLKSource)); |
840 | assert_param(IS_RCC_SYSCLKSOURCE(RCC_ClkInitStruct->SYSCLKSource)); |
787 | 841 | ||
788 | /* HSE is selected as System Clock Source */ |
842 | /* HSE is selected as System Clock Source */ |
789 | if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE) |
843 | if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE) |
790 | { |
844 | { |
791 | /* Check the HSE ready flag */ |
845 | /* Check the HSE ready flag */ |
792 | if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET) |
846 | if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == 0U) |
793 | { |
847 | { |
794 | return HAL_ERROR; |
848 | return HAL_ERROR; |
795 | } |
849 | } |
796 | } |
850 | } |
797 | /* PLL is selected as System Clock Source */ |
851 | /* PLL is selected as System Clock Source */ |
798 | else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK) |
852 | else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK) |
799 | { |
853 | { |
800 | /* Check the PLL ready flag */ |
854 | /* Check the PLL ready flag */ |
801 | if(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET) |
855 | if(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == 0U) |
802 | { |
856 | { |
803 | return HAL_ERROR; |
857 | return HAL_ERROR; |
804 | } |
858 | } |
805 | } |
859 | } |
806 | /* HSI is selected as System Clock Source */ |
860 | /* HSI is selected as System Clock Source */ |
807 | else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSI) |
861 | else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSI) |
808 | { |
862 | { |
809 | /* Check the HSI ready flag */ |
863 | /* Check the HSI ready flag */ |
810 | if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET) |
864 | if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == 0U) |
811 | { |
865 | { |
812 | return HAL_ERROR; |
866 | return HAL_ERROR; |
813 | } |
867 | } |
814 | } |
868 | } |
815 | /* MSI is selected as System Clock Source */ |
869 | /* MSI is selected as System Clock Source */ |
816 | else |
870 | else |
817 | { |
871 | { |
818 | /* Check the MSI ready flag */ |
872 | /* Check the MSI ready flag */ |
819 | if(__HAL_RCC_GET_FLAG(RCC_FLAG_MSIRDY) == RESET) |
873 | if(__HAL_RCC_GET_FLAG(RCC_FLAG_MSIRDY) == 0U) |
820 | { |
874 | { |
821 | return HAL_ERROR; |
875 | return HAL_ERROR; |
822 | } |
876 | } |
823 | } |
877 | } |
824 | __HAL_RCC_SYSCLK_CONFIG(RCC_ClkInitStruct->SYSCLKSource); |
878 | __HAL_RCC_SYSCLK_CONFIG(RCC_ClkInitStruct->SYSCLKSource); |
825 | 879 | ||
826 | /* Get Start Tick */ |
880 | /* Get Start Tick */ |
827 | tickstart = HAL_GetTick(); |
881 | tickstart = HAL_GetTick(); |
828 | 882 | ||
829 | if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE) |
883 | if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE) |
830 | { |
884 | { |
831 | while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_HSE) |
885 | while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_HSE) |
832 | { |
886 | { |
833 | if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE) |
887 | if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE) |
Line 853... | Line 907... | ||
853 | if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE) |
907 | if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE) |
854 | { |
908 | { |
855 | return HAL_TIMEOUT; |
909 | return HAL_TIMEOUT; |
856 | } |
910 | } |
857 | } |
911 | } |
858 | } |
912 | } |
859 | else |
913 | else |
860 | { |
914 | { |
861 | while(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_MSI) |
915 | while(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_MSI) |
862 | { |
916 | { |
863 | if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE) |
917 | if((HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE) |
864 | { |
918 | { |
865 | return HAL_TIMEOUT; |
919 | return HAL_TIMEOUT; |
866 | } |
920 | } |
867 | } |
921 | } |
868 | } |
922 | } |
869 | } |
923 | } |
870 | /* Decreasing the number of wait states because of lower CPU frequency */ |
924 | /* Decreasing the number of wait states because of lower CPU frequency */ |
871 | if(FLatency < (FLASH->ACR & FLASH_ACR_LATENCY)) |
925 | if(FLatency < __HAL_FLASH_GET_LATENCY()) |
872 | { |
926 | { |
873 | /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */ |
927 | /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */ |
874 | __HAL_FLASH_SET_LATENCY(FLatency); |
928 | __HAL_FLASH_SET_LATENCY(FLatency); |
875 | 929 | ||
876 | /* Check that the new number of wait states is taken into account to access the Flash |
930 | /* Check that the new number of wait states is taken into account to access the Flash |
877 | memory by reading the FLASH_ACR register */ |
931 | memory by reading the FLASH_ACR register */ |
878 | if((FLASH->ACR & FLASH_ACR_LATENCY) != FLatency) |
932 | if(__HAL_FLASH_GET_LATENCY() != FLatency) |
879 | { |
933 | { |
880 | return HAL_ERROR; |
934 | return HAL_ERROR; |
881 | } |
935 | } |
882 | } |
936 | } |
883 | 937 | ||
884 | /*-------------------------- PCLK1 Configuration ---------------------------*/ |
938 | /*-------------------------- PCLK1 Configuration ---------------------------*/ |
885 | if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1) |
939 | if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1) |
886 | { |
940 | { |
887 | assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB1CLKDivider)); |
941 | assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB1CLKDivider)); |
888 | MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE1, RCC_ClkInitStruct->APB1CLKDivider); |
942 | MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE1, RCC_ClkInitStruct->APB1CLKDivider); |
889 | } |
943 | } |
890 | 944 | ||
891 | /*-------------------------- PCLK2 Configuration ---------------------------*/ |
945 | /*-------------------------- PCLK2 Configuration ---------------------------*/ |
892 | if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2) |
946 | if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2) |
893 | { |
947 | { |
894 | assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB2CLKDivider)); |
948 | assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB2CLKDivider)); |
895 | MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE2, ((RCC_ClkInitStruct->APB2CLKDivider) << 3)); |
949 | MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE2, ((RCC_ClkInitStruct->APB2CLKDivider) << 3U)); |
896 | } |
950 | } |
897 | 951 | ||
898 | /* Update the SystemCoreClock global variable */ |
952 | /* Update the SystemCoreClock global variable */ |
899 | SystemCoreClock = HAL_RCC_GetSysClockFreq() >> AHBPrescTable[(RCC->CFGR & RCC_CFGR_HPRE)>> RCC_CFGR_HPRE_BITNUMBER]; |
953 | SystemCoreClock = HAL_RCC_GetSysClockFreq() >> AHBPrescTable[(RCC->CFGR & RCC_CFGR_HPRE)>> RCC_CFGR_HPRE_Pos]; |
900 | 954 | ||
901 | /* Configure the source of time base considering new system clocks settings*/ |
955 | /* Configure the source of time base considering new system clocks settings*/ |
902 | HAL_InitTick (TICK_INT_PRIORITY); |
956 | status = HAL_InitTick(uwTickPrio); |
903 | 957 | ||
904 | return HAL_OK; |
958 | return status; |
905 | } |
959 | } |
906 | 960 | ||
907 | /** |
961 | /** |
908 | * @} |
962 | * @} |
909 | */ |
963 | */ |
910 | 964 | ||
911 | /** @defgroup RCC_Exported_Functions_Group2 Peripheral Control functions |
965 | /** @defgroup RCC_Exported_Functions_Group2 Peripheral Control functions |
912 | * @brief RCC clocks control functions |
966 | * @brief RCC clocks control functions |
913 | * |
967 | * |
914 | @verbatim |
968 | @verbatim |
915 | =============================================================================== |
969 | =============================================================================== |
916 | ##### Peripheral Control functions ##### |
970 | ##### Peripheral Control functions ##### |
917 | =============================================================================== |
971 | =============================================================================== |
918 | [..] |
972 | [..] |
919 | This subsection provides a set of functions allowing to control the RCC Clocks |
973 | This subsection provides a set of functions allowing to control the RCC Clocks |
920 | frequencies. |
974 | frequencies. |
921 | 975 | ||
922 | @endverbatim |
976 | @endverbatim |
923 | * @{ |
977 | * @{ |
924 | */ |
978 | */ |
Line 933... | Line 987... | ||
933 | * This parameter can be one of the following values: |
987 | * This parameter can be one of the following values: |
934 | * @arg @ref RCC_MCO1SOURCE_NOCLOCK No clock selected as MCO clock |
988 | * @arg @ref RCC_MCO1SOURCE_NOCLOCK No clock selected as MCO clock |
935 | * @arg @ref RCC_MCO1SOURCE_SYSCLK System clock selected as MCO clock |
989 | * @arg @ref RCC_MCO1SOURCE_SYSCLK System clock selected as MCO clock |
936 | * @arg @ref RCC_MCO1SOURCE_HSI HSI selected as MCO clock |
990 | * @arg @ref RCC_MCO1SOURCE_HSI HSI selected as MCO clock |
937 | * @arg @ref RCC_MCO1SOURCE_HSE HSE selected as MCO clock |
991 | * @arg @ref RCC_MCO1SOURCE_HSE HSE selected as MCO clock |
938 | * @arg @ref RCC_MCO1SOURCE_MSI MSI oscillator clock selected as MCO clock |
992 | * @arg @ref RCC_MCO1SOURCE_MSI MSI oscillator clock selected as MCO clock |
939 | * @arg @ref RCC_MCO1SOURCE_PLLCLK PLL clock selected as MCO clock |
993 | * @arg @ref RCC_MCO1SOURCE_PLLCLK PLL clock selected as MCO clock |
940 | * @arg @ref RCC_MCO1SOURCE_LSI LSI clock selected as MCO clock |
994 | * @arg @ref RCC_MCO1SOURCE_LSI LSI clock selected as MCO clock |
941 | * @arg @ref RCC_MCO1SOURCE_LSE LSE clock selected as MCO clock |
995 | * @arg @ref RCC_MCO1SOURCE_LSE LSE clock selected as MCO clock |
942 | * @param RCC_MCODiv specifies the MCO DIV. |
996 | * @param RCC_MCODiv specifies the MCO DIV. |
943 | * This parameter can be one of the following values: |
997 | * This parameter can be one of the following values: |
Line 948... | Line 1002... | ||
948 | * @arg @ref RCC_MCODIV_16 division by 16 applied to MCO clock |
1002 | * @arg @ref RCC_MCODIV_16 division by 16 applied to MCO clock |
949 | * @retval None |
1003 | * @retval None |
950 | */ |
1004 | */ |
951 | void HAL_RCC_MCOConfig(uint32_t RCC_MCOx, uint32_t RCC_MCOSource, uint32_t RCC_MCODiv) |
1005 | void HAL_RCC_MCOConfig(uint32_t RCC_MCOx, uint32_t RCC_MCOSource, uint32_t RCC_MCODiv) |
952 | { |
1006 | { |
953 | GPIO_InitTypeDef gpio = {0}; |
1007 | GPIO_InitTypeDef gpio; |
954 | 1008 | ||
955 | /* Check the parameters */ |
1009 | /* Check the parameters */ |
956 | assert_param(IS_RCC_MCO(RCC_MCOx)); |
1010 | assert_param(IS_RCC_MCO(RCC_MCOx)); |
957 | assert_param(IS_RCC_MCODIV(RCC_MCODiv)); |
1011 | assert_param(IS_RCC_MCODIV(RCC_MCODiv)); |
958 | assert_param(IS_RCC_MCO1SOURCE(RCC_MCOSource)); |
1012 | assert_param(IS_RCC_MCO1SOURCE(RCC_MCOSource)); |
959 | 1013 | ||
960 | /* Configure the MCO1 pin in alternate function mode */ |
1014 | /* Configure the MCO1 pin in alternate function mode */ |
961 | gpio.Mode = GPIO_MODE_AF_PP; |
1015 | gpio.Mode = GPIO_MODE_AF_PP; |
962 | gpio.Speed = GPIO_SPEED_FREQ_HIGH; |
1016 | gpio.Speed = GPIO_SPEED_FREQ_HIGH; |
963 | gpio.Pull = GPIO_NOPULL; |
1017 | gpio.Pull = GPIO_NOPULL; |
964 | gpio.Pin = MCO1_PIN; |
1018 | gpio.Pin = MCO1_PIN; |
965 | gpio.Alternate = GPIO_AF0_MCO; |
1019 | gpio.Alternate = GPIO_AF0_MCO; |
966 | 1020 | ||
967 | /* MCO1 Clock Enable */ |
1021 | /* MCO1 Clock Enable */ |
968 | MCO1_CLK_ENABLE(); |
1022 | MCO1_CLK_ENABLE(); |
969 | 1023 | ||
970 | HAL_GPIO_Init(MCO1_GPIO_PORT, &gpio); |
1024 | HAL_GPIO_Init(MCO1_GPIO_PORT, &gpio); |
971 | 1025 | ||
972 | /* Configure the MCO clock source */ |
1026 | /* Configure the MCO clock source */ |
973 | __HAL_RCC_MCO1_CONFIG(RCC_MCOSource, RCC_MCODiv); |
1027 | __HAL_RCC_MCO1_CONFIG(RCC_MCOSource, RCC_MCODiv); |
974 | } |
1028 | } |
975 | 1029 | ||
976 | /** |
1030 | /** |
977 | * @brief Enables the Clock Security System. |
1031 | * @brief Enables the Clock Security System. |
978 | * @note If a failure is detected on the HSE oscillator clock, this oscillator |
1032 | * @note If a failure is detected on the HSE oscillator clock, this oscillator |
979 | * is automatically disabled and an interrupt is generated to inform the |
1033 | * is automatically disabled and an interrupt is generated to inform the |
980 | * software about the failure (Clock Security System Interrupt, CSSI), |
1034 | * software about the failure (Clock Security System Interrupt, CSSI), |
981 | * allowing the MCU to perform rescue operations. The CSSI is linked to |
1035 | * allowing the MCU to perform rescue operations. The CSSI is linked to |
982 | * the Cortex-M3 NMI (Non-Maskable Interrupt) exception vector. |
1036 | * the Cortex-M3 NMI (Non-Maskable Interrupt) exception vector. |
983 | * @retval None |
1037 | * @retval None |
984 | */ |
1038 | */ |
985 | void HAL_RCC_EnableCSS(void) |
1039 | void HAL_RCC_EnableCSS(void) |
986 | { |
1040 | { |
987 | *(__IO uint32_t *) RCC_CR_CSSON_BB = (uint32_t)ENABLE; |
1041 | *(__IO uint32_t *) RCC_CR_CSSON_BB = (uint32_t)ENABLE; |
Line 995... | Line 1049... | ||
995 | { |
1049 | { |
996 | *(__IO uint32_t *) RCC_CR_CSSON_BB = (uint32_t)DISABLE; |
1050 | *(__IO uint32_t *) RCC_CR_CSSON_BB = (uint32_t)DISABLE; |
997 | } |
1051 | } |
998 | 1052 | ||
999 | /** |
1053 | /** |
1000 | * @brief Returns the SYSCLK frequency |
1054 | * @brief Returns the SYSCLK frequency |
1001 | * @note The system frequency computed by this function is not the real |
1055 | * @note The system frequency computed by this function is not the real |
1002 | * frequency in the chip. It is calculated based on the predefined |
1056 | * frequency in the chip. It is calculated based on the predefined |
1003 | * constant and the selected clock source: |
1057 | * constant and the selected clock source: |
1004 | * @note If SYSCLK source is MSI, function returns a value based on MSI |
1058 | * @note If SYSCLK source is MSI, function returns a value based on MSI |
1005 | * Value as defined by the MSI range. |
1059 | * Value as defined by the MSI range. |
1006 | * @note If SYSCLK source is HSI, function returns values based on HSI_VALUE(*) |
1060 | * @note If SYSCLK source is HSI, function returns values based on HSI_VALUE(*) |
1007 | * @note If SYSCLK source is HSE, function returns a value based on HSE_VALUE(**) |
1061 | * @note If SYSCLK source is HSE, function returns a value based on HSE_VALUE(**) |
1008 | * @note If SYSCLK source is PLL, function returns a value based on HSE_VALUE(**) |
1062 | * @note If SYSCLK source is PLL, function returns a value based on HSE_VALUE(**) |
1009 | * or HSI_VALUE(*) multiplied/divided by the PLL factors. |
1063 | * or HSI_VALUE(*) multiplied/divided by the PLL factors. |
1010 | * @note (*) HSI_VALUE is a constant defined in stm32l1xx_hal_conf.h file (default value |
1064 | * @note (*) HSI_VALUE is a constant defined in stm32l1xx_hal_conf.h file (default value |
1011 | * 16 MHz) but the real value may vary depending on the variations |
1065 | * 16 MHz) but the real value may vary depending on the variations |
1012 | * in voltage and temperature. |
1066 | * in voltage and temperature. |
1013 | * @note (**) HSE_VALUE is a constant defined in stm32l1xx_hal_conf.h file (default value |
1067 | * @note (**) HSE_VALUE is a constant defined in stm32l1xx_hal_conf.h file (default value |
1014 | * 8 MHz), user has to ensure that HSE_VALUE is same as the real |
1068 | * 8 MHz), user has to ensure that HSE_VALUE is same as the real |
1015 | * frequency of the crystal used. Otherwise, this function may |
1069 | * frequency of the crystal used. Otherwise, this function may |
1016 | * have wrong result. |
1070 | * have wrong result. |
1017 | * |
1071 | * |
1018 | * @note The result of this function could be not correct when using fractional |
1072 | * @note The result of this function could be not correct when using fractional |
1019 | * value for HSE crystal. |
1073 | * value for HSE crystal. |
1020 | * |
1074 | * |
1021 | * @note This function can be used by the user application to compute the |
1075 | * @note This function can be used by the user application to compute the |
1022 | * baud-rate for the communication peripherals or configure other parameters. |
1076 | * baud-rate for the communication peripherals or configure other parameters. |
1023 | * |
1077 | * |
1024 | * @note Each time SYSCLK changes, this function must be called to update the |
1078 | * @note Each time SYSCLK changes, this function must be called to update the |
1025 | * right SYSCLK value. Otherwise, any configuration based on this function will be incorrect. |
1079 | * right SYSCLK value. Otherwise, any configuration based on this function will be incorrect. |
1026 | * |
1080 | * |
1027 | * @retval SYSCLK frequency |
1081 | * @retval SYSCLK frequency |
1028 | */ |
1082 | */ |
1029 | uint32_t HAL_RCC_GetSysClockFreq(void) |
1083 | uint32_t HAL_RCC_GetSysClockFreq(void) |
1030 | { |
1084 | { |
1031 | uint32_t tmpreg = 0, pllm = 0, plld = 0, pllvco = 0, msiclkrange = 0; |
1085 | uint32_t tmpreg, pllm, plld, pllvco, msiclkrange, sysclockfreq; |
1032 | uint32_t sysclockfreq = 0; |
- | |
1033 | 1086 | ||
1034 | tmpreg = RCC->CFGR; |
1087 | tmpreg = RCC->CFGR; |
1035 | 1088 | ||
1036 | /* Get SYSCLK source -------------------------------------------------------*/ |
1089 | /* Get SYSCLK source -------------------------------------------------------*/ |
1037 | switch (tmpreg & RCC_CFGR_SWS) |
1090 | switch (tmpreg & RCC_CFGR_SWS) |
1038 | { |
1091 | { |
1039 | case RCC_SYSCLKSOURCE_STATUS_HSI: /* HSI used as system clock source */ |
1092 | case RCC_SYSCLKSOURCE_STATUS_HSI: /* HSI used as system clock source */ |
1040 | { |
1093 | { |
Line 1046... | Line 1099... | ||
1046 | sysclockfreq = HSE_VALUE; |
1099 | sysclockfreq = HSE_VALUE; |
1047 | break; |
1100 | break; |
1048 | } |
1101 | } |
1049 | case RCC_SYSCLKSOURCE_STATUS_PLLCLK: /* PLL used as system clock */ |
1102 | case RCC_SYSCLKSOURCE_STATUS_PLLCLK: /* PLL used as system clock */ |
1050 | { |
1103 | { |
1051 | pllm = PLLMulTable[(uint32_t)(tmpreg & RCC_CFGR_PLLMUL) >> RCC_CFGR_PLLMUL_BITNUMBER]; |
1104 | pllm = PLLMulTable[(uint32_t)(tmpreg & RCC_CFGR_PLLMUL) >> RCC_CFGR_PLLMUL_Pos]; |
1052 | plld = ((uint32_t)(tmpreg & RCC_CFGR_PLLDIV) >> RCC_CFGR_PLLDIV_BITNUMBER) + 1; |
1105 | plld = ((uint32_t)(tmpreg & RCC_CFGR_PLLDIV) >> RCC_CFGR_PLLDIV_Pos) + 1U; |
1053 | if (__HAL_RCC_GET_PLL_OSCSOURCE() != RCC_PLLSOURCE_HSI) |
1106 | if (__HAL_RCC_GET_PLL_OSCSOURCE() != RCC_PLLSOURCE_HSI) |
1054 | { |
1107 | { |
1055 | /* HSE used as PLL clock source */ |
1108 | /* HSE used as PLL clock source */ |
1056 | pllvco = (HSE_VALUE * pllm) / plld; |
1109 | pllvco = (uint32_t)(((uint64_t)HSE_VALUE * (uint64_t)pllm) / (uint64_t)plld); |
1057 | } |
1110 | } |
1058 | else |
1111 | else |
1059 | { |
1112 | { |
1060 | /* HSI used as PLL clock source */ |
1113 | /* HSI used as PLL clock source */ |
1061 | pllvco = (HSI_VALUE * pllm) / plld; |
1114 | pllvco = (uint32_t)(((uint64_t)HSI_VALUE * (uint64_t)pllm) / (uint64_t)plld); |
1062 | } |
1115 | } |
1063 | sysclockfreq = pllvco; |
1116 | sysclockfreq = pllvco; |
1064 | break; |
1117 | break; |
1065 | } |
1118 | } |
1066 | case RCC_SYSCLKSOURCE_STATUS_MSI: /* MSI used as system clock source */ |
1119 | case RCC_SYSCLKSOURCE_STATUS_MSI: /* MSI used as system clock source */ |
1067 | default: /* MSI used as system clock */ |
1120 | default: /* MSI used as system clock */ |
1068 | { |
1121 | { |
1069 | msiclkrange = (RCC->ICSCR & RCC_ICSCR_MSIRANGE ) >> RCC_ICSCR_MSIRANGE_BITNUMBER; |
1122 | msiclkrange = (RCC->ICSCR & RCC_ICSCR_MSIRANGE ) >> RCC_ICSCR_MSIRANGE_Pos; |
1070 | sysclockfreq = (32768 * (1 << (msiclkrange + 1))); |
1123 | sysclockfreq = (32768U * (1UL << (msiclkrange + 1U))); |
1071 | break; |
1124 | break; |
1072 | } |
1125 | } |
1073 | } |
1126 | } |
1074 | return sysclockfreq; |
1127 | return sysclockfreq; |
1075 | } |
1128 | } |
1076 | 1129 | ||
1077 | /** |
1130 | /** |
1078 | * @brief Returns the HCLK frequency |
1131 | * @brief Returns the HCLK frequency |
1079 | * @note Each time HCLK changes, this function must be called to update the |
1132 | * @note Each time HCLK changes, this function must be called to update the |
1080 | * right HCLK value. Otherwise, any configuration based on this function will be incorrect. |
1133 | * right HCLK value. Otherwise, any configuration based on this function will be incorrect. |
1081 | * |
1134 | * |
1082 | * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency |
1135 | * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency |
1083 | * and updated within this function |
1136 | * and updated within this function |
1084 | * @retval HCLK frequency |
1137 | * @retval HCLK frequency |
1085 | */ |
1138 | */ |
1086 | uint32_t HAL_RCC_GetHCLKFreq(void) |
1139 | uint32_t HAL_RCC_GetHCLKFreq(void) |
1087 | { |
1140 | { |
1088 | return SystemCoreClock; |
1141 | return SystemCoreClock; |
1089 | } |
1142 | } |
1090 | 1143 | ||
1091 | /** |
1144 | /** |
1092 | * @brief Returns the PCLK1 frequency |
1145 | * @brief Returns the PCLK1 frequency |
1093 | * @note Each time PCLK1 changes, this function must be called to update the |
1146 | * @note Each time PCLK1 changes, this function must be called to update the |
1094 | * right PCLK1 value. Otherwise, any configuration based on this function will be incorrect. |
1147 | * right PCLK1 value. Otherwise, any configuration based on this function will be incorrect. |
1095 | * @retval PCLK1 frequency |
1148 | * @retval PCLK1 frequency |
1096 | */ |
1149 | */ |
1097 | uint32_t HAL_RCC_GetPCLK1Freq(void) |
1150 | uint32_t HAL_RCC_GetPCLK1Freq(void) |
1098 | { |
1151 | { |
1099 | /* Get HCLK source and Compute PCLK1 frequency ---------------------------*/ |
1152 | /* Get HCLK source and Compute PCLK1 frequency ---------------------------*/ |
1100 | return (HAL_RCC_GetHCLKFreq() >> APBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE1) >> RCC_CFGR_PPRE1_BITNUMBER]); |
1153 | return (HAL_RCC_GetHCLKFreq() >> APBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE1) >> RCC_CFGR_PPRE1_Pos]); |
1101 | } |
1154 | } |
1102 | 1155 | ||
1103 | /** |
1156 | /** |
1104 | * @brief Returns the PCLK2 frequency |
1157 | * @brief Returns the PCLK2 frequency |
1105 | * @note Each time PCLK2 changes, this function must be called to update the |
1158 | * @note Each time PCLK2 changes, this function must be called to update the |
1106 | * right PCLK2 value. Otherwise, any configuration based on this function will be incorrect. |
1159 | * right PCLK2 value. Otherwise, any configuration based on this function will be incorrect. |
1107 | * @retval PCLK2 frequency |
1160 | * @retval PCLK2 frequency |
1108 | */ |
1161 | */ |
1109 | uint32_t HAL_RCC_GetPCLK2Freq(void) |
1162 | uint32_t HAL_RCC_GetPCLK2Freq(void) |
1110 | { |
1163 | { |
1111 | /* Get HCLK source and Compute PCLK2 frequency ---------------------------*/ |
1164 | /* Get HCLK source and Compute PCLK2 frequency ---------------------------*/ |
1112 | return (HAL_RCC_GetHCLKFreq()>> APBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE2) >> RCC_CFGR_PPRE2_BITNUMBER]); |
1165 | return (HAL_RCC_GetHCLKFreq()>> APBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE2) >> RCC_CFGR_PPRE2_Pos]); |
1113 | } |
1166 | } |
1114 | 1167 | ||
1115 | /** |
1168 | /** |
1116 | * @brief Configures the RCC_OscInitStruct according to the internal |
1169 | * @brief Configures the RCC_OscInitStruct according to the internal |
1117 | * RCC configuration registers. |
1170 | * RCC configuration registers. |
1118 | * @param RCC_OscInitStruct pointer to an RCC_OscInitTypeDef structure that |
1171 | * @param RCC_OscInitStruct pointer to an RCC_OscInitTypeDef structure that |
1119 | * will be configured. |
1172 | * will be configured. |
1120 | * @retval None |
1173 | * @retval None |
1121 | */ |
1174 | */ |
1122 | void HAL_RCC_GetOscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct) |
1175 | void HAL_RCC_GetOscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct) |
1123 | { |
1176 | { |
1124 | /* Check the parameters */ |
1177 | /* Check the parameters */ |
1125 | assert_param(RCC_OscInitStruct != NULL); |
1178 | assert_param(RCC_OscInitStruct != (void *)NULL); |
1126 | 1179 | ||
1127 | /* Set all possible values for the Oscillator type parameter ---------------*/ |
1180 | /* Set all possible values for the Oscillator type parameter ---------------*/ |
1128 | RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI \ |
1181 | RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI \ |
1129 | | RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_MSI; |
1182 | | RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_MSI; |
1130 | 1183 | ||
Line 1150... | Line 1203... | ||
1150 | } |
1203 | } |
1151 | else |
1204 | else |
1152 | { |
1205 | { |
1153 | RCC_OscInitStruct->HSIState = RCC_HSI_OFF; |
1206 | RCC_OscInitStruct->HSIState = RCC_HSI_OFF; |
1154 | } |
1207 | } |
1155 | 1208 | ||
1156 | RCC_OscInitStruct->HSICalibrationValue = (uint32_t)((RCC->ICSCR & RCC_ICSCR_HSITRIM) >> POSITION_VAL(RCC_ICSCR_HSITRIM)); |
1209 | RCC_OscInitStruct->HSICalibrationValue = (uint32_t)((RCC->ICSCR & RCC_ICSCR_HSITRIM) >> RCC_ICSCR_HSITRIM_Pos); |
1157 | 1210 | ||
1158 | /* Get the MSI configuration -----------------------------------------------*/ |
1211 | /* Get the MSI configuration -----------------------------------------------*/ |
1159 | if((RCC->CR &RCC_CR_MSION) == RCC_CR_MSION) |
1212 | if((RCC->CR &RCC_CR_MSION) == RCC_CR_MSION) |
1160 | { |
1213 | { |
1161 | RCC_OscInitStruct->MSIState = RCC_MSI_ON; |
1214 | RCC_OscInitStruct->MSIState = RCC_MSI_ON; |
1162 | } |
1215 | } |
1163 | else |
1216 | else |
1164 | { |
1217 | { |
1165 | RCC_OscInitStruct->MSIState = RCC_MSI_OFF; |
1218 | RCC_OscInitStruct->MSIState = RCC_MSI_OFF; |
1166 | } |
1219 | } |
1167 | 1220 | ||
1168 | RCC_OscInitStruct->MSICalibrationValue = (uint32_t)((RCC->ICSCR & RCC_ICSCR_MSITRIM) >> RCC_ICSCR_MSITRIM_BITNUMBER); |
1221 | RCC_OscInitStruct->MSICalibrationValue = (uint32_t)((RCC->ICSCR & RCC_ICSCR_MSITRIM) >> RCC_ICSCR_MSITRIM_Pos); |
1169 | RCC_OscInitStruct->MSIClockRange = (uint32_t)((RCC->ICSCR & RCC_ICSCR_MSIRANGE)); |
1222 | RCC_OscInitStruct->MSIClockRange = (uint32_t)((RCC->ICSCR & RCC_ICSCR_MSIRANGE)); |
1170 | 1223 | ||
1171 | /* Get the LSE configuration -----------------------------------------------*/ |
1224 | /* Get the LSE configuration -----------------------------------------------*/ |
1172 | if((RCC->CSR &RCC_CSR_LSEBYP) == RCC_CSR_LSEBYP) |
1225 | if((RCC->CSR &RCC_CSR_LSEBYP) == RCC_CSR_LSEBYP) |
1173 | { |
1226 | { |
1174 | RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS; |
1227 | RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS; |
1175 | } |
1228 | } |
Line 1179... | Line 1232... | ||
1179 | } |
1232 | } |
1180 | else |
1233 | else |
1181 | { |
1234 | { |
1182 | RCC_OscInitStruct->LSEState = RCC_LSE_OFF; |
1235 | RCC_OscInitStruct->LSEState = RCC_LSE_OFF; |
1183 | } |
1236 | } |
1184 | 1237 | ||
1185 | /* Get the LSI configuration -----------------------------------------------*/ |
1238 | /* Get the LSI configuration -----------------------------------------------*/ |
1186 | if((RCC->CSR &RCC_CSR_LSION) == RCC_CSR_LSION) |
1239 | if((RCC->CSR &RCC_CSR_LSION) == RCC_CSR_LSION) |
1187 | { |
1240 | { |
1188 | RCC_OscInitStruct->LSIState = RCC_LSI_ON; |
1241 | RCC_OscInitStruct->LSIState = RCC_LSI_ON; |
1189 | } |
1242 | } |
1190 | else |
1243 | else |
1191 | { |
1244 | { |
1192 | RCC_OscInitStruct->LSIState = RCC_LSI_OFF; |
1245 | RCC_OscInitStruct->LSIState = RCC_LSI_OFF; |
1193 | } |
1246 | } |
1194 | 1247 | ||
1195 | 1248 | ||
1196 | /* Get the PLL configuration -----------------------------------------------*/ |
1249 | /* Get the PLL configuration -----------------------------------------------*/ |
1197 | if((RCC->CR &RCC_CR_PLLON) == RCC_CR_PLLON) |
1250 | if((RCC->CR &RCC_CR_PLLON) == RCC_CR_PLLON) |
1198 | { |
1251 | { |
1199 | RCC_OscInitStruct->PLL.PLLState = RCC_PLL_ON; |
1252 | RCC_OscInitStruct->PLL.PLLState = RCC_PLL_ON; |
Line 1206... | Line 1259... | ||
1206 | RCC_OscInitStruct->PLL.PLLMUL = (uint32_t)(RCC->CFGR & RCC_CFGR_PLLMUL); |
1259 | RCC_OscInitStruct->PLL.PLLMUL = (uint32_t)(RCC->CFGR & RCC_CFGR_PLLMUL); |
1207 | RCC_OscInitStruct->PLL.PLLDIV = (uint32_t)(RCC->CFGR & RCC_CFGR_PLLDIV); |
1260 | RCC_OscInitStruct->PLL.PLLDIV = (uint32_t)(RCC->CFGR & RCC_CFGR_PLLDIV); |
1208 | } |
1261 | } |
1209 | 1262 | ||
1210 | /** |
1263 | /** |
1211 | * @brief Get the RCC_ClkInitStruct according to the internal |
1264 | * @brief Get the RCC_ClkInitStruct according to the internal |
1212 | * RCC configuration registers. |
1265 | * RCC configuration registers. |
1213 | * @param RCC_ClkInitStruct pointer to an RCC_ClkInitTypeDef structure that |
1266 | * @param RCC_ClkInitStruct pointer to an RCC_ClkInitTypeDef structure that |
1214 | * contains the current clock configuration. |
1267 | * contains the current clock configuration. |
1215 | * @param pFLatency Pointer on the Flash Latency. |
1268 | * @param pFLatency Pointer on the Flash Latency. |
1216 | * @retval None |
1269 | * @retval None |
1217 | */ |
1270 | */ |
1218 | void HAL_RCC_GetClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t *pFLatency) |
1271 | void HAL_RCC_GetClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t *pFLatency) |
1219 | { |
1272 | { |
1220 | /* Check the parameters */ |
1273 | /* Check the parameters */ |
1221 | assert_param(RCC_ClkInitStruct != NULL); |
1274 | assert_param(RCC_ClkInitStruct != (void *)NULL); |
1222 | assert_param(pFLatency != NULL); |
1275 | assert_param(pFLatency != (void *)NULL); |
1223 | 1276 | ||
1224 | /* Set all possible values for the Clock type parameter --------------------*/ |
1277 | /* Set all possible values for the Clock type parameter --------------------*/ |
1225 | RCC_ClkInitStruct->ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; |
1278 | RCC_ClkInitStruct->ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; |
1226 | 1279 | ||
1227 | /* Get the SYSCLK configuration --------------------------------------------*/ |
1280 | /* Get the SYSCLK configuration --------------------------------------------*/ |
1228 | RCC_ClkInitStruct->SYSCLKSource = (uint32_t)(RCC->CFGR & RCC_CFGR_SW); |
1281 | RCC_ClkInitStruct->SYSCLKSource = (uint32_t)(RCC->CFGR & RCC_CFGR_SW); |
1229 | 1282 | ||
1230 | /* Get the HCLK configuration ----------------------------------------------*/ |
1283 | /* Get the HCLK configuration ----------------------------------------------*/ |
1231 | RCC_ClkInitStruct->AHBCLKDivider = (uint32_t)(RCC->CFGR & RCC_CFGR_HPRE); |
1284 | RCC_ClkInitStruct->AHBCLKDivider = (uint32_t)(RCC->CFGR & RCC_CFGR_HPRE); |
1232 | 1285 | ||
1233 | /* Get the APB1 configuration ----------------------------------------------*/ |
1286 | /* Get the APB1 configuration ----------------------------------------------*/ |
1234 | RCC_ClkInitStruct->APB1CLKDivider = (uint32_t)(RCC->CFGR & RCC_CFGR_PPRE1); |
1287 | RCC_ClkInitStruct->APB1CLKDivider = (uint32_t)(RCC->CFGR & RCC_CFGR_PPRE1); |
1235 | 1288 | ||
1236 | /* Get the APB2 configuration ----------------------------------------------*/ |
1289 | /* Get the APB2 configuration ----------------------------------------------*/ |
1237 | RCC_ClkInitStruct->APB2CLKDivider = (uint32_t)((RCC->CFGR & RCC_CFGR_PPRE2) >> 3); |
1290 | RCC_ClkInitStruct->APB2CLKDivider = (uint32_t)((RCC->CFGR & RCC_CFGR_PPRE2) >> 3U); |
1238 | 1291 | ||
1239 | /* Get the Flash Wait State (Latency) configuration ------------------------*/ |
1292 | /* Get the Flash Wait State (Latency) configuration ------------------------*/ |
1240 | *pFLatency = (uint32_t)(FLASH->ACR & FLASH_ACR_LATENCY); |
1293 | *pFLatency = __HAL_FLASH_GET_LATENCY(); |
1241 | } |
1294 | } |
1242 | 1295 | ||
1243 | /** |
1296 | /** |
1244 | * @brief This function handles the RCC CSS interrupt request. |
1297 | * @brief This function handles the RCC CSS interrupt request. |
1245 | * @note This API should be called under the NMI_Handler(). |
1298 | * @note This API should be called under the NMI_Handler(). |
Line 1250... | Line 1303... | ||
1250 | /* Check RCC CSSF flag */ |
1303 | /* Check RCC CSSF flag */ |
1251 | if(__HAL_RCC_GET_IT(RCC_IT_CSS)) |
1304 | if(__HAL_RCC_GET_IT(RCC_IT_CSS)) |
1252 | { |
1305 | { |
1253 | /* RCC Clock Security System interrupt user callback */ |
1306 | /* RCC Clock Security System interrupt user callback */ |
1254 | HAL_RCC_CSSCallback(); |
1307 | HAL_RCC_CSSCallback(); |
1255 | 1308 | ||
1256 | /* Clear RCC CSS pending bit */ |
1309 | /* Clear RCC CSS pending bit */ |
1257 | __HAL_RCC_CLEAR_IT(RCC_IT_CSS); |
1310 | __HAL_RCC_CLEAR_IT(RCC_IT_CSS); |
1258 | } |
1311 | } |
1259 | } |
1312 | } |
1260 | 1313 | ||
Line 1264... | Line 1317... | ||
1264 | */ |
1317 | */ |
1265 | __weak void HAL_RCC_CSSCallback(void) |
1318 | __weak void HAL_RCC_CSSCallback(void) |
1266 | { |
1319 | { |
1267 | /* NOTE : This function Should not be modified, when the callback is needed, |
1320 | /* NOTE : This function Should not be modified, when the callback is needed, |
1268 | the HAL_RCC_CSSCallback could be implemented in the user file |
1321 | the HAL_RCC_CSSCallback could be implemented in the user file |
1269 | */ |
1322 | */ |
1270 | } |
1323 | } |
1271 | 1324 | ||
1272 | /** |
1325 | /** |
1273 | * @} |
1326 | * @} |
1274 | */ |
1327 | */ |
Line 1280... | Line 1333... | ||
1280 | /* Private function prototypes -----------------------------------------------*/ |
1333 | /* Private function prototypes -----------------------------------------------*/ |
1281 | /** @addtogroup RCC_Private_Functions |
1334 | /** @addtogroup RCC_Private_Functions |
1282 | * @{ |
1335 | * @{ |
1283 | */ |
1336 | */ |
1284 | /** |
1337 | /** |
1285 | * @brief Update number of Flash wait states in line with MSI range and current |
1338 | * @brief Update number of Flash wait states in line with MSI range and current |
1286 | voltage range |
1339 | voltage range |
1287 | * @param MSIrange MSI range value from RCC_MSIRANGE_0 to RCC_MSIRANGE_6 |
1340 | * @param MSIrange MSI range value from RCC_MSIRANGE_0 to RCC_MSIRANGE_6 |
1288 | * @retval HAL status |
1341 | * @retval HAL status |
1289 | */ |
1342 | */ |
1290 | static HAL_StatusTypeDef RCC_SetFlashLatencyFromMSIRange(uint32_t MSIrange) |
1343 | static HAL_StatusTypeDef RCC_SetFlashLatencyFromMSIRange(uint32_t MSIrange) |
1291 | { |
1344 | { |
1292 | uint32_t vos = 0; |
1345 | uint32_t vos; |
1293 | uint32_t latency = FLASH_LATENCY_0; /* default value 0WS */ |
1346 | uint32_t latency = FLASH_LATENCY_0; /* default value 0WS */ |
1294 | 1347 | ||
1295 | /* HCLK can reach 4 MHz only if AHB prescaler = 1 */ |
1348 | /* HCLK can reach 4 MHz only if AHB prescaler = 1 */ |
1296 | if (READ_BIT(RCC->CFGR, RCC_CFGR_HPRE) == RCC_SYSCLK_DIV1) |
1349 | if (READ_BIT(RCC->CFGR, RCC_CFGR_HPRE) == RCC_SYSCLK_DIV1) |
1297 | { |
1350 | { |
1298 | if(__HAL_RCC_PWR_IS_CLK_ENABLED()) |
1351 | if(__HAL_RCC_PWR_IS_CLK_ENABLED()) |
1299 | { |
1352 | { |
1300 | vos = HAL_PWREx_GetVoltageRange(); |
1353 | vos = READ_BIT(PWR->CR, PWR_CR_VOS); |
1301 | } |
1354 | } |
1302 | else |
1355 | else |
1303 | { |
1356 | { |
1304 | __HAL_RCC_PWR_CLK_ENABLE(); |
1357 | __HAL_RCC_PWR_CLK_ENABLE(); |
1305 | vos = HAL_PWREx_GetVoltageRange(); |
1358 | vos = READ_BIT(PWR->CR, PWR_CR_VOS); |
1306 | __HAL_RCC_PWR_CLK_DISABLE(); |
1359 | __HAL_RCC_PWR_CLK_DISABLE(); |
1307 | } |
1360 | } |
1308 | 1361 | ||
1309 | /* Check if need to set latency 1 only for Range 3 & HCLK = 4MHz */ |
1362 | /* Check if need to set latency 1 only for Range 3 & HCLK = 4MHz */ |
1310 | if((vos == PWR_REGULATOR_VOLTAGE_SCALE3) && (MSIrange == RCC_MSIRANGE_6)) |
1363 | if((vos == PWR_REGULATOR_VOLTAGE_SCALE3) && (MSIrange == RCC_MSIRANGE_6)) |
1311 | { |
1364 | { |
1312 | latency = FLASH_LATENCY_1; /* 1WS */ |
1365 | latency = FLASH_LATENCY_1; /* 1WS */ |
1313 | } |
1366 | } |
1314 | } |
1367 | } |
1315 | 1368 | ||
1316 | __HAL_FLASH_SET_LATENCY(latency); |
1369 | __HAL_FLASH_SET_LATENCY(latency); |
1317 | 1370 | ||
1318 | /* Check that the new number of wait states is taken into account to access the Flash |
1371 | /* Check that the new number of wait states is taken into account to access the Flash |
1319 | memory by reading the FLASH_ACR register */ |
1372 | memory by reading the FLASH_ACR register */ |
1320 | if((FLASH->ACR & FLASH_ACR_LATENCY) != latency) |
1373 | if(__HAL_FLASH_GET_LATENCY() != latency) |
1321 | { |
1374 | { |
1322 | return HAL_ERROR; |
1375 | return HAL_ERROR; |
1323 | } |
1376 | } |
1324 | 1377 | ||
1325 | return HAL_OK; |
1378 | return HAL_OK; |
1326 | } |
1379 | } |
1327 | 1380 | ||
1328 | /** |
1381 | /** |
1329 | * @} |
1382 | * @} |
1330 | */ |
1383 | */ |
1331 | 1384 | ||
1332 | #endif /* HAL_RCC_MODULE_ENABLED */ |
1385 | #endif /* HAL_RCC_MODULE_ENABLED */ |
1333 | /** |
1386 | /** |
1334 | * @} |
1387 | * @} |
1335 | */ |
1388 | */ |
1336 | 1389 |