STM32F407VG microcontroller - multiple GPIO interrupts

Post here first, or if you can't find a relevant section!
pkrobot
Posts: 3
Joined: Sat Mar 21, 2020 4:57 am

STM32F407VG microcontroller - multiple GPIO interrupts

Post by pkrobot »

Unlike AVR based Arduino boards, my understanding is that interrupts can be configured on any GPIO pin on STM32 boards. I'm using STM32F407VG based board. STM32 processors have seven GPIO lines, and seven interrupt handlers. I need to configure 30 pins to interrupt lines. Fortunately, the service that the ISR has to provide is the same. On bare-bones STM32 microcontroller, I know how to do this. However, Arduino libraries add an additional layer of abstraction, and you need to use attachInterrupt (digitalPinToInterrupt(pin), ISR, mode).

My question is: I suppose I'll have to call attachInterrupt() for each of the 30 lines. Can I point to the same ISR in these attachInterrupt() calls, and use same mode for all 30 pins? Or perhaps I can use seven different ISRs, and within the ISR, I can call the same function? Has someone tried this before? What is the correct way in Arduino STM32 for doing something similar? I've tried looking at different forums, but couldn't find an answer.

Thanks
-pk
ag123
Posts: 1656
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: STM32F407VG microcontroller - multiple GPIO interrupts

Post by ag123 »

i'd guess you would need to read up on rm0009 reference manual STM32F405/415, STM32F407/417, STM32F427/437 and
STM32F429/439 advanced Arm®-based 32-bit MCUs
https://www.st.com/resource/en/referenc ... 031020.pdf
gpio and exti chapters to see how they work
i'm not too sure how the official core implement support for EXTI too, it probably isn't there for libmaple core and would be same as barebone implementation
the official core may have it but i'm not too sure about it, most likely the HAL library apis
User avatar
fpiSTM
Posts: 1745
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: STM32F407VG microcontroller - multiple GPIO interrupts

Post by fpiSTM »

stevestrong
Posts: 502
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 8
Location: Munich, Germany
Contact:

Re: STM32F407VG microcontroller - multiple GPIO interrupts

Post by stevestrong »

You can only use 16 GPIO interrupts at the same time.
But you may eventually use only one (timer triggered) interrupt and within that check the status of GPIO inputs. In this case you can monitor as many IOs as available.
pkrobot
Posts: 3
Joined: Sat Mar 21, 2020 4:57 am

Re: STM32F407VG microcontroller - multiple GPIO interrupts

Post by pkrobot »

Thanks for the quick feedback. I can use it in bare-bones implementation (outside of Arduino structure, as suggested), but wanted to use generic Arduino interrupt structure for uniformity. I’d considered poling (GPIO monitoring via timer interrupts), but wanted to consider GPIO interrupts first to avoid the additional overhead. I wanted to keep as many cycles available for other processing (graphics in this case), but it is possible that the overhead for timing interrupt is negligible in comparison, and the difference is not noticeable.

Talking about overheads, does STM32 implementation for Arduino provide reading of the entire GPIO port simultaneously, or do I have to do a digitalRead() individually on each pin? That would allow me to read all the GPIOs in max five instructions, instead of 30 instructions. I know that some implementations of Arduino provide special processor-specific variations in the library (e.g. Tweensy 3.x/4.0 based on ARM-based NXP chips provides digitalReadFast() for a single clock cycle read). If such instructions are provided, where can I find information on that?

Thanks
-pk
stas2z
Posts: 131
Joined: Mon Feb 24, 2020 8:17 pm
Answers: 8

Re: STM32F407VG microcontroller - multiple GPIO interrupts

Post by stas2z »

Basically, exti interrupt can be attached to any pin (0-15) but you have to choose only one port for every pin.
For example, you can attach it to pa3, pb4, pc5 but in this case you cant attach it to pb3 cuz pa3 already attached.
But be careful with exti, you circuit have to be designed well to avoid noise causing fake pin triggering. It can stuck your device by only handling noise glitches instead of doing expected job.

For reading ports fast, you can use bare metal registry access to read all values from port for example. Also most cores providing some nonstandard calls for faster reading/writing.
ag123
Posts: 1656
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: STM32F407VG microcontroller - multiple GPIO interrupts

Post by ag123 »

if you are thinking about reading a set of gpio pins in parallel, the easiest (and possibly a fastest) way is to read the gpio registers directly.e.g.

Code: Select all

uint32_t data = GPIOA->IDR
you can read a whole set of gpio pins that way in a single instruction
you can even hook a timer on dma to read from the register, thereafter it runs completely on hardware for the read into a buffer.
but as these use cases are pretty much non-standard i doubt there are standard functions for them.
digitalRead() is made for compatibility and convenience as the Arduino api is well understood
these (i.e. timers and dma) can be considered 'advanced' topics with the use of stm32 mcus, do some google search for them and it would turn up various app notes from ST. do also review the reference manual rm0009 given above, it is nearly the 'bible' for the chip you are working with.
pkrobot
Posts: 3
Joined: Sat Mar 21, 2020 4:57 am

Re: STM32F407VG microcontroller - multiple GPIO interrupts

Post by pkrobot »

The system is not in a noisy environment. The GPIO lines are coming from capacitive touch sensors, so there is no switch bounce either. But perhaps expecting external interrupts on 30 GPIO lines at the same time was too much. I’d thought that the GPIO lines would ORed together (like in TI’s MSP430) for generating the external interrupt, but after reading the documentation, I realize that they are multiplexed. So it seems that the optimal solution is to implement an external OR gate with 30 inputs using an op amp or a transistor, and resistors. OR all the GPIO inputs, as well as bring them to the microcontroller, and generate the interrupt using this ORed line. In the ISR, use direct register read, or configure DMA to put data in the buffers. For dealing with DMA, I’ll have to use bare-metal code, but haven’t read DMA documentation. Is direct DMA trigger on GPIO allowed? If not, then, software trigger should be available, and the GPIO external ISR can trigger DMA transfer, as well as post an event for the application code that new input is available.
stas2z
Posts: 131
Joined: Mon Feb 24, 2020 8:17 pm
Answers: 8

Re: STM32F407VG microcontroller - multiple GPIO interrupts

Post by stas2z »

afaik no direct exti->dma trigger possible, but it can be done using timer
stevestrong
Posts: 502
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 8
Location: Munich, Germany
Contact:

Re: STM32F407VG microcontroller - multiple GPIO interrupts

Post by stevestrong »

Periodic timer generated interrupt would cause the less overhead.
You can then read all inputs and debounce the inputs within the ISR. I think DMA would be an overkill in this case.
Post Reply

Return to “General discussion”