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

Last change on this file since d8017f20 was d8017f20, checked in by Karel Gardas <karel@…>, on 05/15/22 at 20:10:40

bsp/stm32h7: update stm32h7b3i-dk board system_stm32h7xx.c file

Updated content comes from STM32CubeIDE 1.9.0 generated for STM32H7B3I-DK
board and have RTEMS related changes merged in.

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