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