AnsweredAssumed Answered

Interrupt on output PWM

Question asked by 0.tom on Sep 5, 2014
Latest reply on Sep 10, 2014 by Clive One
I'm trying to set up interrupt on falling/raising edge on output PWM line. This line drives LEDs (through TI LED driver chip). The reason why I need this is that the TI chip would report issues on a different pin but this pin is synchronized with PWM line - one comms bit per PWM signal.

I've tried to configure update interrupt but the handler never gets triggered. Any ideas why?

Here's my code

#define LED_GPIO_PORT             GPIOA
#define LED_GPIO_RCC              RCC_APB2Periph_GPIOA
#define LED_GPIO_PWM_PIN          GPIO_Pin_11

#define PWM_FREQUENCY_Hz 3000
#define PWM_MAX_DUTY_CYCLE    40 // 40%

#define LED_TIMER TIM1
#define LED_TIMER_RCC RCC_APB2Periph_TIM1

void LEDInit( void )
{
    ....


    GPIO_InitTypeDef GPIO_InitStructure;
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    TIM_OCInitTypeDef  TIM_OCInitStructure;


    // turn on clocks
    RCC_APB2PeriphClockCmd( LED_GPIO_RCC | LED_TIMER_RCC, ENABLE );


    NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);


    /* TIM IT enable */
    TIM_ITConfig( LED_TIMER, TIM_IT_Update, ENABLE );
    TIM_Cmd( LED_TIMER, ENABLE );


    GPIO_InitStructure.GPIO_Pin = LED_GPIO_PWM_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init( LED_GPIO_PORT2, &GPIO_InitStructure );


    TIM_TimeBaseStructure.TIM_Period = (SystemCoreClock/2) / PWM_FREQUENCY_Hz - 1;
    TIM_TimeBaseStructure.TIM_Prescaler = 0;
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned1;
    TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit( LED_TIMER, &TIM_TimeBaseStructure );

    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
    TIM_OCInitStructure.TIM_Pulse = period - dutyLength;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;

    TIM_OC4Init( TIM1, &TIM_OCInitStructure );
    TIM_OC4PreloadConfig( LED_TIMER, TIM_OCPreload_Enable );
    TIM_CtrlPWMOutputs( LED_TIMER, ENABLE );
}

void TIM1_UP_IRQHandler(void)
{
    if (TIM_GetITStatus( LED_TIMER, TIM_IT_Update ) != RESET)
    {
     // do something here

        TIM_ClearITPendingBit( LED_TIMER, TIM_IT_Update );
    }
}

Outcomes