]> rtime.felk.cvut.cz Git - arc.git/blob - arch/arm/arm_cm3/kernel/system_stm32f10x.c
1023f6f273ffc1ef6bd740170d1e74b56c312188
[arc.git] / arch / arm / arm_cm3 / kernel / system_stm32f10x.c
1 /* -------------------------------- Arctic Core ------------------------------
2  * Arctic Core - the open source AUTOSAR platform http://arccore.com
3  *
4  * Copyright (C) 2009  ArcCore AB <contact@arccore.com>
5  *
6  * This source code is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 as published by the
8  * Free Software Foundation; See <http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt>.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13  * for more details.
14  * -------------------------------- Arctic Core ------------------------------*/
15
16
17
18
19
20
21
22
23 /**\r
24   ******************************************************************************\r
25   * @file    system_stm32f10x.c\r
26   * @author  MCD Application Team\r
27   * @version V3.1.0\r
28   * @date    06/19/2009\r
29   * @brief   CMSIS Cortex-M3 Device Peripheral Access Layer System Source File.\r
30   ******************************************************************************  \r
31   *\r
32   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS\r
33   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE\r
34   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY\r
35   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING\r
36   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE\r
37   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.\r
38   *\r
39   * <h2><center>&copy; COPYRIGHT 2009 STMicroelectronics</center></h2>\r
40   ******************************************************************************\r
41   */\r
42 \r
43 /** @addtogroup CMSIS\r
44   * @{\r
45   */\r
46 \r
47 /** @addtogroup stm32f10x_system\r
48   * @{\r
49   */  \r
50   \r
51 /** @addtogroup STM32F10x_System_Private_Includes\r
52   * @{\r
53   */\r
54 \r
55 #include "stm32f10x.h"\r
56 \r
57 /**\r
58   * @}\r
59   */\r
60 \r
61 /** @addtogroup STM32F10x_System_Private_TypesDefinitions\r
62   * @{\r
63   */\r
64 \r
65 /**\r
66   * @}\r
67   */\r
68 \r
69 /** @addtogroup STM32F10x_System_Private_Defines\r
70   * @{\r
71   */\r
72 \r
73 /*!< Uncomment the line corresponding to the desired System clock (SYSCLK)\r
74    frequency (after reset the HSI is used as SYSCLK source)\r
75    \r
76    IMPORTANT NOTE:\r
77    ============== \r
78    1. After each device reset the HSI is used as System clock source.\r
79 \r
80    2. Please make sure that the selected System clock doesn't exceed your device's\r
81       maximum frequency.\r
82       \r
83    3. If none of the define below is enabled, the HSI is used as System clock\r
84     source.\r
85 \r
86    4. The System clock configuration functions provided within this file assume that:\r
87         - For Low, Medium and High density devices an external 8MHz crystal is\r
88           used to drive the System clock.\r
89         - For Connectivity line devices an external 25MHz crystal is used to drive\r
90           the System clock.\r
91      If you are using different crystal you have to adapt those functions accordingly.\r
92     */\r
93     \r
94 /* #define SYSCLK_FREQ_HSE    HSE_Value */\r
95 /* #define SYSCLK_FREQ_24MHz  24000000 */\r
96 /* #define SYSCLK_FREQ_36MHz  36000000 */\r
97 /* #define SYSCLK_FREQ_48MHz  48000000 */\r
98 /* #define SYSCLK_FREQ_56MHz  56000000 */\r
99 #define SYSCLK_FREQ_72MHz  72000000\r
100 \r
101 /*!< Uncomment the following line if you need to use external SRAM mounted\r
102      on STM3210E-EVAL board (STM32 High density devices) as data memory  */ \r
103 #ifdef STM32F10X_HD\r
104 /* #define DATA_IN_ExtSRAM */\r
105 #endif /* STM32F10X_HD */\r
106 \r
107 /**\r
108   * @}\r
109   */\r
110 \r
111 /** @addtogroup STM32F10x_System_Private_Macros\r
112   * @{\r
113   */\r
114 \r
115 /**\r
116   * @}\r
117   */\r
118 \r
119 /** @addtogroup STM32F10x_System_Private_Variables\r
120   * @{\r
121   */\r
122 \r
123 /*******************************************************************************\r
124 *  Clock Definitions\r
125 *******************************************************************************/\r
126 #ifdef SYSCLK_FREQ_HSE\r
127   const uint32_t SystemFrequency         = SYSCLK_FREQ_HSE;        /*!< System Clock Frequency (Core Clock) */\r
128   const uint32_t SystemFrequency_SysClk  = SYSCLK_FREQ_HSE;        /*!< System clock                        */\r
129   const uint32_t SystemFrequency_AHBClk  = SYSCLK_FREQ_HSE;        /*!< AHB System bus speed                */\r
130   const uint32_t SystemFrequency_APB1Clk = SYSCLK_FREQ_HSE;        /*!< APB Peripheral bus 1 (low)  speed   */\r
131   const uint32_t SystemFrequency_APB2Clk = SYSCLK_FREQ_HSE;        /*!< APB Peripheral bus 2 (high) speed   */\r
132 #elif defined SYSCLK_FREQ_24MHz\r
133   const uint32_t SystemFrequency         = SYSCLK_FREQ_24MHz;      /*!< System Clock Frequency (Core Clock) */\r
134   const uint32_t SystemFrequency_SysClk  = SYSCLK_FREQ_24MHz;      /*!< System clock                        */\r
135   const uint32_t SystemFrequency_AHBClk  = SYSCLK_FREQ_24MHz;      /*!< AHB System bus speed                */\r
136   const uint32_t SystemFrequency_APB1Clk = SYSCLK_FREQ_24MHz;      /*!< APB Peripheral bus 1 (low)  speed   */\r
137   const uint32_t SystemFrequency_APB2Clk = SYSCLK_FREQ_24MHz;      /*!< APB Peripheral bus 2 (high) speed   */\r
138 #elif defined SYSCLK_FREQ_36MHz\r
139   const uint32_t SystemFrequency         = SYSCLK_FREQ_36MHz;      /*!< System Clock Frequency (Core Clock) */\r
140   const uint32_t SystemFrequency_SysClk  = SYSCLK_FREQ_36MHz;      /*!< System clock                        */\r
141   const uint32_t SystemFrequency_AHBClk  = SYSCLK_FREQ_36MHz;      /*!< AHB System bus speed                */\r
142   const uint32_t SystemFrequency_APB1Clk = SYSCLK_FREQ_36MHz;      /*!< APB Peripheral bus 1 (low)  speed   */\r
143   const uint32_t SystemFrequency_APB2Clk = SYSCLK_FREQ_36MHz;      /*!< APB Peripheral bus 2 (high) speed   */\r
144 #elif defined SYSCLK_FREQ_48MHz\r
145   const uint32_t SystemFrequency         = SYSCLK_FREQ_48MHz;      /*!< System Clock Frequency (Core Clock) */\r
146   const uint32_t SystemFrequency_SysClk  = SYSCLK_FREQ_48MHz;      /*!< System clock                        */\r
147   const uint32_t SystemFrequency_AHBClk  = SYSCLK_FREQ_48MHz;      /*!< AHB System bus speed                */\r
148   const uint32_t SystemFrequency_APB1Clk = (SYSCLK_FREQ_48MHz/2);  /*!< APB Peripheral bus 1 (low)  speed   */\r
149   const uint32_t SystemFrequency_APB2Clk = SYSCLK_FREQ_48MHz;      /*!< APB Peripheral bus 2 (high) speed   */\r
150 #elif defined SYSCLK_FREQ_56MHz\r
151   const uint32_t SystemFrequency         = SYSCLK_FREQ_56MHz;      /*!< System Clock Frequency (Core Clock) */\r
152   const uint32_t SystemFrequency_SysClk  = SYSCLK_FREQ_56MHz;      /*!< System clock                        */\r
153   const uint32_t SystemFrequency_AHBClk  = SYSCLK_FREQ_56MHz;      /*!< AHB System bus speed                */\r
154   const uint32_t SystemFrequency_APB1Clk = (SYSCLK_FREQ_56MHz/2);  /*!< APB Peripheral bus 1 (low)  speed   */\r
155   const uint32_t SystemFrequency_APB2Clk = SYSCLK_FREQ_56MHz;      /*!< APB Peripheral bus 2 (high) speed   */  \r
156 #elif defined SYSCLK_FREQ_72MHz\r
157   const uint32_t SystemFrequency         = SYSCLK_FREQ_72MHz;      /*!< System Clock Frequency (Core Clock) */\r
158   const uint32_t SystemFrequency_SysClk  = SYSCLK_FREQ_72MHz;      /*!< System clock                        */\r
159   const uint32_t SystemFrequency_AHBClk  = SYSCLK_FREQ_72MHz;      /*!< AHB System bus speed                */\r
160   const uint32_t SystemFrequency_APB1Clk = (SYSCLK_FREQ_72MHz/2);  /*!< APB Peripheral bus 1 (low)  speed   */\r
161   const uint32_t SystemFrequency_APB2Clk = SYSCLK_FREQ_72MHz;      /*!< APB Peripheral bus 2 (high) speed   */\r
162 #else /*!< HSI Selected as System Clock source */\r
163   const uint32_t SystemFrequency         = HSI_Value;              /*!< System Clock Frequency (Core Clock) */\r
164   const uint32_t SystemFrequency_SysClk  = HSI_Value;              /*!< System clock                        */\r
165   const uint32_t SystemFrequency_AHBClk  = HSI_Value;              /*!< AHB System bus speed                */\r
166   const uint32_t SystemFrequency_APB1Clk = HSI_Value;              /*!< APB Peripheral bus 1 (low)  speed   */\r
167   const uint32_t SystemFrequency_APB2Clk = HSI_Value;              /*!< APB Peripheral bus 2 (high) speed   */\r
168 #endif\r
169 \r
170 /**\r
171   * @}\r
172   */\r
173 \r
174 /** @addtogroup STM32F10x_System_Private_FunctionPrototypes\r
175   * @{\r
176   */\r
177 \r
178 static void SetSysClock(void);\r
179 \r
180 #ifdef SYSCLK_FREQ_HSE\r
181   static void SetSysClockToHSE(void);\r
182 #elif defined SYSCLK_FREQ_24MHz\r
183   static void SetSysClockTo24(void);\r
184 #elif defined SYSCLK_FREQ_36MHz\r
185   static void SetSysClockTo36(void);\r
186 #elif defined SYSCLK_FREQ_48MHz\r
187   static void SetSysClockTo48(void);\r
188 #elif defined SYSCLK_FREQ_56MHz\r
189   static void SetSysClockTo56(void);  \r
190 #elif defined SYSCLK_FREQ_72MHz\r
191   static void SetSysClockTo72(void);\r
192 #endif\r
193 \r
194 /**\r
195   * @}\r
196   */\r
197 \r
198 /** @addtogroup STM32F10x_System_Private_Functions\r
199   * @{\r
200   */\r
201 \r
202 /**\r
203   * @brief  Setup the microcontroller system\r
204   *         Initialize the Embedded Flash Interface, the PLL and update the SystemFrequency variable.\r
205   * @note   This function should be used only after reset.\r
206   * @param  None\r
207   * @retval None\r
208   */\r
209 void SystemInit (void)\r
210 {\r
211   /* Reset the RCC clock configuration to the default reset state(for debug purpose) */\r
212   /* Set HSION bit */\r
213   RCC->CR |= (uint32_t)0x00000001;\r
214 \r
215   /* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */\r
216 #ifndef STM32F10X_CL\r
217   RCC->CFGR &= (uint32_t)0xF8FF0000;\r
218 #else\r
219   RCC->CFGR &= (uint32_t)0xF0FF0000;\r
220 #endif /* STM32F10X_CL */   \r
221   \r
222   /* Reset HSEON, CSSON and PLLON bits */\r
223   RCC->CR &= (uint32_t)0xFEF6FFFF;\r
224 \r
225   /* Reset HSEBYP bit */\r
226   RCC->CR &= (uint32_t)0xFFFBFFFF;\r
227 \r
228   /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */\r
229   RCC->CFGR &= (uint32_t)0xFF80FFFF;\r
230 \r
231 #ifndef STM32F10X_CL\r
232   /* Disable all interrupts and clear pending bits  */\r
233   RCC->CIR = 0x009F0000;\r
234 #else\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 #endif /* STM32F10X_CL */\r
244     \r
245   /* Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers */\r
246   /* Configure the Flash Latency cycles and enable prefetch buffer */\r
247   SetSysClock();\r
248 \r
249 }\r
250 \r
251 /**\r
252   * @brief  Configures the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers.\r
253   * @param  None\r
254   * @retval None\r
255   */\r
256 static void SetSysClock(void)\r
257 {\r
258 #ifdef SYSCLK_FREQ_HSE\r
259   SetSysClockToHSE();\r
260 #elif defined SYSCLK_FREQ_24MHz\r
261   SetSysClockTo24();\r
262 #elif defined SYSCLK_FREQ_36MHz\r
263   SetSysClockTo36();\r
264 #elif defined SYSCLK_FREQ_48MHz\r
265   SetSysClockTo48();\r
266 #elif defined SYSCLK_FREQ_56MHz\r
267   SetSysClockTo56();  \r
268 #elif defined SYSCLK_FREQ_72MHz\r
269   SetSysClockTo72();\r
270 #endif\r
271  \r
272  /* If none of the define above is enabled, the HSI is used as System clock\r
273     source (default after reset) */ \r
274 }\r
275 \r
276 /**\r
277   * @brief  Setup the external memory controller. Called in startup_stm32f10x.s \r
278   *          before jump to __main\r
279   * @param  None\r
280   * @retval None\r
281   */ \r
282 #ifdef DATA_IN_ExtSRAM\r
283 /**\r
284   * @brief  Setup the external memory controller. \r
285   *         Called in startup_stm32f10x_xx.s/.c before jump to main.\r
286   *           This function configures the external SRAM mounted on STM3210E-EVAL\r
287   *         board (STM32 High density devices). This SRAM will be used as program\r
288   *         data memory (including heap and stack).\r
289   * @param  None\r
290   * @retval None\r
291   */ \r
292 void SystemInit_ExtMemCtl(void) \r
293 {\r
294 /*!< FSMC Bank1 NOR/SRAM3 is used for the STM3210E-EVAL, if another Bank is \r
295   required, then adjust the Register Addresses */\r
296 \r
297   /* Enable FSMC clock */\r
298   RCC->AHBENR = 0x00000114;\r
299   \r
300   /* Enable GPIOD, GPIOE, GPIOF and GPIOG clocks */  \r
301   RCC->APB2ENR = 0x000001E0;\r
302   \r
303 /* ---------------  SRAM Data lines, NOE and NWE configuration ---------------*/\r
304 /*----------------  SRAM Address lines configuration -------------------------*/\r
305 /*----------------  NOE and NWE configuration --------------------------------*/  \r
306 /*----------------  NE3 configuration ----------------------------------------*/\r
307 /*----------------  NBL0, NBL1 configuration ---------------------------------*/\r
308   \r
309   GPIOD->CRL = 0x44BB44BB;  \r
310   GPIOD->CRH = 0xBBBBBBBB;\r
311 \r
312   GPIOE->CRL = 0xB44444BB;  \r
313   GPIOE->CRH = 0xBBBBBBBB;\r
314 \r
315   GPIOF->CRL = 0x44BBBBBB;  \r
316   GPIOF->CRH = 0xBBBB4444;\r
317 \r
318   GPIOG->CRL = 0x44BBBBBB;  \r
319   GPIOG->CRH = 0x44444B44;\r
320    \r
321 /*----------------  FSMC Configuration ---------------------------------------*/  \r
322 /*----------------  Enable FSMC Bank1_SRAM Bank ------------------------------*/\r
323   \r
324   FSMC_Bank1->BTCR[4] = 0x00001011;\r
325   FSMC_Bank1->BTCR[5] = 0x00000200;\r
326 }\r
327 #endif /* DATA_IN_ExtSRAM */\r
328 \r
329 #ifdef SYSCLK_FREQ_HSE\r
330 /**\r
331   * @brief  Selects HSE as System clock source and configure HCLK, PCLK2\r
332   *          and PCLK1 prescalers.\r
333   * @note   This function should be used only after reset.\r
334   * @param  None\r
335   * @retval None\r
336   */\r
337 static void SetSysClockToHSE(void)\r
338 {\r
339   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;\r
340   \r
341   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/    \r
342   /* Enable HSE */    \r
343   RCC->CR |= ((uint32_t)RCC_CR_HSEON);\r
344  \r
345   /* Wait till HSE is ready and if Time out is reached exit */\r
346   do\r
347   {\r
348     HSEStatus = RCC->CR & RCC_CR_HSERDY;\r
349     StartUpCounter++;  \r
350   } while((HSEStatus == 0) && (StartUpCounter != HSEStartUp_TimeOut));\r
351 \r
352   if ((RCC->CR & RCC_CR_HSERDY) != RESET)\r
353   {\r
354     HSEStatus = (uint32_t)0x01;\r
355   }\r
356   else\r
357   {\r
358     HSEStatus = (uint32_t)0x00;\r
359   }  \r
360 \r
361   if (HSEStatus == (uint32_t)0x01)\r
362   {\r
363     /* Enable Prefetch Buffer */\r
364     FLASH->ACR |= FLASH_ACR_PRFTBE;\r
365 \r
366     /* Flash 0 wait state */\r
367     FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);\r
368 \r
369 #ifndef STM32F10X_CL\r
370     FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_0;\r
371 #else\r
372     if (HSE_Value <= 24000000)\r
373         {\r
374       FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_0;\r
375         }\r
376         else\r
377         {\r
378       FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;\r
379         }\r
380 #endif /* STM32F10X_CL */\r
381  \r
382     /* HCLK = SYSCLK */\r
383     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;\r
384       \r
385     /* PCLK2 = HCLK */\r
386     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;\r
387     \r
388     /* PCLK1 = HCLK */\r
389     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;\r
390     \r
391     /* Select HSE as system clock source */\r
392     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));\r
393     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_HSE;    \r
394 \r
395     /* Wait till HSE is used as system clock source */\r
396     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x04)\r
397     {\r
398     }\r
399   }\r
400   else\r
401   { /* If HSE fails to start-up, the application will have wrong clock \r
402          configuration. User can add here some code to deal with this error */    \r
403 \r
404     /* Go to infinite loop */\r
405     while (1)\r
406     {\r
407     }\r
408   }  \r
409 }\r
410 #elif defined SYSCLK_FREQ_24MHz\r
411 /**\r
412   * @brief  Sets System clock frequency to 24MHz and configure HCLK, PCLK2 \r
413   *          and PCLK1 prescalers.\r
414   * @note   This function should be used only after reset.\r
415   * @param  None\r
416   * @retval None\r
417   */\r
418 static void SetSysClockTo24(void)\r
419 {\r
420   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;\r
421   \r
422   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/    \r
423   /* Enable HSE */    \r
424   RCC->CR |= ((uint32_t)RCC_CR_HSEON);\r
425  \r
426   /* Wait till HSE is ready and if Time out is reached exit */\r
427   do\r
428   {\r
429     HSEStatus = RCC->CR & RCC_CR_HSERDY;\r
430     StartUpCounter++;  \r
431   } while((HSEStatus == 0) && (StartUpCounter != HSEStartUp_TimeOut));\r
432 \r
433   if ((RCC->CR & RCC_CR_HSERDY) != RESET)\r
434   {\r
435     HSEStatus = (uint32_t)0x01;\r
436   }\r
437   else\r
438   {\r
439     HSEStatus = (uint32_t)0x00;\r
440   }  \r
441 \r
442   if (HSEStatus == (uint32_t)0x01)\r
443   {\r
444     /* Enable Prefetch Buffer */\r
445     FLASH->ACR |= FLASH_ACR_PRFTBE;\r
446 \r
447     /* Flash 0 wait state */\r
448     FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);\r
449     FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_0;    \r
450  \r
451     /* HCLK = SYSCLK */\r
452     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;\r
453       \r
454     /* PCLK2 = HCLK */\r
455     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;\r
456     \r
457     /* PCLK1 = HCLK */\r
458     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;\r
459     \r
460 #ifdef STM32F10X_CL\r
461     /* Configure PLLs ------------------------------------------------------*/\r
462     /* PLL configuration: PLLCLK = PREDIV1 * 6 = 24 MHz */ \r
463     RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);\r
464     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 | \r
465                             RCC_CFGR_PLLMULL6); \r
466 \r
467     /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */\r
468     /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 10 = 4 MHz */       \r
469     RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |\r
470                               RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);\r
471     RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |\r
472                              RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV10);\r
473   \r
474     /* Enable PLL2 */\r
475     RCC->CR |= RCC_CR_PLL2ON;\r
476     /* Wait till PLL2 is ready */\r
477     while((RCC->CR & RCC_CR_PLL2RDY) == 0)\r
478     {\r
479     }   \r
480 #else    \r
481     /*  PLL configuration:  = (HSE / 2) * 6 = 24 MHz */\r
482     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));\r
483     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLXTPRE_HSE_Div2 | RCC_CFGR_PLLMULL6);\r
484 #endif /* STM32F10X_CL */\r
485 \r
486     /* Enable PLL */\r
487     RCC->CR |= RCC_CR_PLLON;\r
488 \r
489     /* Wait till PLL is ready */\r
490     while((RCC->CR & RCC_CR_PLLRDY) == 0)\r
491     {\r
492     }\r
493 \r
494     /* Select PLL as system clock source */\r
495     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));\r
496     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;    \r
497 \r
498     /* Wait till PLL is used as system clock source */\r
499     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)\r
500     {\r
501     }\r
502   }\r
503   else\r
504   { /* If HSE fails to start-up, the application will have wrong clock \r
505          configuration. User can add here some code to deal with this error */    \r
506 \r
507     /* Go to infinite loop */\r
508     while (1)\r
509     {\r
510     }\r
511   } \r
512 }\r
513 #elif defined SYSCLK_FREQ_36MHz\r
514 /**\r
515   * @brief  Sets System clock frequency to 36MHz and configure HCLK, PCLK2 \r
516   *          and PCLK1 prescalers. \r
517   * @note   This function should be used only after reset.\r
518   * @param  None\r
519   * @retval None\r
520   */\r
521 static void SetSysClockTo36(void)\r
522 {\r
523   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;\r
524   \r
525   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/    \r
526   /* Enable HSE */    \r
527   RCC->CR |= ((uint32_t)RCC_CR_HSEON);\r
528  \r
529   /* Wait till HSE is ready and if Time out is reached exit */\r
530   do\r
531   {\r
532     HSEStatus = RCC->CR & RCC_CR_HSERDY;\r
533     StartUpCounter++;  \r
534   } while((HSEStatus == 0) && (StartUpCounter != HSEStartUp_TimeOut));\r
535 \r
536   if ((RCC->CR & RCC_CR_HSERDY) != RESET)\r
537   {\r
538     HSEStatus = (uint32_t)0x01;\r
539   }\r
540   else\r
541   {\r
542     HSEStatus = (uint32_t)0x00;\r
543   }  \r
544 \r
545   if (HSEStatus == (uint32_t)0x01)\r
546   {\r
547     /* Enable Prefetch Buffer */\r
548     FLASH->ACR |= FLASH_ACR_PRFTBE;\r
549 \r
550     /* Flash 1 wait state */\r
551     FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);\r
552     FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;    \r
553  \r
554     /* HCLK = SYSCLK */\r
555     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;\r
556       \r
557     /* PCLK2 = HCLK */\r
558     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;\r
559     \r
560     /* PCLK1 = HCLK */\r
561     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;\r
562     \r
563 #ifdef STM32F10X_CL\r
564     /* Configure PLLs ------------------------------------------------------*/\r
565     \r
566     /* PLL configuration: PLLCLK = PREDIV1 * 9 = 36 MHz */ \r
567     RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);\r
568     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 | \r
569                             RCC_CFGR_PLLMULL9); \r
570 \r
571         /*!< PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */\r
572     /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 10 = 4 MHz */\r
573         \r
574     RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |\r
575                               RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);\r
576     RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |\r
577                              RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV10);\r
578   \r
579     /* Enable PLL2 */\r
580     RCC->CR |= RCC_CR_PLL2ON;\r
581     /* Wait till PLL2 is ready */\r
582     while((RCC->CR & RCC_CR_PLL2RDY) == 0)\r
583     {\r
584     }\r
585     \r
586 #else    \r
587     /*  PLL configuration: PLLCLK = (HSE / 2) * 9 = 36 MHz */\r
588     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));\r
589     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLXTPRE_HSE_Div2 | RCC_CFGR_PLLMULL9);\r
590 #endif /* STM32F10X_CL */\r
591 \r
592     /* Enable PLL */\r
593     RCC->CR |= RCC_CR_PLLON;\r
594 \r
595     /* Wait till PLL is ready */\r
596     while((RCC->CR & RCC_CR_PLLRDY) == 0)\r
597     {\r
598     }\r
599 \r
600     /* Select PLL as system clock source */\r
601     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));\r
602     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;    \r
603 \r
604     /* Wait till PLL is used as system clock source */\r
605     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)\r
606     {\r
607     }\r
608   }\r
609   else\r
610   { /* If HSE fails to start-up, the application will have wrong clock \r
611          configuration. User can add here some code to deal with this error */    \r
612 \r
613     /* Go to infinite loop */\r
614     while (1)\r
615     {\r
616     }\r
617   } \r
618 }\r
619 #elif defined SYSCLK_FREQ_48MHz\r
620 /**\r
621   * @brief  Sets System clock frequency to 48MHz and configure HCLK, PCLK2 \r
622   *          and PCLK1 prescalers. \r
623   * @note   This function should be used only after reset.\r
624   * @param  None\r
625   * @retval None\r
626   */\r
627 static void SetSysClockTo48(void)\r
628 {\r
629   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;\r
630   \r
631   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/    \r
632   /* Enable HSE */    \r
633   RCC->CR |= ((uint32_t)RCC_CR_HSEON);\r
634  \r
635   /* Wait till HSE is ready and if Time out is reached exit */\r
636   do\r
637   {\r
638     HSEStatus = RCC->CR & RCC_CR_HSERDY;\r
639     StartUpCounter++;  \r
640   } while((HSEStatus == 0) && (StartUpCounter != HSEStartUp_TimeOut));\r
641 \r
642   if ((RCC->CR & RCC_CR_HSERDY) != RESET)\r
643   {\r
644     HSEStatus = (uint32_t)0x01;\r
645   }\r
646   else\r
647   {\r
648     HSEStatus = (uint32_t)0x00;\r
649   }  \r
650 \r
651   if (HSEStatus == (uint32_t)0x01)\r
652   {\r
653     /* Enable Prefetch Buffer */\r
654     FLASH->ACR |= FLASH_ACR_PRFTBE;\r
655 \r
656     /* Flash 1 wait state */\r
657     FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);\r
658     FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;    \r
659  \r
660     /* HCLK = SYSCLK */\r
661     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;\r
662       \r
663     /* PCLK2 = HCLK */\r
664     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;\r
665     \r
666     /* PCLK1 = HCLK */\r
667     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;\r
668     \r
669 #ifdef STM32F10X_CL\r
670     /* Configure PLLs ------------------------------------------------------*/\r
671     /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */\r
672     /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */\r
673         \r
674     RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |\r
675                               RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);\r
676     RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |\r
677                              RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);\r
678   \r
679     /* Enable PLL2 */\r
680     RCC->CR |= RCC_CR_PLL2ON;\r
681     /* Wait till PLL2 is ready */\r
682     while((RCC->CR & RCC_CR_PLL2RDY) == 0)\r
683     {\r
684     }\r
685     \r
686    \r
687     /* PLL configuration: PLLCLK = PREDIV1 * 6 = 48 MHz */ \r
688     RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);\r
689     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 | \r
690                             RCC_CFGR_PLLMULL6); \r
691 #else    \r
692     /*  PLL configuration: PLLCLK = HSE * 6 = 48 MHz */\r
693     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));\r
694     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL6);\r
695 #endif /* STM32F10X_CL */\r
696 \r
697     /* Enable PLL */\r
698     RCC->CR |= RCC_CR_PLLON;\r
699 \r
700     /* Wait till PLL is ready */\r
701     while((RCC->CR & RCC_CR_PLLRDY) == 0)\r
702     {\r
703     }\r
704 \r
705     /* Select PLL as system clock source */\r
706     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));\r
707     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;    \r
708 \r
709     /* Wait till PLL is used as system clock source */\r
710     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)\r
711     {\r
712     }\r
713   }\r
714   else\r
715   { /* If HSE fails to start-up, the application will have wrong clock \r
716          configuration. User can add here some code to deal with this error */    \r
717 \r
718     /* Go to infinite loop */\r
719     while (1)\r
720     {\r
721     }\r
722   } \r
723 }\r
724 \r
725 #elif defined SYSCLK_FREQ_56MHz\r
726 /**\r
727   * @brief  Sets System clock frequency to 56MHz and configure HCLK, PCLK2 \r
728   *          and PCLK1 prescalers. \r
729   * @note   This function should be used only after reset.\r
730   * @param  None\r
731   * @retval None\r
732   */\r
733 static void SetSysClockTo56(void)\r
734 {\r
735   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;\r
736   \r
737   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/   \r
738   /* Enable HSE */    \r
739   RCC->CR |= ((uint32_t)RCC_CR_HSEON);\r
740  \r
741   /* Wait till HSE is ready and if Time out is reached exit */\r
742   do\r
743   {\r
744     HSEStatus = RCC->CR & RCC_CR_HSERDY;\r
745     StartUpCounter++;  \r
746   } while((HSEStatus == 0) && (StartUpCounter != HSEStartUp_TimeOut));\r
747 \r
748   if ((RCC->CR & RCC_CR_HSERDY) != RESET)\r
749   {\r
750     HSEStatus = (uint32_t)0x01;\r
751   }\r
752   else\r
753   {\r
754     HSEStatus = (uint32_t)0x00;\r
755   }  \r
756 \r
757   if (HSEStatus == (uint32_t)0x01)\r
758   {\r
759     /* Enable Prefetch Buffer */\r
760     FLASH->ACR |= FLASH_ACR_PRFTBE;\r
761 \r
762     /* Flash 1 wait state */\r
763     FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);\r
764     FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;    \r
765  \r
766     /* HCLK = SYSCLK */\r
767     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;\r
768       \r
769     /* PCLK2 = HCLK */\r
770     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;\r
771     \r
772     /* PCLK1 = HCLK */\r
773     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;\r
774 \r
775 #ifdef STM32F10X_CL\r
776     /* Configure PLLs ------------------------------------------------------*/\r
777     /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */\r
778     /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */\r
779         \r
780     RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |\r
781                               RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);\r
782     RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |\r
783                              RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);\r
784   \r
785     /* Enable PLL2 */\r
786     RCC->CR |= RCC_CR_PLL2ON;\r
787     /* Wait till PLL2 is ready */\r
788     while((RCC->CR & RCC_CR_PLL2RDY) == 0)\r
789     {\r
790     }\r
791     \r
792    \r
793     /* PLL configuration: PLLCLK = PREDIV1 * 7 = 56 MHz */ \r
794     RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);\r
795     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 | \r
796                             RCC_CFGR_PLLMULL7); \r
797 #else     \r
798     /* PLL configuration: PLLCLK = HSE * 7 = 56 MHz */\r
799     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));\r
800     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL7);\r
801 \r
802 #endif /* STM32F10X_CL */\r
803 \r
804     /* Enable PLL */\r
805     RCC->CR |= RCC_CR_PLLON;\r
806 \r
807     /* Wait till PLL is ready */\r
808     while((RCC->CR & RCC_CR_PLLRDY) == 0)\r
809     {\r
810     }\r
811 \r
812     /* Select PLL as system clock source */\r
813     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));\r
814     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;    \r
815 \r
816     /* Wait till PLL is used as system clock source */\r
817     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)\r
818     {\r
819     }\r
820   }\r
821   else\r
822   { /* If HSE fails to start-up, the application will have wrong clock \r
823          configuration. User can add here some code to deal with this error */    \r
824 \r
825     /* Go to infinite loop */\r
826     while (1)\r
827     {\r
828     }\r
829   } \r
830 }\r
831 \r
832 #elif defined SYSCLK_FREQ_72MHz\r
833 /**\r
834   * @brief  Sets System clock frequency to 72MHz and configure HCLK, PCLK2 \r
835   *          and PCLK1 prescalers. \r
836   * @note   This function should be used only after reset.\r
837   * @param  None\r
838   * @retval None\r
839   */\r
840 static void SetSysClockTo72(void)\r
841 {\r
842   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;\r
843   \r
844   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/    \r
845   /* Enable HSE */    \r
846   RCC->CR |= ((uint32_t)RCC_CR_HSEON);\r
847  \r
848   /* Wait till HSE is ready and if Time out is reached exit */\r
849   do\r
850   {\r
851     HSEStatus = RCC->CR & RCC_CR_HSERDY;\r
852     StartUpCounter++;  \r
853   } while((HSEStatus == 0) && (StartUpCounter != HSEStartUp_TimeOut));\r
854 \r
855   if ((RCC->CR & RCC_CR_HSERDY) != RESET)\r
856   {\r
857     HSEStatus = (uint32_t)0x01;\r
858   }\r
859   else\r
860   {\r
861     HSEStatus = (uint32_t)0x00;\r
862   }  \r
863 \r
864   if (HSEStatus == (uint32_t)0x01)\r
865   {\r
866     /* Enable Prefetch Buffer */\r
867     FLASH->ACR |= FLASH_ACR_PRFTBE;\r
868 \r
869     /* Flash 2 wait state */\r
870     FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);\r
871     FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;    \r
872 \r
873  \r
874     /* HCLK = SYSCLK */\r
875     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;\r
876       \r
877     /* PCLK2 = HCLK */\r
878     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;\r
879     \r
880     /* PCLK1 = HCLK */\r
881     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;\r
882 \r
883 #ifdef STM32F10X_CL\r
884     /* Configure PLLs ------------------------------------------------------*/\r
885     /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */\r
886     /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */\r
887         \r
888     RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |\r
889                               RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);\r
890     RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |\r
891                              RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);\r
892   \r
893     /* Enable PLL2 */\r
894     RCC->CR |= RCC_CR_PLL2ON;\r
895     /* Wait till PLL2 is ready */\r
896     while((RCC->CR & RCC_CR_PLL2RDY) == 0)\r
897     {\r
898     }\r
899     \r
900    \r
901     /* PLL configuration: PLLCLK = PREDIV1 * 9 = 72 MHz */ \r
902     RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);\r
903     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 | \r
904                             RCC_CFGR_PLLMULL9); \r
905 #else    \r
906     /*  PLL configuration: PLLCLK = HSE * 9 = 72 MHz */\r
907     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE |\r
908                                         RCC_CFGR_PLLMULL));\r
909     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL9);\r
910 #endif /* STM32F10X_CL */\r
911 \r
912     /* Enable PLL */\r
913     RCC->CR |= RCC_CR_PLLON;\r
914 \r
915     /* Wait till PLL is ready */\r
916     while((RCC->CR & RCC_CR_PLLRDY) == 0)\r
917     {\r
918     }\r
919     \r
920     /* Select PLL as system clock source */\r
921     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));\r
922     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;    \r
923 \r
924     /* Wait till PLL is used as system clock source */\r
925     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)\r
926     {\r
927     }\r
928   }\r
929   else\r
930   { /* If HSE fails to start-up, the application will have wrong clock \r
931          configuration. User can add here some code to deal with this error */    \r
932 \r
933     /* Go to infinite loop */\r
934     while (1)\r
935     {\r
936     }\r
937   }\r
938 }\r
939 #endif\r
940 \r
941 /**\r
942   * @}\r
943   */\r
944 \r
945 /**\r
946   * @}\r
947   */\r
948   \r
949 /**\r
950   * @}\r
951   */    \r
952 /******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/\r