/* * Copyright (C) 2013 INRIA * Copyright (C) 2014 Freie Universität Berlin * * This file is subject to the terms and conditions of the GNU Lesser General * Public License v2.1. See the file LICENSE in the top level directory for more * details. */ /** * @ingroup cpu_stm32f1 * @{ * * @file * @brief Implementation of the kernel cpu functions * * @author Stefan Pfeiffer * @author Alaeddine Weslati * @author Thomas Eichinger * @author Hauke Petersen * @author Nick van IJzendoorn * * @} */ #include "cpu.h" #include "periph_conf.h" /* Check the source to be used for the PLL */ #if defined(CLOCK_HSI) && defined(CLOCK_HSE) #error "Only provide one of two CLOCK_HSI/CLOCK_HSE" #elif CLOCK_HSI #define CLOCK_CR_SOURCE RCC_CR_HSION #define CLOCK_CR_SOURCE_RDY RCC_CR_HSIRDY #define CLOCK_PLL_SOURCE (0) #elif CLOCK_HSE #define CLOCK_CR_SOURCE RCC_CR_HSEON #define CLOCK_CR_SOURCE_RDY RCC_CR_HSERDY #define CLOCK_PLL_SOURCE RCC_CFGR_PLLSRC #else #error "Please provide CLOCK_HSI or CLOCK_HSE in boards/NAME/includes/perhip_cpu.h" #endif static void clk_init(void); void cpu_init(void) { /* initialize the Cortex-M core */ cortexm_init(); /* initialize system clocks */ clk_init(); } /** * @brief Configure the clock system of the stm32f1 */ static void clk_init(void) { /* Reset the RCC clock configuration to the default reset state(for debug purpose) */ /* Set HSION bit */ RCC->CR |= (uint32_t)0x00000001; /* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */ RCC->CFGR &= (uint32_t)0xF0FF0000; /* Reset HSEON, CSSON and PLLON bits */ RCC->CR &= (uint32_t)0xFEF6FFFF; /* Reset HSEBYP bit */ RCC->CR &= (uint32_t)0xFFFBFFFF; /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */ RCC->CFGR &= (uint32_t)0xFF80FFFF; /* Disable all interrupts and clear pending bits */ RCC->CIR = (uint32_t)0x009F0000; /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration */ /* Enable high speed clock source */ RCC->CR |= ((uint32_t)CLOCK_CR_SOURCE); /* Wait till the high speed clock source is ready * NOTE: the MCU will stay here forever if you use an external clock source and it's not connected */ while ((RCC->CR & CLOCK_CR_SOURCE_RDY) == 0) {} /* Enable Prefetch Buffer */ FLASH->ACR |= FLASH_ACR_PRFTBE; /* Set the flash wait state */ FLASH->ACR &= ~((uint32_t)FLASH_ACR_LATENCY); FLASH->ACR |= (uint32_t)CLOCK_FLASH_LATENCY; /* HCLK = SYSCLK */ RCC->CFGR |= (uint32_t)CLOCK_AHB_DIV; /* PCLK2 = HCLK */ RCC->CFGR |= (uint32_t)CLOCK_APB2_DIV; /* PCLK1 = HCLK */ RCC->CFGR |= (uint32_t)CLOCK_APB1_DIV; /* PLL configuration: PLLCLK = CLOCK_SOURCE / PLL_DIV * PLL_MUL */ RCC->CFGR &= ~((uint32_t)(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL)); RCC->CFGR |= (uint32_t)(CLOCK_PLL_SOURCE | CLOCK_PLL_DIV | CLOCK_PLL_MUL); /* Enable PLL */ RCC->CR |= RCC_CR_PLLON; /* Wait till PLL is ready */ while ((RCC->CR & RCC_CR_PLLRDY) == 0) {} /* Select PLL as system clock source */ RCC->CFGR &= ~((uint32_t)(RCC_CFGR_SW)); RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL; /* Wait till PLL is used as system clock source */ while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL) {} }