source: rtems/bsps/arm/stm32h7/boards/stm/stm32h7b3i-dk/system_stm32h7xx.c @ 1a4e78b3

Last change on this file since 1a4e78b3 was 1a4e78b3, checked in by Karel Gardas <karel@…>, on 03/09/23 at 13:15:53

bsps/stm32h7: fix propagation of configured HSE freq. value into the code

Sponsored-By: Precidata

  • Property mode set to 100644
File size: 11.5 KB
Line 
1/**
2  ******************************************************************************
3  * @file    system_stm32h7xx.c
4  * @author  MCD Application Team
5  * @brief   CMSIS Cortex-Mx 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_stm32h7xx.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  * Copyright (c) 2018 STMicroelectronics.
26  * All rights reserved.
27  *
28  * This software is licensed under terms that can be found in the LICENSE file
29  * in the root directory of this software component.
30  * If no LICENSE file comes with this software, it is provided AS-IS.
31  *
32  ******************************************************************************
33  */
34
35/** @addtogroup CMSIS
36  * @{
37  */
38
39/** @addtogroup stm32h7xx_system
40  * @{
41  */
42
43/** @addtogroup STM32H7xx_System_Private_Includes
44  * @{
45  */
46
47#include "stm32h7xx.h"
48#include <math.h>
49#ifdef __rtems__
50#include <bsp/linker-symbols.h>
51#include <bspopts.h>
52
53#define HSE_VALUE STM32H7_HSE_FREQUENCY
54
55#endif /* __rtems__ */
56#if !defined  (HSE_VALUE)
57#define HSE_VALUE    ((uint32_t)24000000) /*!< Value of the External oscillator in Hz */
58#endif /* HSE_VALUE */
59
60#if !defined  (CSI_VALUE)
61  #define CSI_VALUE    ((uint32_t)4000000) /*!< Value of the Internal oscillator in Hz*/
62#endif /* CSI_VALUE */
63
64#if !defined  (HSI_VALUE)
65  #define HSI_VALUE    ((uint32_t)64000000) /*!< Value of the Internal oscillator in Hz*/
66#endif /* HSI_VALUE */
67
68
69/**
70  * @}
71  */
72
73/** @addtogroup STM32H7xx_System_Private_TypesDefinitions
74  * @{
75  */
76
77/**
78  * @}
79  */
80
81/** @addtogroup STM32H7xx_System_Private_Defines
82  * @{
83  */
84
85/************************* Miscellaneous Configuration ************************/
86/*!< Uncomment the following line if you need to use initialized data in CD domain AHB SRAM */
87/* #define DATA_IN_CD_AHB_SRAM */
88
89/*!< Uncomment the following line if you need to relocate your vector Table in
90     Internal SRAM. */
91/* #define VECT_TAB_SRAM */
92#define VECT_TAB_OFFSET  0x00000000UL /*!< Vector Table base offset field.
93                                      This value must be a multiple of 0x200. */
94/******************************************************************************/
95
96/**
97  * @}
98  */
99
100/** @addtogroup STM32H7xx_System_Private_Macros
101  * @{
102  */
103
104/**
105  * @}
106  */
107
108/** @addtogroup STM32H7xx_System_Private_Variables
109  * @{
110  */
111  /* This variable is updated in three ways:
112      1) by calling CMSIS function SystemCoreClockUpdate()
113      2) by calling HAL API function HAL_RCC_GetHCLKFreq()
114      3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
115         Note: If you use this function to configure the system clock; then there
116               is no need to call the 2 first functions listed above, since SystemCoreClock
117               variable is updated automatically.
118  */
119#ifndef __rtems__
120  uint32_t SystemCoreClock = 64000000;
121  uint32_t SystemD2Clock = 64000000;  /* AXI and AHBs Clock frequency */
122#else /* __rtems__ */
123  RTEMS_SECTION(".rtemsstack") uint32_t SystemCoreClock;
124  RTEMS_SECTION(".rtemsstack") uint32_t SystemD2Clock;
125#endif /* __rtems__ */
126  const  uint8_t D1CorePrescTable[16] = {0, 0, 0, 0, 1, 2, 3, 4, 1, 2, 3, 4, 6, 7, 8, 9}; /* CPU Domain Core Prescaler Table */
127
128/**
129  * @}
130  */
131
132/** @addtogroup STM32H7xx_System_Private_FunctionPrototypes
133  * @{
134  */
135
136/**
137  * @}
138  */
139
140/** @addtogroup STM32H7xx_System_Private_Functions
141  * @{
142  */
143
144/**
145  * @brief  Setup the microcontroller system
146  *         Initialize the FPU setting and  vector table location
147  *         configuration.
148  * @param  None
149  * @retval None
150  */
151void SystemInit (void)
152{
153#ifdef DATA_IN_CD_AHB_SRAM
154 __IO uint32_t tmpreg;
155#endif /* DATA_IN_CD_AHB_SRAM */
156
157  /* FPU settings ------------------------------------------------------------*/
158  #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
159    SCB->CPACR |= ((3UL << (10*2))|(3UL << (11*2)));  /* set CP10 and CP11 Full Access */
160  #endif
161  /* Reset the RCC clock configuration to the default reset state ------------*/
162  /* Set HSION bit */
163  RCC->CR |= RCC_CR_HSION;
164
165  /* Reset CFGR register */
166  RCC->CFGR = 0x00000000;
167
168  /* Reset HSEON, CSSON , CSION,RC48ON, CSIKERON PLL1ON, PLL2ON and PLL3ON bits */
169  RCC->CR &= 0xEAF6ED7FU;
170
171  /* Reset CDCFGR1 register */
172  RCC->CDCFGR1 = 0x00000000;
173
174  /* Reset CDCFGR2 register */
175  RCC->CDCFGR2 = 0x00000000;
176
177  /* Reset SRDCFGR register */
178  RCC->SRDCFGR = 0x00000000;
179
180  /* Reset PLLCKSELR register */
181  RCC->PLLCKSELR = 0x02020200;
182
183  /* Reset PLLCFGR register */
184  RCC->PLLCFGR = 0x01FF0000;
185  /* Reset PLL1DIVR register */
186  RCC->PLL1DIVR = 0x01010280;
187  /* Reset PLL1FRACR register */
188  RCC->PLL1FRACR = 0x00000000;
189
190  /* Reset PLL2DIVR register */
191  RCC->PLL2DIVR = 0x01010280;
192
193  /* Reset PLL2FRACR register */
194
195  RCC->PLL2FRACR = 0x00000000;
196  /* Reset PLL3DIVR register */
197  RCC->PLL3DIVR = 0x01010280;
198
199  /* Reset PLL3FRACR register */
200  RCC->PLL3FRACR = 0x00000000;
201
202  /* Reset HSEBYP bit */
203  RCC->CR &= 0xFFFBFFFFU;
204
205  /* Disable all interrupts */
206  RCC->CIER = 0x00000000;
207
208#ifdef DATA_IN_CD_AHB_SRAM
209  /* in case of initialized data in CD AHB SRAM, enable the CD AHB SRAM clock */
210  RCC->AHB2ENR |= (RCC_AHB2ENR_AHBSRAM1EN | RCC_AHB2ENR_AHBSRAM2EN);
211#ifndef __rtems__
212  tmpreg = RCC->AHB2ENR;
213  (void) tmpreg;
214#else /* __rtems__ */
215  RCC->AHB2ENR;
216#endif /* __rtems__ */
217#endif /* DATA_IN_CD_AHB_SRAM */
218
219/*
220   * Disable the FMC bank1 (enabled after reset).
221   * This, prevents CPU speculation access on this bank which blocks the use of FMC during
222   * 24us. During this time the others FMC master (such as LTDC) cannot use it!
223   */
224  FMC_Bank1_R->BTCR[0] = 0x000030D2;
225
226#ifndef __rtems__
227  /* Configure the Vector Table location add offset address for cortex-M7 ------------------*/
228#ifdef VECT_TAB_SRAM
229  SCB->VTOR = CD_AXISRAM1_BASE  | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal CD AXI-RAM */
230#else
231  SCB->VTOR = FLASH_BANK1_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
232#endif
233#else /* __rtems__ */
234  SCB->VTOR = (uint32_t) bsp_start_vector_table_begin;
235#endif /* __rtems__ */
236
237}
238
239/**
240   * @brief  Update SystemCoreClock variable according to Clock Register Values.
241  *         The SystemCoreClock variable contains the core clock , it can
242  *         be used by the user application to setup the SysTick timer or configure
243  *         other parameters.
244  *
245  * @note   Each time the core clock changes, this function must be called
246  *         to update SystemCoreClock variable value. Otherwise, any configuration
247  *         based on this variable will be incorrect.
248  *
249  * @note   - The system frequency computed by this function is not the real
250  *           frequency in the chip. It is calculated based on the predefined
251  *           constant and the selected clock source:
252  *
253  *           - If SYSCLK source is CSI, SystemCoreClock will contain the CSI_VALUE(*)
254  *           - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(**)
255  *           - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(***)
256  *           - If SYSCLK source is PLL, SystemCoreClock will contain the CSI_VALUE(*),
257  *             HSI_VALUE(**) or HSE_VALUE(***) multiplied/divided by the PLL factors.
258  *
259  *         (*) CSI_VALUE is a constant defined in stm32h7xx_hal.h file (default value
260  *             4 MHz) but the real value may vary depending on the variations
261  *             in voltage and temperature.
262  *         (**) HSI_VALUE is a constant defined in stm32h7xx_hal.h file (default value
263  *             64 MHz) but the real value may vary depending on the variations
264  *             in voltage and temperature.
265  *
266  *         (***)HSE_VALUE is a constant defined in stm32h7xx_hal.h file (default value
267  *              25 MHz), user has to ensure that HSE_VALUE is same as the real
268  *              frequency of the crystal used. Otherwise, this function may
269  *              have wrong result.
270  *
271  *         - The result of this function could be not correct when using fractional
272  *           value for HSE crystal.
273  * @param  None
274  * @retval None
275  */
276void SystemCoreClockUpdate (void)
277{
278  uint32_t pllp, pllsource, pllm, pllfracen, hsivalue, tmp;
279  float_t fracn1, pllvco;
280
281  /* Get SYSCLK source -------------------------------------------------------*/
282
283  switch (RCC->CFGR & RCC_CFGR_SWS)
284  {
285  case RCC_CFGR_SWS_HSI:  /* HSI used as system clock source */
286    SystemCoreClock = (uint32_t) (HSI_VALUE >> ((RCC->CR & RCC_CR_HSIDIV)>> 3));
287    break;
288
289  case RCC_CFGR_SWS_CSI:  /* CSI used as system clock  source */
290    SystemCoreClock = CSI_VALUE;
291    break;
292
293  case RCC_CFGR_SWS_HSE:  /* HSE used as system clock  source */
294    SystemCoreClock = HSE_VALUE;
295    break;
296
297  case RCC_CFGR_SWS_PLL1:  /* PLL1 used as system clock  source */
298
299    /* PLL_VCO = (HSE_VALUE or HSI_VALUE or CSI_VALUE/ PLLM) * PLLN
300    SYSCLK = PLL_VCO / PLLR
301    */
302    pllsource = (RCC->PLLCKSELR & RCC_PLLCKSELR_PLLSRC);
303    pllm = ((RCC->PLLCKSELR & RCC_PLLCKSELR_DIVM1)>> 4)  ;
304    pllfracen = ((RCC->PLLCFGR & RCC_PLLCFGR_PLL1FRACEN)>>RCC_PLLCFGR_PLL1FRACEN_Pos);
305    fracn1 = (float_t)(uint32_t)(pllfracen* ((RCC->PLL1FRACR & RCC_PLL1FRACR_FRACN1)>> 3));
306
307    if (pllm != 0U)
308    {
309      switch (pllsource)
310      {
311        case RCC_PLLCKSELR_PLLSRC_HSI:  /* HSI used as PLL clock source */
312
313        hsivalue = (HSI_VALUE >> ((RCC->CR & RCC_CR_HSIDIV)>> 3)) ;
314        pllvco = ( (float_t)hsivalue / (float_t)pllm) * ((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/(float_t)0x2000) +(float_t)1 );
315
316        break;
317
318        case RCC_PLLCKSELR_PLLSRC_CSI:  /* CSI used as PLL clock source */
319          pllvco = ((float_t)CSI_VALUE / (float_t)pllm) * ((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/(float_t)0x2000) +(float_t)1 );
320        break;
321
322        case RCC_PLLCKSELR_PLLSRC_HSE:  /* HSE used as PLL clock source */
323          pllvco = ((float_t)HSE_VALUE / (float_t)pllm) * ((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/(float_t)0x2000) +(float_t)1 );
324        break;
325
326      default:
327          pllvco = ((float_t)CSI_VALUE / (float_t)pllm) * ((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/(float_t)0x2000) +(float_t)1 );
328        break;
329      }
330      pllp = (((RCC->PLL1DIVR & RCC_PLL1DIVR_P1) >>9) + 1U ) ;
331      SystemCoreClock =  (uint32_t)(float_t)(pllvco/(float_t)pllp);
332    }
333    else
334    {
335      SystemCoreClock = 0U;
336    }
337    break;
338
339  default:
340    SystemCoreClock = CSI_VALUE;
341    break;
342  }
343
344  /* Compute SystemClock frequency --------------------------------------------------*/
345
346  tmp = D1CorePrescTable[(RCC->CDCFGR1 & RCC_CDCFGR1_CDCPRE)>> RCC_CDCFGR1_CDCPRE_Pos];
347
348  /* SystemCoreClock frequency : CM7 CPU frequency  */
349  SystemCoreClock >>= tmp;
350
351  /* SystemD2Clock frequency : AXI and AHBs Clock frequency  */
352  SystemD2Clock = (SystemCoreClock >> ((D1CorePrescTable[(RCC->CDCFGR1 & RCC_CDCFGR1_HPRE)>> RCC_CDCFGR1_HPRE_Pos]) & 0x1FU));
353
354}
355
356
357/**
358  * @}
359  */
360
361/**
362  * @}
363  */
364
365/**
366  * @}
367  */
Note: See TracBrowser for help on using the repository browser.