Author Topic: STM32F303 - EXTI2 triggers both EXTI2 and EXTI1  (Read 678 times)

0 Members and 1 Guest are viewing this topic.

Offline ppTRNTopic starter

  • Regular Contributor
  • *
  • Posts: 169
  • Country: it
STM32F303 - EXTI2 triggers both EXTI2 and EXTI1
« on: March 22, 2025, 03:16:19 pm »
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: [Select]
#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 variable
accordingly, 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 reference
D3 (PB0): Phase B
A7 (PA2): Pushbutton EXTI2

*/

extern int encoderCounter;

void EXTI1Config(){
RCC->AHBENR |= RCC_AHBENR_GPIOFEN; // Enable GPIOF clock
RCC->AHBENR |= RCC_AHBENR_GPIOBEN; // Enable GPIOB clock
RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN; // Enable SysConfig clock

GPIOF->MODER &= ~(1<<2); // GPIOF1 in input mode
GPIOF->MODER &= ~(1<<3);

GPIOF->PUPDR |= 1<<2; // GPIOF1 internal pullup enable
GPIOF->PUPDR &= ~(1<<3);

SYSCFG->EXTICR[0] |= 1<<0 | 1<<2; // EXTI1 assigned to GPIOF0
EXTI->IMR |= 1<<1; // EXTI1 unmasked
EXTI->FTSR |= 1<<1; // EXTI1 sensitive to falling edge

GPIOB->MODER &= ~(1<<0); // GPIOB0 in input mode
GPIOB->MODER &= ~(1<<1);

GPIOB->PUPDR |= 1<<0; // GPIOB0 internal pullup enable
GPIOB->PUPDR &= ~(1<<1);

NVIC_EnableIRQ(EXTI1_IRQn);
//NVIC_SetPriority(EXTI1_IRQn, 2);
}

void EXTI2Config(){
RCC->AHBENR |= RCC_AHBENR_GPIOAEN; // Enable GPIOA clock
RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN; // Enable SysConfig clock

GPIOA->MODER &= ~(1<<4); // GPIOA2 in input mode
GPIOA->MODER &= ~(1<<5);

GPIOA->PUPDR |= 1<<4; // GPIOA2 internal pullup enable
GPIOA->PUPDR &= ~(1<<5);

SYSCFG->EXTICR[0] |= 0; // EXTI2 assigned to GPIOA2 by default
EXTI->IMR |= 1<<2; // EXTI2 unmasked
EXTI->FTSR |= 1<<2; // EXTI2 sensitive to falling edge

NVIC_EnableIRQ(EXTI2_TSC_IRQn);
//NVIC_SetPriority(EXTI2_TSC_IRQn, 3);
}

void EXTI1_IRQHandler(){
EXTI->PR |= 1<<1; // Clear pending EXTI1 interrupt flag

if(GPIOB->IDR & (1<<0)) // Depending on the direction of the rotation
encoderCounter++; // it increments
else encoderCounter--; // or decrements a variable
}

void EXTI2_TSC_IRQHandler(){
EXTI->PR |= 1<<2; // Clear pending EXTI2 interrupt flag
encoderCounter = 0; // Pushing the button resets the variable
}
 

Offline Dazed_N_Confused

  • Contributor
  • Posts: 12
  • Country: us
Re: STM32F303 - EXTI2 triggers both EXTI2 and EXTI1
« Reply #1 on: March 22, 2025, 03:23:19 pm »
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: [Select]
// For PF1 (EXTI1)
SYSCFG->EXTICR[0] |= (5 << 4);  // 5 is the value for Port F, shifted to bits 4-7 for EXTI1

// For PA2 (EXTI2)
SYSCFG->EXTICR[0] |= (0 << 8);  // 0 is the value for Port A, shifted to bits 8-11 for EXTI2
Windows: Tailor-Made Technology for the Soccer Mom!
Linux: Unleashing the Alpha Male in the Digital Domain!
"Premature optimization is the root of all evil" ~ Donald Knuth
 

Offline ppTRNTopic starter

  • Regular Contributor
  • *
  • Posts: 169
  • Country: it
Re: STM32F303 - EXTI2 triggers both EXTI2 and EXTI1
« Reply #2 on: March 22, 2025, 03:36:11 pm »
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!
 

Offline Dan N

  • Contributor
  • Posts: 25
  • Country: us
Re: STM32F303 - EXTI2 triggers both EXTI2 and EXTI1
« Reply #3 on: March 25, 2025, 12:56:01 pm »
Code: [Select]
EXTI->PR |= 1<<1; // Clear pending EXTI1 interrupt flag
...
EXTI->PR |= 1<<2; // Clear pending EXTI2 interrupt flag

Not 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: [Select]
EXTI->PR = 1<<1; // Clear pending EXTI1 interrupt flag
« Last Edit: March 25, 2025, 12:58:33 pm by Dan N »
 

Offline ppTRNTopic starter

  • Regular Contributor
  • *
  • Posts: 169
  • Country: it
Re: STM32F303 - EXTI2 triggers both EXTI2 and EXTI1
« Reply #4 on: March 26, 2025, 06:42:52 am »
That is a precious advice, thank you. Another user in another thread already warned be about it but i forgot to implement it.
Thanks!
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf