5.25 __set_interrupt_state Intrinsic Function

The IAR __set_interrupt_state intrinsic function sets the global interrupt state to that saved by a previous call to the __get_interrupt_state intrinsic function.

Suggested Replacement

There is no MPLAB XC8 equivalent built-in function; however, a similar code sequence can be inserted explicitly using in-line assembly code.

Use in-line assembly to directly set the state of the register to that saved by previously called in-line assembly code.

Caveats

None

Examples

Consider migrating IAR code such as:
extern int mode;

void clearMode_safe()
{
  __istate_t s = __get_interrupt_state();
  __disable_interrupt();
  mode = 0x0;
  __set_interrupt_state(s);
}
to MPLAB XC8 code similar to:
#include <xc.h>

unsigned char get_interrupt_state(void) {
  unsigned char s;
  __asm__ ("in %0, __SREG__"
           :"=d" (s));
  return s;
}

void set_interrupt_state(unsigned char s) {
  __asm__ ("sbrs %0, 7 \n\t"
           "rjmp .+4 \n\t"
           "sei \n\t"
           "rjmp .+2 \n\t"
           "cli \n\t"
           : : "r" (s));
}

extern int mode;

void clearMode_safe()
{
  unsigned char s = get_interrupt_state();
  di();
  mode = 0x0;
  set_interrupt_state(s);
}

Further Information

See the In-line Assembly section in the MPLAB XC8 C Compiler User's Guide for AVR MCUs for more information on adding in-line assembly.