STM32F303 - EXTI2 triggers both EXTI2 and EXTI1 - Page 1
Forum section: electronics / general

author: ppTRN
author_rank: Regular Contributor
timestamp: March 22, 2025, 03:16:19 pm
content: Hi everyone,I wrote some simple code that handles an encoder. It checks for the encoder pushbutton and to one of the phases with interrupts.the problem is that for some reason when EXTI2 is fired (encoder pushbutton), ALSO EXTI1 is triggered. And when I try and trigger EXTI1, nothing happends.What did I do wrong? I really cannot see anithing weird in the code that explains this behavior. All of the tests were conducted in debug mode by phisically connecting the pins to ground. I can see by peeking in the GPIO IDR registers that the signal arrives correctly to the relative pins.Can you please help me? Thanks!Code:#include "stm32f303x8.h"/*This code is used to manage the encoder knob. It is usage-independent,meaning that it only sense if it is turned and update a counter variableaccordingly, decreasing or increasing it based on the turn direction.It also manages the pushbutton embedded into the encoder.D8 (PF1): Phase A used as interrupt EXTI1 referenceD3 (PB0): Phase BA7 (PA2): Pushbutton EXTI2*/extern int encoderCounter;void EXTI1Config(){RCC->AHBENR |= RCC_AHBENR_GPIOFEN;// Enable GPIOF clockRCC->AHBENR |= RCC_AHBENR_GPIOBEN;// Enable GPIOB clockRCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;// Enable SysConfig clockGPIOF->MODER &= ~(1<<2);// GPIOF1 in input modeGPIOF->MODER &= ~(1<<3);GPIOF->PUPDR |= 1<<2;// GPIOF1 internal pullup enableGPIOF->PUPDR &= ~(1<<3);SYSCFG->EXTICR |= 1<<0 | 1<<2;// EXTI1 assigned to GPIOF0EXTI->IMR |= 1<<1;// EXTI1 unmaskedEXTI->FTSR |= 1<<1;// EXTI1 sensitive to falling edgeGPIOB->MODER &= ~(1<<0);// GPIOB0 in input modeGPIOB->MODER &= ~(1<<1);GPIOB->PUPDR |= 1<<0;// GPIOB0 internal pullup enableGPIOB->PUPDR &= ~(1<<1);NVIC_EnableIRQ(EXTI1_IRQn);//NVIC_SetPriority(EXTI1_IRQn, 2);}void EXTI2Config(){RCC->AHBENR |= RCC_AHBENR_GPIOAEN;// Enable GPIOA clockRCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;// Enable SysConfig clockGPIOA->MODER &= ~(1<<4);// GPIOA2 in input modeGPIOA->MODER &= ~(1<<5);GPIOA->PUPDR |= 1<<4;// GPIOA2 internal pullup enableGPIOA->PUPDR &= ~(1<<5);SYSCFG->EXTICR |= 0;// EXTI2 assigned to GPIOA2 by defaultEXTI->IMR |= 1<<2;// EXTI2 unmaskedEXTI->FTSR |= 1<<2;// EXTI2 sensitive to falling edgeNVIC_EnableIRQ(EXTI2_TSC_IRQn);//NVIC_SetPriority(EXTI2_TSC_IRQn, 3);}void EXTI1_IRQHandler(){EXTI->PR |= 1<<1;// Clear pending EXTI1 interrupt flagif(GPIOB->IDR & (1<<0))// Depending on the direction of the rotationencoderCounter++;// it incrementselse encoderCounter--;// or decrements a variable}void EXTI2_TSC_IRQHandler(){EXTI->PR |= 1<<2;// Clear pending EXTI2 interrupt flagencoderCounter = 0;// Pushing the button resets the variable}

author: Dazed_N_Confused
author_rank: Contributor
timestamp: March 22, 2025, 03:23:19 pm
content: EXTI1 doesn't trigger when you expect it to (it's not properly mapped to PF1)EXTI2 triggers EXTI1 (you've accidentally configured EXTI0 to respond to PF0, which might be getting triggered along with PA2)Code:// For PF1 (EXTI1)SYSCFG->EXTICR |= (5 << 4); // 5 is the value for Port F, shifted to bits 4-7 for EXTI1// For PA2 (EXTI2)SYSCFG->EXTICR |= (0 << 8); // 0 is the value for Port A, shifted to bits 8-11 for EXTI2

author: ppTRN
author_rank: Regular Contributor
timestamp: March 22, 2025, 03:36:11 pm
content: Silly me. I can see the mistake: I wanted to push 0101 into the rigth position in the EXTICR but i pushed it into the EXTI0 field.Thank you!

author: Dan N
author_rank: Contributor
timestamp: March 25, 2025, 12:56:01 pm
content: Quote from: ppTRN on March 22, 2025, 03:16:19 pmNot picking on you but I see this incorrect code in lots of tutorials. The PR register bits are cleared by writing 1, and writing 0 has no effect, so a read-modify-write will clear every interrupt flag. Even EXTI->PR |= 0 will clear every interrupt flag. EXTI->PR = 0 won't change anything.To clear only 1 flag just write 1 bit:Code:EXTI->PR = 1<<1;// Clear pending EXTI1 interrupt flag

author: ppTRN
author_rank: Regular Contributor
timestamp: March 26, 2025, 06:42:52 am
content: That is a precious advice, thank you. Another user in another thread already warned be about it but i forgot to implement it.Thanks!