Oriolshhh
Oriolshhh

Reputation: 11

STM32 Interrupts Not Triggering Correctly with Specific Priority Settings

I'm working on a project with an STM32 microcontroller where I need to handle interrupts generated by PWM signals on pins PE2 and PC11. Despite configuring the interrupts and ensuring global interrupts are enabled, I'm encountering an issue where my interrupt service routines (ISRs) for these pins are not being triggered as expected.

My GPIO configuration looks like this:

    GPIO_InitTypeDef GPIO_InitStructure;
    EXTI_InitTypeDef EXTI_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE | RCC_AHB1Periph_GPIOC, ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_Init(GPIOE, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
    GPIO_Init(GPIOC, &GPIO_InitStructure);

    SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE, EXTI_PinSource2);
    SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource11);

    EXTI_InitStructure.EXTI_Line = EXTI_Line2;
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; 


    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    EXTI_Init(&EXTI_InitStructure);

    EXTI_InitStructure.EXTI_Line = EXTI_Line11; 
    EXTI_Init(&EXTI_InitStructure);

    NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; 
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
    NVIC_Init(&NVIC_InitStructure);

The ISRs are supposed to calculate the wheel speed from the PWM signal's rising edge. However, they're not being called despite the signals being present (confirmed with an oscilloscope). I've set the priority for these interrupts to 1 with a subpriority of 0, while a button interrupt with priority 0 works fine.

An example of one of the ISRs:

void EXTI2_IRQHandler(void) {
    if (EXTI_GetITStatus(EXTI_Line2) != RESET) {
        uint32_t current_timer_value = TIM5->CNT; 
        uint32_t diff;
        float Tp, omegaR;

        if (current_timer_value >= last_left_timer_value) {
            diff = current_timer_value - last_left_timer_value;
        } else {
            diff = (0xFFFF - last_left_timer_value) + current_timer_value + 1;
        }

        Tp = diff * (1.0 / 1000000.0); 

        if (Tp > 0) {
            omegaR = 1 / (32 * Tp); 
            left_wheel_ticks = (uint32_t)(omegaR * 100); 
        }

        last_left_timer_value = current_timer_value; 
        EXTI_ClearITPendingBit(EXTI_Line2); 
    }
}

I've verified the GPIO and NVIC configurations, ensured global interrupts are enabled, and checked the signal with an oscilloscope. I've also experimented with different priority and subpriority settings without success.

What could be preventing my ISRs from being triggered? Could it be related to the priority configuration, or is there something else I might be overlooking?

Upvotes: 0

Views: 172

Answers (1)

Oriolshhh
Oriolshhh

Reputation: 11

I've managed to solve the issue and wanted to share the solution here for anyone who might encounter a similar problem in the future.

It turns out the root of the problem was that I was attempting to use external interrupts (EXTI) with pins from two different GPIO ports on my STM32 microcontroller. Despite the STM32's flexibility in handling external interrupts across different ports, my initial approach of using PE2 (from GPIOE) and PC11 (from GPIOC) together didn't work as expected.

I decided to try using pins from only one GPIO port for the external interrupts. This change made everything work as intended.

Upvotes: 1

Related Questions