Hi All, first post having recently got a Due.
I've noticed a difference in the behaviour of interrupts on a Due, after comparison to an Uno (well actually a Duemilanove).
When I attach an interrupt to FALLING on a digital pin, and drive the pin as an OUTPUT between HIGH and LOW, I would expect the interrupt to trigger each time the output pin is set low. This happens on the Uno, but NOT on the Due, where the interrupt only seems to fire once.
Note that I've tested this both with and without a load attached to the output pin (an LED).
Simple test case posted below.
** UPDATE**
After a fruitless search through the SAM 3x datasheet, I came to the conclusion this was something specific in the arduino library. On a bit of a hunch, I replaced digitalWrite with digitalWriteDirect (thanks to Stimmer):
inline void digitalWriteDirect(int pin, boolean val){
if(val) g_APinDescription[pin].pPort -> PIO_SODR = g_APinDescription[pin].ulPin;
else g_APinDescription[pin].pPort -> PIO_CODR = g_APinDescription[pin].ulPin;
}
And the counter now increases on the Due, just like it does in the Uno!
Does digitalWrite disable the interrupt, which is then missed? Why isn't it missed on the Uno? And surely digitalWrite shouldn't cause interrupts to be missed completely - just postpone them until it finishes?
Anyone have any ideas? Is this a bug in the Due core library?
Original test case below:
// "Self-driven" Interrupt comparison between Uno & Due
// Expected output (obtained on Uno)
// Pin high, count=0
// Pin low, count=1
// Pin high, count=1
// Pin low, count=2
// BUT...
// On due, count remains 1
int pin = 3; // INT 1 on UNO
volatile int i = 0;
void test() { i++; }
void setup() {
Serial.begin(9600);
pinMode(pin, OUTPUT);
// attachInterrupt(1, test, FALLING); // Uno
attachInterrupt(pin, test, FALLING); // Due
}
void loop() {
digitalWrite(pin, HIGH);
Serial.print("Pin high, count="); Serial.println(i);
delay(500);
digitalWrite(pin, LOW);
Serial.print("Pin low, count="); Serial.println(i);
delay(500);
}