0

I am using the following simple code to test the interrupt functionality of due:

void aMillion(){
    for(int i = 0; i < 1000000; i++){
        if(i % 1000 == 0){
            Serial.print("m");
        }
    }
}

void setup(){
  Serial.begin(9600); 
  pinMode(2, INPUT);
  attachInterrupt(digitalPinToInterrupt(2), aMillion, CHANGE);
}
void loop()
{
}

obviously what we expect to see is a thousand "m" characters after each interrupt event. On Arduino Mega, this worked fine. But I have tested it on multiple Due boards.

Very odd news is that on the reset event of due, I do get a single "m" character printed.

Makan
  • 239
  • 4
  • 14
  • Just a thought, check http://arduino.stackexchange.com/questions/23006/digitalpintointerrupt-doesnt-work – Jesse Sep 16 '16 at 15:34
  • 1
    Doing a lot of stuff inside the ISR isn't advised. Especially if you are doing serial communication. What happens if you do just a single serial.print? – Gerben Sep 16 '16 at 15:52
  • 2
    I am surprised this could ever work on the Mega. The test i < 1000000 is always true due to the limited range of int type. And you get undefined behavior when i overflows. – Edgar Bonet Sep 16 '16 at 15:55
  • @EdgarBonet haha yea, but an automatic cast during the comparison would make the loop run forever. wouldn't it? That's what I saw with MEGA – Makan Sep 19 '16 at 09:46
  • I don't understand your comment. Could you be more explicit? I just compiled aMillion() for AVR and got “warning: comparison is always true due to limited range of data type”. And gcc optimized out the test i < 1000000. The generated assembly is just an infinite loop. – Edgar Bonet Sep 19 '16 at 10:38
  • @EdgarBonet I guessed that it might have been cast to 'long' automatically. But apparently that is not what happens. In any case you are right and this is an infinite loop. – Makan Sep 19 '16 at 11:35
  • @EdgarBonet Initially I had extra delays in the loop, So I never reached a thousand output characters. That is why I did not notice this in the beginning. – Makan Sep 19 '16 at 11:37
  • thanks @Jesse but that link was a totally different issue. That was a Pin mismatch. – Makan Sep 22 '16 at 09:54
  • as @EdgarBonet and Gerben pointed out, It is not possible to have very long ISRs in the DUE. For the MEGA however, an infinite loop including anything, will be executed with no problem. that is why the infinite loop ran a good time in the MEGA. I will post this as the answer – Makan Sep 22 '16 at 10:01

1 Answers1

0

An attempt to run long loops in the ISR for the DUE fails. This is the result of some sort of a run time monitoring system. An infinite loop like this one here, or even a loop with a thousand repetitions of a Serial Print will result in an early halt of ISR on the DUE.

On the MEGA however, This is not the case. ISRs can run much longer. (At least ten minutes as I tried)

Makan
  • 239
  • 4
  • 14