void USART1_IRQHandler(void)//串口1中断服务程序
{
u8 Res;
#ifdef SYSTEM_SUPPORT_OS
OSIntEnter();
#endif
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)//接收中断(接收到的数据必须是0x0d 0x0a结尾)
{
Res =USART_ReceiveData(USART1);
//(USART1->DR);
//读取接收到的数据if((USART_RX_STA&0x8000)==0)//接收未完成
{
if(USART_RX_STA&0x4000)//接收到了0x0d
{
if(Res!=0x0a)USART_RX_STA=0;
//接收错误,重新开始
else USART_RX_STA|=0x8000;
//接收完成了
}
else //还没收到0X0D
{
if(Res==0x0d)USART_RX_STA|=0x4000;
else
{
USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
USART_RX_STA++;
if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;
//接收数据错误,重新开始接收
}
}
}
}
#ifdef SYSTEM_SUPPORT_OS
OSIntExit();
#endif
}
以串口中断服务函数为例
void XXX_IQHandler()
{
OSIntEnter();
用户自己编写的服务程序
OSIntExit();
}
其中OSIntEnter()函数
voidOSIntEnter (void)
{
if (OSRunning != OS_STATE_OS_RUNNING) {/* Is OS running?*/
return;
/* No*/
}if (OSIntNestingCtr >= (OS_NESTING_CTR)250u) {/* Have we nested past 250 levels?*/
return;
/* Yes*/
}OSIntNestingCtr++;
/* Increment ISR nesting level*/
}
主要作用:
1、系统是否运行,没运行退出
2、记录中断嵌套次数
3、中断嵌套次数是否超出250次,超出退出
OSIntExit()函数:
voidOSIntExit (void)
{
CPU_SR_ALLOC();
if (OSRunning != OS_STATE_OS_RUNNING) {/* Has the OS started?*/
return;
/* No*/
}CPU_INT_DIS();
if (OSIntNestingCtr == (OS_NESTING_CTR)0) {/* Prevent OSIntNestingCtr from wrapping*/
CPU_INT_EN();
return;
}
OSIntNestingCtr--;
if (OSIntNestingCtr > (OS_NESTING_CTR)0) {/* ISRs still nested?*/
CPU_INT_EN();
/* Yes*/
return;
}if (OSSchedLockNestingCtr > (OS_NESTING_CTR)0) {/* Scheduler still locked?*/
CPU_INT_EN();
/* Yes*/
return;
}OSPrioHighRdy= OS_PrioGetHighest();
/* Find highest priority*/
OSTCBHighRdyPtr = OSRdyList[OSPrioHighRdy].HeadPtr;
/* Get highest priority task ready-to-run*/
if (OSTCBHighRdyPtr == OSTCBCurPtr) {/* Current task still the highest priority?*/
CPU_INT_EN();
/* Yes*/
return;
}#if OS_CFG_TASK_PROFILE_EN > 0u
OSTCBHighRdyPtr->CtxSwCtr++;
/* Inc. # of context switches for this new task*/
#endif
OSTaskCtxSwCtr++;
/* Keep track of the total number of ctx switches*/#if defined(OS_CFG_TLS_TBL_SIZE) && (OS_CFG_TLS_TBL_SIZE > 0u)
OS_TLS_TaskSw();
#endifOSIntCtxSw();
/* Perform interrupt level ctx switch*/
CPU_INT_EN();
}
主要作用:
1、系统是否运行,没运行退出
2、关闭全局中断,包括系统滴答定时器中断
3、记录中断嵌套次数以及人物任务切换次数
【UCOS|UCOS III 中断管理】4、进入中断级任务调度