Search code examples
arminterruptstm32stm32f0

STM32F0: Interrupt on EXTI1 / EXTI2 not firing, while EXTI4 is working. *confused*


can you please help me out here and tell me what I am doing wrong? EXTI1 and EXTI2 will not fire, while EXTI4 is however working flawlessly. It's not the hardware. If I switch the pins, the new EXTI4 button will keep firing, while the button that I switched it with doesn't anymore.

I have thoroughly checked all segments, and I cannot figure out why EXTI0_1_IRQHandler doesn't fire, while EXTI4_15_IRQHandler does. Please see the two files below that I use for the library. I'm using SPL with OpenSTM32 IDE. "main.c" only calls

ENCODER_STM32_configureInterface();

so there really isn't anything else going on.

// ENCODER_STM32 library: This library shall enable interfacing an encoder on an STM32F0 chip.

// GPIO Definitions
#define    ENCODER_GPIO_PORT       GPIOA
#define    ENCODER_GPIO_CLK_PIN    GPIO_Pin_1
#define    ENCODER_GPIO_DT_PIN     GPIO_Pin_2
#define    ENCODER_GPIO_SW_PIN     GPIO_Pin_4
#define    ENCODER_GPIO_PERIPH     RCC_AHBPeriph_GPIOA

// EXTI Definitions
#define    ENCODER_EXTI_PORTSRC    EXTI_PortSourceGPIOA
#define    ENCODER_EXTI_CLK_PINSRC EXTI_PinSource1
#define    ENCODER_EXTI_CLK_LINE   EXTI_Line1
#define    ENCODER_EXTI_DT_PINSRC  EXTI_PinSource2
#define    ENCODER_EXTI_DT_LINE    EXTI_Line2
#define    ENCODER_EXTI_SW_PINSRC  EXTI_PinSource4
#define    ENCODER_EXTI_SW_LINE    EXTI_Line4
#define    ENCODER_EXTI_PERIPH     RCC_APB2Periph_SYSCFG
#define    ENCODER_EXTI_MODE       EXTI_Mode_Interrupt
#define    ENCODER_EXTI_TRIGGER    EXTI_Trigger_Falling

// NVIC Definitions
#define    ENCODER_NVIC_CLK_CHA    EXTI0_1_IRQn
#define    ENCODER_NVIC_DT_CHA     EXTI2_3_IRQn
#define    ENCODER_NVIC_SW_CHA     EXTI4_15_IRQn
#define    ENCODER_NVIC_PRIORITY   0x03

// Variable Definitions

// Parameter Definitions

// Function Definitions

// Function Declarations
void ENCODER_STM32_configureInterface(void);

and...

#include "stm32f0xx.h"
#include "usart_stm32_console.h"
#include "encoder_stm32.h"

void ENCODER_STM32_configureInterface(void) {

    // Enabling Clocks
    RCC_AHBPeriphClockCmd(ENCODER_GPIO_PERIPH, ENABLE);
    RCC_APB2PeriphClockCmd(ENCODER_EXTI_PERIPH, ENABLE);

    // Configuring GPIO
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
    GPIO_InitStructure.GPIO_Pin = ENCODER_GPIO_CLK_PIN | ENCODER_GPIO_DT_PIN | ENCODER_GPIO_SW_PIN;
    GPIO_Init(ENCODER_GPIO_PORT, &GPIO_InitStructure);

    // Configuring EXTI
    EXTI_InitTypeDef EXTI_InitStructure;
    EXTI_InitStructure.EXTI_Line = ENCODER_EXTI_CLK_LINE | ENCODER_EXTI_DT_LINE | ENCODER_EXTI_SW_LINE;
    EXTI_InitStructure.EXTI_Mode = ENCODER_EXTI_MODE;
    EXTI_InitStructure.EXTI_Trigger = ENCODER_EXTI_TRIGGER;
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    SYSCFG_EXTILineConfig(ENCODER_EXTI_PORTSRC, ENCODER_EXTI_CLK_PINSRC);
    SYSCFG_EXTILineConfig(ENCODER_EXTI_PORTSRC, ENCODER_EXTI_DT_PINSRC);
    SYSCFG_EXTILineConfig(ENCODER_EXTI_PORTSRC, ENCODER_EXTI_SW_PINSRC);
    EXTI_Init(&EXTI_InitStructure);

    // Configuring NVIC
    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_InitStructure.NVIC_IRQChannel = ENCODER_NVIC_CLK_CHA | ENCODER_NVIC_DT_CHA | ENCODER_NVIC_SW_CHA;
    NVIC_InitStructure.NVIC_IRQChannelPriority = ENCODER_NVIC_PRIORITY;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

}


void EXTI0_1_IRQHandler(void) {

    USART_STM32_sendToUSART("IRQ0_1 triggered");

    if(EXTI_GetITStatus(ENCODER_EXTI_CLK_LINE) != RESET) {
        USART_STM32_sendToUSART("CLK triggered");
        EXTI_ClearITPendingBit(ENCODER_EXTI_CLK_LINE);
    }

}


void EXTI2_3_IRQHandler(void) {

    USART_STM32_sendToUSART("IRQ2_3 triggered");

    if(EXTI_GetITStatus(ENCODER_EXTI_DT_LINE) != RESET) {
        USART_STM32_sendToUSART("DT triggered");
        EXTI_ClearITPendingBit(ENCODER_EXTI_DT_LINE);
    }

}


void EXTI4_15_IRQHandler(void) {

    USART_STM32_sendToUSART("IRQ4_15 triggered");

    if(EXTI_GetITStatus(ENCODER_EXTI_SW_LINE) != RESET) {
        USART_STM32_sendToUSART("SW triggered");
        EXTI_ClearITPendingBit(ENCODER_EXTI_SW_LINE);
    }

}

Solution

  • NVIC_Init() works with a single interrupt at a time, because NVIC_InitStructure.NVIC_IRQChannel is not a bitmask, but a simple identifier.

    Look at the definition of the IRQ channels in the header:

      EXTI0_1_IRQn                = 5,      /*!< EXTI Line 0 and 1 Interrupts                            */
      EXTI2_3_IRQn                = 6,      /*!< EXTI Line 2 and 3 Interrupts                            */
      EXTI4_15_IRQn               = 7,      /*!< EXTI Line 4 to 15 Interrupts                            */
    

    Or'ing them together like mask bits makes no sense, but in this particular case, 5|6|7 yields 7, which accidentally equals the number of the EXTI4 interrupt, that's why this button works.

    Simply call NVIC_Init() three times:

    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_InitStructure.NVIC_IRQChannel = ENCODER_NVIC_CLK_CHA;
    NVIC_InitStructure.NVIC_IRQChannelPriority = ENCODER_NVIC_PRIORITY;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    NVIC_InitStructure.NVIC_IRQChannel = ENCODER_NVIC_DT_CHA;
    NVIC_Init(&NVIC_InitStructure);
    NVIC_InitStructure.NVIC_IRQChannel = ENCODER_NVIC_SW_CHA;
    NVIC_Init(&NVIC_InitStructure);