Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Space_Invaders_Demo neopixels gpio_test_stm32f3_discovery gpio_test_systimer ... more
system_stm32f30x.c
00001 /** 00002 ****************************************************************************** 00003 * @file system_stm32f30x.c 00004 * @author MCD Application Team 00005 * @version V1.2.2 00006 * @date 27-February-2015 00007 * @brief CMSIS Cortex-M4 Device Peripheral Access Layer System Source File. 00008 * This file contains the system clock configuration for STM32F30x devices, 00009 * and is generated by the clock configuration tool 00010 * stm32f30x_Clock_Configuration_V1.0.0.xls 00011 * 00012 * 1. This file provides two functions and one global variable to be called from 00013 * user application: 00014 * - SystemInit(): Setups the system clock (System clock source, PLL Multiplier 00015 * and Divider factors, AHB/APBx prescalers and Flash settings), 00016 * depending on the configuration made in the clock xls tool. 00017 * This function is called at startup just after reset and 00018 * before branch to main program. This call is made inside 00019 * the "startup_stm32f30x.s" file. 00020 * 00021 * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used 00022 * by the user application to setup the SysTick 00023 * timer or configure other parameters. 00024 * 00025 * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must 00026 * be called whenever the core clock is changed 00027 * during program execution. 00028 * 00029 * 2. After each device reset the HSI (8 MHz) is used as system clock source. 00030 * Then SystemInit() function is called, in "startup_stm32f30x.s" file, to 00031 * configure the system clock before to branch to main program. 00032 * 00033 * 3. If the system clock source selected by user fails to startup, the SystemInit() 00034 * function will do nothing and HSI still used as system clock source. User can 00035 * add some code to deal with this issue inside the SetSysClock() function. 00036 * 00037 * 4. The default value of HSE crystal is set to 8MHz, refer to "HSE_VALUE" define 00038 * in "stm32f30x.h" file. When HSE is used as system clock source, directly or 00039 * through PLL, and you are using different crystal you have to adapt the HSE 00040 * value to your own configuration. 00041 * 00042 * 5. This file configures the system clock as follows: 00043 *============================================================================= 00044 * Supported STM32F30x device 00045 *----------------------------------------------------------------------------- 00046 * System Clock source | PLL (HSE) 00047 *----------------------------------------------------------------------------- 00048 * SYSCLK(Hz) | 72000000 00049 *----------------------------------------------------------------------------- 00050 * HCLK(Hz) | 72000000 00051 *----------------------------------------------------------------------------- 00052 * AHB Prescaler | 1 00053 *----------------------------------------------------------------------------- 00054 * APB2 Prescaler | 1 00055 *----------------------------------------------------------------------------- 00056 * APB1 Prescaler | 2 00057 *----------------------------------------------------------------------------- 00058 * HSE Frequency(Hz) | 8000000 00059 *---------------------------------------------------------------------------- 00060 * PLLMUL | 9 00061 *----------------------------------------------------------------------------- 00062 * PREDIV | 1 00063 *----------------------------------------------------------------------------- 00064 * USB Clock | DISABLE 00065 *----------------------------------------------------------------------------- 00066 * Flash Latency(WS) | 2 00067 *----------------------------------------------------------------------------- 00068 * Prefetch Buffer | ON 00069 *----------------------------------------------------------------------------- 00070 *============================================================================= 00071 ****************************************************************************** 00072 * @attention 00073 * 00074 * <h2><center>© COPYRIGHT 2015 STMicroelectronics</center></h2> 00075 * 00076 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 00077 * You may not use this file except in compliance with the License. 00078 * You may obtain a copy of the License at: 00079 * 00080 * http://www.st.com/software_license_agreement_liberty_v2 00081 * 00082 * Unless required by applicable law or agreed to in writing, software 00083 * distributed under the License is distributed on an "AS IS" BASIS, 00084 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00085 * See the License for the specific language governing permissions and 00086 * limitations under the License. 00087 * 00088 ****************************************************************************** 00089 */ 00090 /** @addtogroup CMSIS 00091 * @{ 00092 */ 00093 00094 /** @addtogroup stm32f30x_system 00095 * @{ 00096 */ 00097 00098 /** @addtogroup STM32F30x_System_Private_Includes 00099 * @{ 00100 */ 00101 00102 #include "stm32f30x.h" 00103 00104 /** 00105 * @} 00106 */ 00107 00108 /* Private typedef -----------------------------------------------------------*/ 00109 00110 /** @addtogroup STM32F30x_System_Private_Defines 00111 * @{ 00112 */ 00113 /*!< Uncomment the following line if you need to relocate your vector Table in 00114 Internal SRAM. */ 00115 /* #define VECT_TAB_SRAM */ 00116 #define VECT_TAB_OFFSET 0x0 /*!< Vector Table base offset field. 00117 This value must be a multiple of 0x200. */ 00118 /** 00119 * @} 00120 */ 00121 00122 /* Private macro -------------------------------------------------------------*/ 00123 00124 /** @addtogroup STM32F30x_System_Private_Variables 00125 * @{ 00126 */ 00127 00128 uint32_t SystemCoreClock = 72000000; 00129 00130 __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9}; 00131 00132 /** 00133 * @} 00134 */ 00135 00136 /** @addtogroup STM32F30x_System_Private_FunctionPrototypes 00137 * @{ 00138 */ 00139 00140 static void SetSysClock(void); 00141 00142 /** 00143 * @} 00144 */ 00145 00146 /** @addtogroup STM32F30x_System_Private_Functions 00147 * @{ 00148 */ 00149 00150 /** 00151 * @brief Setup the microcontroller system 00152 * Initialize the Embedded Flash Interface, the PLL and update the 00153 * SystemFrequency variable. 00154 * @param None 00155 * @retval None 00156 */ 00157 void SystemInit(void) 00158 { 00159 /* FPU settings ------------------------------------------------------------*/ 00160 #if (__FPU_PRESENT == 1) && (__FPU_USED == 1) 00161 SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */ 00162 #endif 00163 00164 /* Reset the RCC clock configuration to the default reset state ------------*/ 00165 /* Set HSION bit */ 00166 RCC->CR |= (uint32_t)0x00000001; 00167 00168 /* Reset CFGR register */ 00169 RCC->CFGR &= 0xF87FC00C; 00170 00171 /* Reset HSEON, CSSON and PLLON bits */ 00172 RCC->CR &= (uint32_t)0xFEF6FFFF; 00173 00174 /* Reset HSEBYP bit */ 00175 RCC->CR &= (uint32_t)0xFFFBFFFF; 00176 00177 /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE bits */ 00178 RCC->CFGR &= (uint32_t)0xFF80FFFF; 00179 00180 /* Reset PREDIV1[3:0] bits */ 00181 RCC->CFGR2 &= (uint32_t)0xFFFFFFF0; 00182 00183 /* Reset USARTSW[1:0], I2CSW and TIMs bits */ 00184 RCC->CFGR3 &= (uint32_t)0xFF00FCCC; 00185 00186 /* Disable all interrupts */ 00187 RCC->CIR = 0x00000000; 00188 00189 /* Configure the System clock source, PLL Multiplier and Divider factors, 00190 AHB/APBx prescalers and Flash settings ----------------------------------*/ 00191 SetSysClock(); 00192 00193 #ifdef VECT_TAB_SRAM 00194 SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */ 00195 #else 00196 SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */ 00197 #endif 00198 } 00199 00200 struct __FILE { int handle; }; 00201 00202 typedef struct __FILE FILE; 00203 00204 FILE __stdout; 00205 FILE __stdin; 00206 00207 static int serial_init_done=0; 00208 00209 static void serial_init() { 00210 if(!serial_init_done) { 00211 serial_init_done=1; 00212 RCC->AHBENR |= RCC_AHBPeriph_GPIOC; 00213 RCC->APB2ENR |= RCC_APB2Periph_USART1; 00214 GPIOC->AFR[0] = (GPIOC->AFR[0] & 0xff00ffff) | 0x770000; // alt funcs for GPIOC4 and 5 are 7 00215 GPIOC->OSPEEDR |= 0xf00; // speed high 00216 GPIOC->OTYPER &= ~0x30; // output type pp 00217 GPIOC->MODER = (GPIOC->MODER & ~0xf00) | 0xa00; // mode af 00218 GPIOC->PUPDR = (GPIOC->PUPDR & ~0xf00) | 0x500; // pull up 00219 USART1->CR1 &= ~USART_CR1_UE; // disable so we can change settings 00220 USART1->CR2 = (USART1->CR2 & ~USART_CR2_STOP) | USART_StopBits_1; 00221 USART1->CR1 = (USART1->CR1 & 0xFFFFE6F3) | USART_WordLength_8b | USART_Parity_No | USART_Mode_Rx | USART_Mode_Tx; 00222 USART1->CR3 = (USART1->CR3 & 0xFFFFFCFF) | USART_HardwareFlowControl_None; 00223 USART1->BRR = SystemCoreClock/115200; // set baud rate 00224 USART1->CR1 |= USART_CR1_UE; // enable serial port 00225 } 00226 } 00227 00228 int fputc(int ch, FILE *f) { 00229 serial_init(); 00230 USART1->TDR = ch & 0x1ff; 00231 while (!(USART1->ISR & USART_FLAG_TXE)); 00232 return ch; 00233 } 00234 00235 int fgetc(FILE *f) { 00236 serial_init(); 00237 if(USART1->ISR & USART_FLAG_ORE) 00238 USART1->ISR &= ~USART_FLAG_ORE; 00239 while(!(USART1->ISR & USART_FLAG_RXNE)); 00240 return USART1->RDR & 0xff; 00241 } 00242 00243 /** 00244 * @brief Update SystemCoreClock variable according to Clock Register Values. 00245 * The SystemCoreClock variable contains the core clock (HCLK), it can 00246 * be used by the user application to setup the SysTick timer or configure 00247 * other parameters. 00248 * 00249 * @note Each time the core clock (HCLK) changes, this function must be called 00250 * to update SystemCoreClock variable value. Otherwise, any configuration 00251 * based on this variable will be incorrect. 00252 * 00253 * @note - The system frequency computed by this function is not the real 00254 * frequency in the chip. It is calculated based on the predefined 00255 * constant and the selected clock source: 00256 * 00257 * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*) 00258 * 00259 * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**) 00260 * 00261 * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**) 00262 * or HSI_VALUE(*) multiplied/divided by the PLL factors. 00263 * 00264 * (*) HSI_VALUE is a constant defined in stm32f30x.h file (default value 00265 * 8 MHz) but the real value may vary depending on the variations 00266 * in voltage and temperature. 00267 * 00268 * (**) HSE_VALUE is a constant defined in stm32f30x.h file (default value 00269 * 8 MHz), user has to ensure that HSE_VALUE is same as the real 00270 * frequency of the crystal used. Otherwise, this function may 00271 * have wrong result. 00272 * 00273 * - The result of this function could be not correct when using fractional 00274 * value for HSE crystal. 00275 * 00276 * @param None 00277 * @retval None 00278 */ 00279 void SystemCoreClockUpdate (void) 00280 { 00281 uint32_t tmp = 0, pllmull = 0, pllsource = 0, prediv1factor = 0; 00282 00283 /* Get SYSCLK source -------------------------------------------------------*/ 00284 tmp = RCC->CFGR & RCC_CFGR_SWS; 00285 00286 switch (tmp) 00287 { 00288 case 0x00: /* HSI used as system clock */ 00289 SystemCoreClock = HSI_VALUE; 00290 break; 00291 case 0x04: /* HSE used as system clock */ 00292 SystemCoreClock = HSE_VALUE; 00293 break; 00294 case 0x08: /* PLL used as system clock */ 00295 /* Get PLL clock source and multiplication factor ----------------------*/ 00296 pllmull = RCC->CFGR & RCC_CFGR_PLLMULL; 00297 pllsource = RCC->CFGR & RCC_CFGR_PLLSRC; 00298 pllmull = ( pllmull >> 18) + 2; 00299 00300 if (pllsource == 0x00) 00301 { 00302 /* HSI oscillator clock divided by 2 selected as PLL clock entry */ 00303 SystemCoreClock = (HSI_VALUE >> 1) * pllmull; 00304 } 00305 else 00306 { 00307 prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1; 00308 /* HSE oscillator clock selected as PREDIV1 clock entry */ 00309 SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull; 00310 } 00311 break; 00312 default: /* HSI used as system clock */ 00313 SystemCoreClock = HSI_VALUE; 00314 break; 00315 } 00316 /* Compute HCLK clock frequency ----------------*/ 00317 /* Get HCLK prescaler */ 00318 tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)]; 00319 /* HCLK clock frequency */ 00320 SystemCoreClock >>= tmp; 00321 } 00322 00323 /** 00324 * @brief Configures the System clock source, PLL Multiplier and Divider factors, 00325 * AHB/APBx prescalers and Flash settings 00326 * @note This function should be called only once the RCC clock configuration 00327 * is reset to the default reset state (done in SystemInit() function). 00328 * @param None 00329 * @retval None 00330 */ 00331 static void SetSysClock(void) 00332 { 00333 __IO uint32_t StartUpCounter = 0, HSEStatus = 0; 00334 00335 /******************************************************************************/ 00336 /* PLL (clocked by HSE) used as System clock source */ 00337 /******************************************************************************/ 00338 00339 /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration -----------*/ 00340 /* Enable HSE */ 00341 RCC->CR |= ((uint32_t)RCC_CR_HSEON); 00342 00343 /* Wait till HSE is ready and if Time out is reached exit */ 00344 do 00345 { 00346 HSEStatus = RCC->CR & RCC_CR_HSERDY; 00347 StartUpCounter++; 00348 } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT)); 00349 00350 if ((RCC->CR & RCC_CR_HSERDY) != RESET) 00351 { 00352 HSEStatus = (uint32_t)0x01; 00353 } 00354 else 00355 { 00356 HSEStatus = (uint32_t)0x00; 00357 } 00358 00359 if (HSEStatus == (uint32_t)0x01) 00360 { 00361 /* Enable Prefetch Buffer and set Flash Latency */ 00362 FLASH->ACR = FLASH_ACR_PRFTBE | (uint32_t)FLASH_ACR_LATENCY_1; 00363 00364 /* HCLK = SYSCLK / 1 */ 00365 RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1; 00366 00367 /* PCLK2 = HCLK / 1 */ 00368 RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1; 00369 00370 /* PCLK1 = HCLK / 2 */ 00371 RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2; 00372 00373 /* PLL configuration */ 00374 RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL)); 00375 RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLMULL9); 00376 00377 /* Enable PLL */ 00378 RCC->CR |= RCC_CR_PLLON; 00379 00380 /* Wait till PLL is ready */ 00381 while((RCC->CR & RCC_CR_PLLRDY) == 0) 00382 { 00383 } 00384 00385 /* Select PLL as system clock source */ 00386 RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW)); 00387 RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL; 00388 00389 /* Wait till PLL is used as system clock source */ 00390 while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_PLL) 00391 { 00392 } 00393 } 00394 else 00395 { /* If HSE fails to start-up, the application will have wrong clock 00396 configuration. User can add here some code to deal with this error */ 00397 } 00398 } 00399 00400 /** 00401 * @} 00402 */ 00403 00404 /** 00405 * @} 00406 */ 00407 00408 /** 00409 * @} 00410 */ 00411 00412 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 00413
Generated on Tue Jul 12 2022 17:34:45 by
 1.7.2
 1.7.2