[BY DESIGN] Slow response to external ISR when using AttachInterrupt

LibMaple (The core that Roger's repo uses)
buaus
Posts: 2
Joined: Wed Aug 23, 2017 4:33 pm

[BY DESIGN] Slow response to external ISR when using AttachInterrupt

Post by buaus » Wed Aug 23, 2017 4:40 pm

Hi,
I tried today the attachInterrupt function and I saw that this slow approx 5 to 10usec. Is there any chance to speed up to approx 200nsec?
buaus

User avatar
Pito
Posts: 1592
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: AttachInterrupt for STM32F103

Post by Pito » Wed Aug 23, 2017 5:10 pm

How did you measure the 5-10us interrupt latency?
Pukao Hats Cleaning Services Ltd.

victor_pv
Posts: 1681
Joined: Mon Apr 27, 2015 12:12 pm

Re: AttachInterrupt for STM32F103

Post by victor_pv » Wed Aug 23, 2017 8:46 pm

Is that the latency you measure between driving a pin to a certain level and your function being called? that seems like a lot.
There is some overhead because the interrupt will not call your function right away, instead it calls a function in the core, that keeps a table of what functions to call for each interrupt, and that's going to call yours, but but still 10uS are 720 cpu cycles, seems a bit too much.

peekay123
Posts: 18
Joined: Tue Nov 08, 2016 8:39 pm

Re: AttachInterrupt for STM32F103

Post by peekay123 » Thu Aug 24, 2017 6:30 pm

Well, kind of. For external interrupts that share a common IRQ (exti9_5 and exti15_10), the firmware will scan each of the interrupt pending bits (of the shared IRQ register) and, if defined, call the ISR pointed to in the vector table. If multiple ISRs fire at the same time or the interrupt pending bit is sampled later in the sampling loop then there will be delays in calling that specific ISR (scanning delay + time to service other ISR). This may or may not explain the 5us to 10us latency in your situation.

Take a look at exti.c and ext_interrupts.cpp.

buaus
Posts: 2
Joined: Wed Aug 23, 2017 4:33 pm

Re: AttachInterrupt for STM32F103

Post by buaus » Thu Aug 24, 2017 7:26 pm

Mandy thanks for the many helpfull comments.
I used to measure the time a routine which was setting directly a Port Pin after receiving the Interrupt.
I saw in meantime also within an other thread this topic, which confirm "weakness" of the attachInterrupt function, so I set up today in IAR workbench a C_Project. Finally I reach now approx 470ns.😀
buaus

User avatar
RogerClark
Posts: 7145
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Slow response to external ISR when using AttachInterrupt

Post by RogerClark » Thu Aug 24, 2017 10:07 pm

The Arduino API is not written to be fast, so if you want quick response times, you would need to do more low level programming.

That being said, I'm surprised that the response time is as slow as you mentioned.

You didn't describe exactly how you tested this

I presume you must have an external signal generator, creating the pulses get the ISR code to set a GPIO pin and connect both the input pin and the GPIO to a logic analyser to measure the time delay

Presumably you have factored in the time to set the GPIO bit by using digitalWrite - which in its self is not that fast, or did you directly set the BSSR register after pre-defining the device and the pin data in your global setup ?

User avatar
RogerClark
Posts: 7145
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Slow response to external ISR when using AttachInterrupt

Post by RogerClark » Thu Aug 24, 2017 10:37 pm

I looked at how this operates and I'm not surprised its slow

Take a look at

https://github.com/rogerclarkmelbourne/ ... #L273-L292

But I presume LeafLabs had a reason to do it that way, rather than simply using a lookup table

victor_pv
Posts: 1681
Joined: Mon Apr 27, 2015 12:12 pm

Re: Slow response to external ISR when using AttachInterrupt

Post by victor_pv » Thu Aug 24, 2017 10:41 pm

RogerClark wrote:
Thu Aug 24, 2017 10:37 pm
I looked at how this operates and I'm not surprised its slow

Take a look at

https://github.com/rogerclarkmelbourne/ ... #L273-L292

But I presume LeafLabs had a reason to do it that way, rather than simply using a lookup table
I believe that's for the lines that share 1 interrupt vector for several lines.

User avatar
RogerClark
Posts: 7145
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Slow response to external ISR when using AttachInterrupt

Post by RogerClark » Thu Aug 24, 2017 11:56 pm

Victor.

Yes. Thats the worst case.

There do some to be some possibilities for optimisation, depending if the compiler has already done the optimisation...

If all the handlers were initially set to go to a dummy function, there would be no need to check whether the handler was valid

This would save 1 "if" function, and "if"s seem to be where ARM code gets slowed down

User avatar
Pito
Posts: 1592
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: Slow response to external ISR when using AttachInterrupt

Post by Pito » Fri Aug 25, 2017 8:47 am

Pukao Hats Cleaning Services Ltd.

Post Reply