• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2   ******************************************************************************
3   * @file    system_stm32mp1xx.c
4   * @author  MCD Application Team
5   * @brief   CMSIS Cortex 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_stm32mp1xx.s" file.
12   *
13   *      - SystemCoreClock variable: Contains the core clock frequency, it can
14   *                                  be used by the user application to setup
15   *                                  the SysTick timer or configure other
16   *                                  parameters.
17   *
18   *      - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
19   *                                 be called whenever the core clock is changed
20   *                                 during program execution.
21   *
22   *
23   ******************************************************************************
24   *
25   * @attention
26   *
27   * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
28   * All rights reserved.</center></h2>
29   *
30   * This software component is licensed by ST under BSD 3-Clause license,
31   * the "License"; You may not use this file except in compliance with the
32   * License. You may obtain a copy of the License at:
33   *                       opensource.org/licenses/BSD-3-Clause
34   *
35   *
36   ******************************************************************************
37   */
38 
39 /** @addtogroup CMSIS
40   * @{
41   */
42 
43 /** @addtogroup stm32mp1xx_system
44   * @{
45   */
46 
47 /** @addtogroup STM32MP1xx_System_Private_Includes
48   * @{
49   */
50 
51 #include "stm32mp1xx_hal.h"
52 
53 /**
54   * @}
55   */
56 
57 /** @addtogroup STM32MP1xx_System_Private_TypesDefinitions
58   * @{
59   */
60 
61 
62 /**
63   * @}
64   */
65 
66 /** @addtogroup STM32MP1xx_System_Private_Defines
67   * @{
68   */
69 
70 
71 /************************* Miscellaneous Configuration ************************/
72 /*!< Uncomment the following line if you need to use external SRAM mounted
73      on EVAL board as data memory  */
74 /* #define DATA_IN_ExtSRAM */
75 
76 /*!< Uncomment the following line if you need to relocate your vector Table in
77      Internal SRAM. */
78 /* #define VECT_TAB_SRAM */
79 #define VECT_TAB_OFFSET  0x00 /*!< Vector Table base offset field.
80                                    This value must be a multiple of 0x200. */
81 /******************************************************************************/
82 
83 /**
84   * @}
85   */
86 
87 /** @addtogroup STM32MP1xx_System_Private_Macros
88   * @{
89   */
90 
91 /**
92   * @}
93   */
94 
95 /** @addtogroup STM32MP1xx_System_Private_Variables
96   * @{
97   */
98   /* This variable is updated in three ways:
99       1) by calling CMSIS function SystemCoreClockUpdate()
100       2) each time HAL_RCC_ClockConfig() is called to configure the system clock
101          frequency
102          Note: If you use this function to configure the system clock;
103                then there is no need to call the first functions listed above,
104                since SystemCoreClock variable is updated automatically.
105   */
106   uint32_t SystemCoreClock = HSI_VALUE;
107 /**
108   * @}
109   */
110 
111 /** @addtogroup STM32MP1xx_System_Private_FunctionPrototypes
112   * @{
113   */
114 
115 #if defined (DATA_IN_ExtSRAM)
116   static void SystemInit_ExtMemCtl(void);
117 #endif /* DATA_IN_ExtSRAM */
118 
119 /**
120   * @}
121   */
122 
123 /** @addtogroup STM32MP1xx_System_Private_Functions
124   * @{
125   */
126 
127   /**
128   * @brief  Setup the microcontroller system
129   *         Initialize the FPU setting, vector table location and External memory
130   *         configuration.
131   * @param  None
132   * @retval None
133   */
Hal_SystemInit(void)134 void Hal_SystemInit (void)
135 {
136   /* FPU settings ------------------------------------------------------------*/
137 // #if defined (CORE_CM4)
138 //   #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
139 //    SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2));  /* set CP10 and CP11 Full Access */
140 //   #endif
141 
142 //   /* Configure the Vector Table location add offset address ------------------*/
143 // #if defined (VECT_TAB_SRAM)
144 //   SCB->VTOR = MCU_AHB_SRAM | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
145 // #endif
146 //   /* Disable all interrupts and events */
147 //   CLEAR_REG(EXTI_C2->IMR1);
148 //   CLEAR_REG(EXTI_C2->IMR2);
149 //   CLEAR_REG(EXTI_C2->IMR3);
150 //   CLEAR_REG(EXTI_C2->EMR1);
151 //   CLEAR_REG(EXTI_C2->EMR2);
152 //   CLEAR_REG(EXTI_C2->EMR3);
153 // #else
154 // #error Please #define CORE_CM4
155 // #endif
156   SystemCoreClockUpdate();
157 }
158 
159 /**
160    * @brief Update SystemCoreClock variable according to Clock Register Values.
161   *         The SystemCoreClock variable contains the core clock frequency (Hz),
162   *         it can be used by the user application to setup the SysTick timer or
163   *         configure other parameters.
164   *
165   * @note   Each time the core clock changes, this function must be called to
166   *         update SystemCoreClock variable value. Otherwise, any configuration
167   *         based on this variable will be incorrect.
168   *
169   * @note   - The system frequency computed by this function is not the real
170   *           frequency in the chip. It is calculated based on the predefined
171   *           constant and the selected clock source:
172   *
173   *           - If SYSCLK source is HSI, SystemCoreClock will contain the
174   *             HSI_VALUE(*)
175   *
176   *           - If SYSCLK source is HSE, SystemCoreClock will contain the
177   *             HSE_VALUE(**)
178   *
179   *           - If SYSCLK source is CSI, SystemCoreClock will contain the
180   *             CSI_VALUE(***)
181   *
182   *           - If SYSCLK source is PLL3_P, SystemCoreClock will contain the
183   *             HSI_VALUE(*) or the HSE_VALUE(*) or the CSI_VALUE(***)
184   *             multiplied/divided by the PLL3 factors.
185   *
186   *         (*) HSI_VALUE is a constant defined in stm32mp1xx_hal_conf.h file
187   *             (default value 64 MHz) but the real value may vary depending
188   *             on the variations in voltage and temperature.
189   *
190   *         (**) HSE_VALUE is a constant defined in stm32mp1xx_hal_conf.h file
191   *              (default value 24 MHz), user has to ensure that HSE_VALUE is
192   *              same as the real frequency of the crystal used. Otherwise, this
193   *              function may have wrong result.
194   *
195   *         (***) CSI_VALUE is a constant defined in stm32mp1xx_hal_conf.h file
196   *              (default value 4 MHz)but the real value may vary depending
197   *              on the variations in voltage and temperature.
198   *
199   *         - The result of this function could be not correct when using
200   *           fractional value for HSE crystal.
201   *
202   * @param  None
203   * @retval None
204   */
SystemCoreClockUpdate(void)205 void SystemCoreClockUpdate (void)
206 {
207   uint32_t   pllsource, pll3m, pll3fracen;
208   float fracn1, pll3vco;
209 
210   switch (RCC->MSSCKSELR & RCC_MSSCKSELR_MCUSSRC)
211   {
212   case 0x00:  /* HSI used as system clock source */
213     SystemCoreClock = (HSI_VALUE >> (RCC->HSICFGR & RCC_HSICFGR_HSIDIV));
214     break;
215 
216   case 0x01:  /* HSE used as system clock source */
217     SystemCoreClock = HSE_VALUE;
218     break;
219 
220   case 0x02:  /* CSI used as system clock source */
221     SystemCoreClock = CSI_VALUE;
222     break;
223 
224   case 0x03:  /* PLL3_P used as system clock source */
225     pllsource = (RCC->RCK3SELR & RCC_RCK3SELR_PLL3SRC);
226     pll3m = ((RCC->PLL3CFGR1 & RCC_PLL3CFGR1_DIVM3) >> RCC_PLL3CFGR1_DIVM3_Pos) + 1U;
227     pll3fracen = (RCC->PLL3FRACR & RCC_PLL3FRACR_FRACLE) >> 16U;
228     fracn1 = (pll3fracen * ((RCC->PLL3FRACR & RCC_PLL3FRACR_FRACV) >> 3U));
229     pll3vco = (((RCC->PLL3CFGR1 & RCC_PLL3CFGR1_DIVN) + 1U) + (fracn1/0x1FFFU));
230 
231     if (pll3m != 0U)
232     {
233       switch (pllsource)
234       {
235         case 0x00:  /* HSI used as PLL clock source */
236           pll3vco *= ((HSI_VALUE >> (RCC->HSICFGR & RCC_HSICFGR_HSIDIV)) / pll3m);
237           break;
238 
239         case 0x01:  /* HSE used as PLL clock source */
240           pll3vco *= (HSE_VALUE / pll3m);
241           break;
242 
243         case 0x02:  /* CSI used as PLL clock source */
244           pll3vco *= (CSI_VALUE / pll3m);
245           break;
246 
247         case 0x03:  /* No clock source for PLL */
248           pll3vco = 0U;
249           break;
250        }
251       SystemCoreClock = (uint32_t)(pll3vco/((RCC->PLL3CFGR2 & RCC_PLL3CFGR2_DIVP) + 1U));
252     }
253     else
254     {
255       SystemCoreClock = 0U;
256     }
257     break;
258   }
259 
260   /* Compute mcu_ck */
261   SystemCoreClock = SystemCoreClock >> (RCC->MCUDIVR & RCC_MCUDIVR_MCUDIV);
262 }
263 
264 
265 #ifdef DATA_IN_ExtSRAM
266 /**
267   * @brief  Setup the external memory controller.
268   *         Called in startup_stm32L4xx.s before jump to main.
269   *         This function configures the external SRAM mounted on Eval boards
270   *         This SRAM will be used as program data memory (including heap and stack).
271   * @param  None
272   * @retval None
273   */
SystemInit_ExtMemCtl(void)274 void SystemInit_ExtMemCtl(void)
275 {
276 
277 }
278 #endif /* DATA_IN_ExtSRAM */
279 
280 /**
281   * @}
282   */
283 
284 /**
285   * @}
286   */
287 
288 /**
289   * @}
290   */
291 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
292