2 ******************************************************************************
3 * @file system_stm32f7xx.c
4 * @author MCD Application Team
7 * @brief CMSIS Cortex-M7 Device Peripheral Access Layer System Source File.
9 * This file provides two functions and one global variable to be called from
11 * - SystemInit(): This function is called at startup just after reset and
12 * before branch to main program. This call is made inside
13 * the "startup_stm32f7xx.s" file.
15 * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
16 * by the user application to setup the SysTick
17 * timer or configure other parameters.
19 * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
20 * be called whenever the core clock is changed
21 * during program execution.
24 ******************************************************************************
27 * <h2><center>© COPYRIGHT 2016 STMicroelectronics</center></h2>
29 * Redistribution and use in source and binary forms, with or without modification,
30 * are permitted provided that the following conditions are met:
31 * 1. Redistributions of source code must retain the above copyright notice,
32 * this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright notice,
34 * this list of conditions and the following disclaimer in the documentation
35 * and/or other materials provided with the distribution.
36 * 3. Neither the name of STMicroelectronics nor the names of its contributors
37 * may be used to endorse or promote products derived from this software
38 * without specific prior written permission.
40 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
41 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
46 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
47 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
49 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51 ******************************************************************************
58 /** @addtogroup stm32f7xx_system
62 /** @addtogroup STM32F7xx_System_Private_Includes
67 #include "stm32f7xx.h"
68 #include "drivers/system.h"
70 #if !defined (HSE_VALUE)
71 #define HSE_VALUE ((uint32_t)8000000) /*!< Default value of the External oscillator in Hz */
72 #endif /* HSE_VALUE */
74 #if !defined (HSI_VALUE)
75 #define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/
76 #endif /* HSI_VALUE */
78 #if !defined(MHZ_VALUE)
85 #elif MHZ_VALUE == 168
89 #error "Unsupported MHZ_VALUE!"
93 #define PLL_P RCC_PLLP_DIV2 /* 2 */
97 #define PLL_SAIP RCC_PLLSAIP_DIV8
103 /** @addtogroup STM32F7xx_System_Private_TypesDefinitions
111 /** @addtogroup STM32F7xx_System_Private_Defines
115 /************************* Miscellaneous Configuration ************************/
117 /*!< Uncomment the following line if you need to relocate your vector Table in
119 /* #define VECT_TAB_SRAM */
120 #define VECT_TAB_OFFSET 0x00 /*!< Vector Table base offset field.
121 This value must be a multiple of 0x200. */
122 /******************************************************************************/
128 /** @addtogroup STM32F7xx_System_Private_Macros
136 /** @addtogroup STM32F7xx_System_Private_Variables
140 /* This variable is updated in three ways:
141 1) by calling CMSIS function SystemCoreClockUpdate()
142 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
143 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
144 Note: If you use this function to configure the system clock; then there
145 is no need to call the 2 first functions listed above, since SystemCoreClock
146 variable is updated automatically.
148 uint32_t SystemCoreClock
= (PLL_N
/ PLL_P
) * 1000000;
149 const uint8_t AHBPrescTable
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
150 const uint8_t APBPrescTable
[8] = {0, 0, 0, 0, 1, 2, 3, 4};
156 /** @addtogroup STM32F7xx_System_Private_FunctionPrototypes
160 /// TODO: F7 check if this is the best configuration for the clocks.
161 // current settings are just a copy from one of the example projects
162 void SystemClock_Config(void)
164 RCC_ClkInitTypeDef RCC_ClkInitStruct
;
165 RCC_OscInitTypeDef RCC_OscInitStruct
;
166 RCC_PeriphCLKInitTypeDef PeriphClkInitStruct
;
167 HAL_StatusTypeDef ret
;
169 __HAL_RCC_PWR_CLK_ENABLE();
171 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1
);
173 /* Enable HSE Oscillator and activate PLL with HSE as source */
174 RCC_OscInitStruct
.OscillatorType
= RCC_OSCILLATORTYPE_HSE
;
175 RCC_OscInitStruct
.HSEState
= RCC_HSE_ON
;
176 RCC_OscInitStruct
.PLL
.PLLState
= RCC_PLL_ON
;
177 RCC_OscInitStruct
.PLL
.PLLSource
= RCC_PLLSOURCE_HSE
;
178 RCC_OscInitStruct
.PLL
.PLLM
= PLL_M
;
179 RCC_OscInitStruct
.PLL
.PLLN
= PLL_N
;
180 RCC_OscInitStruct
.PLL
.PLLP
= PLL_P
;
181 RCC_OscInitStruct
.PLL
.PLLQ
= PLL_Q
;
183 ret
= HAL_RCC_OscConfig(&RCC_OscInitStruct
);
189 /* Activate the OverDrive to reach the 216 MHz Frequency */
190 ret
= HAL_PWREx_EnableOverDrive();
195 /* Select PLLSAI output as USB clock source */
196 PeriphClkInitStruct
.PeriphClockSelection
= RCC_PERIPHCLK_CLK48
;
197 PeriphClkInitStruct
.Clk48ClockSelection
= RCC_CLK48SOURCE_PLLSAIP
;
198 PeriphClkInitStruct
.PLLSAI
.PLLSAIN
= PLL_SAIN
;
199 PeriphClkInitStruct
.PLLSAI
.PLLSAIQ
= PLL_SAIQ
;
200 PeriphClkInitStruct
.PLLSAI
.PLLSAIP
= PLL_SAIP
;
201 if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct
) != HAL_OK
)
206 /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */
207 RCC_ClkInitStruct
.ClockType
= (RCC_CLOCKTYPE_SYSCLK
| RCC_CLOCKTYPE_HCLK
| RCC_CLOCKTYPE_PCLK1
| RCC_CLOCKTYPE_PCLK2
);
208 RCC_ClkInitStruct
.SYSCLKSource
= RCC_SYSCLKSOURCE_PLLCLK
;
209 RCC_ClkInitStruct
.AHBCLKDivider
= RCC_SYSCLK_DIV1
;
210 RCC_ClkInitStruct
.APB1CLKDivider
= RCC_HCLK_DIV4
;
211 RCC_ClkInitStruct
.APB2CLKDivider
= RCC_HCLK_DIV2
;
213 ret
= HAL_RCC_ClockConfig(&RCC_ClkInitStruct
, FLASH_LATENCY_7
);
219 PeriphClkInitStruct
.PeriphClockSelection
= RCC_PERIPHCLK_USART1
|RCC_PERIPHCLK_USART2
220 |RCC_PERIPHCLK_USART3
|RCC_PERIPHCLK_USART6
221 |RCC_PERIPHCLK_UART4
|RCC_PERIPHCLK_UART5
222 |RCC_PERIPHCLK_UART7
|RCC_PERIPHCLK_UART8
223 |RCC_PERIPHCLK_I2C1
|RCC_PERIPHCLK_I2C3
224 |RCC_PERIPHCLK_I2C2
|RCC_PERIPHCLK_I2C4
;
225 PeriphClkInitStruct
.Usart1ClockSelection
= RCC_USART1CLKSOURCE_PCLK2
;
226 PeriphClkInitStruct
.Usart2ClockSelection
= RCC_USART2CLKSOURCE_PCLK1
;
227 PeriphClkInitStruct
.Usart3ClockSelection
= RCC_USART3CLKSOURCE_PCLK1
;
228 PeriphClkInitStruct
.Uart4ClockSelection
= RCC_UART4CLKSOURCE_PCLK1
;
229 PeriphClkInitStruct
.Uart5ClockSelection
= RCC_UART5CLKSOURCE_PCLK1
;
230 PeriphClkInitStruct
.Usart6ClockSelection
= RCC_USART6CLKSOURCE_PCLK2
;
231 PeriphClkInitStruct
.Uart7ClockSelection
= RCC_UART7CLKSOURCE_PCLK1
;
232 PeriphClkInitStruct
.Uart8ClockSelection
= RCC_UART8CLKSOURCE_PCLK1
;
233 PeriphClkInitStruct
.I2c1ClockSelection
= RCC_I2C1CLKSOURCE_PCLK1
;
234 PeriphClkInitStruct
.I2c2ClockSelection
= RCC_I2C2CLKSOURCE_PCLK1
;
235 PeriphClkInitStruct
.I2c3ClockSelection
= RCC_I2C3CLKSOURCE_PCLK1
;
236 PeriphClkInitStruct
.I2c4ClockSelection
= RCC_I2C4CLKSOURCE_PCLK1
;
237 ret
= HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct
);
243 // Activating the timerprescalers while the APBx prescalers are 1/2/4 will connect the TIMxCLK to HCLK which has been configured to 216MHz
244 __HAL_RCC_TIMCLKPRESCALER(RCC_TIMPRES_ACTIVATED
);
246 SystemCoreClockUpdate();
253 /** @addtogroup STM32F7xx_System_Private_Functions
258 * @brief Setup the microcontroller system
259 * Initialize the Embedded Flash Interface, the PLL and update the
260 * SystemFrequency variable.
264 void SystemInit(void)
266 initialiseMemorySections();
268 /* FPU settings ------------------------------------------------------------*/
269 #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
270 SCB
->CPACR
|= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */
272 /* Reset the RCC clock configuration to the default reset state ------------*/
274 RCC
->CR
|= (uint32_t)0x00000001;
276 /* Reset CFGR register */
277 RCC
->CFGR
= 0x00000000;
279 /* Reset HSEON, CSSON and PLLON bits */
280 RCC
->CR
&= (uint32_t)0xFEF6FFFF;
282 /* Reset PLLCFGR register */
283 RCC
->PLLCFGR
= 0x24003010;
285 /* Reset HSEBYP bit */
286 RCC
->CR
&= (uint32_t)0xFFFBFFFF;
288 /* Disable all interrupts */
289 RCC
->CIR
= 0x00000000;
291 /* Configure the Vector Table location add offset address ------------------*/
293 SCB
->VTOR
= RAMDTCM_BASE
| VECT_TAB_OFFSET
; /* Vector Table Relocation in Internal SRAM */
295 extern uint8_t isr_vector_table_base
; /* Vector Table Relocation in Internal FLASH */
296 SCB
->VTOR
= (uint32_t) &isr_vector_table_base
;
303 //SCB_EnableDCache();
305 /* Configure the system clock to 216 MHz */
306 SystemClock_Config();
308 if (SystemCoreClock
!= MHZ_VALUE
* 1000000)
312 // There is a mismatch between the configured clock and the expected clock in portable.h
320 * @brief Update SystemCoreClock variable according to Clock Register Values.
321 * The SystemCoreClock variable contains the core clock (HCLK), it can
322 * be used by the user application to setup the SysTick timer or configure
325 * @note Each time the core clock (HCLK) changes, this function must be called
326 * to update SystemCoreClock variable value. Otherwise, any configuration
327 * based on this variable will be incorrect.
329 * @note - The system frequency computed by this function is not the real
330 * frequency in the chip. It is calculated based on the predefined
331 * constant and the selected clock source:
333 * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
335 * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
337 * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
338 * or HSI_VALUE(*) multiplied/divided by the PLL factors.
340 * (*) HSI_VALUE is a constant defined in stm32f7xx_hal_conf.h file (default value
341 * 16 MHz) but the real value may vary depending on the variations
342 * in voltage and temperature.
344 * (**) HSE_VALUE is a constant defined in stm32f7xx_hal_conf.h file (default value
345 * 25 MHz), user has to ensure that HSE_VALUE is same as the real
346 * frequency of the crystal used. Otherwise, this function may
349 * - The result of this function could be not correct when using fractional
350 * value for HSE crystal.
355 void SystemCoreClockUpdate(void)
357 uint32_t tmp
= 0, pllvco
= 0, pllp
= 2, pllsource
= 0, pllm
= 2;
359 /* Get SYSCLK source -------------------------------------------------------*/
360 tmp
= RCC
->CFGR
& RCC_CFGR_SWS
;
364 case 0x00: /* HSI used as system clock source */
365 SystemCoreClock
= HSI_VALUE
;
367 case 0x04: /* HSE used as system clock source */
368 SystemCoreClock
= HSE_VALUE
;
370 case 0x08: /* PLL used as system clock source */
372 /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N
373 SYSCLK = PLL_VCO / PLL_P
375 pllsource
= (RCC
->PLLCFGR
& RCC_PLLCFGR_PLLSRC
) >> 22;
376 pllm
= RCC
->PLLCFGR
& RCC_PLLCFGR_PLLM
;
380 /* HSE used as PLL clock source */
381 pllvco
= (HSE_VALUE
/ pllm
) * ((RCC
->PLLCFGR
& RCC_PLLCFGR_PLLN
) >> 6);
385 /* HSI used as PLL clock source */
386 pllvco
= (HSI_VALUE
/ pllm
) * ((RCC
->PLLCFGR
& RCC_PLLCFGR_PLLN
) >> 6);
389 pllp
= (((RCC
->PLLCFGR
& RCC_PLLCFGR_PLLP
) >>16) + 1 ) *2;
390 SystemCoreClock
= pllvco
/pllp
;
393 SystemCoreClock
= HSI_VALUE
;
396 /* Compute HCLK frequency --------------------------------------------------*/
397 /* Get HCLK prescaler */
398 tmp
= AHBPrescTable
[((RCC
->CFGR
& RCC_CFGR_HPRE
) >> 4)];
400 SystemCoreClock
>>= tmp
;
414 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/