AnsweredAssumed Answered

STM32F103C8 Incorrect HCLK Issue

Question asked by Bhatnagar.Ankit.001 on Feb 4, 2016
Latest reply on Feb 4, 2016 by Bhatnagar.Ankit.001
I am using a minimal develpment board based on STM32F103C8. The board has an external crystal of 8Mhz.

I am trying to use PLL as my clock source (with input from HSE 8Mhz) upon boot but facing the following 2 issues:

1. When I use PLL multiplier as 9, the clock does not get generated at all.

2. When I use PLL multiplier as 8, the clock does get generated. SYSCLK is 64Mhz, however my HCLK is half of SYSCLK even though I am configuring it to be the same as SYSCLK

Below is my code (using PLL multiplier 8)

01.#include "stm32f10x.h"
02.#include "delay.h"
03. 
04.void setupClock(void);
05.void setupPins(void);
06. 
07.int main(void)
08.{
09.    RCC_ClocksTypeDef clock;
10.     
11.    setupClock();
12.    setupPins();
13.     
14.    RCC_GetClocksFreq(&clock);
15.    /*if(clock.HCLK_Frequency==64000000)
16.    {
17.        GPIO_SetBits(GPIOA, GPIO_Pin_6);
18.    }*/
19.     
20.    if(RCC_GetSYSCLKSource() == 0x08 && clock.SYSCLK_Frequency == 64000000 && clock.HCLK_Frequency==32000000)
21.    {
22.        GPIO_SetBits(GPIOA, GPIO_Pin_6);
23.    }
24.     
25.    while(1)
26.    {
27.        /*GPIO_SetBits(GPIOA, GPIO_Pin_6);
28.        Delay_Ms(1000);
29.        GPIO_ResetBits(GPIOA, GPIO_Pin_6);
30.        Delay_Ms(1000);*/
31.    }
32.}
33. 
34.void setupClock(void)
35.{
36.    ErrorStatus result;
37.     
38.    RCC_DeInit();
39.     
40.    RCC_HSEConfig(RCC_HSE_ON);
41.    result = RCC_WaitForHSEStartUp();
42.    if(result==SUCCESS)
43.    {      
44.        FLASH_PrefetchBufferCmd(ENABLE);
45.        FLASH_SetLatency(FLASH_Latency_2);
46.                 
47.        //PLLCLK = (8Mhz * 9 = 72Mhz
48.        RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_8);
49.        RCC_PLLCmd(ENABLE);
50.         
51.        //wait till PLL ready
52.        while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET)
53.        {
54.        }
55.         
56.        //set PLL as SYSCLK source
57.        RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
58.         
59.        //wait to verify pll is being used aS SYSCLK source
60.        while(RCC_GetSYSCLKSource() != 0x08)
61.        {
62.        }
63.         
64.        //RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
65.        //setupPins();
66.        //GPIO_SetBits(GPIOA, GPIO_Pin_6);
67.         
68.        RCC_HCLKConfig(RCC_SYSCLK_Div1);
69.        RCC_PCLK1Config(RCC_SYSCLK_Div2);
70.        RCC_PCLK2Config(RCC_SYSCLK_Div1);
71.         
72.        SystemCoreClockUpdate();
73.    }
74.    else
75.    {
76.         
77.    }
78. 
79.    //enable clock to GPIOA
80.    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
81.}
82. 
83.void setupPins(void)
84.{
85.    GPIO_InitTypeDef gpio;
86. 
87.    //set A6 as output
88.    gpio.GPIO_Mode = GPIO_Mode_Out_PP;
89.    gpio.GPIO_Pin = GPIO_Pin_6;
90.    gpio.GPIO_Speed = GPIO_Speed_2MHz;
91.    GPIO_Init(GPIOA, &gpio);
92.}

When I run the code, the led connected to PORT A6 lights up.

Please help. This is driving me nuts. I have checked it over and over again but can't find any issues

Outcomes