cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f4 discovery clock appears to be 1/2 speed

darin722
Associate
Posted on December 29, 2013 at 18:57

Hi, I'm fairly new to embedded system programming and I've just started experimenting with the 32f4 and 32f3 discovery boards.

I'm working with a simple interrupt driven timer to flash one of the LEDs at 1Hz, but my update calculations seem to be off as if the clock is running at 42MHz instead of 84MHz.

Can anyone tell me what I'm missing?

I'm using atolic trueSTUDIO if it matters, and

I'm using the calculation updateEventHz = timerClock(84Mhz) / (tim_psc + 1) * (tim_arr+1)

My code is as follows

/**

*****************************************************************************

**

**  File        : main.c

**

**  Abstract    : main function.

**

**  Functions   : main

**

**  Environment : Atollic TrueSTUDIO(R)

**                STMicroelectronics STM32F4xx Standard Peripherals Library

**

**  Distribution: The file is distributed �as is,� without any warranty

**                of any kind.

**

**  (c)Copyright Atollic AB.

**  You may use this file as-is or modify it according to the needs of your

**  project. This file may only be built (assembled or compiled and linked)

**  using the Atollic TrueSTUDIO(R) product. The use of this file together

**  with other tools than Atollic TrueSTUDIO(R) is not permitted.

**

*****************************************************************************

*/

/* Includes */

&sharpinclude ''stm32f4xx.h''

&sharpinclude ''stm32f4_discovery.h''

//&sharpinclude <stdio.h>

/* Private typedef */

GPIO_InitTypeDef GPIO_InitStructure;

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

/* Private macro */

/* Private variables */

/* Private function prototypes */

void INTTIM_Config(void);

void LED_Config(void);

/* Private functions */

void TIM2_IRQHandler(void)

{

    if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)

    {

        TIM_ClearITPendingBit(TIM2, TIM_IT_Update);

        GPIO_ToggleBits(GPIOD, GPIO_Pin_13);

    }

}

/**

**===========================================================================

**

**  Abstract: main program

**

**===========================================================================

*/

int main(void)

{

    INTTIM_Config();

    LED_Config();

    while(1)

    {

        //GPIO_ToggleBits(GPIOD, GPIO_Pin_13);

    }

}

  void INTTIM_Config(void)

  {

      NVIC_InitTypeDef NVIC_InitStructure;

      // Enable the time2 global Interupt

      NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;

      NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

      NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

      NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

      NVIC_Init(&NVIC_InitStructure);

      // TIM2 Clock Enable

      RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

      // Time Base configuration clock is 42Mhz

      TIM_TimeBaseStructure.TIM_Period = 1000000-1; // 1Mhz to 1Hz--1Mhz down to 1 Khz(1 ms)

      TIM_TimeBaseStructure.TIM_Prescaler = 42 - 1; // 42Mhz to 1Mhz--24Mhz clock own to 1Mhz

      TIM_TimeBaseStructure.TIM_ClockDivision = 0;

      TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

      TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

      // Tim IT enable

      TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);

      // Tim2 Enable counter

      TIM_Cmd(TIM2, ENABLE);

  }

void LED_Config(void)

{

    //GPIOD Periph Clock Enable

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

    // Configure pd12, pd13, pd14, pd15 in output pushpull mode

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 |GPIO_Pin_13 |GPIO_Pin_14 |GPIO_Pin_15;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;

    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

    GPIO_Init(GPIOD, &GPIO_InitStructure);

}

/*

&sharpifdef USE_FULL_ASSERT

void assert_failed(uint8_t* file, uint32_t line)

{

    while(1)

        ()

}

&sharpendif

*/

/* LEFT OVER FROM ORIGINAL EXAMPLE FILE

 * Callback used by stm32f4_discovery_audio_codec.c.

 * Refer to stm32f4_discovery_audio_codec.h for more info.

 */

void EVAL_AUDIO_TransferComplete_CallBack(uint32_t pBuffer, uint32_t Size){

  /* TODO, implement your code here */

  return;

}

/*

 * Callback used by stm324xg_eval_audio_codec.c.

 * Refer to stm324xg_eval_audio_codec.h for more info.

 */

uint16_t EVAL_AUDIO_GetSampleCallBack(void){

  /* TODO, implement your code here */

  return -1;

}

#stm32f4-timer-clock-frequency
2 REPLIES 2
Posted on December 29, 2013 at 20:12

Well I'll observe that toggling will halve the frequency.Then I'll suggest you review the clock and PLL settings in system_stm32f4xx.c. Make sure you are using the settings for an 8 MHz external crystal, not a 25 MHz one.Finally, if you want to check internal clocks you can route them out via the MCOx pins and verify them on a scope.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
darin722
Associate
Posted on December 31, 2013 at 07:04

Yup, you nailed it Clive. This appears to be an ID-10-T error.

I somehow managed to confuse timer update frequency with my desired IO Frequency.  I know better. Sometimes I need a whack up side the head to get my brain working correctly.

Thanks!