AnsweredAssumed Answered

stm32f4 discovery clock appears to be 1/2 speed

Question asked by cyr.darin on Dec 29, 2013
Latest reply on Dec 31, 2013 by cyr.darin
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 */
#include "stm32f4xx.h"
#include "stm32f4_discovery.h"
//#include <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);
}
/*
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t* file, uint32_t line)
{
    while(1)
        ()

}
#endif
*/

/* 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;
}

Outcomes