cancel
Showing results for 
Search instead for 
Did you mean: 

STM32f4 Discovery Timer Clock Problem

aliburakparim
Associate
Posted on March 06, 2014 at 11:23

Hi you all,

I am trying to produce a low jittered sharp clock signal at 96kHz freq. for my I2S application. The purpose is obtaining a interrupt at every 96kHz cycle (10.4us) and with each interrupt, sending one sample of the I2S data (a sine wave).

I calculate the clock divider values as (7-1) for PSC, (250-1) for ARR and Period, 0 for clockDivision for TIM1 I assume that work with 84MHz clock frequency according to referance manual and app. notes.  But, I can obtain 96kHz clock freq. with (7-1) for PSC and (125-1) for ARR and Period, CLockDivision is same as 0. Moreover, if I configure all divider values as 0 (PSC) and 1(ARR and Period), I see 1.8us clock frequency on ToggleBit() with my Tektronix oscilloscope and it don't change with small divider values like (7-1) for PSC and 1 for ARR. Even though, I see the correct values as 168MHz, 42Mhz with RCC_GetClocksFreq() function, I see different thing on scope. Can you help me or any idea why I am facing that weird problem?

Thanks a lot!

You can see my code in below.

&sharpinclude ''stm32f4xx.h''

&sharpinclude ''stm32f4xx_gpio.h''

&sharpinclude ''stm32f4xx_rcc.h''

&sharpinclude ''stm32f4xx_tim.h''

&sharpinclude ''stm32f4xx_usart.h''

&sharpinclude ''misc.h''

&sharpinclude ''stdio.h''

void RCC_Config();

void TIM_NVIC_Config();

void GPIO_Config();

void Delay(__IO uint32_t nCount);

void USART2_Config();

void TIM1_CC_IRQHandler(void);

int main(void)

{

    SystemInit();

    TIM_DeInit(TIM1);

    RCC_Config();

    GPIO_Config();

    TIM_NVIC_Config();

    //USART2_Config();

    RCC_ClocksTypeDef RCC_Clocks;

    RCC_GetClocksFreq(&RCC_Clocks);

    GPIO_SetBits(GPIOD, GPIO_Pin_15);

    Delay(0x0fffff);

    GPIO_ResetBits(GPIOD, GPIO_Pin_15);

    Delay(0x0fffff);

while(1){

    TIM1_CC_IRQHandler();

}

    return 0;

}

void RCC_Config()

{

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA

                            |RCC_AHB1Periph_GPIOB

                            |RCC_AHB1Periph_GPIOD, ENABLE);

    RCC_PLLCmd(ENABLE);

}

void GPIO_Config()

{

    GPIO_InitTypeDef GPIOStruct;

    GPIOStruct.GPIO_Mode = GPIO_Mode_OUT;

    GPIOStruct.GPIO_Speed = GPIO_Speed_100MHz;

    GPIOStruct.GPIO_OType = GPIO_OType_PP;

    GPIOStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;

    GPIOStruct.GPIO_Pin = GPIO_Pin_7;

    GPIO_Init(GPIOB,&GPIOStruct);

/*

    GPIOStruct.GPIO_Mode = GPIO_Mode_AF;

    GPIOStruct.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_8|GPIO_Pin_9

                                    |GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_15;

    GPIOStruct.GPIO_Speed = GPIO_Speed_100MHz;

    GPIOStruct.GPIO_OType = GPIO_OType_PP;

    GPIOStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;

    GPIO_Init(GPIOA, &GPIOStruct);

*/

    //TIM1

    /*

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_TIM1);

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_TIM1);

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_TIM1);

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_TIM1);

    */

    //Alternate Func. Configurations for USART2

    /*

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);    //USART2_TX

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);     //USART2_RX

*/

}

void TIM_NVIC_Config()

{

    TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;

    NVIC_InitTypeDef NVICStruct;

    /*TIM Configurations*/

    TIM_TimeBaseStruct.TIM_CounterMode = TIM_CounterMode_Up;

    TIM_TimeBaseStruct.TIM_Prescaler = 0;

    TIM_TimeBaseStruct.TIM_Period = 1;

    TIM_TimeBaseStruct.TIM_ClockDivision = 0;

    TIM_TimeBaseStruct.TIM_RepetitionCounter = 0;

    TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStruct);

    TIM_SetAutoreload(TIM1, 1);

    /*NVIC configurations*/

    NVICStruct.NVIC_IRQChannel = TIM1_CC_IRQn;

    NVICStruct.NVIC_IRQChannelPreemptionPriority = 0;

    NVICStruct.NVIC_IRQChannelSubPriority = 1;

    NVICStruct.NVIC_IRQChannelCmd = ENABLE;

    NVIC_Init(&NVICStruct);

    TIM_Cmd(TIM1, ENABLE);

    TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);

}

/*----------------------------Delay Function--------------------------------------*/

void Delay(__IO uint32_t nCount)

{

  while(nCount--)

  {

  }

}

/*--------------------------------------------------------------------------------*/

/*---------------------------USART Configurations---------------------------------*/

void USART2_Config(void){

    USART_InitTypeDef USART_InitStructure;

    USART_InitStructure.USART_BaudRate = 115200;

    USART_InitStructure.USART_WordLength = USART_WordLength_8b;

    USART_InitStructure.USART_StopBits = USART_StopBits_1;

    USART_InitStructure.USART_Parity = USART_Parity_No;

    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

    USART_Init(USART2, &USART_InitStructure);

    USART_Cmd(USART2, ENABLE);

}

/*--------------------------------------------------------------------------------*/

void TIM1_CC_IRQHandler(void)

{

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

    {

        TIM_ClearITPendingBit(TIM1,TIM_IT_Update);

        GPIO_ToggleBits(GPIOB, GPIO_Pin_7);

    }

}

#stm32f4-timer-clock-frequency
3 REPLIES 3
chen
Associate II
Posted on March 06, 2014 at 12:08

Hi

I do not know the answer to your question.

I have a questions for you :

Are you trying to bit bang I2S ?

If so - why?

The STMF4 has a SPI peripheral which will send I2S. It will when correctly configured generate the I2S clock as needed.

aliburakparim
Associate
Posted on March 06, 2014 at 13:16

I realized that you are right. I think I act so suspiciously about clock of  I2S, but, there's no need to do that. I wanted to be sure about clock with timer. I can cancel it anyway and use SPI periph clock. Thanks

However, I still curious about the timer clock problem.

Posted on March 06, 2014 at 13:57

However, I still curious about the timer clock problem.

Review the clock tree in RM0090 more carefully, with APB2 at DIV2 (84 MHz) the TIMCLK to the TIM on APB2 will be DIV1 (168 MHz)
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..