Arduino Forum

Using Arduino => General Electronics => Topic started by: -horn- on Jun 23, 2015, 10:21 pm

Title: What is the reaction time of the Arduino interrupt?
Post by: -horn- on Jun 23, 2015, 10:21 pm
Hi there,

I couldn't find an answer to my question on the internet, so I would like to ask heere.

What is the reaction time of the interrupt, if programmed with the arduion code?

I ask, because I want to trigger it with a 1 pulse per second GPS chip with a +-10 nanoseconds jitter and it must trigger faster than 250 nano seconds and then pulling up a pin from low to high.

I hope you have an answer for me :).

Best regards,


Andreas
Title: Re: What is the reaction time of the Arduino interrupt?
Post by: DrAzzy on Jun 23, 2015, 10:24 pm
Check the interrupts section of the datasheet, it tells you how many clock cycles that takes - but with 250ns, you have only 4 clock cycles to do it in, which I don't think is enough, no matter how you do it.
Title: Re: What is the reaction time of the Arduino interrupt?
Post by: jboyton on Jun 23, 2015, 10:52 pm
It may be possible to just barely do it using timer 1, assuming you're using an Atmega328 or something similar. What I think might work is to feed the PPS signal into the timer 1 external clock pin and configure the timer so that a single clock pulse will cause it to generate an output compare, thus setting the OC1A pin. According to the data sheet, the external clock pin is sampled each system clock cycle and then there is a delay of up to 3.5 clock cycles before the timer is clocked. So it might just get you under that 250ns requirement. Maybe. :-)

It would probably be easier to add some external logic.
Title: Re: What is the reaction time of the Arduino interrupt?
Post by: MarkT on Jun 23, 2015, 11:15 pm
I think you are looking for a flip-flop, not a microcontroller...
Title: Maybe Arduino is the wrong platform
Post by: arduinoaleman on Jun 23, 2015, 11:24 pm
I love Arduino, so it hurts me when i have to recommend you a Raspberry Pi when it comes to higher speed or higher level programming.

Title: Re: What is the reaction time of the Arduino interrupt?
Post by: DrAzzy on Jun 23, 2015, 11:31 pm
I agree with MarkT - what you need is a discrete part with the necessary speed to do the converting of the pulse to a pin transition - since presumably you need to do more than that, you could then use an Arduino alongside it, to do the other stuff (and enable/disable that part, etcetera).

I don't think this calls for a Pi, since you don't need fast processing, just fast reaction to a pulse, which is easy to do with a separate IC or two.
Title: Re: What is the reaction time of the Arduino interrupt?
Post by: Coding Badly on Jun 23, 2015, 11:37 pm
I love Arduino, so it hurts me when i have to recommend you a Raspberry Pi when it comes to higher speed or higher level programming.
Don't think it is up to the task either...
https://www.raspberrypi.org/forums/viewtopic.php?f=44&t=9207 (https://www.raspberrypi.org/forums/viewtopic.php?f=44&t=9207)

Title: Re: What is the reaction time of the Arduino interrupt?
Post by: Grumpy_Mike on Jun 24, 2015, 07:33 am
The Raspberry Pi is very poor at responding to interrupts due to Linux constantly interrupting any code. If you did want to use a Pi then you would have to run it as a Bair Metal system.
Title: Re: What is the reaction time of the Arduino interrupt?
Post by: JoeN on Jun 24, 2015, 07:54 am
I can't see why you don't want to use a T-flip flop like the other poster suggested.  That part is just about as cheap as you can get and can be had in pretty much any package.
Title: Re: What is the reaction time of the Arduino interrupt?
Post by: MarkT on Jun 24, 2015, 11:46 am
The Propeller microcontroller can respond to input changes with a granularity of 12.5ns and a delay
or about 100ns if you dedicate an entire cog to watching that pin.  Since the design philosophy of
that machine is about guaranteed cycle-accurate timing and uses multiple cores in place of interrupts,
it is a good match to a problem that would normally require special hardware.

Interrupt handling in most chips has jitter (it depends on the timing of the instructions being
interrupted - a divide instruction typically takes more clocks than an add for instance, so delays
the interrupt longer).  Also in most architectures a new stack frame is started (so a couple
of registers are written to stack and stack pointer altered) before your ISR code even starts running,
so there is a definite delay involved.  ISRs that react within 10 machine cycles would normally be
thought "ultra-fast".  In a full-blown CPU you'd expect the interrupt latency to be measured in
hundreds/thousands of cycles and timing completely at the mercy of the cache and memory
subsystem.  It doesn't matter what the OS policy is, you do not get +/-10ns repeatability from
an interrupt handler in a modern CPU...

I suspect you need some specialised hardware and an ISR to accomplish your task - though its
not impossible that one of the timer units can be pressed into service if you can clock it
externally and route one of its output compares to a pin too.

Some more information about what you are trying to do would help...
Title: Re: What is the reaction time of the Arduino interrupt?
Post by: AWOL on Jun 24, 2015, 03:01 pm
I love Arduino, so it hurts me when i have to recommend you a Raspberry Pi when it comes to higher speed or higher level programming.


Unfortunately, the Pi comes with a lot of other baggage, and I suspect it is also incapable of the sort of timings required by the OP.
Title: Re: What is the reaction time of the Arduino interrupt?
Post by: Grumpy_Mike on Jun 24, 2015, 06:31 pm
I ask, because I want to trigger it with a 1 pulse per second GPS chip with a +-10 nanoseconds jitter and it must trigger faster than 250 nano seconds and then pulling up a pin from low to high.
From what I can gather this signal is pulsing at the rate of one per second with a +-10 nanoseconds jitter. That means that the pulse can be 10 nanoseconds early or 10 nanoseconds late. Given that I am not sure what you want to do or why you want to do it.

Quote
it must trigger faster than 250 nano seconds and then pulling up a pin from low to high.
What and when does this pin go from a high to a low?
Title: Re: What is the reaction time of the Arduino interrupt?
Post by: polymorph on Jun 24, 2015, 08:07 pm
In ignorance, the Due or Teensy 3.1? Both use an ARM Cortex running at a clock speed considerably faster than a 16MHz Arduino without the baggage of Linux on a Pi.

A flip flop does seem simpler, depending on what you intend on doing with this time-sensitive information.
Title: Re: What is the reaction time of the Arduino interrupt?
Post by: polymorph on Jun 24, 2015, 08:17 pm
High Resolution PulsePosition library on the Teensy 3.1 (http://www.pjrc.com/teensy/td_libs_PulsePosition.html)

Quote
PulsePosition Library

PulsePosition can transmit and receive PPM (Pulse Position Modulated) signals commonly used to control RC aircraft and servo motors. Up to 8 simultaneous input and/or output PPM streams may be used, with each stream conveying up to 16 signals.
PulsePosition is in beta testing. Please get the latest code from GitHub:
https://github.com/PaulStoffregen/PulsePosition

PulsePosition is designed for 0.02 ┬Ás accuracy (approx 24X better than most Arduino implementations using AVR Timer1) with tolerance for significant interrupt latency caused by other libraries. All output waveforms are generated by hardware timer compare and all input waveforms are read using hardware timer input capture, for extremely precise timing. Pin change interrupts, which add error due to interrupt latency, are never used.
So, 20ns accuracy.
Title: Re: What is the reaction time of the Arduino interrupt?
Post by: MarkT on Jun 24, 2015, 08:54 pm
In ignorance, the Due or Teensy 3.1? Both use an ARM Cortex running at a clock speed considerably faster than a 16MHz Arduino without the baggage of Linux on a Pi.

Doesn't mean it will respond to interrupts any faster...  It has a lot more hardware baggage (an
instruction cache I think, as well as lots of registers).
Title: Re: What is the reaction time of the Arduino interrupt?
Post by: polymorph on Jun 25, 2015, 08:59 pm
Well, PulsePosition library claims 20ns response times. That's what I'm going by.

Good point about interrupt speed.
Title: interrupts
Post by: arduinoaleman on Jun 25, 2015, 10:14 pm
If it is Linux or something else ... interrupts will be handled immediately (within a few clock cycles). The new Raspberry Pi runs at 900 MHz having 4 cores. Only one of them is needed to handle the interrupt routine.

And when it comes to speed - 900 MHz vs 16 MHz is just like comparing a Ferrari with a bycicle.




Title: Re: What is the reaction time of the Arduino interrupt?
Post by: AWOL on Jun 25, 2015, 10:21 pm
If it is Linux or something else ... interrupts will be handled immediately (within a few clock cycles). The new Raspberry Pi runs at 900 MHz having 4 cores. Only one of them is needed to handle the interrupt routine.
Show us the scope/logic analyser traces.
Title: Re: What is the reaction time of the Arduino interrupt?
Post by: jboyton on Jun 25, 2015, 10:37 pm
I tried out the timer idea I had, assuming an Atmega328 based "Arduino". It looks like it actually works although it's not that straightforward. Because the timer compare output is double buffered, getting it to generate a signal in one clock cycle (and then clearing the latch afterward) required feeding it internal clock pulses, then switching over to the external source. I don't have an oscilloscope, so to measure the delay I used a second Atmega328 and slowed the target processor down to 1/256 it's normal clock rate so I had enough measurement resolution.

I never saw it take more than 2.7 clocks, which at 16MHz would be about 170ns. Now maybe there is additional delay that my timing method couldn't capture. But I think it would work. Atmel says 3.5 clocks worst case. That would be 219ns.

This is the code (with the clock division code I used for testing deleted):

Code: [Select]
#define T1_INPUT_PIN          5   // 328 pin 11
#define OC1A_OUTPUT_PIN       9   // 328 pin 15

void setup()
{
  pinMode(OC1A_OUTPUT_PIN, OUTPUT);  // OC1A timer 1 compare output, digital pin 9, chip pin 15
  pinMode(T1_INPUT_PIN, INPUT);
 
  TCCR1A = 0;           // Reset timer
  TCCR1B = 0;
  OCR1A = 1;            // Output compare = 1
  TCCR1A = 0xC0;        // set 0C1A on compare match
  TCCR1B = 0x0F;        // CTC, external clock T1, clock on rising edge
  TCNT1 = 0;            // counter = 0
}

void loop()
{
  if (digitalRead(OC1A_OUTPUT_PIN)) {
    // Do something...
    // Instead of polling this could be interrupt driven via the timer 1 compare ISR

    delay(100);  // just a placeholder

    // When whatever we're driving with this pin needs to go low again, do this:
    //
    resetLatch();
  }
}

void resetLatch()
{
  TCCR1A = 0x40;    // toggle OC1A on compare match
  TIFR1 = 0x02;     // clear compare flag
  TCCR1B = 0x0C;    // CTC, internal clock, prescaler = 256
  while ((TIFR1 & 0x02) == 0) {}  // wait for toggle
  TCCR1B = 0;       // disable clock
  TCCR1A = 0;

  OCR1A = 1;            // output compare = 1
  TCCR1A = 0xC0;        // set 0C1A on compare match
  TCCR1B = 0x0D;        // CTC, prescaler = 1024
  TCNT1 = 0;
  delayMicroseconds(64+8);  // clock period is 64us, make sure we get one clock
  TCCR1B = 0x0F; // CTC, external clock T1, clock on rising edge
}


It's still probably simpler to use a 7474 or whatever the modern version of that part is.
Title: Interrupts
Post by: arduinoaleman on Jun 25, 2015, 10:48 pm
It is NOT the operating system that controls the time until an interrupt is handled. Why do you not have a look at the datasheet of the ARM Cortex-A7 Quad?

When an interrupt comes in, the microprocessor will finish the current instruction, save the return address on the stack and start immediately to handle the interrupt routine.

This usually requires only 3 to 5 clock cycles.

And if one chip runs at 16MHz and the other one at 900MHz - which one do you think will start and finish the interruprt routine faster?





Title: Re: What is the reaction time of the Arduino interrupt?
Post by: AWOL on Jun 25, 2015, 11:11 pm
Quote
When an interrupt comes in, the microprocessor will finish the current instruction, save the return address on the stack and start immediately to handle the interrupt routine.
You missed out the vectors there.

Like I said, show us the logic analyser or scope trace.
Title: Re: What is the reaction time of the Arduino interrupt?
Post by: Coding Badly on Jun 26, 2015, 12:05 am
It is NOT the operating system that controls the time until an interrupt is handled.
Actually, it is.  An operating system like Linux is free to mask interrupts as it pleases.  Which includes prioritizing interrupt handling and even outright ignoring interrupts.

In any case, you are proposing @-horn- write a Linux kernel interrupt service routine.  I have no doubt, given enough time / money / interest, @-horn- is up to the task.

However, the effort is surely a waste given the fact that a readily available part costing significantly less than $1 is a perfect solution for the task.

However, the effort is surely a waste given the fact that @jboyton's, now publicly available, proposal has merit.

However, the effort is surely a waste given the fact that SAM-based Arduino compatible boards are readily available at a reasonable price.

However, the effort is surely a waste given the fact that the Propeller is readily available at a reasonable price.

Title: Re: What is the reaction time of the Arduino interrupt?
Post by: MorganS on Jun 26, 2015, 02:03 am
And when it comes to speed - 900 MHz vs 16 MHz is just like comparing a Ferrari with a bycicle.
I'm pretty sure that my bicycle can get a front wheel across the white line at the traffic lights before the Ferrari, and that's the speed that we're talking about here. Yes, the Ferrari might be 50m down the road before my back tyre crosses the line but the bike got there first.

A Ferrari is also much more difficult to get up onto the kerb and onto bike paths.
Title: Re: What is the reaction time of the Arduino interrupt?
Post by: polymorph on Jun 26, 2015, 02:46 am
I guess someone will have to test these.
Title: Re: What is the reaction time of the Arduino interrupt?
Post by: arduinoaleman on Jun 26, 2015, 02:54 am
I cannot imagine you wrote your answer on a PC/notebook that runs with less than 1 GHz. When it comes to speed, the MHz and GHz do count.

Your example (traffic light) is only valid when booting a system. A Ferrary can reach 100 km/h in less than 4 seconds. How many meters will you be able to cover in 4 seconds?

I do love my two Arduinos.

However, when it comes to real speed, the Arduino might not be the proper solution.


Title: Re: What is the reaction time of the Arduino interrupt?
Post by: polymorph on Jun 26, 2015, 02:58 am
Going back to what the original poster asked, I'd agree that a flip flop is the right chip for the job, without knowing anything else about what he/she is trying to do.

Hi there,

I couldn't find an answer to my question on the internet, so I would like to ask heere.

What is the reaction time of the interrupt, if programmed with the arduion code?

I ask, because I want to trigger it with a 1 pulse per second GPS chip with a +-10 nanoseconds jitter and it must trigger faster than 250 nano seconds and then pulling up a pin from low to high.

I hope you have an answer for me :).

Best regards,


Andreas
Title: Re: What is the reaction time of the Arduino interrupt?
Post by: arduinoaleman on Jun 26, 2015, 03:07 am
Absolutely right, as long as no code execution is required.
Title: Re: What is the reaction time of the Arduino interrupt?
Post by: MorganS on Jun 26, 2015, 07:01 am
The GHz doesn't matter in this case. If the operating system has a higher-priority interrupt then your incoming interrupt is going to get delayed by a random amount. The whole purpose is accurate timing, which is not possible when the computer has other jobs to do.

And that's before you consider multitasking, which will add all sorts of delays to unload and load register contexts.

If you're on the wrong on-ramp on the freeway interchange then a Ferrari will get you to the correct one very fast, but it would be quicker if you had got on the correct on-ramp in the first place.