1

I see this in examples of ISR code but what is it?

2
  • 1
    cli() disables interrupts so that the ISR itself is not interrupted ... sei() enables the interrupts again
    – jsotola
    Commented Jul 19, 2019 at 3:49
  • ok. Thanks for all answers. This problem can be closed. I found the problem with the ISR routine and not knowing what the cli() was for just slowed me down a little.
    – Martin
    Commented Jul 21, 2019 at 13:22

1 Answer 1

2

Interrupts are normally turned off automatically when an ISR is called.

There are two instructions that allow software to turn off (cli) and on (sei) interrupts to create a sequence of non-interruptable instructions, i.e. an atomic operation or critical section.

In some MCU there are several levels of interrupt priority and an ISR with lower priority may be interrupted by an ISR with higher priority. An atomic operation may also be required in an ISR with lower priority.

There are also cases where an ISR may allow interrupts (with the same or lower priority) to occur after the critical section of the ISR.

Normally an ISR should not contain cli()-sei() especially for the AVR. The hardware will automatically turn off interrupts during the execution of the ISR.

Cheers!

2
  • Thank you both for the clarification. It's interesting that the ISR that I was looking at uses the cli() at its start but forgot the esi() when closing. But it's got other problems also. Regarding the Arduino as a SPI slave, I know I have good data coming in at a very low clock rate by checking with a Logic Analyzer but the Arduino(UNO or Mega) shows garbage (all '?'). It never sees the '\n' and I can only stop it by counting characters to set the received_data flag before ending the ISR. Weird. Can't figure out why.
    – Martin
    Commented Jul 20, 2019 at 4:31
  • Post the sketch or the ISR in a new question if you are happy with the answer to your original question. We try to keep a clean question-answer session to allow others to find and follow. Extending with additional questions does not help reuse. Commented Jul 20, 2019 at 6:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.