Subversion Repositories DashDisplay

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 mjames 1
/**
2
  ******************************************************************************
3
  * @file    system_stm32f1xx.c
4
  * @author  MCD Application Team
5
  * @version V4.0.1
6
  * @date    31-July-2015
7
  * @brief   CMSIS Cortex-M3 Device Peripheral Access Layer System Source File.
8
  *
9
  * 1.  This file provides two functions and one global variable to be called from
10
  *     user application:
11
  *      - SystemInit(): Setups the system clock (System clock source, PLL Multiplier
12
  *                      factors, AHB/APBx prescalers and Flash settings).
13
  *                      This function is called at startup just after reset and
14
  *                      before branch to main program. This call is made inside
15
  *                      the "startup_stm32f1xx_xx.s" file.
16
  *
17
  *      - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
18
  *                                  by the user application to setup the SysTick
19
  *                                  timer or configure other parameters.
20
  *                                    
21
  *      - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
22
  *                                 be called whenever the core clock is changed
23
  *                                 during program execution.
24
  *
25
  * 2. After each device reset the HSI (8 MHz) is used as system clock source.
26
  *    Then SystemInit() function is called, in "startup_stm32f1xx_xx.s" file, to
27
  *    configure the system clock before to branch to main program.
28
  *
29
  * 4. The default value of HSE crystal is set to 8 MHz (or 25 MHz, depending on
30
  *    the product used), refer to "HSE_VALUE".
31
  *    When HSE is used as system clock source, directly or through PLL, and you
32
  *    are using different crystal you have to adapt the HSE value to your own
33
  *    configuration.
34
  *        
35
  ******************************************************************************
36
  * @attention
37
  *
38
  * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
39
  *
40
  * Redistribution and use in source and binary forms, with or without modification,
41
  * are permitted provided that the following conditions are met:
42
  *   1. Redistributions of source code must retain the above copyright notice,
43
  *      this list of conditions and the following disclaimer.
44
  *   2. Redistributions in binary form must reproduce the above copyright notice,
45
  *      this list of conditions and the following disclaimer in the documentation
46
  *      and/or other materials provided with the distribution.
47
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
48
  *      may be used to endorse or promote products derived from this software
49
  *      without specific prior written permission.
50
  *
51
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
52
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
54
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
55
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
57
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
58
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
59
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61
  *
62
  ******************************************************************************
63
  */
64
 
65
/** @addtogroup CMSIS
66
  * @{
67
  */
68
 
69
/** @addtogroup stm32f1xx_system
70
  * @{
71
  */  
72
 
73
/** @addtogroup STM32F1xx_System_Private_Includes
74
  * @{
75
  */
76
 
77
#include "stm32f1xx.h"
78
 
79
/**
80
  * @}
81
  */
82
 
83
/** @addtogroup STM32F1xx_System_Private_TypesDefinitions
84
  * @{
85
  */
86
 
87
/**
88
  * @}
89
  */
90
 
91
/** @addtogroup STM32F1xx_System_Private_Defines
92
  * @{
93
  */
94
 
95
#if !defined  (HSE_VALUE) 
96
  #define HSE_VALUE    ((uint32_t)8000000) /*!< Default value of the External oscillator in Hz.
97
                                                This value can be provided and adapted by the user application. */
98
#endif /* HSE_VALUE */
99
 
100
#if !defined  (HSI_VALUE)
101
  #define HSI_VALUE    ((uint32_t)8000000) /*!< Default value of the Internal oscillator in Hz.
102
                                                This value can be provided and adapted by the user application. */
103
#endif /* HSI_VALUE */
104
 
105
/*!< Uncomment the following line if you need to use external SRAM  */
106
#if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG)
107
/* #define DATA_IN_ExtSRAM */
108
#endif /* STM32F100xE || STM32F101xE || STM32F101xG || STM32F103xE || STM32F103xG */
109
 
110
/*!< Uncomment the following line if you need to relocate your vector Table in
111
     Internal SRAM. */
112
/* #define VECT_TAB_SRAM */
113
#define VECT_TAB_OFFSET  0x0 /*!< Vector Table base offset field. 
114
                                  This value must be a multiple of 0x200. */
115
 
116
 
117
/**
118
  * @}
119
  */
120
 
121
/** @addtogroup STM32F1xx_System_Private_Macros
122
  * @{
123
  */
124
 
125
/**
126
  * @}
127
  */
128
 
129
/** @addtogroup STM32F1xx_System_Private_Variables
130
  * @{
131
  */
132
 
133
/*******************************************************************************
134
*  Clock Definitions
135
*******************************************************************************/
136
#if defined(STM32F100xB) ||defined(STM32F100xE)
137
  uint32_t SystemCoreClock         = 24000000;        /*!< System Clock Frequency (Core Clock) */
138
#else /*!< HSI Selected as System Clock source */
139
  uint32_t SystemCoreClock         = 72000000;        /*!< System Clock Frequency (Core Clock) */
140
#endif
141
 
142
const uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
143
/**
144
  * @}
145
  */
146
 
147
/** @addtogroup STM32F1xx_System_Private_FunctionPrototypes
148
  * @{
149
  */
150
 
151
#if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG)
152
#ifdef DATA_IN_ExtSRAM
153
  static void SystemInit_ExtMemCtl(void);
154
#endif /* DATA_IN_ExtSRAM */
155
#endif /* STM32F100xE || STM32F101xE || STM32F101xG || STM32F103xE || STM32F103xG */
156
 
157
/**
158
  * @}
159
  */
160
 
161
/** @addtogroup STM32F1xx_System_Private_Functions
162
  * @{
163
  */
164
 
165
/**
166
  * @brief  Setup the microcontroller system
167
  *         Initialize the Embedded Flash Interface, the PLL and update the
168
  *         SystemCoreClock variable.
169
  * @note   This function should be used only after reset.
170
  * @param  None
171
  * @retval None
172
  */
173
void SystemInit (void)
174
{
175
  /* Reset the RCC clock configuration to the default reset state(for debug purpose) */
176
  /* Set HSION bit */
177
  RCC->CR |= (uint32_t)0x00000001;
178
 
179
  /* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */
180
#if !defined(STM32F105xC) && !defined(STM32F107xC)
181
  RCC->CFGR &= (uint32_t)0xF8FF0000;
182
#else
183
  RCC->CFGR &= (uint32_t)0xF0FF0000;
184
#endif /* STM32F105xC */   
185
 
186
  /* Reset HSEON, CSSON and PLLON bits */
187
  RCC->CR &= (uint32_t)0xFEF6FFFF;
188
 
189
  /* Reset HSEBYP bit */
190
  RCC->CR &= (uint32_t)0xFFFBFFFF;
191
 
192
  /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */
193
  RCC->CFGR &= (uint32_t)0xFF80FFFF;
194
 
195
#if defined(STM32F105xC) || defined(STM32F107xC)
196
  /* Reset PLL2ON and PLL3ON bits */
197
  RCC->CR &= (uint32_t)0xEBFFFFFF;
198
 
199
  /* Disable all interrupts and clear pending bits  */
200
  RCC->CIR = 0x00FF0000;
201
 
202
  /* Reset CFGR2 register */
203
  RCC->CFGR2 = 0x00000000;
204
#elif defined(STM32F100xB) || defined(STM32F100xE)
205
  /* Disable all interrupts and clear pending bits  */
206
  RCC->CIR = 0x009F0000;
207
 
208
  /* Reset CFGR2 register */
209
  RCC->CFGR2 = 0x00000000;      
210
#else
211
  /* Disable all interrupts and clear pending bits  */
212
  RCC->CIR = 0x009F0000;
213
#endif /* STM32F105xC */
214
 
215
#if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG)
216
  #ifdef DATA_IN_ExtSRAM
217
    SystemInit_ExtMemCtl();
218
  #endif /* DATA_IN_ExtSRAM */
219
#endif 
220
 
221
#ifdef VECT_TAB_SRAM
222
  SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */
223
#else
224
  SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */
225
#endif 
226
}
227
 
228
/**
229
  * @brief  Update SystemCoreClock variable according to Clock Register Values.
230
  *         The SystemCoreClock variable contains the core clock (HCLK), it can
231
  *         be used by the user application to setup the SysTick timer or configure
232
  *         other parameters.
233
  *          
234
  * @note   Each time the core clock (HCLK) changes, this function must be called
235
  *         to update SystemCoreClock variable value. Otherwise, any configuration
236
  *         based on this variable will be incorrect.        
237
  *    
238
  * @note   - The system frequency computed by this function is not the real
239
  *           frequency in the chip. It is calculated based on the predefined
240
  *           constant and the selected clock source:
241
  *            
242
  *           - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
243
  *                                              
244
  *           - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
245
  *                          
246
  *           - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
247
  *             or HSI_VALUE(*) multiplied by the PLL factors.
248
  *        
249
  *         (*) HSI_VALUE is a constant defined in stm32f1xx.h file (default value
250
  *             8 MHz) but the real value may vary depending on the variations
251
  *             in voltage and temperature.  
252
  *    
253
  *         (**) HSE_VALUE is a constant defined in stm32f1xx.h file (default value
254
  *              8 MHz or 25 MHz, depending on the product used), user has to ensure
255
  *              that HSE_VALUE is same as the real frequency of the crystal used.
256
  *              Otherwise, this function may have wrong result.
257
  *                
258
  *         - The result of this function could be not correct when using fractional
259
  *           value for HSE crystal.
260
  * @param  None
261
  * @retval None
262
  */
263
void SystemCoreClockUpdate (void)
264
{
265
  uint32_t tmp = 0, pllmull = 0, pllsource = 0;
266
 
267
#if defined(STM32F105xC) || defined(STM32F107xC)
268
  uint32_t prediv1source = 0, prediv1factor = 0, prediv2factor = 0, pll2mull = 0;
269
#endif /* STM32F105xC */
270
 
271
#if defined(STM32F100xB) || defined(STM32F100xE)
272
  uint32_t prediv1factor = 0;
273
#endif /* STM32F100xB or STM32F100xE */
274
 
275
  /* Get SYSCLK source -------------------------------------------------------*/
276
  tmp = RCC->CFGR & RCC_CFGR_SWS;
277
 
278
  switch (tmp)
279
  {
280
    case 0x00:  /* HSI used as system clock */
281
      SystemCoreClock = HSI_VALUE;
282
      break;
283
    case 0x04:  /* HSE used as system clock */
284
      SystemCoreClock = HSE_VALUE;
285
      break;
286
    case 0x08:  /* PLL used as system clock */
287
 
288
      /* Get PLL clock source and multiplication factor ----------------------*/
289
      pllmull = RCC->CFGR & RCC_CFGR_PLLMULL;
290
      pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
291
 
292
#if !defined(STM32F105xC) && !defined(STM32F107xC)      
293
      pllmull = ( pllmull >> 18) + 2;
294
 
295
      if (pllsource == 0x00)
296
      {
297
        /* HSI oscillator clock divided by 2 selected as PLL clock entry */
298
        SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
299
      }
300
      else
301
      {
302
 #if defined(STM32F100xB) || defined(STM32F100xE)
303
       prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1;
304
       /* HSE oscillator clock selected as PREDIV1 clock entry */
305
       SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;
306
 #else
307
        /* HSE selected as PLL clock entry */
308
        if ((RCC->CFGR & RCC_CFGR_PLLXTPRE) != (uint32_t)RESET)
309
        {/* HSE oscillator clock divided by 2 */
310
          SystemCoreClock = (HSE_VALUE >> 1) * pllmull;
311
        }
312
        else
313
        {
314
          SystemCoreClock = HSE_VALUE * pllmull;
315
        }
316
 #endif
317
      }
318
#else
319
      pllmull = pllmull >> 18;
320
 
321
      if (pllmull != 0x0D)
322
      {
323
         pllmull += 2;
324
      }
325
      else
326
      { /* PLL multiplication factor = PLL input clock * 6.5 */
327
        pllmull = 13 / 2;
328
      }
329
 
330
      if (pllsource == 0x00)
331
      {
332
        /* HSI oscillator clock divided by 2 selected as PLL clock entry */
333
        SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
334
      }
335
      else
336
      {/* PREDIV1 selected as PLL clock entry */
337
 
338
        /* Get PREDIV1 clock source and division factor */
339
        prediv1source = RCC->CFGR2 & RCC_CFGR2_PREDIV1SRC;
340
        prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1;
341
 
342
        if (prediv1source == 0)
343
        {
344
          /* HSE oscillator clock selected as PREDIV1 clock entry */
345
          SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;          
346
        }
347
        else
348
        {/* PLL2 clock selected as PREDIV1 clock entry */
349
 
350
          /* Get PREDIV2 division factor and PLL2 multiplication factor */
351
          prediv2factor = ((RCC->CFGR2 & RCC_CFGR2_PREDIV2) >> 4) + 1;
352
          pll2mull = ((RCC->CFGR2 & RCC_CFGR2_PLL2MUL) >> 8 ) + 2;
353
          SystemCoreClock = (((HSE_VALUE / prediv2factor) * pll2mull) / prediv1factor) * pllmull;                        
354
        }
355
      }
356
#endif /* STM32F105xC */ 
357
      break;
358
 
359
    default:
360
      SystemCoreClock = HSI_VALUE;
361
      break;
362
  }
363
 
364
  /* Compute HCLK clock frequency ----------------*/
365
  /* Get HCLK prescaler */
366
  tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
367
  /* HCLK clock frequency */
368
  SystemCoreClock >>= tmp;  
369
}
370
 
371
#if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG)
372
/**
373
  * @brief  Setup the external memory controller. Called in startup_stm32f1xx.s
374
  *          before jump to __main
375
  * @param  None
376
  * @retval None
377
  */
378
#ifdef DATA_IN_ExtSRAM
379
/**
380
  * @brief  Setup the external memory controller.
381
  *         Called in startup_stm32f1xx_xx.s/.c before jump to main.
382
  *         This function configures the external SRAM mounted on STM3210E-EVAL
383
  *         board (STM32 High density devices). This SRAM will be used as program
384
  *         data memory (including heap and stack).
385
  * @param  None
386
  * @retval None
387
  */
388
void SystemInit_ExtMemCtl(void)
389
{
390
  __IO uint32_t tmpreg;
391
  /*!< FSMC Bank1 NOR/SRAM3 is used for the STM3210E-EVAL, if another Bank is
392
    required, then adjust the Register Addresses */
393
 
394
  /* Enable FSMC clock */
395
  RCC->AHBENR = 0x00000114;
396
 
397
  /* Delay after an RCC peripheral clock enabling */
398
  tmpreg = READ_BIT(RCC->AHBENR, RCC_AHBENR_FSMCEN);
399
 
400
  /* Enable GPIOD, GPIOE, GPIOF and GPIOG clocks */
401
  RCC->APB2ENR = 0x000001E0;
402
 
403
  /* Delay after an RCC peripheral clock enabling */
404
  tmpreg = READ_BIT(RCC->APB2ENR, RCC_APB2ENR_IOPDEN);
405
 
406
  (void)(tmpreg);
407
 
408
/* ---------------  SRAM Data lines, NOE and NWE configuration ---------------*/
409
/*----------------  SRAM Address lines configuration -------------------------*/
410
/*----------------  NOE and NWE configuration --------------------------------*/  
411
/*----------------  NE3 configuration ----------------------------------------*/
412
/*----------------  NBL0, NBL1 configuration ---------------------------------*/
413
 
414
  GPIOD->CRL = 0x44BB44BB;  
415
  GPIOD->CRH = 0xBBBBBBBB;
416
 
417
  GPIOE->CRL = 0xB44444BB;  
418
  GPIOE->CRH = 0xBBBBBBBB;
419
 
420
  GPIOF->CRL = 0x44BBBBBB;  
421
  GPIOF->CRH = 0xBBBB4444;
422
 
423
  GPIOG->CRL = 0x44BBBBBB;  
424
  GPIOG->CRH = 0x444B4B44;
425
 
426
/*----------------  FSMC Configuration ---------------------------------------*/  
427
/*----------------  Enable FSMC Bank1_SRAM Bank ------------------------------*/
428
 
429
  FSMC_Bank1->BTCR[4] = 0x00001091;
430
  FSMC_Bank1->BTCR[5] = 0x00110212;
431
}
432
#endif /* DATA_IN_ExtSRAM */
433
#endif /* STM32F100xE || STM32F101xE || STM32F101xG || STM32F103xE || STM32F103xG */
434
 
435
/**
436
  * @}
437
  */
438
 
439
/**
440
  * @}
441
  */
442
 
443
/**
444
  * @}
445
  */    
446
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/