Skip to main content
_behn4m
Associate III
October 31, 2013
Question

Timer clock

  • October 31, 2013
  • 8 replies
  • 1172 views
Posted on October 31, 2013 at 15:17

Hi

when I set hclk to 72 for STM32F103 micro the timer doesnt work! I set APB1 pll to devide by 2, but in downt work aftar that also

whats the problem?

#too-vague
    This topic has been closed for replies.

    8 replies

    Tesla DeLorean
    Guru
    October 31, 2013
    Posted on October 31, 2013 at 17:35

    To address the earlier version of the question, the clock details can be computed by decomposing the RCC settings, and doing the math. The library function RCC_GetClocksFreq() does this based on HSI_VALUE and HSE_VALUE, the PLL settings, and clock source.

    RCC_ClocksTypeDef RCC_ClockFreq;
    RCC_GetClocksFreq(&RCC_ClockFreq);
    printf(''SYS:%d H:%d, P1:%d, P2:%d, ADC:%d
    
    '',
    RCC_ClockFreq.SYSCLK_Frequency,
    RCC_ClockFreq.HCLK_Frequency, // AHB
    RCC_ClockFreq.PCLK1_Frequency, // APB1
    RCC_ClockFreq.PCLK2_Frequency, // APB2
    RCC_ClockFreq.ADCCLK_Frequency);

    You later question depend on what your code is actually doing, I'm not going to try and guess what you're doing, present a clear example, I'll suggest why it might not be working, or what to change.
    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    _behn4m
    _behn4mAuthor
    Associate III
    October 31, 2013
    Posted on October 31, 2013 at 18:24 thank you, I want to use micro on 64 mhz clock, but when I set it like folowing, it doesnt work, I think it may be because of APB over clock?!

    void config_TIM2()
    {
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
    /* TIM2 clock enable */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
    /* Time base configuration */
    TIM_TimeBaseInitStructure.TIM_Period = 65000 - 1; // 100 KHz down to 10 Hz (100 ms)
    TIM_TimeBaseInitStructure.TIM_Prescaler = 64000 - 1; // 36 MHz Clock down to 100 KHz (adjust per your clock)
    TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
    /* Initialize TIM2 */
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);
    /* TIM2 enable counter */
    TIM_Cmd(TIM2, DISABLE);
    }
    void delay_ms(u16 ms)
    {
    config_TIM2();
    TIM_Cmd(TIM2, ENABLE);
    while ((TIM2->CNT) < ms );
    TIM_Cmd(TIM2, DISABLE);
    }
    void main(void)
    {
    RCC_HCLKConfig(RCC_SYSCLK_Div1);// HCLK = 64 MHz, AHB
    RCC_PCLK1Config(RCC_HCLK_Div2); // APB1 = 32 MHz
    RCC_PCLK2Config(RCC_HCLK_Div1); // APB2 = 64 MHz 
    /* PLLCLK = 4MHz * 16 = 64 MHz */
    RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_16);
    /* Enable PLL */
    RCC_PLLCmd(ENABLE);
    /* Wait till PLL is ready */
    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
    /* Select PLL as system clock source */
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
    /* Wait till PLL is used as system clock source */
    while(RCC_GetSYSCLKSource() != 0x08);
    //RCC_HCLKConfig(RCC_SYSCLK_Div2);
    //RCC_PCLK1Config(RCC_HCLK_Div8);
    config_TIM2();
    config_IO();
    GPIO_SetBits(GPIOA, GPIO_Pin_1);
    while (1)
    {
    delay_ms(10000);
    GPIO_ResetBits(GPIOA, GPIO_Pin_1);
    delay_ms(10000);
    GPIO_SetBits(GPIOA, GPIO_Pin_1); 
    }
    }

    Tesla DeLorean
    Guru
    October 31, 2013
    Posted on October 31, 2013 at 18:33

    Doesn't work how? Does the clock tick, does it get stuck in a loop, does the GPIO not change.

    You must be using an older version of the library, v3.5.0 should have the PLL setting code in SystemInit() in system_stm32f1xx.c, which is called prior to main().

    What tools and library are you using?
    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    _behn4m
    _behn4mAuthor
    Associate III
    October 31, 2013
    Posted on October 31, 2013 at 18:39

    version is @version V3.5.0

    no blink, LED is off. I dont know about clock tick, I dont have any debug tool

    I use KEIL v4.7

    what should I do about  SystemInit()? 

    Tesla DeLorean
    Guru
    October 31, 2013
    Posted on October 31, 2013 at 18:55

    I don't see the code initializing the GPIO/LED, check that is correct.

    Try setting the LED on/off prior to setting the clocks (ie whilst still running at 8 MHz). Toggle the GPIO in a tight loop, no delays, observe with a scope.

    Modify the clock set up code in system_stm32f1xx.c to match your HSI clock source and PLL settings.

    Use the project template from the firmware library.

    Get a debugger, otherwise you're going to spend forever pondering what is not working and why.
    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    _behn4m
    _behn4mAuthor
    Associate III
    October 31, 2013
    Posted on October 31, 2013 at 19:08

    ok, SOLVED

    I don't know why! but flash latency should be set :

    RCC_HCLKConfig(RCC_SYSCLK_Div1);// HCLK = 64 MHz, AHB
    RCC_PCLK1Config(RCC_HCLK_Div2); // APB1 = 32 MHz
    RCC_PCLK2Config(RCC_HCLK_Div1); // APB2 = 64 MHz 
    /* set up FLASH */
    FLASH_SetLatency(FLASH_Latency_2);
    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
    /* PLLCLK = 4MHz * 16 = 64 MHz */
    RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_16);
    /* Enable PLL */
    RCC_PLLCmd(ENABLE);
    /* Wait till PLL is ready */
    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
    /* Select PLL as system clock source */
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
    /* Wait till PLL is used as system clock source */
    while(RCC_GetSYSCLKSource() != 0x08);

    So I still don't know how I should know points like this before start programming!? there is no good documentation about libraries? also I need more information for when ever I should wait on e flag for a while?
    _behn4m
    _behn4mAuthor
    Associate III
    October 31, 2013
    Posted on October 31, 2013 at 19:09

    it blinks when I set on up to RCC_PLLMul_14, but when 15 or 16 it stucks!

    I use the the template project! every thing works correct on 8 MHZ

    I use my custom PCB and no debug pin is available on it 

    tank you for your helps, I'm going work on it

    Tesla DeLorean
    Guru
    October 31, 2013
    Posted on October 31, 2013 at 22:29

    If you want to understand how the part functions, then you'll need to digest the Reference Manual, Data Manual, and the Cortex-M3 TRM. There is also a presumption of some general familiarity with micro-processors/controllers.

    The firmware library comes with many examples, and sample code. There should also be a .CHM help file describing the functions, but they are basically masking the data in the RM

    The code in system_stm32f1xx.c should have an example of how the clocks are set up. If you use the startup_stm32f1xx.s, it calls SystemInit()

    The flash operates at about 24 MHz, for each increment of 24 MHz you need to insert an additional wait state.

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..