This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Intrinsic functions

I don't understand the following intrinsic functions

__get_interrupt_state() and __set_interrupt_state(x)

Can you guys explain what these functions do or point me to a source of information.

 

  • These are IAR intrinsics which can be located at:

    ftp://ftp.iar.se/WWWfiles/msp430/guides/EW430_CompilerReference.pdf

    Pages 237 and 239.

    Or if you like you can click on the help pulldown menu within IAR and look in that compiler reference on your machine.

     

    What's going on is you have a chunk of code which you want interrupts off before you execute, like so:

    void function(void) {

    __disable_interrupt();

    Code.....

    __enable_interrupt();

    }

    This function is dangerous in that if you called this function when interrupts WERE ALREADY disabled, it would turn them on! Which is probably not what you want. Instead, you do this:

    void function(void) {

    __istate_t s = __get_interrupt_state();

    __disable_interrupt();

    /* Do something here. */

    __set_interrupt_state(s);

    }

     

    This code is safe because it restores the "interrupt state" to what it previous was when it completes. If interrupts were disabled, they stay disabled. If interrupts were enabled, they are enabled again.

     

    Now that I've shown you where the compiler manual is, go read it. Five times.

  • Excellent. But what exactly is interrupt state? where does the function get it from. Is it the SR register?

    I googled for the IAR MSP430 userguide, and for intrinsic functions before I posted here. But couldn't find anything.

    Thanks for the doc.

    darkwzrd said:

    Now that I've shown you where the compiler manual is, go read it. Five times.

    5 times! 5*336pages = 1680 pages. I shall try!

     

  • Interrupt state refers to the GIE bit within the SR.

     

    Now that I've told you this.....go read the MSP430 users guide (which is about equivalent in size to the compiler manual times 5). Five times! ;)

  • darkwzrd said:

    Interrupt state refers to the GIE bit within the SR.

    If it's just the GIE bit, then why use unsigned short to store one bit.

     

     

  • Well, you got me. I wasn't very clear. The function is implemented by copying the entire SR (16 bits) into your RAM variable.

    However, the effect is that you are restoring the interrupt state, by in fact, restoring the entire SR. The other bits are don't care since they are used only in the realm of writing MSP430 code in assembler, which shouldn't be mixed with this C intrinsic.

    Just use the mechanism they specify in the manual (which is where I cut and paste my previous examples from). Don't try to mess with it, or you might end up accidentally setting sleep bits or something within the RAM variable before you restore it.

  • Well, reason for the intrinsic are two facts:

    1) the interrupt state, represented by the GIE bit, is stored in the SR register and nowhere in memory.
    2) C(++) does not know about registers. It only knows memory locations. There is no way in the C(++) language to access a register.

    The reason why a short is used is that the complete SR is copied. In assembly language, it is a single instruction. Everything else would require bit-banging or bit-extracting and result in much larger and slower code.

    And since the content of the status register is unimportant across C instructions, it is safe to do so.

    You'll get problems, however, when you alter other bits like OSCOFF or the LPM bits between saving and restoring state. It will undo your changes. But these manipulations are outside the C language too, so it's up to you to do 'the right thing'.

    P.s.: you cannot simply push the SR onto stack, as this would clobber the stack frame and mess up access to local variables. It is safe in inline assembly as long as you pop the SR back before the inline assembly block ends. It's not faster anyway.

  • Is it only the GIE bit that is saved and restored or the entire set of interrupt bits for every peripheral available.

    Though I understood it as the GIE bit only, but the IAR guide mentions it as interrupts (in plural form).

  • It's only the GIE bit. However,. the GIE bit is the 'global-interrupt-enable' bit. If it is clear, all other IE bits are meaningless and no interrupts will happen. (except those for NMI).

**Attention** This is a public forum