rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
system_stm32f7xx.c
Go to the documentation of this file.
1/**
2 ******************************************************************************
3 * @file system_stm32f7xx.c
4 * @author MCD Application Team
5 * @brief CMSIS Cortex-M7 Device Peripheral Access Layer System Source File.
6 *
7 * This file provides two functions and one global variable to be called from
8 * user application:
9 * - SystemInit(): This function is called at startup just after reset and
10 * before branch to main program. This call is made inside
11 * the "startup_stm32f7xx.s" file.
12 *
13 * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
14 * by the user application to setup the SysTick
15 * timer or configure other parameters.
16 *
17 * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
18 * be called whenever the core clock is changed
19 * during program execution.
20 *
21 *
22 ******************************************************************************
23 * @attention
24 *
25 * <h2><center>&copy; COPYRIGHT 2016 STMicroelectronics</center></h2>
26 *
27 * Redistribution and use in source and binary forms, with or without modification,
28 * are permitted provided that the following conditions are met:
29 * 1. Redistributions of source code must retain the above copyright notice,
30 * this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright notice,
32 * this list of conditions and the following disclaimer in the documentation
33 * and/or other materials provided with the distribution.
34 * 3. Neither the name of STMicroelectronics nor the names of its contributors
35 * may be used to endorse or promote products derived from this software
36 * without specific prior written permission.
37 *
38 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
39 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
42 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
43 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
44 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
45 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48 *
49 ******************************************************************************
50 */
51
52/** @addtogroup CMSIS
53 * @{
54 */
55
56/** @addtogroup stm32f7xx_system
57 * @{
58 */
59
60/** @addtogroup STM32F7xx_System_Private_Includes
61 * @{
62 */
63
64#include "stm32f7xx.h"
65
66/* No default! Should be explictly defined in openblt/board.mk */
67#if 0
68#if !defined (HSE_VALUE)
69 #define HSE_VALUE ((uint32_t)25000000) /*!< Default value of the External oscillator in Hz */
70#endif /* HSE_VALUE */
71#endif
72
73#if !defined (HSI_VALUE)
74 #define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/
75#endif /* HSI_VALUE */
76
77/**
78 * @}
79 */
80
81/** @addtogroup STM32F7xx_System_Private_TypesDefinitions
82 * @{
83 */
84
85/**
86 * @}
87 */
88
89/** @addtogroup STM32F7xx_System_Private_Defines
90 * @{
91 */
92
93/************************* Miscellaneous Configuration ************************/
94
95/*!< Uncomment the following line if you need to relocate your vector Table in
96 Internal SRAM. */
97/* #define VECT_TAB_SRAM */
98#define VECT_TAB_OFFSET 0x00 /*!< Vector Table base offset field.
99 This value must be a multiple of 0x200. */
100/******************************************************************************/
101
102/**
103 * @}
104 */
105
106/** @addtogroup STM32F7xx_System_Private_Macros
107 * @{
108 */
109
110/**
111 * @}
112 */
113
114/** @addtogroup STM32F7xx_System_Private_Variables
115 * @{
116 */
117
118 /* This variable is updated in three ways:
119 1) by calling CMSIS function SystemCoreClockUpdate()
120 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
121 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
122 Note: If you use this function to configure the system clock; then there
123 is no need to call the 2 first functions listed above, since SystemCoreClock
124 variable is updated automatically.
125 */
126 uint32_t SystemCoreClock = 16000000;
127 const uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
128 const uint8_t APBPrescTable[8] = {0, 0, 0, 0, 1, 2, 3, 4};
129
130/**
131 * @}
132 */
133
134/** @addtogroup STM32F7xx_System_Private_FunctionPrototypes
135 * @{
136 */
137
138/**
139 * @}
140 */
141
142/** @addtogroup STM32F7xx_System_Private_Functions
143 * @{
144 */
145
146/**
147 * @brief Setup the microcontroller system
148 * Initialize the Embedded Flash Interface, the PLL and update the
149 * SystemFrequency variable.
150 * @param None
151 * @retval None
152 */
153void SystemInit(void)
154{
155 /* FPU settings ------------------------------------------------------------*/
156 #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
157 SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */
158 #endif
159 /* Reset the RCC clock configuration to the default reset state ------------*/
160 /* Set HSION bit */
161 RCC->CR |= (uint32_t)0x00000001;
162
163 /* Reset CFGR register */
164 RCC->CFGR = 0x00000000;
165
166 /* Reset HSEON, CSSON and PLLON bits */
167 RCC->CR &= (uint32_t)0xFEF6FFFF;
168
169 /* Reset PLLCFGR register */
170 RCC->PLLCFGR = 0x24003010;
171
172 /* Reset HSEBYP bit */
173 RCC->CR &= (uint32_t)0xFFFBFFFF;
174
175 /* Disable all interrupts */
176 RCC->CIR = 0x00000000;
177
178 /* Configure the Vector Table location add offset address ------------------*/
179#ifdef VECT_TAB_SRAM
180 SCB->VTOR = RAMDTCM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
181#else
182 SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
183#endif
184}
185
186/**
187 * @brief Update SystemCoreClock variable according to Clock Register Values.
188 * The SystemCoreClock variable contains the core clock (HCLK), it can
189 * be used by the user application to setup the SysTick timer or configure
190 * other parameters.
191 *
192 * @note Each time the core clock (HCLK) changes, this function must be called
193 * to update SystemCoreClock variable value. Otherwise, any configuration
194 * based on this variable will be incorrect.
195 *
196 * @note - The system frequency computed by this function is not the real
197 * frequency in the chip. It is calculated based on the predefined
198 * constant and the selected clock source:
199 *
200 * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
201 *
202 * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
203 *
204 * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
205 * or HSI_VALUE(*) multiplied/divided by the PLL factors.
206 *
207 * (*) HSI_VALUE is a constant defined in stm32f7xx_hal_conf.h file (default value
208 * 16 MHz) but the real value may vary depending on the variations
209 * in voltage and temperature.
210 *
211 * (**) HSE_VALUE is a constant defined in stm32f7xx_hal_conf.h file (default value
212 * 25 MHz), user has to ensure that HSE_VALUE is same as the real
213 * frequency of the crystal used. Otherwise, this function may
214 * have wrong result.
215 *
216 * - The result of this function could be not correct when using fractional
217 * value for HSE crystal.
218 *
219 * @param None
220 * @retval None
221 */
223{
224 uint32_t tmp = 0, pllvco = 0, pllp = 2, pllsource = 0, pllm = 2;
225
226 /* Get SYSCLK source -------------------------------------------------------*/
227 tmp = RCC->CFGR & RCC_CFGR_SWS;
228
229 switch (tmp)
230 {
231 case 0x00: /* HSI used as system clock source */
232 SystemCoreClock = HSI_VALUE;
233 break;
234 case 0x04: /* HSE used as system clock source */
235 SystemCoreClock = HSE_VALUE;
236 break;
237 case 0x08: /* PLL used as system clock source */
238
239 /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N
240 SYSCLK = PLL_VCO / PLL_P
241 */
242 pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) >> 22;
243 pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
244
245 if (pllsource != 0)
246 {
247 /* HSE used as PLL clock source */
248 pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
249 }
250 else
251 {
252 /* HSI used as PLL clock source */
253 pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
254 }
255
256 pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >>16) + 1 ) *2;
257 SystemCoreClock = pllvco/pllp;
258 break;
259 default:
260 SystemCoreClock = HSI_VALUE;
261 break;
262 }
263 /* Compute HCLK frequency --------------------------------------------------*/
264 /* Get HCLK prescaler */
265 tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
266 /* HCLK frequency */
267 SystemCoreClock >>= tmp;
268}
269
270/**
271 * @}
272 */
273
274/**
275 * @}
276 */
277
278/**
279 * @}
280 */
281/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
void SystemInit(void)
Setup the microcontroller system Initialize the Embedded Flash Interface, the PLL and update the Syst...
void SystemCoreClockUpdate(void)
Update SystemCoreClock variable according to Clock Register Values. The SystemCoreClock variable cont...
const uint8_t APBPrescTable[8]
const uint8_t AHBPrescTable[16]
uint32_t SystemCoreClock