1 /**
2   ******************************************************************************
3   * @file    system_stm32u5xx_ns.c
4   * @author  MCD Application Team
5   * @brief   CMSIS Cortex-M33 Device Peripheral Access Layer System Source File
6   *          to be used in non-secure application when the system implements
7   *          the TrustZone-M security.
8   *
9   *   This file provides two functions and one global variable to be called from
10   *   user application:
11   *      - SystemInit(): This function is called at non-secure startup before
12   *                      branch to non-secure main program.
13   *                      This call is made inside the "startup_stm32u5xx.s" file.
14   *
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.
18   *
19   *      - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
20   *                                 be called whenever the core clock is changed
21   *                                 during program execution.
22   *
23   *   After each device reset the MSI (4 MHz) is used as system clock source.
24   *   Then SystemInit() function is called, in "startup_stm32u5xx.s" file, to
25   *   configure the system clock before to branch to main secure program.
26   *   Later, when non-secure SystemInit() function is called, in "startup_stm32u5xx.s"
27   *   file, the system clock may have been updated from reset value by the main
28   *   secure program.
29   *
30   ******************************************************************************
31   * @attention
32   *
33   * Copyright (c) 2021 STMicroelectronics.
34   * All rights reserved.
35   *
36   * This software is licensed under terms that can be found in the LICENSE file
37   * in the root directory of this software component.
38   * If no LICENSE file comes with this software, it is provided AS-IS.
39   *
40   ******************************************************************************
41   */
42 
43 /** @addtogroup CMSIS
44   * @{
45   */
46 
47 /** @addtogroup STM32U5xx_system
48   * @{
49   */
50 
51 /** @addtogroup STM32U5xx_System_Private_Includes
52   * @{
53   */
54 
55 #include "stm32u5xx.h"
56 
57 
58 /**
59   * @}
60   */
61 
62 /** @addtogroup STM32U5xx_System_Private_TypesDefinitions
63   * @{
64   */
65 
66 /**
67   * @}
68   */
69 
70 /** @addtogroup STM32U5xx_System_Private_Defines
71   * @{
72   */
73 
74 #if !defined  (HSE_VALUE)
75   #define HSE_VALUE    16000000U /*!< Value of the External oscillator in Hz */
76 #endif /* HSE_VALUE */
77 
78 #if !defined  (MSI_VALUE)
79   #define MSI_VALUE    4000000U /*!< Value of the Internal oscillator in Hz*/
80 #endif /* MSI_VALUE */
81 
82 #if !defined  (HSI_VALUE)
83   #define HSI_VALUE    16000000U /*!< Value of the Internal oscillator in Hz*/
84 #endif /* HSI_VALUE */
85 
86 /**
87   * @}
88   */
89 
90 /** @addtogroup STM32U5xx_System_Private_Macros
91   * @{
92   */
93 
94 /**
95   * @}
96   */
97 
98 /** @addtogroup STM32U5xx_System_Private_Variables
99   * @{
100   */
101   /* The SystemCoreClock variable is updated in three ways:
102       1) by calling CMSIS function SystemCoreClockUpdate()
103       2) by calling HAL API function HAL_RCC_GetHCLKFreq()
104       3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
105          Note: If you use this function to configure the system clock; then there
106                is no need to call the 2 first functions listed above, since SystemCoreClock
107                variable is updated automatically.
108   */
109   uint32_t SystemCoreClock = 4000000U;
110 
111   const uint8_t  AHBPrescTable[16] = {0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U, 6U, 7U, 8U, 9U};
112   const uint8_t  APBPrescTable[8] =  {0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U};
113   const uint32_t MSIRangeTable[16] = {48000000U,24000000U,16000000U,12000000U, 4000000U, 2000000U, 1330000U,\
114                                       1000000U, 3072000U, 1536000U,1024000U, 768000U, 400000U, 200000U, 133000U, 100000U};
115 /**
116   * @}
117   */
118 
119 /** @addtogroup STM32U5xx_System_Private_FunctionPrototypes
120   * @{
121   */
122 
123 /**
124   * @}
125   */
126 
127 /** @addtogroup STM32U5xx_System_Private_Functions
128   * @{
129   */
130 
131 /**
132   * @brief  Setup the microcontroller system.
133   * @retval None
134   */
135 
SystemInit(void)136 void SystemInit(void)
137 {
138   /* Nothing done in non-secure */
139 
140   /* Non-secure main application shall call SystemCoreClockUpdate() to update */
141   /* the SystemCoreClock variable to insure non-secure application relies on  */
142   /* the initial clock reference set by secure application.                   */
143 }
144 
145 /**
146   * @brief  Update SystemCoreClock variable according to Clock Register Values.
147   *         The SystemCoreClock variable contains the core clock (HCLK), it can
148   *         be used by the user application to setup the SysTick timer or configure
149   *         other parameters.
150   *
151   * @note   From the non-secure application, the SystemCoreClock value is
152   *         retrieved from the secure domain via a Non-Secure Callable function
153   *         since the RCC peripheral may be protected with security attributes
154   *         that prevent to compute the SystemCoreClock variable from the RCC
155   *         peripheral registers.
156   *
157   * @note   Each time the core clock (HCLK) changes, this function must be called
158   *         to update SystemCoreClock variable value. Otherwise, any configuration
159   *         based on this variable will be incorrect.
160   *
161   * @note   - The system frequency computed by this function is not the real
162   *           frequency in the chip. It is calculated based on the predefined
163   *           constant and the selected clock source:
164   *
165   *           - If SYSCLK source is MSI, SystemCoreClock will contain the MSI_VALUE(*)
166   *
167   *           - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(**)
168   *
169   *           - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(***)
170   *
171   *           - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(***)
172   *             or HSI_VALUE(*) or MSI_VALUE(*) multiplied/divided by the PLL factors.
173   *
174   *         (*) MSI_VALUE is a constant defined in stm32u5xx_hal.h file (default value
175   *             4 MHz) but the real value may vary depending on the variations
176   *             in voltage and temperature.
177   *
178   *         (**) HSI_VALUE is a constant defined in stm32u5xx_hal.h file (default value
179   *              16 MHz) but the real value may vary depending on the variations
180   *              in voltage and temperature.
181   *
182   *         (***) HSE_VALUE is a constant defined in stm32u5xx_hal.h file (default value
183   *              8 MHz), user has to ensure that HSE_VALUE is same as the real
184   *              frequency of the crystal used. Otherwise, this function may
185   *              have wrong result.
186   *
187   *         - The result of this function could be not correct when using fractional
188   *           value for HSE crystal.
189   *
190   * @retval None
191   */
SystemCoreClockUpdate(void)192 void SystemCoreClockUpdate(void)
193 {
194   /* Get the SystemCoreClock value from the secure domain */
195   SystemCoreClock = SECURE_SystemCoreClockUpdate();
196 }
197 
198 
199 /**
200   * @}
201   */
202 
203 /**
204   * @}
205   */
206 
207 /**
208   * @}
209   */
210 
211