在uC/OS-II中,通常在进入中断时需要使用OSIntEnter() ;退出中断前使用OSIntExit();
分析一下OSIntEnter() 的代码
<span style="font-size:18px;">void OSIntEnter (void)
{
if (OSRunning == OS_TRUE) {
if (OSIntNesting < 255u) {
OSIntNesting++; /* Increment ISR nesting level */
}
}
}</span>
这个函数的作用是对全局变量OSIntNesting增1,OSIntNesting为中断嵌套深度。
再看看OSIntExit()
void OSIntExit (void)
{
#if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr = 0u;
#endif
if (OSRunning == OS_TRUE) {
OS_ENTER_CRITICAL();
if (OSIntNesting > 0u) { /* Prevent OSIntNesting from wrapping */
OSIntNesting--;
}
if (OSIntNesting == 0u) { /* Reschedule only if all ISRs complete ... */
if (OSLockNesting == 0u) { /* ... and not locked. */
OS_SchedNew();
OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy];
if (OSPrioHighRdy != OSPrioCur) { /* No Ctx Sw if current task is highest rdy */
#if OS_TASK_PROFILE_EN > 0u
OSTCBHighRdy->OSTCBCtxSwCtr++; /* Inc. # of context switches to this task */
#endif
OSCtxSwCtr++; /* Keep track of the number of ctx switches */
OSIntCtxSw(); /* Perform interrupt level ctx switch */
}
}
}
OS_EXIT_CRITICAL();
}
}
函数的前面部分对OSIntNesting减1,刚好与OSIntEnter() 相对应;后面部分则进行任务调度。
由此可知,在中断服务函数中使用这对函数,乃是为了在中断结束后可进行任务调度,使得系统更加“实时”。假如中断服务函数里没有进行任何的与任务有关操作,而且时间比较短,是没必要使用这对函数的。譬如,我们使用定时器模拟串口发送,中断服务函数里只是进行IO口的翻转,这个时候加入OSIntExit()简直是不可忍受的,因为开销太大,大大地影响模拟串口的波特率。