分类: 嵌入式
2015-07-21 22:26:29
问题来源:
好几个任务,其中一个优先级最高的任务优先级为5,处理数据。平时跑没事,但是进行数据压力测试的时候(就是狂发数据),发着发着发现发不了了,调试程序看看,程序没有跑飞,但是只执行一个任务了(这个任务不固定,有时候是我的其他任务之一,有时候是空闲任务,但是优先级都低于5),跟中了一下,发现没有发生任务调度,Systick中断正常进入,但是PendSV中断不进,原因是没有被触发,再深入跟踪,发现问题所在:在OSIntExit中,判断if (OSPrioHighRdy != OSPrioCur) ,才进行调度,但是OSPrioHighRdy = 5,这个是正常的,不正常的是OSPrioCur也是5,可是明明目前执行的任务优先级不是5且低于5. 进一步查看了每个任务的TCB(为此我专门为每个任务设置了名字,方便分辨),其内容也没有被篡改(至少优先级还是创建任务的时候分配的优先级),检查各个任务堆栈,也没有溢出(每个堆栈数组最开始的地方都有很多0,我的堆栈分配也足够大),系统堆栈也有0x600,1.5K,应该也足够了。所以一直找不到问题所在!OSPrioCur是在任务切换(PendSV中断ISR)中被赋值的,
LDR R0, =OSPrioCur ; OSPrioCur = OSPrioHighRdy;
LDR R1, =OSPrioHighRdy
LDRB R2, [R1]
STRB R2, [R0]
那难道在赋值过程中进行LDRB R2, [R1]时,OSPrioHighRdy改变了? 但是执行切换代码前都是关了中断的,应该是原子操作,不应该出现这种情况。或者切换后应该进入优先级为5的任务但是错误的跳转到了其他任务? 这也很难理解
终于还是在网上找到了答案,是ucos的BUG,2.86存在,2.88已经修改,需要小小调整OSIntExit() 和 OS_Sched()函数,原文如下:
OS_CORE.C:
OSIntExit() and OS_Sched() have changed slightly because of a boundary condition found with the Cortex-M3 port. Specifically, we needed to move the statement:
OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy];
Before testing for the priority.
可能有的朋友已经看到过相关帖子并且已经了然这个问题了,但是还是写出来给想我这样的没看到过的也一时半会没想到是ucos BUG问题的朋友提个醒。
但是不明白为什么把OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy]; 放到if前面会好!
请高手指点!
有问题的源码如下:
void OSIntExit (void)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr = 0;
#endif
if (OSRunning == OS_TRUE) {
OS_ENTER_CRITICAL();
if (OSIntNesting > 0) { /* Prevent OSIntNesting from wrapping */
OSIntNesting--;
}
if (OSIntNesting == 0) { /* Reschedule only if all ISRs complete ... */
if (OSLockNesting == 0) { /* ... and not locked. */
OS_SchedNew();
if (OSPrioHighRdy != OSPrioCur) { /* No Ctx Sw if current task is highest rdy */
OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy];
#if OS_TASK_PROFILE_EN > 0
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();
}
}
问题是这个BUG出现的原因未知。留待以后慢慢嚼吧。
看看uC/OS-III版本的如下
void OSIntExit (void)
{
CPU_SR_ALLOC();
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();
#endif
OSIntCtxSw(); /* Perform interrupt level ctx switch */
CPU_INT_EN();
}
uc/os_v2.88版本
void OSIntExit (void)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr = 0;
#endif
if (OSRunning == OS_TRUE) {
OS_ENTER_CRITICAL();
if (OSIntNesting > 0) { /* Prevent OSIntNesting from wrapping */
OSIntNesting--;
}
if (OSIntNesting == 0) { /* Reschedule only if all ISRs complete ... */
if (OSLockNesting == 0) { /* ... 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 > 0
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();
}
}
uc/os_v2.88版本
void OS_Sched (void)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr = 0;
#endif
OS_ENTER_CRITICAL();
if (OSIntNesting == 0) { /* Schedule only if all ISRs done and ... */
if (OSLockNesting == 0) { /* ... scheduler is not locked */
OS_SchedNew();
OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy];
if (OSPrioHighRdy != OSPrioCur) { /* No Ctx Sw if current task is highest rdy */
#if OS_TASK_PROFILE_EN > 0
OSTCBHighRdy->OSTCBCtxSwCtr++; /* Inc. # of context switches to this task */
#endif
OSCtxSwCtr++; /* Increment context switch counter */
OS_TASK_SW(); /* Perform a context switch */
}
}
}
OS_EXIT_CRITICAL();
}
uc/os_v2.86版本
void OS_Sched (void)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr = 0;
#endif
OS_ENTER_CRITICAL();
if (OSIntNesting == 0) { /* Schedule only if all ISRs done and ... */
if (OSLockNesting == 0) { /* ... scheduler is not locked */
OS_SchedNew();
if (OSPrioHighRdy != OSPrioCur) { /* No Ctx Sw if current task is highest rdy */
OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy];
#if OS_TASK_PROFILE_EN > 0
OSTCBHighRdy->OSTCBCtxSwCtr++; /* Inc. # of context switches to this task */
#endif
OSCtxSwCtr++; /* Increment context switch counter */
OS_TASK_SW(); /* Perform a context switch */
}
}
}
OS_EXIT_CRITICAL();
}
问题来源:
好几个任务,其中一个优先级最高的任务优先级为5,处理数据。平时跑没事,但是进行数据压力测试的时候(就是狂发数据),发着发着发现发不了了,调试程序看看,程序没有跑飞,但是只执行一个任务了(这个任务不固定,有时候是我的其他任务之一,有时候是空闲任务,但是优先级都低于5),跟中了一下,发现没有发生任务调度,Systick中断正常进入,但是PendSV中断不进,原因是没有被触发,再深入跟踪,发现问题所在:在OSIntExit中,判断if (OSPrioHighRdy != OSPrioCur) ,才进行调度,但是OSPrioHighRdy = 5,这个是正常的,不正常的是OSPrioCur也是5,可是明明目前执行的任务优先级不是5且低于5. 进一步查看了每个任务的TCB(为此我专门为每个任务设置了名字,方便分辨),其内容也没有被篡改(至少优先级还是创建任务的时候分配的优先级),检查各个任务堆栈,也没有溢出(每个堆栈数组最开始的地方都有很多0,我的堆栈分配也足够大),系统堆栈也有0x600,1.5K,应该也足够了。所以一直找不到问题所在!OSPrioCur是在任务切换(PendSV中断ISR)中被赋值的,
LDR R0, =OSPrioCur ; OSPrioCur = OSPrioHighRdy;
LDR R1, =OSPrioHighRdy
LDRB R2, [R1]
STRB R2, [R0]
那难道在赋值过程中进行LDRB R2, [R1]时,OSPrioHighRdy改变了? 但是执行切换代码前都是关了中断的,应该是原子操作,不应该出现这种情况。或者切换后应该进入优先级为5的任务但是错误的跳转到了其他任务? 这也很难理解
终于还是在网上找到了答案,是ucos的BUG,2.86存在,2.88已经修改,需要小小调整OSIntExit() 和 OS_Sched()函数,原文如下:
OS_CORE.C:
OSIntExit() and OS_Sched() have changed slightly because of a boundary condition found with the Cortex-M3 port. Specifically, we needed to move the statement:
OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy];
Before testing for the priority.
可能有的朋友已经看到过相关帖子并且已经了然这个问题了,但是还是写出来给想我这样的没看到过的也一时半会没想到是ucos BUG问题的朋友提个醒。
但是不明白为什么把OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy]; 放到if前面会好!
请高手指点!
有问题的源码如下:
void OSIntExit (void)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr = 0;
#endif
if (OSRunning == OS_TRUE) {
OS_ENTER_CRITICAL();
if (OSIntNesting > 0) { /* Prevent OSIntNesting from wrapping */
OSIntNesting--;
}
if (OSIntNesting == 0) { /* Reschedule only if all ISRs complete ... */
if (OSLockNesting == 0) { /* ... and not locked. */
OS_SchedNew();
if (OSPrioHighRdy != OSPrioCur) { /* No Ctx Sw if current task is highest rdy */
OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy];
#if OS_TASK_PROFILE_EN > 0
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();
}
}
问题是这个BUG出现的原因未知。留待以后慢慢嚼吧。
看看uC/OS-III版本的如下
void OSIntExit (void)
{
CPU_SR_ALLOC();
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();
#endif
OSIntCtxSw(); /* Perform interrupt level ctx switch */
CPU_INT_EN();
}
uc/os_v2.88版本
void OSIntExit (void)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr = 0;
#endif
if (OSRunning == OS_TRUE) {
OS_ENTER_CRITICAL();
if (OSIntNesting > 0) { /* Prevent OSIntNesting from wrapping */
OSIntNesting--;
}
if (OSIntNesting == 0) { /* Reschedule only if all ISRs complete ... */
if (OSLockNesting == 0) { /* ... 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 > 0
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();
}
}
uc/os_v2.88版本
void OS_Sched (void)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr = 0;
#endif
OS_ENTER_CRITICAL();
if (OSIntNesting == 0) { /* Schedule only if all ISRs done and ... */
if (OSLockNesting == 0) { /* ... scheduler is not locked */
OS_SchedNew();
OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy];
if (OSPrioHighRdy != OSPrioCur) { /* No Ctx Sw if current task is highest rdy */
#if OS_TASK_PROFILE_EN > 0
OSTCBHighRdy->OSTCBCtxSwCtr++; /* Inc. # of context switches to this task */
#endif
OSCtxSwCtr++; /* Increment context switch counter */
OS_TASK_SW(); /* Perform a context switch */
}
}
}
OS_EXIT_CRITICAL();
}
uc/os_v2.86版本
void OS_Sched (void)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr = 0;
#endif
OS_ENTER_CRITICAL();
if (OSIntNesting == 0) { /* Schedule only if all ISRs done and ... */
if (OSLockNesting == 0) { /* ... scheduler is not locked */
OS_SchedNew();
if (OSPrioHighRdy != OSPrioCur) { /* No Ctx Sw if current task is highest rdy */
OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy];
#if OS_TASK_PROFILE_EN > 0
OSTCBHighRdy->OSTCBCtxSwCtr++; /* Inc. # of context switches to this task */
#endif
OSCtxSwCtr++; /* Increment context switch counter */
OS_TASK_SW(); /* Perform a context switch */
}
}
}
OS_EXIT_CRITICAL();
}