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.
1 Answer 1
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)
i < 1000000
is always true due to the limited range ofint
type. And you get undefined behavior wheni
overflows.aMillion()
for AVR and got "warning: comparison is always true due to limited range of data type". And gcc optimized out the testi < 1000000
. The generated assembly is just an infinite loop.