Is it safe to nest calls to DisableGlobalIRQ()?

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

Is it safe to nest calls to DisableGlobalIRQ()?

跳至解决方案
2,285 次查看
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?

标签 (1)
0 项奖励
回复
1 解答
2,058 次查看
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!
-----------------------------------------------------------------------------------------------------------------------

在原帖中查看解决方案

3 回复数
2,058 次查看
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 项奖励
回复
2,058 次查看
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 项奖励
回复
2,059 次查看
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!
-----------------------------------------------------------------------------------------------------------------------