cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F437 timer input capture

John Hite
Associate III
Posted on March 16, 2017 at 20:16

I have attempted to set up timer 4 channel 1 for input capture but unsuccessfully. The scope indicates that my 1KHz, 3vdc, 35 uSec pulses are making it to PD12 (TIM4, ch 1). However, CC1IF is never set. Although channels 2-4 are not configured CC2IF - CC4IF are being set and thus so is UIF but my ISR is never called. So I have two problems no CC1IF and no call to the ISR. Suggestions and advice would be much appreciated. The code follows.

Thanks,

JH

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

uint32_t rpm4;

void RpmTask(void  *p_arg);

void RpmTask(void *p_arg)

{

    static int i = 0;

    uint16_t itstatus = 0;

    GPIO_StructInit(&GPIO_InitStruct);

    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12;

    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;

    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;

    GPIO_InitStruct.GPIO_Speed = GPIO_High_Speed;

    GPIO_Init(GPIOD, &GPIO_InitStruct);

    GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_TIM4);

    NVIC_InitTypeDef NVIC_InitStruct;

    NVIC_InitStruct.NVIC_IRQChannel = TIM4_IRQn;

    NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;

    NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;

    NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;

    NVIC_Init(&NVIC_InitStruct);

    //RPM timer setups

    // APB1PeriphClock - 42 MHz

    // APB2PeriphClock - 84 MHz

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3 | RCC_APB1Periph_TIM4, ENABLE);

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 | RCC_APB2Periph_TIM9, ENABLE);

    TIM_TimeBaseInitTypeDef tim4_base;

    tim4_base.TIM_Period = 0xFFFFFFFF;

    tim4_base.TIM_Prescaler = 48;

    tim4_base.TIM_ClockDivision = TIM_CKD_DIV1;  //digital filter clk div, don't care, filtering is off

    tim4_base.TIM_CounterMode = TIM_CounterMode_Up;

    tim4_base.TIM_RepetitionCounter = 0; //t1&t8 pwm, don't care

    TIM_TimeBaseInit(TIM4, &tim4_base);

    TIM_ICInitTypeDef tim4_icInit;

    tim4_icInit.TIM_Channel = TIM_Channel_1;

    tim4_icInit.TIM_ICPolarity = TIM_ICPolarity_Rising; //r, f, both

    tim4_icInit.TIM_ICSelection = TIM_ICSelection_DirectTI; //dir, ind, trc

    tim4_icInit.TIM_ICPrescaler = TIM_ICPSC_DIV1; //1-8

    tim4_icInit.TIM_ICFilter = 0x0;

    TIM_ICInit(TIM4, &tim4_icInit);

    /* Enable the CC2 Interrupt Request */

    TIM_ITConfig(TIM4, TIM_IT_CC1, ENABLE);

    /* TIM enable counter */

    TIM_Cmd(TIM4, ENABLE);

    while (1)

    {

        rpm4 = TIM_GetCounter(TIM4);

        itstatus = TIM4->SR;

    }

}

void timer4_isr(void)

{

    if (TIM_GetITStatus(TIM4, TIM_IT_CC1) == SET) {

        TIM_ClearITPendingBit(TIM4, TIM_IT_CC1);

        rpm4 = TIM_GetCounter(TIM4);

    }

    TIM_ClearITPendingBit(TIM4, TIM_IT_Update);

}

#input-capture
1 ACCEPTED SOLUTION

Accepted Solutions
Posted on March 17, 2017 at 00:37

Timer registers look all OK (FYI checked in this order: CCMR1.CC1S if set to 'straight in'; CCER.CC1E if set to enable; DIER.CC1IE if set to enable).

Went to the GPIO registers, this was the very first to check:

MODER        0x5455555A

Lower two bits of that nibble are 00 which means that MODE of pin 12 is *not* AF as it is supposed to be, but In.

Now going in reverse:

GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;

I don't 'library' but this is probabaly the culprit.

JW

View solution in original post

5 REPLIES 5
Posted on March 16, 2017 at 20:48

Post the content of relevant GPIO and TIM registers.

Did you observe the input signal directly on the MCU pin?

JW

Posted on March 16, 2017 at 21:35

First perhaps I should have stated that I am trying to measure RPM counting the time between pulses.

Yes I piled on two pair of reading glasses and put my scope probe right on pin 81 of the 144 pin package.

I will collect the registers now. The TIM4 registers are below. Which GPIO registers would be useful?

cr1        0x0001    

cr2        0x0000    

dier        0x0002    

sr            0x001D    

egr         0x0000    

ccmr1    0x0001    

ccmr2    0x0000    

ccer        0x0001    

psc          0x0064    

arr          0xFFFF    

ccr1        0x0000    

ccr2        0x0000   
Posted on March 16, 2017 at 22:07

Figured out how to get the watch list to give me GPIOD...

gpiod            0x40020C00

MODER        0x5455555A

OTYPER        0x00000000

OSPEEDR    0x0000000F

PUPDR            0x00000000

IDR                0x0000000B

ODR                0x00000008

BSRRL            0x0000        

BSRRH            0x0000        

LCKR            0x00000000

AFR    <array>                

[0]    0x00000099            

[1]    0x00020000   
Posted on March 17, 2017 at 00:37

Timer registers look all OK (FYI checked in this order: CCMR1.CC1S if set to 'straight in'; CCER.CC1E if set to enable; DIER.CC1IE if set to enable).

Went to the GPIO registers, this was the very first to check:

MODER        0x5455555A

Lower two bits of that nibble are 00 which means that MODE of pin 12 is *not* AF as it is supposed to be, but In.

Now going in reverse:

GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;

I don't 'library' but this is probabaly the culprit.

JW

Posted on March 17, 2017 at 13:40

JW,

Yes, that was it! Thank you very much for taking the time to look through all my register settings, I know that took some time.

I inherited the GPIO from a 'working' sister project and I assumed that all of the GPIO settings were correct as they have been until now. This is my mistake, I blame no one but myself for failing to see this.

Thank you again,

JH