Skip to content

Commit 6521c41

Browse files
committed
[L5] Add STM32L5xx system source files
CMSIS Cortex-M33 Device Peripheral Access Layer System Source File to be used in non-secure application when the system implements the TrustZone-M security. Signed-off-by: Frederic Pillon <[email protected]>
1 parent f2f824d commit 6521c41

File tree

2 files changed

+291
-0
lines changed

2 files changed

+291
-0
lines changed

libraries/SrcWrapper/src/stm32/system_stm32yyxx.c

+3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
#ifdef STM32L4xx
3737
#include "system_stm32l4xx.c"
3838
#endif
39+
#ifdef STM32L5xx
40+
#include "system_stm32l5xx_ns.c"
41+
#endif
3942
#ifdef STM32MP1xx
4043
#include "system_stm32mp1xx.c"
4144
#endif
+288
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,288 @@
1+
/**
2+
******************************************************************************
3+
* @file system_stm32l5xx_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_stm32l5xx.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_stm32l5xx.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_stm32l5xx.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+
* <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
34+
* All rights reserved.</center></h2>
35+
*
36+
* This software component is licensed by ST under Apache License, Version 2.0,
37+
* the "License"; You may not use this file except in compliance with the
38+
* License. You may obtain a copy of the License at:
39+
* opensource.org/licenses/Apache-2.0
40+
*
41+
******************************************************************************
42+
*/
43+
44+
/** @addtogroup CMSIS
45+
* @{
46+
*/
47+
48+
/** @addtogroup STM32L5xx_System
49+
* @{
50+
*/
51+
52+
/** @addtogroup STM32L5xx_System_Private_Includes
53+
* @{
54+
*/
55+
56+
#include "stm32l5xx.h"
57+
58+
/**
59+
* @}
60+
*/
61+
62+
/** @addtogroup STM32L5xx_System_Private_TypesDefinitions
63+
* @{
64+
*/
65+
66+
/**
67+
* @}
68+
*/
69+
70+
/** @addtogroup STM32L5xx_System_Private_Defines
71+
* @{
72+
*/
73+
#ifndef VECT_TAB_OFFSET
74+
#define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field.
75+
This value must be a multiple of 0x200. */
76+
#else
77+
define USER_VECT_TAB_ADDRESS
78+
#endif
79+
80+
/* Note: Following vector table addresses must be defined in line with linker
81+
configuration. */
82+
/*!< Uncomment the following line if you need to relocate the vector table
83+
anywhere in Flash or Sram, else the vector table is kept at the automatic
84+
remap of boot address selected */
85+
/* #define USER_VECT_TAB_ADDRESS */
86+
87+
#if defined(USER_VECT_TAB_ADDRESS)
88+
/*!< Uncomment the following line if you need to relocate your vector Table
89+
in Sram else user remap will be done in Flash. */
90+
/* #define VECT_TAB_SRAM */
91+
92+
#if defined(VECT_TAB_SRAM)
93+
#define VECT_TAB_BASE_ADDRESS SRAM1_BASE_NS /*!< Vector Table base address field.
94+
This value must be a multiple of 0x200. */
95+
#else
96+
#define VECT_TAB_BASE_ADDRESS FLASH_BASE_NS /*!< Vector Table base address field.
97+
This value must be a multiple of 0x200. */
98+
#endif /* VECT_TAB_SRAM */
99+
#endif /* USER_VECT_TAB_ADDRESS */
100+
101+
/******************************************************************************/
102+
/**
103+
* @}
104+
*/
105+
106+
/** @addtogroup STM32L5xx_System_Private_Macros
107+
* @{
108+
*/
109+
110+
/**
111+
* @}
112+
*/
113+
114+
/** @addtogroup STM32L5xx_System_Private_Variables
115+
* @{
116+
*/
117+
/* The SystemCoreClock variable is updated in three ways:
118+
1) by calling CMSIS function SystemCoreClockUpdate()
119+
2) by calling HAL API function HAL_RCC_GetHCLKFreq()
120+
3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
121+
Note: If you use this function to configure the system clock; then there
122+
is no need to call the 2 first functions listed above, since SystemCoreClock
123+
variable is updated automatically.
124+
*/
125+
uint32_t SystemCoreClock = 4000000U;
126+
127+
const uint8_t AHBPrescTable[16] = {0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U, 6U, 7U, 8U, 9U};
128+
const uint8_t APBPrescTable[8] = {0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U};
129+
const uint32_t MSIRangeTable[16] = {100000U, 200000U, 400000U, 800000U, 1000000U, 2000000U, \
130+
4000000U, 8000000U, 16000000U, 24000000U, 32000000U, 48000000U, \
131+
0U, 0U, 0U, 0U}; /* MISRAC-2012: 0U for unexpected value */
132+
/**
133+
* @}
134+
*/
135+
136+
/** @addtogroup STM32L5xx_System_Private_FunctionPrototypes
137+
* @{
138+
*/
139+
140+
/**
141+
* @}
142+
*/
143+
144+
/** @addtogroup STM32L5xx_System_Private_Functions
145+
* @{
146+
*/
147+
148+
/**
149+
* @brief Setup the microcontroller system.
150+
* @retval None
151+
*/
152+
153+
void SystemInit(void)
154+
{
155+
/* Configure the Vector Table location -------------------------------------*/
156+
#if defined(USER_VECT_TAB_ADDRESS)
157+
SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET;
158+
#endif
159+
160+
/* FPU settings ------------------------------------------------------------*/
161+
#if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
162+
SCB->CPACR |= ((3UL << 20U)|(3UL << 22U)); /* set CP10 and CP11 Full Access */
163+
#endif
164+
}
165+
166+
/**
167+
* @brief Update SystemCoreClock variable according to Clock Register Values.
168+
* The SystemCoreClock variable contains the core clock (HCLK), it can
169+
* be used by the user application to setup the SysTick timer or configure
170+
* other parameters.
171+
*
172+
* @note Each time the core clock (HCLK) changes, this function must be called
173+
* to update SystemCoreClock variable value. Otherwise, any configuration
174+
* based on this variable will be incorrect.
175+
*
176+
* @note - The system frequency computed by this function is not the real
177+
* frequency in the chip. It is calculated based on the predefined
178+
* constant and the selected clock source:
179+
*
180+
* - If SYSCLK source is MSI, SystemCoreClock will contain the MSI_VALUE(*)
181+
*
182+
* - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(**)
183+
*
184+
* - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(***)
185+
*
186+
* - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(***)
187+
* or HSI_VALUE(*) or MSI_VALUE(*) multiplied/divided by the PLL factors.
188+
*
189+
* (*) MSI_VALUE is a constant defined in stm32l5xx_hal.h file (default value
190+
* 4 MHz) but the real value may vary depending on the variations
191+
* in voltage and temperature.
192+
*
193+
* (**) HSI_VALUE is a constant defined in stm32l5xx_hal.h file (default value
194+
* 16 MHz) but the real value may vary depending on the variations
195+
* in voltage and temperature.
196+
*
197+
* (***) HSE_VALUE is a constant defined in stm32l5xx_hal.h file (default value
198+
* 8 MHz), user has to ensure that HSE_VALUE is same as the real
199+
* frequency of the crystal used. Otherwise, this function may
200+
* have wrong result.
201+
*
202+
* - The result of this function could be not correct when using fractional
203+
* value for HSE crystal.
204+
*
205+
* @retval None
206+
*/
207+
void SystemCoreClockUpdate(void)
208+
{
209+
uint32_t tmp, msirange, pllvco, pllsource, pllm, pllr;
210+
211+
/* Get MSI Range frequency--------------------------------------------------*/
212+
if((RCC->CR & RCC_CR_MSIRGSEL) == 0U)
213+
{ /* MSISRANGE from RCC_CSR applies */
214+
msirange = (RCC->CSR & RCC_CSR_MSISRANGE) >> 8U;
215+
}
216+
else
217+
{ /* MSIRANGE from RCC_CR applies */
218+
msirange = (RCC->CR & RCC_CR_MSIRANGE) >> 4U;
219+
}
220+
/*MSI frequency range in HZ*/
221+
msirange = MSIRangeTable[msirange];
222+
223+
/* Get SYSCLK source -------------------------------------------------------*/
224+
switch (RCC->CFGR & RCC_CFGR_SWS)
225+
{
226+
case 0x00: /* MSI used as system clock source */
227+
SystemCoreClock = msirange;
228+
break;
229+
230+
case 0x04: /* HSI used as system clock source */
231+
SystemCoreClock = HSI_VALUE;
232+
break;
233+
234+
case 0x08: /* HSE used as system clock source */
235+
SystemCoreClock = HSE_VALUE;
236+
break;
237+
238+
case 0x0C: /* PLL used as system clock source */
239+
/* PLL_VCO = (HSE_VALUE or HSI_VALUE or MSI_VALUE/ PLLM) * PLLN
240+
SYSCLK = PLL_VCO / PLLR
241+
*/
242+
pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC);
243+
pllm = ((RCC->PLLCFGR & RCC_PLLCFGR_PLLM) >> 4U) + 1U ;
244+
245+
switch (pllsource)
246+
{
247+
case 0x02: /* HSI used as PLL clock source */
248+
pllvco = (HSI_VALUE / pllm);
249+
break;
250+
251+
case 0x03: /* HSE used as PLL clock source */
252+
pllvco = (HSE_VALUE / pllm);
253+
break;
254+
255+
default: /* MSI used as PLL clock source */
256+
pllvco = (msirange / pllm);
257+
break;
258+
}
259+
pllvco = pllvco * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 8U);
260+
pllr = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> 25U) + 1U) * 2U;
261+
SystemCoreClock = pllvco/pllr;
262+
break;
263+
264+
default:
265+
SystemCoreClock = msirange;
266+
break;
267+
}
268+
/* Compute HCLK clock frequency --------------------------------------------*/
269+
/* Get HCLK prescaler */
270+
tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4U)];
271+
/* HCLK clock frequency */
272+
SystemCoreClock >>= tmp;
273+
}
274+
275+
276+
/**
277+
* @}
278+
*/
279+
280+
/**
281+
* @}
282+
*/
283+
284+
/**
285+
* @}
286+
*/
287+
288+
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

0 commit comments

Comments
 (0)