OSIntEnter() 与 OSIntExit()

原文链接:http://blog.csdn.net/wangzibigan/article/details/52350305

[html] 
view plain
 copy

  1.   

在uC/OS-II中,通常在进入中断时需要使用OSIntEnter() ;退出中断前使用OSIntExit();

分析一下OSIntEnter() 的代码

[cpp] 
view plain
 copy

  1. <span style=“font-size:18px;”>void  OSIntEnter (void)  
  2. {  
  3.     if (OSRunning == OS_TRUE) {  
  4.         if (OSIntNesting < 255u) {  
  5.             OSIntNesting++;                      /* Increment ISR nesting level                        */  
  6.         }  
  7.     }  
  8. }</span>  

 这个函数的作用是对全局变量OSIntNesting增1,OSIntNesting为中断嵌套深度。

再看看OSIntExit()

[cpp] 
view plain
 copy

  1. void  OSIntExit (void)  
  2. {  
  3. #if OS_CRITICAL_METHOD == 3u                               /* Allocate storage for CPU status register */  
  4.     OS_CPU_SR  cpu_sr = 0u;  
  5. #endif  
  6.   
  7.   
  8.   
  9.     if (OSRunning == OS_TRUE) {  
  10.         OS_ENTER_CRITICAL();  
  11.         if (OSIntNesting > 0u) {                           /* Prevent OSIntNesting from wrapping       */  
  12.             OSIntNesting–;  
  13.         }  
  14.         if (OSIntNesting == 0u) {                          /* Reschedule only if all ISRs complete … */  
  15.             if (OSLockNesting == 0u) {                     /* … and not locked.                      */  
  16.                 OS_SchedNew();  
  17.                 OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy];  
  18.                 if (OSPrioHighRdy != OSPrioCur) {          /* No Ctx Sw if current task is highest rdy */  
  19. #if OS_TASK_PROFILE_EN > 0u  
  20.                     OSTCBHighRdy->OSTCBCtxSwCtr++;         /* Inc. # of context switches to this task  */  
  21. #endif  
  22.                     OSCtxSwCtr++;                          /* Keep track of the number of ctx switches */  
  23.                     OSIntCtxSw();                          /* Perform interrupt level ctx switch       */  
  24.                 }  
  25.             }  
  26.         }  
  27.         OS_EXIT_CRITICAL();  
  28.     }  
  29. }  

函数的前面部分对OSIntNesting减1,刚好与OSIntEnter() 相对应;后面部分则进行任务调度。

由此可知,在中断服务函数中使用这对函数,乃是为了在中断结束后可进行任务调度,使得系统更加“实时”。假如中断服务函数里没有进行任何的与任务有关操作,而且时间比较短,是没必要使用这对函数的。譬如,我们使用定时器模拟串口发送,中断服务函数里只是进行IO口的翻转,这个时候加入OSIntExit()简直是不可忍受的,因为开销太大,大大地影响模拟串口的波特率。

点赞