原文链接:http://blog.csdn.net/wangzibigan/article/details/52350305
在uC/OS-II中,通常在进入中断时需要使用OSIntEnter() ;退出中断前使用OSIntExit();
分析一下OSIntEnter() 的代码
- <span style="font-size:18px;">void OSIntEnter (void)
- {
- if (OSRunning == OS_TRUE) {
- if (OSIntNesting < 255u) {
- OSIntNesting++;
- }
- }
- }</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) {
- OSIntNesting--;
- }
- if (OSIntNesting == 0u) {
- if (OSLockNesting == 0u) {
- OS_SchedNew();
- OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy];
- if (OSPrioHighRdy != OSPrioCur) {
- #if OS_TASK_PROFILE_EN > 0u
- OSTCBHighRdy->OSTCBCtxSwCtr++;
- #endif
- OSCtxSwCtr++;
- OSIntCtxSw();
- }
- }
- }
- OS_EXIT_CRITICAL();
- }
- }
函数的前面部分对OSIntNesting减1,刚好与OSIntEnter() 相对应;后面部分则进行任务调度。
由此可知,在中断服务函数中使用这对函数,乃是为了在中断结束后可进行任务调度,使得系统更加“实时”。假如中断服务函数里没有进行任何的与任务有关操作,而且时间比较短,是没必要使用这对函数的。譬如,我们使用定时器模拟串口发送,中断服务函数里只是进行IO口的翻转,这个时候加入OSIntExit()简直是不可忍受的,因为开销太大,大大地影响模拟串口的波特率。