📄 emnetrtos.c
字号:
char emSendMal(unsigned char miD,unsigned char *pMesg,unsigned char nByte)
{
unsigned char *ptr,*ptr1;
unsigned char i,j;
ptr=emGetSmal(miD);//ptr is start address
i=*ptr;
i=i&0x0f;
if(i!=malNULL)//邮箱为空才写入,否则直接返回
return -10;
ptr1=ptr;
ptr++;
j=*ptr;
j=j&0xf0;//j中暂存接收消息任务的ID
j=j>>4;
ptr++;//ptr is nsi address
i=*ptr;
if(nByte>i)//exceed mail space
return -1;
for(i=0;i<nByte;i++)
{
ptr++;
*ptr=*pMesg;
pMesg++;
}
INTCONbits.GIEH=0;
i=malExist;
*ptr1=*ptr1&0xf0;
*ptr1=*ptr1|i;//至此已经修改了邮箱属性
if(pTcb[j].status.bits.statu==WAIT)
pTcb[j].status.bits.statu=READY;
INTCONbits.GIEH=1;
return 0;
}
char emBroadMal(unsigned char miD,unsigned char *pMesg,unsigned char nByte)//返回1OK,负值错误
{
unsigned char *ptr,*ptr1;
unsigned char i,j;
ptr=emGetSmal(miD);//ptr is start address
i=*ptr;
i=i&0x0f;
if(i!=malNULL)//邮箱为空才写入,否则直接返回
return -10;
*ptr=*ptr&0xf0;
*ptr=*ptr|malBroadcast;
ptr++;
ptr++;
i=*ptr;
if(nByte>i)//exceed mail space
return -1;
for(i=0;i<nByte;i++)
{
ptr++;
*ptr=*pMesg;
pMesg++;
}
return 1;
}
unsigned char *emRecMal(unsigned char miD)
{
unsigned char i,j,k,*ptr;
ptr=emGetSmal(miD);
i=*ptr;
i=i&0x0f;
if(i==malNULL)
{
j=RunID;
// j=j&0x0f;
j=j<<4;//得到了接收任务的ID
ptr++;
*ptr=*ptr|j;
k=*ptr;
emWait();
return 0;
}
if(i==malExist)//*ptr==malExist)//现在只是设定了三种邮箱属性,所以程序简单,以后增加了属性,程序再修改。
{
*ptr=*ptr&0xf0;
*ptr=*ptr|malReceived;
ptr++;
ptr++;
return ptr;//这里返回的是邮箱nsi值的地址。
}
if(i==malReceived)//已经将内容读走了,将邮箱变为空
{
*ptr=*ptr&0xf0;
*ptr=*ptr|malNULL;
return 0;
}
}
unsigned char *emReadMal(unsigned char miD)
{
unsigned char i,*ptr;
ptr=emGetSmal(miD);
i=*ptr;
i=i&0x0f;
if(i==malNULL)
{
emWait();
return 0;
}
if(i==malBroadcast&&i==malReded)
{
*ptr=*ptr&0xf0;
*ptr=*ptr|malReded;
ptr++;
ptr++;
return ptr;//这里返回的是邮箱nsi值的地址。
}
}
void *emReleaseMal(unsigned char miD)
{
unsigned char *ptr;
ptr=emGetSmal(miD);
*ptr=*ptr&0xf0;
*ptr=*ptr|malNULL;
}
char emCreatSem(unsigned char SemID)//创建一个标志位,在网络版RTOS中,需要创建第4号SemID,此信号灯由于保护公共的TCP数据缓冲区
{
if (SemID>=5)
return 0;
else
{
Sem[SemID].queue=0;
if (Sem[SemID].ResSem.statu!=idle)
Sem[SemID].ResSem.statu=idle;
}
return 1;
}
char emGetSem (unsigned char SemID) //
{
if (Sem[SemID].ResSem.statu==idle)
{
Sem[SemID].ResSem.statu=taken;
Sem[SemID].ResSem.RtaskID=RunID;
return 1;
}
else
{ emInt16 index=1;
if (Sem[SemID].ResSem.RtaskID==RunID)
{
return 1;
}
if (RunID!=0)
{
index=index<<RunID;
}
Sem[SemID].queue|=index;
emWait();
return 0;
}
}
void emRelSem(unsigned char SemID)
{
emInt8 j,m=15,a;
emInt16 n=1;
emInt16 index=1;
if (Sem[SemID].queue==0)
Sem[SemID].ResSem.statu=idle;
else
{ for(a=0;a<=15;a++)//这里可能应该改为14,可能也无所谓,因为RTOS为TASK15,有最低优先级。
{
if ((n&Sem[SemID].queue)!=0)
{
if (pTcb[a].status.bits.prio<=m) //保存最高优先级
{
m=pTcb[a].status.bits.prio;
j=a; //保存最高优先级的任务号
}
}
n*=2;
}
}
if(pTcb[j].status.bits.statu==WAIT)
{
pTcb[j].status.bits.statu=READY;
Sem[SemID].ResSem.RtaskID=j;
}
if (j!=0)
{
index=index<<j;
}
index=~index;
Sem[SemID].queue&=index;
}
emchar8 emCreateMesg(emInt8 mesgID,emInt8 TaskID)
{
unsigned char i;
if(mesgID>=16)
return 0;
mesg[mesgID].Mesg=NULLMESG;
if(TaskID>=16)
return 0;
i=0xf&TaskID;
mesg[mesgID].tID.taskID.txMesgtaskID=i;
return 1;
}
emchar8 emTxMesg(emInt8 mesgID,emInt8 message)
{
unsigned char i;
if((mesg[mesgID].Mesg!=NULLMESG)||(mesgID>=16))
return 0;
INTCONbits.GIEH=0;
mesg[mesgID].Mesg=message;
i=mesg[mesgID].tID.btaskID;
i=i&0xf0;
i=i>>4;
pTcb[i].status.bits.statu=READY;
INTCONbits.GIEH=1;
return 1;
}
emInt8 emRxMesg(emInt8 mesgID)
{
unsigned char i;
if((mesg[mesgID].Mesg==NULLMESG)||(mesgID>=16))
return 0;
INTCONbits.GIEH=0;
i=mesg[mesgID].Mesg;
mesg[mesgID].Mesg=NULLMESG;
INTCONbits.GIEH=1;
return i;
}
emInt8 emWaitMesg(emInt8 mesgID)
{
unsigned char i;
if(mesg[mesgID].Mesg==NULLMESG)
{
i=RunID;
i=i&0x0f;
i=i<<4;
mesg[mesgID].tID.btaskID=mesg[mesgID].tID.taskID.txMesgtaskID|i;
emWait();
return 0;
}
else
return 1;
}
void emDeleteMesg(emInt8 mesgID)
{
mesg[mesgID].Mesg=IDLEMESG;
}
emchar8 emCreateTimer(emInt8 timeID,emInt8 TaskID)
{
unsigned char i;
if((TaskID>=16)||(timeID>=16))
return 0;
tim[timeID].Valuetime=NULLTIME;
i=0x0f&TaskID;
tim[timeID].tID.TaskID.txMesgtaskID=i;
return 1;
}
void emSetimeValue(emInt8 timeID,emInt8 tvalue)
{
unsigned char i;
i=tim[timeID].tID.btaskID;
i=i&0x0f;
if(pTcb[i].delaytime!=0)
return;
else
pTcb[i].delaytime=tvalue;//写入延迟值
}
emInt8 emStartTimer(emInt8 timeID)
{
unsigned char i;
i=tim[timeID].tID.btaskID;
i=i&0x0f;
if(pTcb[i].delaytime==0)
return 1;
else
pTcb[i].status.bits.statu=DELAY;//将任务状态变为定时状态。
return 0;
// emSched1();
}
void emDeleteTimer(emInt8 timeID)
{
unsigned char i;
i=tim[timeID].tID.btaskID;
i=i&0x0f;
// pTcb[i].status.bits.statu=READY;
tim[timeID].Valuetime=IDLETIME;
}
void emWait(void)//将任务挂起
{
pTcb[RunID].status.bits.statu=WAIT;
// emSched1();
}
void emReady(char n)
{
pTcb[n].status.bits.statu=READY;
}
/* tid is number of hard timer(0-15),value is 1-8192,max timer is 41 second*/
emBool emSetHardTimer(emInt8 tid, emInt16 value)//设置定时器
{
// if(pHt[tid].htstatus.IDtimer.Flg>5)//初始状态有可能是随机的,所以可能会大于5而引起错误
// return FALSE;
pHt[tid].htstatus.IDtimer.Flg=HARD_TIMER_NULL;
pHt[tid].Lt_value=value;
// value>>8;
// pHt[tid].htstatus.bIDtimer=pHt[tid].htstatus.bIDtimer|value;
return TRUE;
}
emBool emStartHardTimer(emInt8 tid)
{
if(pHt[tid].htstatus.IDtimer.Flg!=HARD_TIMER_NULL)
return FALSE;
pHt[tid].htstatus.IDtimer.Flg=HARD_TIMER_WORK;
pHt[tid].temp=mSeconds;
return TRUE;
}
emInt8 emCheckHardTimer(emInt8 tid)
{
emInt16 tv;
// tv=pHt[tid].htstatus.bIDtimer&0b00011111;
// tv<<8;
// tv=tv|pHt[tid].Lt_value;
/* if(pHt[tid].htstatus.IDtimer.Flg!=HARD_TIMER_WORK)
return 0;*/
tv=pHt[tid].Lt_value;
if(mSeconds<pHt[tid].temp)//mSecond overflow
{
if((4294967296-pHt[tid].temp+mSeconds)<tv)//没有到设定的定时值,返回0
return 0;
if((4294967296-pHt[tid].temp+mSeconds)>tv)
{
pHt[tid].temp=mSeconds;
return 5;//pHt[tid].htstatus.IDtimer.Flg=5;
}
if((4294967296-pHt[tid].temp+mSeconds)==tv)
{
pHt[tid].temp=mSeconds;
return 1;
}
}
else
{
if((mSeconds-pHt[tid].temp)>tv)
{
pHt[tid].temp=mSeconds;
return 5;
}
if((mSeconds-pHt[tid].temp)<tv)
return 0;
if((mSeconds-pHt[tid].temp)==tv)
{
pHt[tid].temp=mSeconds;
return 1;
}
}
}
void emDelHardTimer(emInt8 tid)
{
pHt[tid].htstatus.IDtimer.Flg=HARD_TIMER_NULL;
}
void emTxSysMesg(emInt8 message)
{
unsigned char i;
INTCONbits.GIEH=0;
mesg[15].Mesg=message;
for(i=0;i<15;i++)//将所有任务都变成READY,以便于所有任务都能接收此消息,前提是RTOS相当于任务15
{
if((pTcb[i].status.bits.statu>1)&&(pTcb[i].status.bits.statu<15))
pTcb[i].status.bits.statu=READY;
}
INTCONbits.GIEH=1;
}
emInt8 emRxSysMesg(void)//用此函数接收系统消息
{
unsigned char i;
if(mesg[15].Mesg==NULLMESG)
return 0;
i=mesg[15].Mesg;
return i;
}
void emDelSysMesg(void)
{
if(mesg[15].Mesg!=NULLMESG)
mesg[15].Mesg=NULLMESG;
}
void emCreatSysMal(void)
{
unsigned char *curPmail;
curPmail=&mainbuf2[0];
*curPmail=SysMailNULL;
curPmail++;
curPmail++;
*curPmail=SysMailLength;
}
char emSendSysMal(emInt8 maila,emInt8 *pMesg,emInt8 nByte)
{
unsigned char *ptr;
unsigned char i;
ptr=&mainbuf2[0];//ptr is start address
i=*ptr;
if(i<=SysMailToSys)//邮箱被读出后才写入,否则直接返回
return -10;
if(nByte>SysMailLength)
return 1;
*ptr=maila;
ptr++;
ptr++;//ptr is nsi address
INTCONbits.GIEH=0;
for(i=0;i<nByte;i++)
{
ptr++;
*ptr=*pMesg;
pMesg++;
}
for(i=0;i<15;i++)//将所有任务都变成READY,以便于所有任务都能接收此消息,前提是RTOS相当于任务15
{
if((pTcb[i].status.bits.statu>1)&&(pTcb[i].status.bits.statu<15))
pTcb[i].status.bits.statu=READY;
}
INTCONbits.GIEH=1;
return 0;
}
emInt8 emGetSysMal(void)
{
unsigned char *ptr;
ptr=emGetSmal(0);
return *ptr;
}
emInt8 *emRecSysMal(emInt8 maila)
{
unsigned char *ptr;
ptr=emGetSmal(0);
*ptr=maila;
ptr++;
return ptr;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -