1 /* -------------------------------- Arctic Core ------------------------------
2 * Arctic Core - the open source AUTOSAR platform http://arccore.com
4 * Copyright (C) 2009 ArcCore AB <contact@arccore.com>
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>.
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
14 * -------------------------------- Arctic Core ------------------------------*/
24 ******************************************************************************
\r
25 * @file system_stm32f10x.c
\r
26 * @author MCD Application Team
\r
29 * @brief CMSIS Cortex-M3 Device Peripheral Access Layer System Source File.
\r
30 ******************************************************************************
\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
39 * <h2><center>© COPYRIGHT 2009 STMicroelectronics</center></h2>
\r
40 ******************************************************************************
\r
43 /** @addtogroup CMSIS
\r
47 /** @addtogroup stm32f10x_system
\r
51 /** @addtogroup STM32F10x_System_Private_Includes
\r
55 #include "stm32f10x.h"
\r
61 /** @addtogroup STM32F10x_System_Private_TypesDefinitions
\r
69 /** @addtogroup STM32F10x_System_Private_Defines
\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
78 1. After each device reset the HSI is used as System clock source.
\r
80 2. Please make sure that the selected System clock doesn't exceed your device's
\r
83 3. If none of the define below is enabled, the HSI is used as System clock
\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
91 If you are using different crystal you have to adapt those functions accordingly.
\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
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
111 /** @addtogroup STM32F10x_System_Private_Macros
\r
119 /** @addtogroup STM32F10x_System_Private_Variables
\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
174 /** @addtogroup STM32F10x_System_Private_FunctionPrototypes
\r
178 static void SetSysClock(void);
\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
198 /** @addtogroup STM32F10x_System_Private_Functions
\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
209 void SystemInit (void)
\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
215 /* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */
\r
216 #ifndef STM32F10X_CL
\r
217 RCC->CFGR &= (uint32_t)0xF8FF0000;
\r
219 RCC->CFGR &= (uint32_t)0xF0FF0000;
\r
220 #endif /* STM32F10X_CL */
\r
222 /* Reset HSEON, CSSON and PLLON bits */
\r
223 RCC->CR &= (uint32_t)0xFEF6FFFF;
\r
225 /* Reset HSEBYP bit */
\r
226 RCC->CR &= (uint32_t)0xFFFBFFFF;
\r
228 /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */
\r
229 RCC->CFGR &= (uint32_t)0xFF80FFFF;
\r
231 #ifndef STM32F10X_CL
\r
232 /* Disable all interrupts and clear pending bits */
\r
233 RCC->CIR = 0x009F0000;
\r
235 /* Reset PLL2ON and PLL3ON bits */
\r
236 RCC->CR &= (uint32_t)0xEBFFFFFF;
\r
238 /* Disable all interrupts and clear pending bits */
\r
239 RCC->CIR = 0x00FF0000;
\r
241 /* Reset CFGR2 register */
\r
242 RCC->CFGR2 = 0x00000000;
\r
243 #endif /* STM32F10X_CL */
\r
245 /* Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers */
\r
246 /* Configure the Flash Latency cycles and enable prefetch buffer */
\r
252 * @brief Configures the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers.
\r
256 static void SetSysClock(void)
\r
258 #ifdef SYSCLK_FREQ_HSE
\r
259 SetSysClockToHSE();
\r
260 #elif defined SYSCLK_FREQ_24MHz
\r
262 #elif defined SYSCLK_FREQ_36MHz
\r
264 #elif defined SYSCLK_FREQ_48MHz
\r
266 #elif defined SYSCLK_FREQ_56MHz
\r
267 SetSysClockTo56();
\r
268 #elif defined SYSCLK_FREQ_72MHz
\r
272 /* If none of the define above is enabled, the HSI is used as System clock
\r
273 source (default after reset) */
\r
277 * @brief Setup the external memory controller. Called in startup_stm32f10x.s
\r
278 * before jump to __main
\r
282 #ifdef DATA_IN_ExtSRAM
\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
292 void SystemInit_ExtMemCtl(void)
\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
297 /* Enable FSMC clock */
\r
298 RCC->AHBENR = 0x00000114;
\r
300 /* Enable GPIOD, GPIOE, GPIOF and GPIOG clocks */
\r
301 RCC->APB2ENR = 0x000001E0;
\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
309 GPIOD->CRL = 0x44BB44BB;
\r
310 GPIOD->CRH = 0xBBBBBBBB;
\r
312 GPIOE->CRL = 0xB44444BB;
\r
313 GPIOE->CRH = 0xBBBBBBBB;
\r
315 GPIOF->CRL = 0x44BBBBBB;
\r
316 GPIOF->CRH = 0xBBBB4444;
\r
318 GPIOG->CRL = 0x44BBBBBB;
\r
319 GPIOG->CRH = 0x44444B44;
\r
321 /*---------------- FSMC Configuration ---------------------------------------*/
\r
322 /*---------------- Enable FSMC Bank1_SRAM Bank ------------------------------*/
\r
324 FSMC_Bank1->BTCR[4] = 0x00001011;
\r
325 FSMC_Bank1->BTCR[5] = 0x00000200;
\r
327 #endif /* DATA_IN_ExtSRAM */
\r
329 #ifdef SYSCLK_FREQ_HSE
\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
337 static void SetSysClockToHSE(void)
\r
339 __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
\r
341 /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
\r
343 RCC->CR |= ((uint32_t)RCC_CR_HSEON);
\r
345 /* Wait till HSE is ready and if Time out is reached exit */
\r
348 HSEStatus = RCC->CR & RCC_CR_HSERDY;
\r
350 } while((HSEStatus == 0) && (StartUpCounter != HSEStartUp_TimeOut));
\r
352 if ((RCC->CR & RCC_CR_HSERDY) != RESET)
\r
354 HSEStatus = (uint32_t)0x01;
\r
358 HSEStatus = (uint32_t)0x00;
\r
361 if (HSEStatus == (uint32_t)0x01)
\r
363 /* Enable Prefetch Buffer */
\r
364 FLASH->ACR |= FLASH_ACR_PRFTBE;
\r
366 /* Flash 0 wait state */
\r
367 FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
\r
369 #ifndef STM32F10X_CL
\r
370 FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_0;
\r
372 if (HSE_Value <= 24000000)
\r
374 FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_0;
\r
378 FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;
\r
380 #endif /* STM32F10X_CL */
\r
382 /* HCLK = SYSCLK */
\r
383 RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
\r
386 RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
\r
389 RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
\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
395 /* Wait till HSE is used as system clock source */
\r
396 while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x04)
\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
404 /* Go to infinite loop */
\r
410 #elif defined SYSCLK_FREQ_24MHz
\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
418 static void SetSysClockTo24(void)
\r
420 __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
\r
422 /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
\r
424 RCC->CR |= ((uint32_t)RCC_CR_HSEON);
\r
426 /* Wait till HSE is ready and if Time out is reached exit */
\r
429 HSEStatus = RCC->CR & RCC_CR_HSERDY;
\r
431 } while((HSEStatus == 0) && (StartUpCounter != HSEStartUp_TimeOut));
\r
433 if ((RCC->CR & RCC_CR_HSERDY) != RESET)
\r
435 HSEStatus = (uint32_t)0x01;
\r
439 HSEStatus = (uint32_t)0x00;
\r
442 if (HSEStatus == (uint32_t)0x01)
\r
444 /* Enable Prefetch Buffer */
\r
445 FLASH->ACR |= FLASH_ACR_PRFTBE;
\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
451 /* HCLK = SYSCLK */
\r
452 RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
\r
455 RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
\r
458 RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
\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
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
475 RCC->CR |= RCC_CR_PLL2ON;
\r
476 /* Wait till PLL2 is ready */
\r
477 while((RCC->CR & RCC_CR_PLL2RDY) == 0)
\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
487 RCC->CR |= RCC_CR_PLLON;
\r
489 /* Wait till PLL is ready */
\r
490 while((RCC->CR & RCC_CR_PLLRDY) == 0)
\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
498 /* Wait till PLL is used as system clock source */
\r
499 while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
\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
507 /* Go to infinite loop */
\r
513 #elif defined SYSCLK_FREQ_36MHz
\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
521 static void SetSysClockTo36(void)
\r
523 __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
\r
525 /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
\r
527 RCC->CR |= ((uint32_t)RCC_CR_HSEON);
\r
529 /* Wait till HSE is ready and if Time out is reached exit */
\r
532 HSEStatus = RCC->CR & RCC_CR_HSERDY;
\r
534 } while((HSEStatus == 0) && (StartUpCounter != HSEStartUp_TimeOut));
\r
536 if ((RCC->CR & RCC_CR_HSERDY) != RESET)
\r
538 HSEStatus = (uint32_t)0x01;
\r
542 HSEStatus = (uint32_t)0x00;
\r
545 if (HSEStatus == (uint32_t)0x01)
\r
547 /* Enable Prefetch Buffer */
\r
548 FLASH->ACR |= FLASH_ACR_PRFTBE;
\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
554 /* HCLK = SYSCLK */
\r
555 RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
\r
558 RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
\r
561 RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
\r
563 #ifdef STM32F10X_CL
\r
564 /* Configure PLLs ------------------------------------------------------*/
\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
571 /*!< PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
\r
572 /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 10 = 4 MHz */
\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
580 RCC->CR |= RCC_CR_PLL2ON;
\r
581 /* Wait till PLL2 is ready */
\r
582 while((RCC->CR & RCC_CR_PLL2RDY) == 0)
\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
593 RCC->CR |= RCC_CR_PLLON;
\r
595 /* Wait till PLL is ready */
\r
596 while((RCC->CR & RCC_CR_PLLRDY) == 0)
\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
604 /* Wait till PLL is used as system clock source */
\r
605 while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
\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
613 /* Go to infinite loop */
\r
619 #elif defined SYSCLK_FREQ_48MHz
\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
627 static void SetSysClockTo48(void)
\r
629 __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
\r
631 /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
\r
633 RCC->CR |= ((uint32_t)RCC_CR_HSEON);
\r
635 /* Wait till HSE is ready and if Time out is reached exit */
\r
638 HSEStatus = RCC->CR & RCC_CR_HSERDY;
\r
640 } while((HSEStatus == 0) && (StartUpCounter != HSEStartUp_TimeOut));
\r
642 if ((RCC->CR & RCC_CR_HSERDY) != RESET)
\r
644 HSEStatus = (uint32_t)0x01;
\r
648 HSEStatus = (uint32_t)0x00;
\r
651 if (HSEStatus == (uint32_t)0x01)
\r
653 /* Enable Prefetch Buffer */
\r
654 FLASH->ACR |= FLASH_ACR_PRFTBE;
\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
660 /* HCLK = SYSCLK */
\r
661 RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
\r
664 RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
\r
667 RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
\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
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
680 RCC->CR |= RCC_CR_PLL2ON;
\r
681 /* Wait till PLL2 is ready */
\r
682 while((RCC->CR & RCC_CR_PLL2RDY) == 0)
\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
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
698 RCC->CR |= RCC_CR_PLLON;
\r
700 /* Wait till PLL is ready */
\r
701 while((RCC->CR & RCC_CR_PLLRDY) == 0)
\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
709 /* Wait till PLL is used as system clock source */
\r
710 while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
\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
718 /* Go to infinite loop */
\r
725 #elif defined SYSCLK_FREQ_56MHz
\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
733 static void SetSysClockTo56(void)
\r
735 __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
\r
737 /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
\r
739 RCC->CR |= ((uint32_t)RCC_CR_HSEON);
\r
741 /* Wait till HSE is ready and if Time out is reached exit */
\r
744 HSEStatus = RCC->CR & RCC_CR_HSERDY;
\r
746 } while((HSEStatus == 0) && (StartUpCounter != HSEStartUp_TimeOut));
\r
748 if ((RCC->CR & RCC_CR_HSERDY) != RESET)
\r
750 HSEStatus = (uint32_t)0x01;
\r
754 HSEStatus = (uint32_t)0x00;
\r
757 if (HSEStatus == (uint32_t)0x01)
\r
759 /* Enable Prefetch Buffer */
\r
760 FLASH->ACR |= FLASH_ACR_PRFTBE;
\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
766 /* HCLK = SYSCLK */
\r
767 RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
\r
770 RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
\r
773 RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
\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
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
786 RCC->CR |= RCC_CR_PLL2ON;
\r
787 /* Wait till PLL2 is ready */
\r
788 while((RCC->CR & RCC_CR_PLL2RDY) == 0)
\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
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
802 #endif /* STM32F10X_CL */
\r
805 RCC->CR |= RCC_CR_PLLON;
\r
807 /* Wait till PLL is ready */
\r
808 while((RCC->CR & RCC_CR_PLLRDY) == 0)
\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
816 /* Wait till PLL is used as system clock source */
\r
817 while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
\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
825 /* Go to infinite loop */
\r
832 #elif defined SYSCLK_FREQ_72MHz
\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
840 static void SetSysClockTo72(void)
\r
842 __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
\r
844 /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
\r
846 RCC->CR |= ((uint32_t)RCC_CR_HSEON);
\r
848 /* Wait till HSE is ready and if Time out is reached exit */
\r
851 HSEStatus = RCC->CR & RCC_CR_HSERDY;
\r
853 } while((HSEStatus == 0) && (StartUpCounter != HSEStartUp_TimeOut));
\r
855 if ((RCC->CR & RCC_CR_HSERDY) != RESET)
\r
857 HSEStatus = (uint32_t)0x01;
\r
861 HSEStatus = (uint32_t)0x00;
\r
864 if (HSEStatus == (uint32_t)0x01)
\r
866 /* Enable Prefetch Buffer */
\r
867 FLASH->ACR |= FLASH_ACR_PRFTBE;
\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
874 /* HCLK = SYSCLK */
\r
875 RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
\r
878 RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
\r
881 RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
\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
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
894 RCC->CR |= RCC_CR_PLL2ON;
\r
895 /* Wait till PLL2 is ready */
\r
896 while((RCC->CR & RCC_CR_PLL2RDY) == 0)
\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
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
913 RCC->CR |= RCC_CR_PLLON;
\r
915 /* Wait till PLL is ready */
\r
916 while((RCC->CR & RCC_CR_PLLRDY) == 0)
\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
924 /* Wait till PLL is used as system clock source */
\r
925 while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
\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
933 /* Go to infinite loop */
\r
952 /******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
\r