1

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

  • 1
    cli() disables interrupts so that the ISR itself is not interrupted ... sei() enables the interrupts again – jsotola Jul 19 '19 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 Jul 21 '19 at 13:22
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!

| improve this answer | |
  • 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 Jul 20 '19 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. – Mikael Patel Jul 20 '19 at 6:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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