2 ******************************************************************************
\r
3 * @file system_stm32f4xx.c
\r
4 * @author MCD Application Team
\r
6 * @date 26-February-2014
\r
7 * @brief CMSIS Cortex-M4 Device Peripheral Access Layer System Source File.
\r
9 * This file provides two functions and one global variable to be called from
\r
11 * - SystemInit(): This function is called at startup just after reset and
\r
12 * before branch to main program. This call is made inside
\r
13 * the "startup_stm32f4xx.s" file.
\r
15 * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
\r
16 * by the user application to setup the SysTick
\r
17 * timer or configure other parameters.
\r
19 * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
\r
20 * be called whenever the core clock is changed
\r
21 * during program execution.
\r
24 ******************************************************************************
\r
27 * <h2><center>© COPYRIGHT 2014 STMicroelectronics</center></h2>
\r
29 * Redistribution and use in source and binary forms, with or without modification,
\r
30 * are permitted provided that the following conditions are met:
\r
31 * 1. Redistributions of source code must retain the above copyright notice,
\r
32 * this list of conditions and the following disclaimer.
\r
33 * 2. Redistributions in binary form must reproduce the above copyright notice,
\r
34 * this list of conditions and the following disclaimer in the documentation
\r
35 * and/or other materials provided with the distribution.
\r
36 * 3. Neither the name of STMicroelectronics nor the names of its contributors
\r
37 * may be used to endorse or promote products derived from this software
\r
38 * without specific prior written permission.
\r
40 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
\r
41 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
\r
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
\r
43 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
\r
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
\r
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
\r
46 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
\r
47 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
\r
48 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
\r
49 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
51 ******************************************************************************
\r
54 /** @addtogroup CMSIS
\r
58 /** @addtogroup stm32f4xx_system
\r
62 /** @addtogroup STM32F4xx_System_Private_Includes
\r
66 #include "stm32f4xx_hal.h"
\r
72 /** @addtogroup STM32F4xx_System_Private_TypesDefinitions
\r
80 /** @addtogroup STM32F4xx_System_Private_Defines
\r
84 /************************* Miscellaneous Configuration ************************/
\r
86 /*!< Uncomment the following line if you need to relocate your vector Table in
\r
88 /* #define VECT_TAB_SRAM */
\r
89 #define VECT_TAB_OFFSET 0x00 /*!< Vector Table base offset field.
\r
90 This value must be a multiple of 0x200. */
\r
91 /******************************************************************************/
\r
97 /** @addtogroup STM32F4xx_System_Private_Macros
\r
105 /** @addtogroup STM32F4xx_System_Private_Variables
\r
108 /* This variable is updated in three ways:
\r
109 1) by calling CMSIS function SystemCoreClockUpdate()
\r
110 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
\r
111 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
\r
112 Note: If you use this function to configure the system clock; then there
\r
113 is no need to call the 2 first functions listed above, since SystemCoreClock
\r
114 variable is updated automatically.
\r
116 uint32_t SystemCoreClock = 16000000;
\r
117 __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
\r
123 /** @addtogroup STM32F4xx_System_Private_FunctionPrototypes
\r
131 /** @addtogroup STM32F4xx_System_Private_Functions
\r
136 * @brief Setup the microcontroller system
\r
137 * Initialize the FPU setting, vector table location and External memory
\r
142 void SystemInit(void)
\r
144 /* FPU settings ------------------------------------------------------------*/
\r
145 #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
\r
146 SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */
\r
148 /* Reset the RCC clock configuration to the default reset state ------------*/
\r
149 /* Set HSION bit */
\r
150 RCC->CR |= (uint32_t)0x00000001;
\r
152 /* Reset CFGR register */
\r
153 RCC->CFGR = 0x00000000;
\r
155 /* Reset HSEON, CSSON and PLLON bits */
\r
156 RCC->CR &= (uint32_t)0xFEF6FFFF;
\r
158 /* Reset PLLCFGR register */
\r
159 RCC->PLLCFGR = 0x24003010;
\r
161 /* Reset HSEBYP bit */
\r
162 RCC->CR &= (uint32_t)0xFFFBFFFF;
\r
164 /* Disable all interrupts */
\r
165 RCC->CIR = 0x00000000;
\r
167 /* Configure the Vector Table location add offset address ------------------*/
\r
168 #ifdef VECT_TAB_SRAM
\r
169 SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
\r
171 SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
\r
176 * @brief Update SystemCoreClock variable according to Clock Register Values.
\r
177 * The SystemCoreClock variable contains the core clock (HCLK), it can
\r
178 * be used by the user application to setup the SysTick timer or configure
\r
179 * other parameters.
\r
181 * @note Each time the core clock (HCLK) changes, this function must be called
\r
182 * to update SystemCoreClock variable value. Otherwise, any configuration
\r
183 * based on this variable will be incorrect.
\r
185 * @note - The system frequency computed by this function is not the real
\r
186 * frequency in the chip. It is calculated based on the predefined
\r
187 * constant and the selected clock source:
\r
189 * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
\r
191 * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
\r
193 * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
\r
194 * or HSI_VALUE(*) multiplied/divided by the PLL factors.
\r
196 * (*) HSI_VALUE is a constant defined in stm32f4xx_hal_conf.h file (default value
\r
197 * 16 MHz) but the real value may vary depending on the variations
\r
198 * in voltage and temperature.
\r
200 * (**) HSE_VALUE is a constant defined in stm32f4xx_hal_conf.h file (its value
\r
201 * depends on the application requirements), user has to ensure that HSE_VALUE
\r
202 * is same as the real frequency of the crystal used. Otherwise, this function
\r
203 * may have wrong result.
\r
205 * - The result of this function could be not correct when using fractional
\r
206 * value for HSE crystal.
\r
211 void SystemCoreClockUpdate(void)
\r
213 uint32_t tmp = 0, pllvco = 0, pllp = 2, pllsource = 0, pllm = 2;
\r
215 /* Get SYSCLK source -------------------------------------------------------*/
\r
216 tmp = RCC->CFGR & RCC_CFGR_SWS;
\r
220 case 0x00: /* HSI used as system clock source */
\r
221 SystemCoreClock = HSI_VALUE;
\r
223 case 0x04: /* HSE used as system clock source */
\r
224 SystemCoreClock = HSE_VALUE;
\r
226 case 0x08: /* PLL used as system clock source */
\r
228 /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N
\r
229 SYSCLK = PLL_VCO / PLL_P
\r
231 pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) >> 22;
\r
232 pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
\r
234 if (pllsource != 0)
\r
236 /* HSE used as PLL clock source */
\r
237 pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
\r
241 /* HSI used as PLL clock source */
\r
242 pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
\r
245 pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >>16) + 1 ) *2;
\r
246 SystemCoreClock = pllvco/pllp;
\r
249 SystemCoreClock = HSI_VALUE;
\r
252 /* Compute HCLK frequency --------------------------------------------------*/
\r
253 /* Get HCLK prescaler */
\r
254 tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
\r
255 /* HCLK frequency */
\r
256 SystemCoreClock >>= tmp;
\r
270 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
\r