Skip to main content
Craig1
Associate
November 19, 2019
Solved

How to disable/MASK an exti line ON THE FLY

  • November 19, 2019
  • 19 replies
  • 6977 views

I'm currently trying to implement a button interrupt and need to disable the button interrupt after the initial call to the isr to avoid excessive interrupt triggering due to debounce. I'm using STM32CUBEIDE/MX currently. The button works as desired if I use

HAL_NVIC_DisableIRQ(EXTI9_5_IRQn); (in interrupt)

//do some stuff

HAL_NVIC_EnableIRQ(EXTI9_5_IRQn); (in main)

but this is non-ideal as it disables exti lines 5 through 9.

I've been trying to mask the EXTI line using EXTI_IMR. This seems to work for the first button press, but I'm never able to re-trigger the ISR

EXTI->IMR &= ~(EXTI_LINE_5);//in ISR

EXTI->IMR &= (EXTI_LINE_5);//in main

Thoughts?

This topic has been closed for replies.
Best answer by Craig1

DOH! forgot my coffee this morning, sorry this works:

EXTI->IMR &= ~(EXTI_LINE_5);//in ISR

EXTI->IMR |= (EXTI_LINE_5);//in main

19 replies

MStra.3
Associate III
May 4, 2021

OK, I found the solution. EXTI_LINE_5 is defined as some large number like: 0x600005. It appears that line 5 corresponds to bit 5 so the following works for me:

EXTI->IMR &= 0xFFFFFFDF; To disable IRQ on line 5

EXTI->IMR |= 0x00000020; To enable IRQ on line 5

Mike.

waclawek.jan
Super User
May 5, 2021

This is a well deserved punishment for using Cube/HAL and its haphazardly defined symbols, instead of direct register access and symbols from CMSIS-mandated device headers.

Cube is good only as long as you stay within clicking distance of CubeMX. Anything other than the "usual usage" as envisaged by Cube's authors leave you vulnerable to Cube's innards. Here, definition of given symbol changed with changing Cube version, who could tell that when this thread started. I tried to find that symbol in some older CubeF4 I happen to have on my disk, but it was not there are all.

JW

Piranha
Principal III
May 22, 2021

It should be noted that operations on EXTI_IMR bits are not atomic. Therefore to use them from interrupts or multiple threads, they must be enclosed in critical sections. One can use OS/platform provided functions or a code like this:

https://community.st.com/s/question/0D50X0000BVmd12SQB/stm32l4-critical-section