attachInterrupt on HIGH level

Hi everyone,
i'm facing problems in implementing an interrupt triggered by HIGH level on a pin

int pin = 12;
volatile int state = LOW;

void setup()
{
  Serial.begin(9600);
  
  pinMode(pin, OUTPUT);
  digitalWrite(pin, LOW);
  Serial.println(state);
  pinMode(2, INPUT);
  Serial.println(state);
  attachInterrupt(2, blink, HIGH);
  Serial.println(state);
}

void loop()
{
  
  digitalWrite(pin, state);
}

void blink()
{
  delayMicroseconds(20000);
  state = !state;
}

the answer i have from the serial monitor is

0
0
1

Therefore i can only suppose that the attachInterrupt function changes in sort of of way the pin status. How come?

Firstly delayMicroseconds() doesn't work for large values (known bug), which is why
you are seeing the state change immediately.

Secondly why are you trying to delay for 20ms in an interrupt routine, that would be
locking up the whole system and breaking millis(), micros() and delay() due to missed
timer interrupts?

Interrupt routines should run fast and lean - many things don't work while it
them (such as delay() and serial IO), since interrupts are off.

Set some variables, digitalRead, digitalWrite, analogWrite, that's the sort of thing to do in an interrupt routine - the heavy lifting (and waiting) is for loop().

[ Oh yes, the other thing, you probably don't want to use attachInterrupt on HIGH, try
RISING ]

[ ARGH, sorry, forgot this was DUE forum - is this the Due? Don't know about
delayMicroseconds () on that, it may well work, but the issues of delaying in an
ISR still apply ]

Right, proper reply for Due:

You really never ever want to use HIGH as an interrupt condition on the Due.

If you do the whole system will block while the pin in question is HIGH since the
interrupt status never clears so the ISR runs repeatedly so long as the pin is high.

Furthermore even if the pin is LOW when you first attach the interrupt it runs
once (probably because some status hasn't been properly cleared).

Just using RISING or CHANGE like one normally would.

i need to use attachInterrupt HIGH. my sam3x is connected to a PIC10F204 which controls the board's switching off. when i press the power button for more than 2 seconds the PIC sends an HIGH signal to the SAM and i'm afraid RISING could be triggered from some kind of noise

That's a question for software debouncing surely.

The Due also has glitch filtering which should stop triggering from electrical noise

Page 647 of the data sheet

Something like this, I haven't tested it yet.

 void deglitch(int pin)
{
  g_APinDescription[pin].pPort->PIO_SCIFSR |= g_APinDescription[pin].ulPin; // Glitch Input Filter Select Register
  g_APinDescription[pin].pPort->PIO_IFER |= g_APinDescription[pin].ulPin;
}

Since i'm using ethernet pins PB3 and PB4 should the function be like this?

void deglitch(Pio* pPio, uint32_t bitMask)
{
    pPio -> PIO_SCIFSR |= bitMask; // Glitch Input Filter Select Register
    pPio -> PIO_IFER |= bitMask;
}