Fixed with HAL.
Fork of ST_L152_32MHZ by
Not finish yet. External crystal doesn't work.
Diff: ST_L152_32MHZ.cpp
- Revision:
- 0:84e23a19e37d
- Child:
- 1:bdeac50afe1a
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ST_L152_32MHZ.cpp Tue Mar 11 20:34:57 2014 +0000 @@ -0,0 +1,46 @@ + + +#include "stm32l1xx.h" +#include "stm32l1xx_flash.h" +#include "stm32l1xx_rcc.h" +#include "ST_L152_32MHZ.h" + +// set cpu clock to 32MHz + + + +L152_init32::L152_init32(unsigned int external){ + clk_err = setup_clock_32MHZ(external); + } + +#define PLL_STARTUP_TIMEOUT 0x5000 + +int L152_init32::setup_clock_32MHZ(int external) +{ + uint32_t PLLStartUpCounter = 0,PLLStatus = 0,error; + + if(external == 0) { // internal Oscillator + RCC_PLLConfig(RCC_PLLSource_HSI,RCC_PLLMul_6,RCC_PLLDiv_3); // setup pll to 96MHz to use USB + } else { + RCC_HSEConfig(RCC_HSE_ON); // start external crystal osc. + error = RCC_WaitForHSEStartUp(); + if(error == ERROR ) { // no external crystal + return(0); + } + RCC_PLLConfig(RCC_PLLSource_HSE,RCC_PLLMul_12,RCC_PLLDiv_3); // setup pll to 96MHz to use USB + } + RCC_PLLCmd(ENABLE); // switch on pll + do { + PLLStatus = RCC->CR & RCC_CR_PLLRDY; + } while((PLLStatus == 0) && (PLLStartUpCounter < PLL_STARTUP_TIMEOUT)); // wait for pll + if(PLLStatus == 0) { + return(0); + } + FLASH_SetLatency(FLASH_Latency_1); + FLASH_PrefetchBufferCmd(ENABLE); + FLASH_ReadAccess64Cmd(ENABLE); + RCC_HCLKConfig(RCC_SYSCLK_Div2); + RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); // switch to 32 MHz clock + SystemCoreClockUpdate(); // update SystemCoreClock var + return(1); +} \ No newline at end of file