Subversion Repositories FuelGauge

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 mjames 1
/**
2
  ******************************************************************************
3
  * @file    system_stm32f0xx.c
4
  * @author  MCD Application Team
5
  * @brief   CMSIS Cortex-M0 Device Peripheral Access Layer System Source File.
6
  *
7
  * 1. 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_stm32f0xx.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
  * 2. After each device reset the HSI (8 MHz) is used as system clock source.
22
  *    Then SystemInit() function is called, in "startup_stm32f0xx.s" file, to
23
  *    configure the system clock before to branch to main program.
24
  *
25
  * 3. This file configures the system clock as follows:
26
  *=============================================================================
27
  *                         Supported STM32F0xx device
28
  *-----------------------------------------------------------------------------
29
  *        System Clock source                    | HSI
30
  *-----------------------------------------------------------------------------
31
  *        SYSCLK(Hz)                             | 8000000
32
  *-----------------------------------------------------------------------------
33
  *        HCLK(Hz)                               | 8000000
34
  *-----------------------------------------------------------------------------
35
  *        AHB Prescaler                          | 1
36
  *-----------------------------------------------------------------------------
37
  *        APB1 Prescaler                         | 1
38
  *-----------------------------------------------------------------------------
39
  *=============================================================================
40
  ******************************************************************************
41
  * @attention
42
  *
43
  * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
44
  * All rights reserved.</center></h2>
45
  *
46
  * This software component is licensed by ST under BSD 3-Clause license,
47
  * the "License"; You may not use this file except in compliance with the
48
  * License. You may obtain a copy of the License at:
49
  *                        opensource.org/licenses/BSD-3-Clause
50
  *
51
  ******************************************************************************
52
  */
53
 
54
/** @addtogroup CMSIS
55
  * @{
56
  */
57
 
58
/** @addtogroup stm32f0xx_system
59
  * @{
60
  */
61
 
62
/** @addtogroup STM32F0xx_System_Private_Includes
63
  * @{
64
  */
65
 
66
#include "stm32f0xx.h"
67
 
68
/**
69
  * @}
70
  */
71
 
72
/** @addtogroup STM32F0xx_System_Private_TypesDefinitions
73
  * @{
74
  */
75
 
76
/**
77
  * @}
78
  */
79
 
80
/** @addtogroup STM32F0xx_System_Private_Defines
81
  * @{
82
  */
83
#if !defined  (HSE_VALUE) 
84
  #define HSE_VALUE    ((uint32_t)8000000) /*!< Default value of the External oscillator in Hz.
85
                                                This value can be provided and adapted by the user application. */
86
#endif /* HSE_VALUE */
87
 
88
#if !defined  (HSI_VALUE)
89
  #define HSI_VALUE    ((uint32_t)8000000) /*!< Default value of the Internal oscillator in Hz.
90
                                                This value can be provided and adapted by the user application. */
91
#endif /* HSI_VALUE */
92
 
93
#if !defined (HSI48_VALUE)
94
#define HSI48_VALUE    ((uint32_t)48000000) /*!< Default value of the HSI48 Internal oscillator in Hz.
95
                                                 This value can be provided and adapted by the user application. */
96
#endif /* HSI48_VALUE */
97
/**
98
  * @}
99
  */
100
 
101
/** @addtogroup STM32F0xx_System_Private_Macros
102
  * @{
103
  */
104
 
105
/**
106
  * @}
107
  */
108
 
109
/** @addtogroup STM32F0xx_System_Private_Variables
110
  * @{
111
  */
112
  /* This variable is updated in three ways:
113
      1) by calling CMSIS function SystemCoreClockUpdate()
114
      2) by calling HAL API function HAL_RCC_GetHCLKFreq()
115
      3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
116
         Note: If you use this function to configure the system clock there is no need to
117
               call the 2 first functions listed above, since SystemCoreClock variable is
118
               updated automatically.
119
  */
120
uint32_t SystemCoreClock = 8000000;
121
 
122
const uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
123
const uint8_t APBPrescTable[8]  = {0, 0, 0, 0, 1, 2, 3, 4};
124
 
125
/**
126
  * @}
127
  */
128
 
129
/** @addtogroup STM32F0xx_System_Private_FunctionPrototypes
130
  * @{
131
  */
132
 
133
/**
134
  * @}
135
  */
136
 
137
/** @addtogroup STM32F0xx_System_Private_Functions
138
  * @{
139
  */
140
 
141
/**
142
  * @brief  Setup the microcontroller system.
143
  * @param  None
144
  * @retval None
145
  */
146
void SystemInit(void)
147
{
148
  /* NOTE :SystemInit(): This function is called at startup just after reset and
149
                         before branch to main program. This call is made inside
150
                         the "startup_stm32f0xx.s" file.
151
                         User can setups the default system clock (System clock source, PLL Multiplier
152
                         and Divider factors, AHB/APBx prescalers and Flash settings).
153
   */
154
}
155
 
156
/**
157
   * @brief  Update SystemCoreClock variable according to Clock Register Values.
158
  *         The SystemCoreClock variable contains the core clock (HCLK), it can
159
  *         be used by the user application to setup the SysTick timer or configure
160
  *         other parameters.
161
  *
162
  * @note   Each time the core clock (HCLK) changes, this function must be called
163
  *         to update SystemCoreClock variable value. Otherwise, any configuration
164
  *         based on this variable will be incorrect.
165
  *
166
  * @note   - The system frequency computed by this function is not the real
167
  *           frequency in the chip. It is calculated based on the predefined
168
  *           constant and the selected clock source:
169
  *
170
  *           - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
171
  *
172
  *           - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
173
  *
174
  *           - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
175
  *             or HSI_VALUE(*) multiplied/divided by the PLL factors.
176
  *
177
  *         (*) HSI_VALUE is a constant defined in stm32f0xx_hal.h file (default value
178
  *             8 MHz) but the real value may vary depending on the variations
179
  *             in voltage and temperature.
180
  *
181
  *         (**) HSE_VALUE is a constant defined in stm32f0xx_hal.h file (default value
182
  *              8 MHz), user has to ensure that HSE_VALUE is same as the real
183
  *              frequency of the crystal used. Otherwise, this function may
184
  *              have wrong result.
185
  *
186
  *         - The result of this function could be not correct when using fractional
187
  *           value for HSE crystal.
188
  *
189
  * @param  None
190
  * @retval None
191
  */
192
void SystemCoreClockUpdate (void)
193
{
194
  uint32_t tmp = 0, pllmull = 0, pllsource = 0, predivfactor = 0;
195
 
196
  /* Get SYSCLK source -------------------------------------------------------*/
197
  tmp = RCC->CFGR & RCC_CFGR_SWS;
198
 
199
  switch (tmp)
200
  {
201
    case RCC_CFGR_SWS_HSI:  /* HSI used as system clock */
202
      SystemCoreClock = HSI_VALUE;
203
      break;
204
    case RCC_CFGR_SWS_HSE:  /* HSE used as system clock */
205
      SystemCoreClock = HSE_VALUE;
206
      break;
207
    case RCC_CFGR_SWS_PLL:  /* PLL used as system clock */
208
      /* Get PLL clock source and multiplication factor ----------------------*/
209
      pllmull = RCC->CFGR & RCC_CFGR_PLLMUL;
210
      pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
211
      pllmull = ( pllmull >> 18) + 2;
212
      predivfactor = (RCC->CFGR2 & RCC_CFGR2_PREDIV) + 1;
213
 
214
      if (pllsource == RCC_CFGR_PLLSRC_HSE_PREDIV)
215
      {
216
        /* HSE used as PLL clock source : SystemCoreClock = HSE/PREDIV * PLLMUL */
217
        SystemCoreClock = (HSE_VALUE/predivfactor) * pllmull;
218
      }
219
#if defined(STM32F042x6) || defined(STM32F048xx) || defined(STM32F072xB) || defined(STM32F078xx) || defined(STM32F091xC) || defined(STM32F098xx)
220
      else if (pllsource == RCC_CFGR_PLLSRC_HSI48_PREDIV)
221
      {
222
        /* HSI48 used as PLL clock source : SystemCoreClock = HSI48/PREDIV * PLLMUL */
223
        SystemCoreClock = (HSI48_VALUE/predivfactor) * pllmull;
224
      }
225
#endif /* STM32F042x6 || STM32F048xx || STM32F072xB || STM32F078xx || STM32F091xC || STM32F098xx */
226
      else
227
      {
228
#if defined(STM32F042x6) || defined(STM32F048xx)  || defined(STM32F070x6) \
229
 || defined(STM32F078xx) || defined(STM32F071xB)  || defined(STM32F072xB) \
230
 || defined(STM32F070xB) || defined(STM32F091xC) || defined(STM32F098xx)  || defined(STM32F030xC)
231
        /* HSI used as PLL clock source : SystemCoreClock = HSI/PREDIV * PLLMUL */
232
        SystemCoreClock = (HSI_VALUE/predivfactor) * pllmull;
233
#else
234
        /* HSI used as PLL clock source : SystemCoreClock = HSI/2 * PLLMUL */
235
        SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
236
#endif /* STM32F042x6 || STM32F048xx || STM32F070x6 || 
237
          STM32F071xB || STM32F072xB || STM32F078xx || STM32F070xB ||
238
          STM32F091xC || STM32F098xx || STM32F030xC */
239
      }
240
      break;
241
    default: /* HSI used as system clock */
242
      SystemCoreClock = HSI_VALUE;
243
      break;
244
  }
245
  /* Compute HCLK clock frequency ----------------*/
246
  /* Get HCLK prescaler */
247
  tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
248
  /* HCLK clock frequency */
249
  SystemCoreClock >>= tmp;
250
}
251
 
252
/**
253
  * @}
254
  */
255
 
256
/**
257
  * @}
258
  */
259
 
260
/**
261
  * @}
262
  */
263
 
264
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
265