102 #include "stm32l1xx.h"
123 #define VECT_TAB_OFFSET 0x0
140 uint32_t SystemCoreClock = 32000000;
141 __I uint8_t PLLMulTable[9] = {3, 4, 6, 8, 12, 16, 24, 32, 48};
142 __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
152 static void SetSysClock(
void);
172 RCC->CR |= (uint32_t)0x00000100;
175 RCC->CFGR &= (uint32_t)0x88FFC00C;
178 RCC->CR &= (uint32_t)0xEEFEFFFE;
181 RCC->CR &= (uint32_t)0xFFFBFFFF;
184 RCC->CFGR &= (uint32_t)0xFF02FFFF;
187 RCC->CIR = 0x00000000;
239 uint32_t tmp = 0, pllmul = 0, plldiv = 0, pllsource = 0, msirange = 0;
242 tmp = RCC->CFGR & RCC_CFGR_SWS;
247 msirange = (RCC->ICSCR & RCC_ICSCR_MSIRANGE) >> 13;
248 SystemCoreClock = (32768 * (1 << (msirange + 1)));
251 SystemCoreClock = HSI_VALUE;
254 SystemCoreClock = HSE_VALUE;
258 pllmul = RCC->CFGR & RCC_CFGR_PLLMUL;
259 plldiv = RCC->CFGR & RCC_CFGR_PLLDIV;
260 pllmul = PLLMulTable[(pllmul >> 18)];
261 plldiv = (plldiv >> 22) + 1;
263 pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
265 if (pllsource == 0x00)
268 SystemCoreClock = (((HSI_VALUE) * pllmul) / plldiv);
273 SystemCoreClock = (((HSE_VALUE) * pllmul) / plldiv);
277 msirange = (RCC->ICSCR & RCC_ICSCR_MSIRANGE) >> 13;
278 SystemCoreClock = (32768 * (1 << (msirange + 1)));
283 tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
285 SystemCoreClock >>= tmp;
296 static void SetSysClock(
void)
298 __IO uint32_t StartUpCounter = 0, HSIStatus = 0;
302 RCC->CR |= ((uint32_t)RCC_CR_HSION);
307 HSIStatus = RCC->CR & RCC_CR_HSIRDY;
308 }
while((HSIStatus == 0) && (StartUpCounter != HSI_STARTUP_TIMEOUT));
310 if ((RCC->CR & RCC_CR_HSIRDY) != RESET)
312 HSIStatus = (uint32_t)0x01;
316 HSIStatus = (uint32_t)0x00;
319 if (HSIStatus == (uint32_t)0x01)
322 FLASH->ACR |= FLASH_ACR_ACC64;
325 FLASH->ACR |= FLASH_ACR_PRFTEN;
328 FLASH->ACR |= FLASH_ACR_LATENCY;
332 RCC->APB1ENR |= RCC_APB1ENR_PWREN;
335 PWR->CR = PWR_CR_VOS_0;
339 while((PWR->CSR & PWR_CSR_VOSF) != RESET)
344 RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
347 RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
350 RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
353 RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLMUL |
355 RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSI | RCC_CFGR_PLLMUL6 | RCC_CFGR_PLLDIV3);
358 RCC->CR |= RCC_CR_PLLON;
361 while((RCC->CR & RCC_CR_PLLRDY) == 0)
366 RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
367 RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
370 while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_PLL)