source: trunk/firmware_v2/SES/STM32C0xx/Device/Source/system_stm32c0xx.c

Last change on this file was 17, checked in by f.jahn, 4 months ago
File size: 7.2 KB
Line 
1/**
2 ******************************************************************************
3 * @file system_stm32c0xx.c
4 * @author MCD Application Team
5 * @brief CMSIS Cortex-M0+ 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_stm32c0xx.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 * @attention
23 *
24 * Copyright (c) 2022 STMicroelectronics.
25 * All rights reserved.
26 *
27 * This software is licensed under terms that can be found in the LICENSE file
28 * in the root directory of this software component.
29 * If no LICENSE file comes with this software, it is provided AS-IS.
30 *
31 ******************************************************************************
32 */
33
34/** @addtogroup CMSIS
35 * @{
36 */
37
38/** @addtogroup stm32c0xx_system
39 * @{
40 */
41
42/** @addtogroup STM32C0xx_System_Private_Includes
43 * @{
44 */
45
46#include "stm32c0xx.h"
47
48#if !defined (HSE_VALUE)
49#define HSE_VALUE (48000000UL) /*!< Value of the External oscillator in Hz */
50#endif /* HSE_VALUE */
51
52#if !defined (HSI_VALUE)
53 #define HSI_VALUE (48000000UL) /*!< Value of the Internal oscillator in Hz*/
54#endif /* HSI_VALUE */
55
56#if !defined (LSI_VALUE)
57 #define LSI_VALUE (32000UL) /*!< Value of LSI in Hz*/
58#endif /* LSI_VALUE */
59
60#if !defined (LSE_VALUE)
61 #define LSE_VALUE (32768UL) /*!< Value of LSE in Hz*/
62#endif /* LSE_VALUE */
63
64/**
65 * @}
66 */
67
68/** @addtogroup STM32C0xx_System_Private_TypesDefinitions
69 * @{
70 */
71
72/**
73 * @}
74 */
75
76/** @addtogroup STM32C0xx_System_Private_Defines
77 * @{
78 */
79
80/************************* Miscellaneous Configuration ************************/
81/*!< Uncomment the following line if you need to relocate your vector Table in
82 Internal SRAM. */
83//#define VECT_TAB_SRAM
84#define VECT_TAB_OFFSET 0x0U /*!< Vector Table base offset field.
85 This value must be a multiple of 0x100. */
86/******************************************************************************/
87/**
88 * @}
89 */
90
91/** @addtogroup STM32C0xx_System_Private_Macros
92 * @{
93 */
94
95/**
96 * @}
97 */
98
99/** @addtogroup STM32C0xx_System_Private_Variables
100 * @{
101 */
102 /* The SystemCoreClock variable is updated in three ways:
103 1) by calling CMSIS function SystemCoreClockUpdate()
104 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
105 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
106 Note: If you use this function to configure the system clock; then there
107 is no need to call the 2 first functions listed above, since SystemCoreClock
108 variable is updated automatically.
109 */
110 uint32_t SystemCoreClock = 48000000UL;
111
112 const uint32_t AHBPrescTable[16UL] = {0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 1UL, 2UL, 3UL, 4UL, 6UL, 7UL, 8UL, 9UL};
113 const uint32_t APBPrescTable[8UL] = {0UL, 0UL, 0UL, 0UL, 1UL, 2UL, 3UL, 4UL};
114
115/**
116 * @}
117 */
118
119/** @addtogroup STM32C0xx_System_Private_FunctionPrototypes
120 * @{
121 */
122
123/**
124 * @}
125 */
126
127/** @addtogroup STM32C0xx_System_Private_Functions
128 * @{
129 */
130
131/**
132 * @brief Setup the microcontroller system.
133 * @param None
134 * @retval None
135 */
136void SystemInit(void)
137{
138
139 /* Configure the Vector Table location add offset address ------------------*/
140#ifdef VECT_TAB_SRAM
141 SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
142#else
143 SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
144#endif
145}
146
147/**
148 * @brief Update SystemCoreClock variable according to Clock Register Values.
149 * The SystemCoreClock variable contains the core clock (HCLK), it can
150 * be used by the user application to setup the SysTick timer or configure
151 * other parameters.
152 *
153 * @note Each time the core clock (HCLK) changes, this function must be called
154 * to update SystemCoreClock variable value. Otherwise, any configuration
155 * based on this variable will be incorrect.
156 *
157 * @note - The system frequency computed by this function is not the real
158 * frequency in the chip. It is calculated based on the predefined
159 * constant and the selected clock source:
160 *
161 * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(**) / HSI division factor
162 *
163 * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(***)
164 *
165 * - If SYSCLK source is LSI, SystemCoreClock will contain the LSI_VALUE
166 *
167 * - If SYSCLK source is LSE, SystemCoreClock will contain the LSE_VALUE
168 *
169 * (**) HSI_VALUE is a constant defined in stm32c0xx_hal_conf.h file (default value
170 * 48 MHz) but the real value may vary depending on the variations
171 * in voltage and temperature.
172 *
173 * (***) HSE_VALUE is a constant defined in stm32c0xx_hal_conf.h file (default value
174 * 48 MHz), user has to ensure that HSE_VALUE is same as the real
175 * frequency of the crystal used. Otherwise, this function may
176 * have wrong result.
177 *
178 * - The result of this function could be not correct when using fractional
179 * value for HSE crystal.
180 *
181 * @param None
182 * @retval None
183 */
184void SystemCoreClockUpdate(void)
185{
186 uint32_t tmp;
187 uint32_t hsidiv;
188
189 /* Get SYSCLK source -------------------------------------------------------*/
190 switch (RCC->CFGR & RCC_CFGR_SWS)
191 {
192 case RCC_CFGR_SWS_0: /* HSE used as system clock */
193 SystemCoreClock = HSE_VALUE;
194 break;
195
196 case (RCC_CFGR_SWS_1 | RCC_CFGR_SWS_0): /* LSI used as system clock */
197 SystemCoreClock = LSI_VALUE;
198 break;
199
200 case RCC_CFGR_SWS_2: /* LSE used as system clock */
201 SystemCoreClock = LSE_VALUE;
202 break;
203
204 case 0x00000000U: /* HSI used as system clock */
205 default: /* HSI used as system clock */
206 hsidiv = (1UL << ((READ_BIT(RCC->CR, RCC_CR_HSIDIV))>> RCC_CR_HSIDIV_Pos));
207 SystemCoreClock = (HSI_VALUE/hsidiv);
208 break;
209 }
210 /* Compute HCLK clock frequency --------------------------------------------*/
211 /* Get HCLK prescaler */
212 tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> RCC_CFGR_HPRE_Pos)];
213 /* HCLK clock frequency */
214 SystemCoreClock >>= tmp;
215}
216
217
218/**
219 * @}
220 */
221
222/**
223 * @}
224 */
225
226/**
227 * @}
228 */
Note: See TracBrowser for help on using the repository browser.