small rtos1.20.3中堆栈初始化程序c版本代码:
if EN_REENTRANT > 0
#define SP_ADD_BYTE 5
#else
#define SP_ADD_BYTE 3
#endif
void OSTaskStkInit(void (code * task)(void), void xdata *ptos, uint8 TaskID) small
{
uint8 i;
uint8 idata * cp;
uint8 idata * cp1;
#if OS_MAX_TASKS < 9
OSFastSwap[1] |= OSMapTbl[TaskID];
#else
if (TaskID < 8)
{
OSFastSwap[1] |= OSMapTbl[TaskID];
}
else
{
OSFastSwap[0] |= OSMapTbl[TaskID & 0x07];
}
#endif
if (TaskID < OSRunningTaskID())
{
i = OSRunningTaskID() - TaskID;
cp = (uint8 idata *)(&(OSTsakStackBotton[TaskID + 1]));
do
{
*cp += SP_ADD_BYTE;
cp++;
} while (--i != 0);
cp1 = (uint8 idata *)SP;
SP = SP + SP_ADD_BYTE;
i = SP - (uint8)(OSTsakStackBotton[TaskID + 1]);
cp = (uint8 idata *)SP;
do
{
*cp-- = *cp1--;
} while (--i != 0);
}
else
{
cp1 = (uint8 idata *)(&(OSTsakStackBotton[OSRunningTaskID() + 1]));
i = TaskID - OSRunningTaskID();
do
{
*cp1 -= SP_ADD_BYTE;
cp1++;
} while (--i != 0);
cp = OSTsakStackBotton[OSRunningTaskID() + 1];
i = OSTsakStackBotton[TaskID] - cp - SP_ADD_BYTE;
cp1 = cp - SP_ADD_BYTE;
do
{
*cp1++ = *cp++;
} while (--i != 0);
}
cp = OSTsakStackBotton[TaskID];
*cp++ = (uint16)task % 256;
*cp++ = (uint16)task / 256;
#if EN_REENTRANT > 0
*cp++ = (uint16)ptos / 256;
*cp++ = (uint16)ptos % 256;
#endif
*cp = 0;
}
其中一段:(当TaskID> OSRunningTaskID())
cp = OSTsakStackBotton[OSRunningTaskID() + 1];
i = OSTsakStackBotton[TaskID] - cp - SP_ADD_BYTE;
cp1 = cp - SP_ADD_BYTE;
do
{
*cp1++ = *cp++;
} while (--i != 0);
应改为:
cp = OSTsakStackBotton[OSRunningTaskID() + 1];
i = OSTsakStackBotton[TaskID] - cp;
cp1 = cp + SP_ADD_BYTE;
do
{
*cp++ = *cp1++;
} while (--i != 0);
不然堆栈会向低地址方向多搬移SP_ADD_BYTE字节,并且任务号为(TaskID-1)的任务堆栈数据会少复制SP_ADD_BYTE字节。
不知是我理解错了还是陈老师的笔误?
请各位发表见解,谢谢! |