stm32f429 demo - system init
[lwext4.git] / demos / stm32f429_disco / system_stm32f4xx.c
1 /**\r
2   ******************************************************************************\r
3   * @file    system_stm32f4xx.c\r
4   * @author  MCD Application Team\r
5   * @version V1.0.1\r
6   * @date    26-February-2014\r
7   * @brief   CMSIS Cortex-M4 Device Peripheral Access Layer System Source File.\r
8   *\r
9   *   This file provides two functions and one global variable to be called from \r
10   *   user application:\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
14   *\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
18   *                                     \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
22   *\r
23   *\r
24   ******************************************************************************\r
25   * @attention\r
26   *\r
27   * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>\r
28   *\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
39   *\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
50   *\r
51   ******************************************************************************\r
52   */\r
53 \r
54 /** @addtogroup CMSIS\r
55   * @{\r
56   */\r
57 \r
58 /** @addtogroup stm32f4xx_system\r
59   * @{\r
60   */  \r
61   \r
62 /** @addtogroup STM32F4xx_System_Private_Includes\r
63   * @{\r
64   */\r
65 \r
66 #include "stm32f4xx_hal.h"\r
67 \r
68 /**\r
69   * @}\r
70   */\r
71 \r
72 /** @addtogroup STM32F4xx_System_Private_TypesDefinitions\r
73   * @{\r
74   */\r
75 \r
76 /**\r
77   * @}\r
78   */\r
79 \r
80 /** @addtogroup STM32F4xx_System_Private_Defines\r
81   * @{\r
82   */\r
83 \r
84 /************************* Miscellaneous Configuration ************************/\r
85 \r
86 /*!< Uncomment the following line if you need to relocate your vector Table in\r
87      Internal SRAM. */\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
92 \r
93 /**\r
94   * @}\r
95   */\r
96 \r
97 /** @addtogroup STM32F4xx_System_Private_Macros\r
98   * @{\r
99   */\r
100 \r
101 /**\r
102   * @}\r
103   */\r
104 \r
105 /** @addtogroup STM32F4xx_System_Private_Variables\r
106   * @{\r
107   */\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
115   */\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
118 \r
119 /**\r
120   * @}\r
121   */\r
122 \r
123 /** @addtogroup STM32F4xx_System_Private_FunctionPrototypes\r
124   * @{\r
125   */\r
126 \r
127 /**\r
128   * @}\r
129   */\r
130 \r
131 /** @addtogroup STM32F4xx_System_Private_Functions\r
132   * @{\r
133   */\r
134 \r
135 /**\r
136   * @brief  Setup the microcontroller system\r
137   *         Initialize the FPU setting, vector table location and External memory \r
138   *         configuration.\r
139   * @param  None\r
140   * @retval None\r
141   */\r
142 void SystemInit(void)\r
143 {\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
147   #endif\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
151 \r
152   /* Reset CFGR register */\r
153   RCC->CFGR = 0x00000000;\r
154 \r
155   /* Reset HSEON, CSSON and PLLON bits */\r
156   RCC->CR &= (uint32_t)0xFEF6FFFF;\r
157 \r
158   /* Reset PLLCFGR register */\r
159   RCC->PLLCFGR = 0x24003010;\r
160 \r
161   /* Reset HSEBYP bit */\r
162   RCC->CR &= (uint32_t)0xFFFBFFFF;\r
163 \r
164   /* Disable all interrupts */\r
165   RCC->CIR = 0x00000000;\r
166 \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
170 #else\r
171   SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */\r
172 #endif\r
173 }\r
174 \r
175 /**\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
180   *           \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
184   *     \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
188   *             \r
189   *           - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)\r
190   *                                              \r
191   *           - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)\r
192   *                          \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
195   *         \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
199   *    \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
204   *                \r
205   *         - The result of this function could be not correct when using fractional\r
206   *           value for HSE crystal.\r
207   *     \r
208   * @param  None\r
209   * @retval None\r
210   */\r
211 void SystemCoreClockUpdate(void)\r
212 {\r
213   uint32_t tmp = 0, pllvco = 0, pllp = 2, pllsource = 0, pllm = 2;\r
214   \r
215   /* Get SYSCLK source -------------------------------------------------------*/\r
216   tmp = RCC->CFGR & RCC_CFGR_SWS;\r
217 \r
218   switch (tmp)\r
219   {\r
220     case 0x00:  /* HSI used as system clock source */\r
221       SystemCoreClock = HSI_VALUE;\r
222       break;\r
223     case 0x04:  /* HSE used as system clock source */\r
224       SystemCoreClock = HSE_VALUE;\r
225       break;\r
226     case 0x08:  /* PLL used as system clock source */\r
227 \r
228       /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N\r
229          SYSCLK = PLL_VCO / PLL_P\r
230          */    \r
231       pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) >> 22;\r
232       pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;\r
233       \r
234       if (pllsource != 0)\r
235       {\r
236         /* HSE used as PLL clock source */\r
237         pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);\r
238       }\r
239       else\r
240       {\r
241         /* HSI used as PLL clock source */\r
242         pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);\r
243       }\r
244 \r
245       pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >>16) + 1 ) *2;\r
246       SystemCoreClock = pllvco/pllp;\r
247       break;\r
248     default:\r
249       SystemCoreClock = HSI_VALUE;\r
250       break;\r
251   }\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
257 }\r
258 \r
259 /**\r
260   * @}\r
261   */\r
262 \r
263 /**\r
264   * @}\r
265   */\r
266   \r
267 /**\r
268   * @}\r
269   */    \r
270 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/\r