Skip to main content
marcogrossi89
Associate III
September 27, 2016
Question

STM32F103C6T6A clock configuration

  • September 27, 2016
  • 1 reply
  • 687 views
Posted on September 27, 2016 at 17:30

I need to configure the system clock to HSE with a 8MHz quartz crystal.

At startup the default is HSI. 

For the STM32L family I used the clock configuration tool provided by ST, but this tool is not available for STM32F103 devices.

What modifications should I make to system_stm32f10x.c to allow use HSE 8MHz at startup?

Thanks.

    This topic has been closed for replies.

    1 reply

    Tesla DeLorean
    Guru
    September 27, 2016
    Posted on September 27, 2016 at 22:48

    Probably none, the vast majority of F103 code expects an 8 MHz HSE, and uses a 9X multiplier to get to 72 MHz

    /**
    * @brief Sets System clock frequency to 72MHz and configure HCLK, PCLK2
    * and PCLK1 prescalers.
    * @note This function should be used only after reset.
    * @param None
    * @retval None
    */
    static void SetSysClockTo72(void)
    {
    __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
    /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
    /* Enable HSE */
    RCC->CR |= ((uint32_t)RCC_CR_HSEON);
    /* Wait till HSE is ready and if Time out is reached exit */
    do
    {
    HSEStatus = RCC->CR & RCC_CR_HSERDY;
    StartUpCounter++;
    } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
    if ((RCC->CR & RCC_CR_HSERDY) != RESET)
    {
    HSEStatus = (uint32_t)0x01;
    }
    else
    {
    HSEStatus = (uint32_t)0x00;
    }
    if (HSEStatus == (uint32_t)0x01)
    {
    /* Enable Prefetch Buffer */
    FLASH->ACR |= FLASH_ACR_PRFTBE;
    /* Flash 2 wait state */
    FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
    FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;
    /* HCLK = SYSCLK */
    RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
    /* PCLK2 = HCLK */
    RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
    /* PCLK1 = HCLK */
    RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
    /* PLL configuration: PLLCLK = HSE * 9 = 72 MHz */
    RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE |
    RCC_CFGR_PLLMULL));
    RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL9);
    /* 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)((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) != (uint32_t)0x08)
    {
    }
    }
    else
    { /* If HSE fails to start-up, the application will have wrong clock
    configuration. User can add here some code to deal with this error */
    }
    }

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..