Is it safe to nest calls to DisableGlobalIRQ()?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Is it safe to nest calls to DisableGlobalIRQ()?

Jump to solution
1,196 Views
pcpro178
Contributor III

I'm working on driver development for the MKL17Z256VLH4.  Is it safe to nest calls to DisableGlobalIRQ()?  How is the primask value used?

Labels (1)
0 Kudos
1 Solution
969 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi Jim,

    About your code, if in the /* ...do some work... */ area, you modify the primask, I think the next

EnableGlobalIRQ(primask);

will be changed, I think this usage may have the risk.

Even if your /* ...do some work... */ didn't modify the primask, I think your code still not very good.

It's better to control the GlobalDisableIRQ and EnableGlobalIRQ in pair, don't have other pair between GlobalDisableIRQ and EnableGlobalIRQ.

I think you can use it like this:

void bar(void)

{

    uint32_t primask = GlobalDisableIRQ();

    /* ...do some work... */

EnableGlobalIRQ(primask);

    foo();

primask = GlobalDisableIRQ();

    /* ...do some work... */

    EnableGlobalIRQ(primask);

}


Have a great day,
Kerry

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

View solution in original post

3 Replies
969 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi Jim,

   You can use the DisableGlobalIRQ, about the primask, it will be returned after your call DisableGlobalIRQ.

You can check the DisableGlobalIRQ function:

static inline uint32_t DisableGlobalIRQ(void)
{
#if defined(CPSR_I_Msk)
    uint32_t cpsr = __get_CPSR() & CPSR_I_Msk;

    __disable_irq();

    return cpsr;
#else
    uint32_t regPrimask = __get_PRIMASK();

    __disable_irq();

    return regPrimask;
#endif
}

About the usage, you can refer to the following code:

 uint32_t irqMaskValue;

irqMaskValue = DisableGlobalIRQ();

    EnableGlobalIRQ(irqMaskValue);

Wish it helps you!


Have a great day,
Kerry

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
969 Views
pcpro178
Contributor III

Thanks, Kerry, but my question is about nesting calls to GlobalDisableIRQ(), such as...

void foo(void)

{

    uint32_t primask = GlobalDisableIRQ();

    /* ...do some work... */

    EnableGlobalIRQ(primask);

}

void bar(void)

{

    uint32_t primask = GlobalDisableIRQ();

    /* ...do some work... */

    foo();

    /* ...do some work... */

    EnableGlobalIRQ(primask);

}

Would the above code be valid, such that interrupts would only be re-enabled in function bar, or would interrupts be enabled in function foo, called from function bar?

0 Kudos
970 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi Jim,

    About your code, if in the /* ...do some work... */ area, you modify the primask, I think the next

EnableGlobalIRQ(primask);

will be changed, I think this usage may have the risk.

Even if your /* ...do some work... */ didn't modify the primask, I think your code still not very good.

It's better to control the GlobalDisableIRQ and EnableGlobalIRQ in pair, don't have other pair between GlobalDisableIRQ and EnableGlobalIRQ.

I think you can use it like this:

void bar(void)

{

    uint32_t primask = GlobalDisableIRQ();

    /* ...do some work... */

EnableGlobalIRQ(primask);

    foo();

primask = GlobalDisableIRQ();

    /* ...do some work... */

    EnableGlobalIRQ(primask);

}


Have a great day,
Kerry

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------