Code: Select all
//simplest routine to switch to HSE
//no error checking
void SystemCoreClock2HSE(void) {
//turn on hse
RCC->CR |= RCC_CR_HSEON; //1->turn on HSE, 0->turn off HSE
//wait for HSE to be ready - error if stuck here
while ((RCC->CR & RCC_CR_HSERDY)==0); //1->HSE ready, 0->HSE not ready
//switch to HSE
RCC->CFGR = (RCC->CFGR &~RCC_CFGR_SW) | //CLEAR_BIT SW bits
(1 << RCC_CFGR_SW_Pos); //0->HSI, 1->HSE, 2->PLLRCLK, 3->LSI, 4->LSE, others->reserved
//wait for the switch to take place
//while ((RCC->CFGR & RCC_CFGR_SWS) != (1<< RCC_CFGR_SWS_Pos)); //if stuck here, there is an error
SystemCoreClockUpdate(); //update systemcoreclock
}
caution: zero error handing.