about function EnterCritical

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

about function EnterCritical

5,065 Views
hgyxbll
Contributor I

I have used Processor Expert Software 10.0.0 and IAR.

And  Processor Expert Software generate the code:

-----------------------------------------------------------------------

/* Save status register and disable interrupts */

#define EnterCritical() \

do {\

  if (++SR_lock == 1u) { \

  SR_reg = (uint8_t)__get_PRIMASK(); \

  __set_PRIMASK(0x01ul); \

  } \

} while(0)

-----------------------------------------------------------------------

I am afraid that it may have some leak.

For example:

when main task run EnterCritical() to change some important thing .

If it run over  ++SR_lock and do not run  SR_lock == 1u,

then an interrupt occurs and run  EnterCritical() to change some important thing too.

Then the interrupt can not run  __set_PRIMASK(0x01ul) because  now SR_lock is 2.

When the interrupt run the important code, a high priority interrupt may occurs and  run  EnterCritical() to change some important thing too.

Then change of the important thing  may be conflict.

Tags (2)
0 Kudos
Reply
4 Replies

1,825 Views
JimDon
Senior Contributor III

If SR_lock >= 1 then interrupts are disabled, so no interrupt will run.

It may be that you do not need to do EnterCritical() in the interrupt handler, since the interrupt can not happen while in a critical section.

0 Kudos
Reply

1,825 Views
hgyxbll
Contributor I

>>If SR_lock >= 1 then interrupts are disabled

When  SR_lock =1, the interrupt may not disable.

Only after run "__set_PRIMASK(0x01ul)", the  interrupt are disabled.

so the order:

++SR_lock;

if(SR_lock == 1u)   --->  then may be broke by interrupt

{

SR_reg = (uint8_t)__get_PRIMASK();  

__set_PRIMASK(0x01ul);

}

>> It may be that you do not need to do EnterCritical() in the interrupt handler, since the interrupt can not happen while in a critical section.

Interrupt may be broke by a high priority interrupt.

So in the interrupt handler should  do EnterCritical() to avoid broke by  a high priority interrupt.

0 Kudos
Reply

1,825 Views
BlackNight
NXP Employee
NXP Employee

The problem outlined by hgyxb II is real :-(

EnterCritical() and ExitCritical(): Why Things are Failing Badly | MCU on Eclipse

I was running into that problem myself. Above post has a fixed implementation of the Enter/ExitCritical() macros.

I'm able to get my program failing with the current macros, while at least with the new macros things are running stable.

Will need more testing, but I'm confident that I have found a solution.

0 Kudos
Reply

1,825 Views
Petr_H
NXP Employee
NXP Employee

Hi,

we confirm the problem. Unfortunately, these macros are not safe for use within the interrupt routines. We are working to fix this for the next version of CodeWarrior (10.6) and DriverSuite (10.4). A workaround is to use plain DisableInt/EnableInt CPU component methods if possible or use workaround described by Erich Styger (see previous post).

Best regards

Petr Hradsky

Processor Expert Support Team

0 Kudos
Reply