]> jspc29.x-matter.uni-frankfurt.de Git - mvd_firmware.git/blob
6fb4579ec804e799132b5587a10d980a573916bb
[mvd_firmware.git] /
1 /**\r
2   ******************************************************************************\r
3   * @file    system_stm32f10x.c\r
4   * @author  MCD Application Team\r
5   * @version V3.5.0\r
6   * @date    11-March-2011\r
7   * @brief   CMSIS Cortex-M3 Device Peripheral Access Layer System Source File.\r
8   * \r
9   * 1.  This file provides two functions and one global variable to be called from \r
10   *     user application:\r
11   *      - SystemInit(): Setups the system clock (System clock source, PLL Multiplier\r
12   *                      factors, AHB/APBx prescalers and Flash settings). \r
13   *                      This function is called at startup just after reset and \r
14   *                      before branch to main program. This call is made inside\r
15   *                      the "startup_stm32f10x_xx.s" file.\r
16   *\r
17   *      - SystemCoreClock variable: Contains the core clock (HCLK), it can be used\r
18   *                                  by the user application to setup the SysTick \r
19   *                                  timer or configure other parameters.\r
20   *                                     \r
21   *      - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must\r
22   *                                 be called whenever the core clock is changed\r
23   *                                 during program execution.\r
24   *\r
25   * 2. After each device reset the HSI (8 MHz) is used as system clock source.\r
26   *    Then SystemInit() function is called, in "startup_stm32f10x_xx.s" file, to\r
27   *    configure the system clock before to branch to main program.\r
28   *\r
29   * 3. If the system clock source selected by user fails to startup, the SystemInit()\r
30   *    function will do nothing and HSI still used as system clock source. User can \r
31   *    add some code to deal with this issue inside the SetSysClock() function.\r
32   *\r
33   * 4. The default value of HSE crystal is set to 8 MHz (or 25 MHz, depedning on\r
34   *    the product used), refer to "HSE_VALUE" define in "stm32f10x.h" file. \r
35   *    When HSE is used as system clock source, directly or through PLL, and you\r
36   *    are using different crystal you have to adapt the HSE value to your own\r
37   *    configuration.\r
38   *        \r
39   ******************************************************************************\r
40   * @attention\r
41   *\r
42   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS\r
43   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE\r
44   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY\r
45   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING\r
46   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE\r
47   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.\r
48   *\r
49   * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>\r
50   ******************************************************************************\r
51   */\r
52 \r
53 /** @addtogroup CMSIS\r
54   * @{\r
55   */\r
56 \r
57 /** @addtogroup stm32f10x_system\r
58   * @{\r
59   */  \r
60   \r
61 /** @addtogroup STM32F10x_System_Private_Includes\r
62   * @{\r
63   */\r
64 \r
65 #include "stm32f10x.h"\r
66 \r
67 /**\r
68   * @}\r
69   */\r
70 \r
71 /** @addtogroup STM32F10x_System_Private_TypesDefinitions\r
72   * @{\r
73   */\r
74 \r
75 /**\r
76   * @}\r
77   */\r
78 \r
79 /** @addtogroup STM32F10x_System_Private_Defines\r
80   * @{\r
81   */\r
82 \r
83 /*!< Uncomment the line corresponding to the desired System clock (SYSCLK)\r
84    frequency (after reset the HSI is used as SYSCLK source)\r
85    \r
86    IMPORTANT NOTE:\r
87    ============== \r
88    1. After each device reset the HSI is used as System clock source.\r
89 \r
90    2. Please make sure that the selected System clock doesn't exceed your device's\r
91       maximum frequency.\r
92       \r
93    3. If none of the define below is enabled, the HSI is used as System clock\r
94     source.\r
95 \r
96    4. The System clock configuration functions provided within this file assume that:\r
97         - For Low, Medium and High density Value line devices an external 8MHz \r
98           crystal is used to drive the System clock.\r
99         - For Low, Medium and High density devices an external 8MHz crystal is\r
100           used to drive the System clock.\r
101         - For Connectivity line devices an external 25MHz crystal is used to drive\r
102           the System clock.\r
103      If you are using different crystal you have to adapt those functions accordingly.\r
104     */\r
105     \r
106 #if defined (STM32F10X_LD_VL) || (defined STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)\r
107 /* #define SYSCLK_FREQ_HSE    HSE_VALUE */\r
108  #define SYSCLK_FREQ_24MHz  24000000\r
109 #else\r
110 /* #define SYSCLK_FREQ_HSE    HSE_VALUE */\r
111 /* #define SYSCLK_FREQ_24MHz  24000000 */ \r
112 /* #define SYSCLK_FREQ_36MHz  36000000 */\r
113 /* #define SYSCLK_FREQ_48MHz  48000000 */\r
114 /* #define SYSCLK_FREQ_56MHz  56000000 */\r
115 #define SYSCLK_FREQ_72MHz  72000000\r
116 #endif\r
117 \r
118 /*!< Uncomment the following line if you need to use external SRAM mounted\r
119      on STM3210E-EVAL board (STM32 High density and XL-density devices) or on \r
120      STM32100E-EVAL board (STM32 High-density value line devices) as data memory */ \r
121 #if defined (STM32F10X_HD) || (defined STM32F10X_XL) || (defined STM32F10X_HD_VL)\r
122 /* #define DATA_IN_ExtSRAM */\r
123 #endif\r
124 \r
125 /*!< Uncomment the following line if you need to relocate your vector Table in\r
126      Internal SRAM. */ \r
127 /* #define VECT_TAB_SRAM */\r
128 #define VECT_TAB_OFFSET  0x0 /*!< Vector Table base offset field. \r
129                                   This value must be a multiple of 0x200. */\r
130 \r
131 \r
132 /**\r
133   * @}\r
134   */\r
135 \r
136 /** @addtogroup STM32F10x_System_Private_Macros\r
137   * @{\r
138   */\r
139 \r
140 /**\r
141   * @}\r
142   */\r
143 \r
144 /** @addtogroup STM32F10x_System_Private_Variables\r
145   * @{\r
146   */\r
147 \r
148 /*******************************************************************************\r
149 *  Clock Definitions\r
150 *******************************************************************************/\r
151 #ifdef SYSCLK_FREQ_HSE\r
152   uint32_t SystemCoreClock         = SYSCLK_FREQ_HSE;        /*!< System Clock Frequency (Core Clock) */\r
153 #elif defined SYSCLK_FREQ_24MHz\r
154   uint32_t SystemCoreClock         = SYSCLK_FREQ_24MHz;        /*!< System Clock Frequency (Core Clock) */\r
155 #elif defined SYSCLK_FREQ_36MHz\r
156   uint32_t SystemCoreClock         = SYSCLK_FREQ_36MHz;        /*!< System Clock Frequency (Core Clock) */\r
157 #elif defined SYSCLK_FREQ_48MHz\r
158   uint32_t SystemCoreClock         = SYSCLK_FREQ_48MHz;        /*!< System Clock Frequency (Core Clock) */\r
159 #elif defined SYSCLK_FREQ_56MHz\r
160   uint32_t SystemCoreClock         = SYSCLK_FREQ_56MHz;        /*!< System Clock Frequency (Core Clock) */\r
161 #elif defined SYSCLK_FREQ_72MHz\r
162   uint32_t SystemCoreClock         = SYSCLK_FREQ_72MHz;        /*!< System Clock Frequency (Core Clock) */\r
163 #else /*!< HSI Selected as System Clock source */\r
164   uint32_t SystemCoreClock         = HSI_VALUE;        /*!< System Clock Frequency (Core Clock) */\r
165 #endif\r
166 \r
167 __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};\r
168 /**\r
169   * @}\r
170   */\r
171 \r
172 /** @addtogroup STM32F10x_System_Private_FunctionPrototypes\r
173   * @{\r
174   */\r
175 \r
176 static void SetSysClock(void);\r
177 \r
178 #ifdef SYSCLK_FREQ_HSE\r
179   static void SetSysClockToHSE(void);\r
180 #elif defined SYSCLK_FREQ_24MHz\r
181   static void SetSysClockTo24(void);\r
182 #elif defined SYSCLK_FREQ_36MHz\r
183   static void SetSysClockTo36(void);\r
184 #elif defined SYSCLK_FREQ_48MHz\r
185   static void SetSysClockTo48(void);\r
186 #elif defined SYSCLK_FREQ_56MHz\r
187   static void SetSysClockTo56(void);  \r
188 #elif defined SYSCLK_FREQ_72MHz\r
189   static void SetSysClockTo72(void);\r
190 #endif\r
191 \r
192 #ifdef DATA_IN_ExtSRAM\r
193   static void SystemInit_ExtMemCtl(void); \r
194 #endif /* DATA_IN_ExtSRAM */\r
195 \r
196 /**\r
197   * @}\r
198   */\r
199 \r
200 /** @addtogroup STM32F10x_System_Private_Functions\r
201   * @{\r
202   */\r
203 \r
204 /**\r
205   * @brief  Setup the microcontroller system\r
206   *         Initialize the Embedded Flash Interface, the PLL and update the \r
207   *         SystemCoreClock variable.\r
208   * @note   This function should be used only after reset.\r
209   * @param  None\r
210   * @retval None\r
211   */\r
212 void SystemInit (void)\r
213 {\r
214   /* Reset the RCC clock configuration to the default reset state(for debug purpose) */\r
215   /* Set HSION bit */\r
216   RCC->CR |= (uint32_t)0x00000001;\r
217 \r
218   /* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */\r
219 #ifndef STM32F10X_CL\r
220   RCC->CFGR &= (uint32_t)0xF8FF0000;\r
221 #else\r
222   RCC->CFGR &= (uint32_t)0xF0FF0000;\r
223 #endif /* STM32F10X_CL */   \r
224   \r
225   /* Reset HSEON, CSSON and PLLON bits */\r
226   RCC->CR &= (uint32_t)0xFEF6FFFF;\r
227 \r
228   /* Reset HSEBYP bit */\r
229   RCC->CR &= (uint32_t)0xFFFBFFFF;\r
230 \r
231   /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */\r
232   RCC->CFGR &= (uint32_t)0xFF80FFFF;\r
233 \r
234 #ifdef STM32F10X_CL\r
235   /* Reset PLL2ON and PLL3ON bits */\r
236   RCC->CR &= (uint32_t)0xEBFFFFFF;\r
237 \r
238   /* Disable all interrupts and clear pending bits  */\r
239   RCC->CIR = 0x00FF0000;\r
240 \r
241   /* Reset CFGR2 register */\r
242   RCC->CFGR2 = 0x00000000;\r
243 #elif defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)\r
244   /* Disable all interrupts and clear pending bits  */\r
245   RCC->CIR = 0x009F0000;\r
246 \r
247   /* Reset CFGR2 register */\r
248   RCC->CFGR2 = 0x00000000;      \r
249 #else\r
250   /* Disable all interrupts and clear pending bits  */\r
251   RCC->CIR = 0x009F0000;\r
252 #endif /* STM32F10X_CL */\r
253     \r
254 #if defined (STM32F10X_HD) || (defined STM32F10X_XL) || (defined STM32F10X_HD_VL)\r
255   #ifdef DATA_IN_ExtSRAM\r
256     SystemInit_ExtMemCtl(); \r
257   #endif /* DATA_IN_ExtSRAM */\r
258 #endif \r
259 \r
260   /* Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers */\r
261   /* Configure the Flash Latency cycles and enable prefetch buffer */\r
262   SetSysClock();\r
263 \r
264 #ifdef VECT_TAB_SRAM\r
265   SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */\r
266 #else\r
267   SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */\r
268 #endif \r
269 }\r
270 \r
271 /**\r
272   * @brief  Update SystemCoreClock variable according to Clock Register Values.\r
273   *         The SystemCoreClock variable contains the core clock (HCLK), it can\r
274   *         be used by the user application to setup the SysTick timer or configure\r
275   *         other parameters.\r
276   *           \r
277   * @note   Each time the core clock (HCLK) changes, this function must be called\r
278   *         to update SystemCoreClock variable value. Otherwise, any configuration\r
279   *         based on this variable will be incorrect.         \r
280   *     \r
281   * @note   - The system frequency computed by this function is not the real \r
282   *           frequency in the chip. It is calculated based on the predefined \r
283   *           constant and the selected clock source:\r
284   *             \r
285   *           - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)\r
286   *                                              \r
287   *           - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)\r
288   *                          \r
289   *           - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**) \r
290   *             or HSI_VALUE(*) multiplied by the PLL factors.\r
291   *         \r
292   *         (*) HSI_VALUE is a constant defined in stm32f1xx.h file (default value\r
293   *             8 MHz) but the real value may vary depending on the variations\r
294   *             in voltage and temperature.   \r
295   *    \r
296   *         (**) HSE_VALUE is a constant defined in stm32f1xx.h file (default value\r
297   *              8 MHz or 25 MHz, depedning on the product used), user has to ensure\r
298   *              that HSE_VALUE is same as the real frequency of the crystal used.\r
299   *              Otherwise, this function may have wrong result.\r
300   *                \r
301   *         - The result of this function could be not correct when using fractional\r
302   *           value for HSE crystal.\r
303   * @param  None\r
304   * @retval None\r
305   */\r
306 void SystemCoreClockUpdate (void)\r
307 {\r
308   uint32_t tmp = 0, pllmull = 0, pllsource = 0;\r
309 \r
310 #ifdef  STM32F10X_CL\r
311   uint32_t prediv1source = 0, prediv1factor = 0, prediv2factor = 0, pll2mull = 0;\r
312 #endif /* STM32F10X_CL */\r
313 \r
314 #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)\r
315   uint32_t prediv1factor = 0;\r
316 #endif /* STM32F10X_LD_VL or STM32F10X_MD_VL or STM32F10X_HD_VL */\r
317     \r
318   /* Get SYSCLK source -------------------------------------------------------*/\r
319   tmp = RCC->CFGR & RCC_CFGR_SWS;\r
320   \r
321   switch (tmp)\r
322   {\r
323     case 0x00:  /* HSI used as system clock */\r
324       SystemCoreClock = HSI_VALUE;\r
325       break;\r
326     case 0x04:  /* HSE used as system clock */\r
327       SystemCoreClock = HSE_VALUE;\r
328       break;\r
329     case 0x08:  /* PLL used as system clock */\r
330 \r
331       /* Get PLL clock source and multiplication factor ----------------------*/\r
332       pllmull = RCC->CFGR & RCC_CFGR_PLLMULL;\r
333       pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;\r
334       \r
335 #ifndef STM32F10X_CL      \r
336       pllmull = ( pllmull >> 18) + 2;\r
337       \r
338       if (pllsource == 0x00)\r
339       {\r
340         /* HSI oscillator clock divided by 2 selected as PLL clock entry */\r
341         SystemCoreClock = (HSI_VALUE >> 1) * pllmull;\r
342       }\r
343       else\r
344       {\r
345  #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)\r
346        prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1;\r
347        /* HSE oscillator clock selected as PREDIV1 clock entry */\r
348        SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull; \r
349  #else\r
350         /* HSE selected as PLL clock entry */\r
351         if ((RCC->CFGR & RCC_CFGR_PLLXTPRE) != (uint32_t)RESET)\r
352         {/* HSE oscillator clock divided by 2 */\r
353           SystemCoreClock = (HSE_VALUE >> 1) * pllmull;\r
354         }\r
355         else\r
356         {\r
357           SystemCoreClock = HSE_VALUE * pllmull;\r
358         }\r
359  #endif\r
360       }\r
361 #else\r
362       pllmull = pllmull >> 18;\r
363       \r
364       if (pllmull != 0x0D)\r
365       {\r
366          pllmull += 2;\r
367       }\r
368       else\r
369       { /* PLL multiplication factor = PLL input clock * 6.5 */\r
370         pllmull = 13 / 2; \r
371       }\r
372             \r
373       if (pllsource == 0x00)\r
374       {\r
375         /* HSI oscillator clock divided by 2 selected as PLL clock entry */\r
376         SystemCoreClock = (HSI_VALUE >> 1) * pllmull;\r
377       }\r
378       else\r
379       {/* PREDIV1 selected as PLL clock entry */\r
380         \r
381         /* Get PREDIV1 clock source and division factor */\r
382         prediv1source = RCC->CFGR2 & RCC_CFGR2_PREDIV1SRC;\r
383         prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1;\r
384         \r
385         if (prediv1source == 0)\r
386         { \r
387           /* HSE oscillator clock selected as PREDIV1 clock entry */\r
388           SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;          \r
389         }\r
390         else\r
391         {/* PLL2 clock selected as PREDIV1 clock entry */\r
392           \r
393           /* Get PREDIV2 division factor and PLL2 multiplication factor */\r
394           prediv2factor = ((RCC->CFGR2 & RCC_CFGR2_PREDIV2) >> 4) + 1;\r
395           pll2mull = ((RCC->CFGR2 & RCC_CFGR2_PLL2MUL) >> 8 ) + 2; \r
396           SystemCoreClock = (((HSE_VALUE / prediv2factor) * pll2mull) / prediv1factor) * pllmull;                         \r
397         }\r
398       }\r
399 #endif /* STM32F10X_CL */ \r
400       break;\r
401 \r
402     default:\r
403       SystemCoreClock = HSI_VALUE;\r
404       break;\r
405   }\r
406   \r
407   /* Compute HCLK clock frequency ----------------*/\r
408   /* Get HCLK prescaler */\r
409   tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];\r
410   /* HCLK clock frequency */\r
411   SystemCoreClock >>= tmp;  \r
412 }\r
413 \r
414 /**\r
415   * @brief  Configures the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers.\r
416   * @param  None\r
417   * @retval None\r
418   */\r
419 static void SetSysClock(void)\r
420 {\r
421 #ifdef SYSCLK_FREQ_HSE\r
422   SetSysClockToHSE();\r
423 #elif defined SYSCLK_FREQ_24MHz\r
424   SetSysClockTo24();\r
425 #elif defined SYSCLK_FREQ_36MHz\r
426   SetSysClockTo36();\r
427 #elif defined SYSCLK_FREQ_48MHz\r
428   SetSysClockTo48();\r
429 #elif defined SYSCLK_FREQ_56MHz\r
430   SetSysClockTo56();  \r
431 #elif defined SYSCLK_FREQ_72MHz\r
432   SetSysClockTo72();\r
433 #endif\r
434  \r
435  /* If none of the define above is enabled, the HSI is used as System clock\r
436     source (default after reset) */ \r
437 }\r
438 \r
439 /**\r
440   * @brief  Setup the external memory controller. Called in startup_stm32f10x.s \r
441   *          before jump to __main\r
442   * @param  None\r
443   * @retval None\r
444   */ \r
445 #ifdef DATA_IN_ExtSRAM\r
446 /**\r
447   * @brief  Setup the external memory controller. \r
448   *         Called in startup_stm32f10x_xx.s/.c before jump to main.\r
449   *           This function configures the external SRAM mounted on STM3210E-EVAL\r
450   *         board (STM32 High density devices). This SRAM will be used as program\r
451   *         data memory (including heap and stack).\r
452   * @param  None\r
453   * @retval None\r
454   */ \r
455 void SystemInit_ExtMemCtl(void) \r
456 {\r
457 /*!< FSMC Bank1 NOR/SRAM3 is used for the STM3210E-EVAL, if another Bank is \r
458   required, then adjust the Register Addresses */\r
459 \r
460   /* Enable FSMC clock */\r
461   RCC->AHBENR = 0x00000114;\r
462   \r
463   /* Enable GPIOD, GPIOE, GPIOF and GPIOG clocks */  \r
464   RCC->APB2ENR = 0x000001E0;\r
465   \r
466 /* ---------------  SRAM Data lines, NOE and NWE configuration ---------------*/\r
467 /*----------------  SRAM Address lines configuration -------------------------*/\r
468 /*----------------  NOE and NWE configuration --------------------------------*/  \r
469 /*----------------  NE3 configuration ----------------------------------------*/\r
470 /*----------------  NBL0, NBL1 configuration ---------------------------------*/\r
471   \r
472   GPIOD->CRL = 0x44BB44BB;  \r
473   GPIOD->CRH = 0xBBBBBBBB;\r
474 \r
475   GPIOE->CRL = 0xB44444BB;  \r
476   GPIOE->CRH = 0xBBBBBBBB;\r
477 \r
478   GPIOF->CRL = 0x44BBBBBB;  \r
479   GPIOF->CRH = 0xBBBB4444;\r
480 \r
481   GPIOG->CRL = 0x44BBBBBB;  \r
482   GPIOG->CRH = 0x44444B44;\r
483    \r
484 /*----------------  FSMC Configuration ---------------------------------------*/  \r
485 /*----------------  Enable FSMC Bank1_SRAM Bank ------------------------------*/\r
486   \r
487   FSMC_Bank1->BTCR[4] = 0x00001011;\r
488   FSMC_Bank1->BTCR[5] = 0x00000200;\r
489 }\r
490 #endif /* DATA_IN_ExtSRAM */\r
491 \r
492 #ifdef SYSCLK_FREQ_HSE\r
493 /**\r
494   * @brief  Selects HSE as System clock source and configure HCLK, PCLK2\r
495   *         and PCLK1 prescalers.\r
496   * @note   This function should be used only after reset.\r
497   * @param  None\r
498   * @retval None\r
499   */\r
500 static void SetSysClockToHSE(void)\r
501 {\r
502   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;\r
503   \r
504   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/    \r
505   /* Enable HSE */    \r
506   RCC->CR |= ((uint32_t)RCC_CR_HSEON);\r
507  \r
508   /* Wait till HSE is ready and if Time out is reached exit */\r
509   do\r
510   {\r
511     HSEStatus = RCC->CR & RCC_CR_HSERDY;\r
512     StartUpCounter++;  \r
513   } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));\r
514 \r
515   if ((RCC->CR & RCC_CR_HSERDY) != RESET)\r
516   {\r
517     HSEStatus = (uint32_t)0x01;\r
518   }\r
519   else\r
520   {\r
521     HSEStatus = (uint32_t)0x00;\r
522   }  \r
523 \r
524   if (HSEStatus == (uint32_t)0x01)\r
525   {\r
526 \r
527 #if !defined STM32F10X_LD_VL && !defined STM32F10X_MD_VL && !defined STM32F10X_HD_VL\r
528     /* Enable Prefetch Buffer */\r
529     FLASH->ACR |= FLASH_ACR_PRFTBE;\r
530 \r
531     /* Flash 0 wait state */\r
532     FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);\r
533 \r
534 #ifndef STM32F10X_CL\r
535     FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_0;\r
536 #else\r
537     if (HSE_VALUE <= 24000000)\r
538         {\r
539       FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_0;\r
540         }\r
541         else\r
542         {\r
543       FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;\r
544         }\r
545 #endif /* STM32F10X_CL */\r
546 #endif\r
547  \r
548     /* HCLK = SYSCLK */\r
549     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;\r
550       \r
551     /* PCLK2 = HCLK */\r
552     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;\r
553     \r
554     /* PCLK1 = HCLK */\r
555     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;\r
556     \r
557     /* Select HSE as system clock source */\r
558     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));\r
559     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_HSE;    \r
560 \r
561     /* Wait till HSE is used as system clock source */\r
562     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x04)\r
563     {\r
564     }\r
565   }\r
566   else\r
567   { /* If HSE fails to start-up, the application will have wrong clock \r
568          configuration. User can add here some code to deal with this error */\r
569   }  \r
570 }\r
571 #elif defined SYSCLK_FREQ_24MHz\r
572 /**\r
573   * @brief  Sets System clock frequency to 24MHz and configure HCLK, PCLK2 \r
574   *         and PCLK1 prescalers.\r
575   * @note   This function should be used only after reset.\r
576   * @param  None\r
577   * @retval None\r
578   */\r
579 static void SetSysClockTo24(void)\r
580 {\r
581   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;\r
582   \r
583   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/    \r
584   /* Enable HSE */    \r
585   RCC->CR |= ((uint32_t)RCC_CR_HSEON);\r
586  \r
587   /* Wait till HSE is ready and if Time out is reached exit */\r
588   do\r
589   {\r
590     HSEStatus = RCC->CR & RCC_CR_HSERDY;\r
591     StartUpCounter++;  \r
592   } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));\r
593 \r
594   if ((RCC->CR & RCC_CR_HSERDY) != RESET)\r
595   {\r
596     HSEStatus = (uint32_t)0x01;\r
597   }\r
598   else\r
599   {\r
600     HSEStatus = (uint32_t)0x00;\r
601   }  \r
602 \r
603   if (HSEStatus == (uint32_t)0x01)\r
604   {\r
605 #if !defined STM32F10X_LD_VL && !defined STM32F10X_MD_VL && !defined STM32F10X_HD_VL \r
606     /* Enable Prefetch Buffer */\r
607     FLASH->ACR |= FLASH_ACR_PRFTBE;\r
608 \r
609     /* Flash 0 wait state */\r
610     FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);\r
611     FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_0;    \r
612 #endif\r
613  \r
614     /* HCLK = SYSCLK */\r
615     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;\r
616       \r
617     /* PCLK2 = HCLK */\r
618     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;\r
619     \r
620     /* PCLK1 = HCLK */\r
621     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;\r
622     \r
623 #ifdef STM32F10X_CL\r
624     /* Configure PLLs ------------------------------------------------------*/\r
625     /* PLL configuration: PLLCLK = PREDIV1 * 6 = 24 MHz */ \r
626     RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);\r
627     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 | \r
628                             RCC_CFGR_PLLMULL6); \r
629 \r
630     /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */\r
631     /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 10 = 4 MHz */       \r
632     RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |\r
633                               RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);\r
634     RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |\r
635                              RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV10);\r
636   \r
637     /* Enable PLL2 */\r
638     RCC->CR |= RCC_CR_PLL2ON;\r
639     /* Wait till PLL2 is ready */\r
640     while((RCC->CR & RCC_CR_PLL2RDY) == 0)\r
641     {\r
642     }   \r
643 #elif defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)\r
644     /*  PLL configuration:  = (HSE / 2) * 6 = 24 MHz */\r
645     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));\r
646     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | RCC_CFGR_PLLXTPRE_PREDIV1_Div2 | RCC_CFGR_PLLMULL6);\r
647 #else    \r
648     /*  PLL configuration:  = (HSE / 2) * 6 = 24 MHz */\r
649     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));\r
650     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLXTPRE_HSE_Div2 | RCC_CFGR_PLLMULL6);\r
651 #endif /* STM32F10X_CL */\r
652 \r
653     /* Enable PLL */\r
654     RCC->CR |= RCC_CR_PLLON;\r
655 \r
656     /* Wait till PLL is ready */\r
657     while((RCC->CR & RCC_CR_PLLRDY) == 0)\r
658     {\r
659     }\r
660 \r
661     /* Select PLL as system clock source */\r
662     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));\r
663     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;    \r
664 \r
665     /* Wait till PLL is used as system clock source */\r
666     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)\r
667     {\r
668     }\r
669   }\r
670   else\r
671   { /* If HSE fails to start-up, the application will have wrong clock \r
672          configuration. User can add here some code to deal with this error */\r
673   } \r
674 }\r
675 #elif defined SYSCLK_FREQ_36MHz\r
676 /**\r
677   * @brief  Sets System clock frequency to 36MHz and configure HCLK, PCLK2 \r
678   *         and PCLK1 prescalers. \r
679   * @note   This function should be used only after reset.\r
680   * @param  None\r
681   * @retval None\r
682   */\r
683 static void SetSysClockTo36(void)\r
684 {\r
685   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;\r
686   \r
687   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/    \r
688   /* Enable HSE */    \r
689   RCC->CR |= ((uint32_t)RCC_CR_HSEON);\r
690  \r
691   /* Wait till HSE is ready and if Time out is reached exit */\r
692   do\r
693   {\r
694     HSEStatus = RCC->CR & RCC_CR_HSERDY;\r
695     StartUpCounter++;  \r
696   } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));\r
697 \r
698   if ((RCC->CR & RCC_CR_HSERDY) != RESET)\r
699   {\r
700     HSEStatus = (uint32_t)0x01;\r
701   }\r
702   else\r
703   {\r
704     HSEStatus = (uint32_t)0x00;\r
705   }  \r
706 \r
707   if (HSEStatus == (uint32_t)0x01)\r
708   {\r
709     /* Enable Prefetch Buffer */\r
710     FLASH->ACR |= FLASH_ACR_PRFTBE;\r
711 \r
712     /* Flash 1 wait state */\r
713     FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);\r
714     FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;    \r
715  \r
716     /* HCLK = SYSCLK */\r
717     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;\r
718       \r
719     /* PCLK2 = HCLK */\r
720     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;\r
721     \r
722     /* PCLK1 = HCLK */\r
723     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;\r
724     \r
725 #ifdef STM32F10X_CL\r
726     /* Configure PLLs ------------------------------------------------------*/\r
727     \r
728     /* PLL configuration: PLLCLK = PREDIV1 * 9 = 36 MHz */ \r
729     RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);\r
730     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 | \r
731                             RCC_CFGR_PLLMULL9); \r
732 \r
733         /*!< PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */\r
734     /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 10 = 4 MHz */\r
735         \r
736     RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |\r
737                               RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);\r
738     RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |\r
739                              RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV10);\r
740   \r
741     /* Enable PLL2 */\r
742     RCC->CR |= RCC_CR_PLL2ON;\r
743     /* Wait till PLL2 is ready */\r
744     while((RCC->CR & RCC_CR_PLL2RDY) == 0)\r
745     {\r
746     }\r
747     \r
748 #else    \r
749     /*  PLL configuration: PLLCLK = (HSE / 2) * 9 = 36 MHz */\r
750     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));\r
751     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLXTPRE_HSE_Div2 | RCC_CFGR_PLLMULL9);\r
752 #endif /* STM32F10X_CL */\r
753 \r
754     /* Enable PLL */\r
755     RCC->CR |= RCC_CR_PLLON;\r
756 \r
757     /* Wait till PLL is ready */\r
758     while((RCC->CR & RCC_CR_PLLRDY) == 0)\r
759     {\r
760     }\r
761 \r
762     /* Select PLL as system clock source */\r
763     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));\r
764     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;    \r
765 \r
766     /* Wait till PLL is used as system clock source */\r
767     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)\r
768     {\r
769     }\r
770   }\r
771   else\r
772   { /* If HSE fails to start-up, the application will have wrong clock \r
773          configuration. User can add here some code to deal with this error */\r
774   } \r
775 }\r
776 #elif defined SYSCLK_FREQ_48MHz\r
777 /**\r
778   * @brief  Sets System clock frequency to 48MHz and configure HCLK, PCLK2 \r
779   *         and PCLK1 prescalers. \r
780   * @note   This function should be used only after reset.\r
781   * @param  None\r
782   * @retval None\r
783   */\r
784 static void SetSysClockTo48(void)\r
785 {\r
786   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;\r
787   \r
788   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/    \r
789   /* Enable HSE */    \r
790   RCC->CR |= ((uint32_t)RCC_CR_HSEON);\r
791  \r
792   /* Wait till HSE is ready and if Time out is reached exit */\r
793   do\r
794   {\r
795     HSEStatus = RCC->CR & RCC_CR_HSERDY;\r
796     StartUpCounter++;  \r
797   } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));\r
798 \r
799   if ((RCC->CR & RCC_CR_HSERDY) != RESET)\r
800   {\r
801     HSEStatus = (uint32_t)0x01;\r
802   }\r
803   else\r
804   {\r
805     HSEStatus = (uint32_t)0x00;\r
806   }  \r
807 \r
808   if (HSEStatus == (uint32_t)0x01)\r
809   {\r
810     /* Enable Prefetch Buffer */\r
811     FLASH->ACR |= FLASH_ACR_PRFTBE;\r
812 \r
813     /* Flash 1 wait state */\r
814     FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);\r
815     FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;    \r
816  \r
817     /* HCLK = SYSCLK */\r
818     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;\r
819       \r
820     /* PCLK2 = HCLK */\r
821     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;\r
822     \r
823     /* PCLK1 = HCLK */\r
824     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;\r
825     \r
826 #ifdef STM32F10X_CL\r
827     /* Configure PLLs ------------------------------------------------------*/\r
828     /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */\r
829     /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */\r
830         \r
831     RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |\r
832                               RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);\r
833     RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |\r
834                              RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);\r
835   \r
836     /* Enable PLL2 */\r
837     RCC->CR |= RCC_CR_PLL2ON;\r
838     /* Wait till PLL2 is ready */\r
839     while((RCC->CR & RCC_CR_PLL2RDY) == 0)\r
840     {\r
841     }\r
842     \r
843    \r
844     /* PLL configuration: PLLCLK = PREDIV1 * 6 = 48 MHz */ \r
845     RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);\r
846     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 | \r
847                             RCC_CFGR_PLLMULL6); \r
848 #else    \r
849     /*  PLL configuration: PLLCLK = HSE * 6 = 48 MHz */\r
850     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));\r
851     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL6);\r
852 #endif /* STM32F10X_CL */\r
853 \r
854     /* Enable PLL */\r
855     RCC->CR |= RCC_CR_PLLON;\r
856 \r
857     /* Wait till PLL is ready */\r
858     while((RCC->CR & RCC_CR_PLLRDY) == 0)\r
859     {\r
860     }\r
861 \r
862     /* Select PLL as system clock source */\r
863     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));\r
864     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;    \r
865 \r
866     /* Wait till PLL is used as system clock source */\r
867     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)\r
868     {\r
869     }\r
870   }\r
871   else\r
872   { /* If HSE fails to start-up, the application will have wrong clock \r
873          configuration. User can add here some code to deal with this error */\r
874   } \r
875 }\r
876 \r
877 #elif defined SYSCLK_FREQ_56MHz\r
878 /**\r
879   * @brief  Sets System clock frequency to 56MHz and configure HCLK, PCLK2 \r
880   *         and PCLK1 prescalers. \r
881   * @note   This function should be used only after reset.\r
882   * @param  None\r
883   * @retval None\r
884   */\r
885 static void SetSysClockTo56(void)\r
886 {\r
887   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;\r
888   \r
889   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/   \r
890   /* Enable HSE */    \r
891   RCC->CR |= ((uint32_t)RCC_CR_HSEON);\r
892  \r
893   /* Wait till HSE is ready and if Time out is reached exit */\r
894   do\r
895   {\r
896     HSEStatus = RCC->CR & RCC_CR_HSERDY;\r
897     StartUpCounter++;  \r
898   } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));\r
899 \r
900   if ((RCC->CR & RCC_CR_HSERDY) != RESET)\r
901   {\r
902     HSEStatus = (uint32_t)0x01;\r
903   }\r
904   else\r
905   {\r
906     HSEStatus = (uint32_t)0x00;\r
907   }  \r
908 \r
909   if (HSEStatus == (uint32_t)0x01)\r
910   {\r
911     /* Enable Prefetch Buffer */\r
912     FLASH->ACR |= FLASH_ACR_PRFTBE;\r
913 \r
914     /* Flash 2 wait state */\r
915     FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);\r
916     FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;    \r
917  \r
918     /* HCLK = SYSCLK */\r
919     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;\r
920       \r
921     /* PCLK2 = HCLK */\r
922     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;\r
923     \r
924     /* PCLK1 = HCLK */\r
925     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;\r
926 \r
927 #ifdef STM32F10X_CL\r
928     /* Configure PLLs ------------------------------------------------------*/\r
929     /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */\r
930     /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */\r
931         \r
932     RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |\r
933                               RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);\r
934     RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |\r
935                              RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);\r
936   \r
937     /* Enable PLL2 */\r
938     RCC->CR |= RCC_CR_PLL2ON;\r
939     /* Wait till PLL2 is ready */\r
940     while((RCC->CR & RCC_CR_PLL2RDY) == 0)\r
941     {\r
942     }\r
943     \r
944    \r
945     /* PLL configuration: PLLCLK = PREDIV1 * 7 = 56 MHz */ \r
946     RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);\r
947     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 | \r
948                             RCC_CFGR_PLLMULL7); \r
949 #else     \r
950     /* PLL configuration: PLLCLK = HSE * 7 = 56 MHz */\r
951     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));\r
952     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL7);\r
953 \r
954 #endif /* STM32F10X_CL */\r
955 \r
956     /* Enable PLL */\r
957     RCC->CR |= RCC_CR_PLLON;\r
958 \r
959     /* Wait till PLL is ready */\r
960     while((RCC->CR & RCC_CR_PLLRDY) == 0)\r
961     {\r
962     }\r
963 \r
964     /* Select PLL as system clock source */\r
965     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));\r
966     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;    \r
967 \r
968     /* Wait till PLL is used as system clock source */\r
969     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)\r
970     {\r
971     }\r
972   }\r
973   else\r
974   { /* If HSE fails to start-up, the application will have wrong clock \r
975          configuration. User can add here some code to deal with this error */\r
976   } \r
977 }\r
978 \r
979 #elif defined SYSCLK_FREQ_72MHz\r
980 /**\r
981   * @brief  Sets System clock frequency to 72MHz and configure HCLK, PCLK2 \r
982   *         and PCLK1 prescalers. \r
983   * @note   This function should be used only after reset.\r
984   * @param  None\r
985   * @retval None\r
986   */\r
987 static void SetSysClockTo72(void)\r
988 {\r
989   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;\r
990   \r
991   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/    \r
992   /* Enable HSE */    \r
993   RCC->CR |= ((uint32_t)RCC_CR_HSEON);\r
994  \r
995   /* Wait till HSE is ready and if Time out is reached exit */\r
996   do\r
997   {\r
998     HSEStatus = RCC->CR & RCC_CR_HSERDY;\r
999     StartUpCounter++;  \r
1000   } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));\r
1001 \r
1002   if ((RCC->CR & RCC_CR_HSERDY) != RESET)\r
1003   {\r
1004     HSEStatus = (uint32_t)0x01;\r
1005   }\r
1006   else\r
1007   {\r
1008     HSEStatus = (uint32_t)0x00;\r
1009   }  \r
1010 \r
1011   if (HSEStatus == (uint32_t)0x01)\r
1012   {\r
1013     /* Enable Prefetch Buffer */\r
1014     FLASH->ACR |= FLASH_ACR_PRFTBE;\r
1015 \r
1016     /* Flash 2 wait state */\r
1017     FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);\r
1018     FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;    \r
1019 \r
1020  \r
1021     /* HCLK = SYSCLK */\r
1022     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;\r
1023       \r
1024     /* PCLK2 = HCLK */\r
1025     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;\r
1026     \r
1027     /* PCLK1 = HCLK */\r
1028     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;\r
1029 \r
1030 #ifdef STM32F10X_CL\r
1031     /* Configure PLLs ------------------------------------------------------*/\r
1032     /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */\r
1033     /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */\r
1034         \r
1035     RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |\r
1036                               RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);\r
1037     RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |\r
1038                              RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);\r
1039   \r
1040     /* Enable PLL2 */\r
1041     RCC->CR |= RCC_CR_PLL2ON;\r
1042     /* Wait till PLL2 is ready */\r
1043     while((RCC->CR & RCC_CR_PLL2RDY) == 0)\r
1044     {\r
1045     }\r
1046     \r
1047    \r
1048     /* PLL configuration: PLLCLK = PREDIV1 * 9 = 72 MHz */ \r
1049     RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);\r
1050     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 | \r
1051                             RCC_CFGR_PLLMULL9); \r
1052 #else    \r
1053     /*  PLL configuration: PLLCLK = HSE * 9 = 72 MHz */\r
1054     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE |\r
1055                                         RCC_CFGR_PLLMULL));\r
1056     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL9);\r
1057 #endif /* STM32F10X_CL */\r
1058 \r
1059     /* Enable PLL */\r
1060     RCC->CR |= RCC_CR_PLLON;\r
1061 \r
1062     /* Wait till PLL is ready */\r
1063     while((RCC->CR & RCC_CR_PLLRDY) == 0)\r
1064     {\r
1065     }\r
1066     \r
1067     /* Select PLL as system clock source */\r
1068     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));\r
1069     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;    \r
1070 \r
1071     /* Wait till PLL is used as system clock source */\r
1072     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)\r
1073     {\r
1074     }\r
1075   }\r
1076   else\r
1077   { /* If HSE fails to start-up, the application will have wrong clock \r
1078          configuration. User can add here some code to deal with this error */\r
1079   }\r
1080 }\r
1081 #endif\r
1082 \r
1083 /**\r
1084   * @}\r
1085   */\r
1086 \r
1087 /**\r
1088   * @}\r
1089   */\r
1090   \r
1091 /**\r
1092   * @}\r
1093   */    \r
1094 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/\r