第八篇:占先式内核(完善的服务)
假如将前面所说到的占先式内核和协作式内核组合在一起,很简单就能够得到一个功用较为完善的占先式内核,它的功用有:
1,挂起和康复使命
2,使命延时
3,信号量(包含同享型和独占型)
别的,在本例中,在各个使命中参加了从串口发送使命状况的功用。
#include <avr/io.h>
#include
#include
unsigned char Stack[400];
register unsigned char OSRdyTbl asm(“r2”); //使命运转安排妥当表
register unsigned char OSTaskRunningPrio asm(“r3”); //正在运转的使命
register unsigned char IntNum asm(“r4”); //中止嵌套计数器
//只要傍边止嵌套数为0,而且有中止要求时,才干在退出中止时,进行使命调度
register unsigned char OSCoreState asm(“r16”); //体系中心标志位,R16编译器没有运用
//只要大于R15的寄存器才干直接赋值例LDI R16,0x01
//0x01正在使命切换0x02有中止要求切换
#define OS_TASKS 3 //设定运转使命的数量
struct TaskCtrBlock
{
unsigned int OSTaskStackTop; //保存使命的仓库顶
unsigned int OSWaitTick; //使命延时时钟
} TCB[OS_TASKS+1];
//避免被编译器占用
//register unsigned char tempR4 asm(“r4”);
register unsigned char tempR5 asm(“r5”);
register unsigned char tempR6 asm(“r6”);
register unsigned char tempR7 asm(“r7”);
register unsigned char tempR8 asm(“r8”);
register unsigned char tempR9 asm(“r9”);
register unsigned char tempR10 asm(“r10”);
register unsigned char tempR11 asm(“r11”);
register unsigned char tempR12 asm(“r12”);
register unsigned char tempR13 asm(“r13”);
register unsigned char tempR14 asm(“r14”);
register unsigned char tempR15 asm(“r15”);
//register unsigned char tempR16 asm(“r16”);
register unsigned char tempR16 asm(“r17”);
//树立使命
void OSTaskCreate(void (*Task)(void),unsigned char *Stack,unsigned char TaskID)
{
unsigned char i;
*Stack–=(unsigned int)Task>>8; //将使命的地址高位压入仓库,
*Stack–=(unsigned int)Task; //将使命的地址低位压入仓库,
*Stack–=0x00; //R1 __zero_reg__
*Stack–=0x00; //R0 __tmp_reg__
*Stack–=0x80;
//SREG在使命中,敞开大局中止
for(i=0;i<14;i++) //在avr-libc中的FAQ中的What registers are used by the C compiler?
*Stack–=i; //描绘了寄存器的效果
TCB[TaskID].OSTaskStackTop=(unsigned int)Stack; //将人工仓库的栈顶,保存到仓库的数组中
OSRdyTbl|=0x01< }
//开端使命调度,从最低优先级的使命的开端
void OSStartTask()
{
OSTaskRunningPrio=OS_TASKS;
SP=TCB[OS_TASKS].OSTaskStackTop+17;
__asm__ __volatile__( “reti” “\n\t” );
}
//进行使命调度
void OSSched(void)
{
__asm__ __volatile__(“LDI R16,0x01 \n\t”);
//铲除中止要求使命切换的标志位,设置正在使命切换标志位
__asm__ __volatile__(“SEI \n\t”);
//开中止,由于假如因中止在使命调度中进行,要从头进行调度时,现已关中止
//依据中止时保存寄存器的次第入栈,模仿一次中止后,入栈的状况
__asm__ __volatile__(“PUSH __zero_reg__ \n\t”); //R1
__asm__ __volatile__(“PUSH __tmp_reg__ \n\t”); //R0
__asm__ __volatile__(“IN __tmp_reg__,__SREG__ \n\t”); //保存状况寄存器SREG
__asm__ __volatile__(“PUSH __tmp_reg__ \n\t”);
__asm__ __volatile__(“CLR __zero_reg__ \n\t”); //R0从头清零
__asm__ __volatile__(“PUSH R18 \n\t”);
__asm__ __volatile__(“PUSH R19 \n\t”);
__asm__ __volatile__(“PUSH R20 \n\t”);
__asm__ __volatile__(“PUSH R21 \n\t”);
__asm__ __volatile__(“PUSH R22 \n\t”);
__asm__ __volatile__(“PUSH R23 \n\t”);
__asm__ __volatile__(“PUSH R24 \n\t”);
__asm__ __volatile__(“PUSH R25 \n\t”);
__asm__ __volatile__(“PUSH R26 \n\t”);
__asm__ __volatile__(“PUSH R27 \n\t”);
__asm__ __volatile__(“PUSH R30 \n\t”);
__asm__ __volatile__(“PUSH R31 \n\t”);
__asm__ __volatile__(“Int_OSSched: \n\t”); //傍边止要求调度,直接进入这儿
__asm__ __volatile__(“SEI \n\t”);
//开中止,由于假如因中止在使命调度中进行,现已关中止
__asm__ __volatile__(“PUSH R28 \n\t”); //R28与R29用于树立在仓库上的指针
__asm__ __volatile__(“PUSH R29 \n\t”); //入栈完结
TCB[OSTaskRunningPrio].OSTaskStackTop=SP; //将正在运转的使命的仓库底保存
unsigned char OSNextTaskPrio; //在现有仓库上开设新的空间
for (OSNextTaskPrio = 0; //进行使命调度
OSNextTaskPrio < OS_TASKS && !(OSRdyTbl & (0x01< OSNextTaskPrio++);
OSTaskRunningPrio = OSNextTaskPrio ;
cli(); //维护仓库转化
SP=TCB[OSTaskRunningPrio].OSTaskStackTop;
sei();
//依据中止时的出栈次第
__asm__ __volatile__(“POP R29 \n\t”);
__asm__ __volatile__(“POP R28 \n\t”);
__asm__ __volatile__(“POP R31 \n\t”);
__asm__ __volatile__(“POP R30 \n\t”);
__asm__ __volatile__(“POP R27 \n\t”);
__asm__ __volatile__(“POP R26 \n\t”);
__asm__ __volatile__(“POP R25 \n\t”);
__asm__ __volatile__(“POP R24 \n\t”);
__asm__ __volatile__(“POP R23 \n\t”);
__asm__ __volatile__(“POP R22 \n\t”);
__asm__ __volatile__(“POP R21 \n\t”);
__asm__ __volatile__(“POP R20 \n\t”);
__asm__ __volatile__(“POP R19 \n\t”);
__asm__ __volatile__(“POP R18 \n\t”);
__asm__ __volatile__(“POP __tmp_reg__ \n\t”); //SERG出栈并康复
__asm__ __volatile__(“OUT __SREG__,__tmp_reg__ \n\t”); //
__asm__ __volatile__(“POP __tmp_reg__ \n\t”); //R0出栈
__asm__ __volatile__(“POP __zero_reg__ \n\t”); //R1出栈
//中止时出栈完结
__asm__ __volatile__(“CLI \n\t”); //关中止
__asm__ __volatile__(“SBRC R16,1 \n\t”); //SBRC当寄存器位为0刚越过下一条指令
//查看是在调度时,是否有中止要求使命调度0x02是中止要求调度的标志位
__asm__ __volatile__(“RJMP OSSched \n\t”); //从头调度
__asm__ __volatile__(“LDI R16,0x00 \n\t”);
//铲除中止要求使命切换的标志位,铲除正在使命切换标志位
__asm__ __volatile__(“RETI \n\t”); //回来并开中止
}
//从中止退出并进行调度
void IntSwitch(void)
{
//傍边止无嵌套,而且没有在切换使命的过程中,直接进行使命切换
if(OSCoreState == 0x02 && IntNum==0)
{
//进入中止时,现已保存了SREG和R0,R1,R18~R27,R30,R31
__asm__ __volatile__(“POP R31 \n\t”); //去除因调用子程序而入栈的PC
__asm__ __volatile__(“POP R31 \n\t”);
__asm__ __volatile__(“LDI R16,0x01 \n\t”);
//铲除中止要求使命切换的标志位,设置正在使命切换标志位
__asm__ __volatile__(“RJMP Int_OSSched \n\t”); //从头调度
}
}
////////////////////////////////////////////使命处理
//挂起使命
void OSTaskSuspend(unsigned char prio)
{
TCB[prio].OSWaitTick=0;
OSRdyTbl &= ~(0x01< if(OSTaskRunningPrio==prio) //当要挂起的使命为当前使命 OSSched(); //从头调度 }
//康复使命能够让被OSTaskSuspend或OSTimeDly暂停的使命康复
void OSTaskResume(unsigned char prio)
{
OSRdyTbl |= 0x01< TCB[prio].OSWaitTick=0; //将时刻计时设为0,届时 if(OSTaskRunningPrio>prio) //当要当前使命的优先级低于重置位的使命的优先级 OSSched(); //从头调度//从头调度 }
//使命延时
void OSTimeDly(unsigned int ticks)
{
if(ticks) //当延时有用
{
OSRdyTbl &= ~(0x01< TCB[OSTaskRunningPrio].OSWaitTick=ticks; OSSched(); //从头调度 } }
//信号量
struct SemBlk
{
unsigned char OSEventType; //类型0,信号量独占型;1信号量同享型
unsigned char OSEventState; //状况0,不可用;1,可用
unsigned char OSTaskPendTbl; //等候信号量的使命列表
} Sem[10];
//初始化信号量
void OSSemCreat(unsigned char Index,unsigned char Type)
{
Sem[Index].OSEventType=Type; //类型0,信号量独占型;1信号量同享型
Sem[Index].OSTaskPendTbl=0;
Sem[Index].OSEventState=0;
}
//使命等候信号量,挂起
//当Timeout==0xffff时,为无限延时
unsigned char OSTaskSemPend(unsigned char Index,unsigned int Timeout)
{
//unsigned char i=0;
if(Sem[Index].OSEventState) //信号量有用
{
if(Sem[Index].OSEventType==0) //假如为独占型
Sem[Index].OSEventState = 0x00; //信号量被独占,不可用
}
else
{ //参加信号的使命等候表
Sem[Index].OSTaskPendTbl |= 0x01< TCB[OSTaskRunningPrio].OSWaitTick=Timeout; //如延时为0,刚无限等候 OSRdyTbl &= ~(0x01< OSSched(); //从头调度 if(TCB[OSTaskRunningPrio].OSWaitTick==0 ) //超时,未能拿到资源 return 0; } return 1; }
//发送一个信号量,能够从使命或中止发送
void OSSemPost(unsigned char Index)
{
if(Sem[Index].OSEventType) //当要求的信号量是同享型
{
Sem[Index].OSEventState=0x01; //使信号量有用
OSRdyTbl |=Sem [Index].OSTaskPendTbl; //使在等候该信号的一切使命安排妥当Sem[Index].OSTaskPendTbl=0; //清空一切等候该信号的等候使命
}
else //当要求的信号量为独占型
{
unsigned char i;
for (i = 0; i < OS_TASKS && !(Sem[Index].OSTaskPendTbl & (0x01<
if(i < OS_TASKS) //假如有使命需求
{
Sem[Index].OSTaskPendTbl &= ~(0x01<
OSRdyTbl |= 0x01<
}
else
{
Sem[Index].OSEventState =1; //使信号量有用
}
}
}
//从使命发送一个信号量,并进行调度
void OSTaskSemPost(unsigned char Index)
{
OSSemPost(Index);
OSSched();
}
//铲除一个信号量,只对同享型的有用。
//关于独占型的信号量,在使命占用后,就交得不能够用了。
void OSSemClean(unsigned char Index)
{
Sem[Index].OSEventState =0; //要求的信号量无效
}
void TCN0Init(void) //计时器0
{
TCCR0 = 0;
TCCR0 |= (1< TIMSK |= (1< TCNT0 = 100; //置计数起始值 } SIGNAL(SIG_OVERFLOW0) { IntNum++; //中止嵌套+1 sei(); //在中止中,重开中止 unsigned char i; for(i=0;i { if(TCB[i].OSWaitTick && TCB[i].OSWaitTick!=0xffff) { TCB[i].OSWaitTick–; if(TCB[i].OSWaitTick==0) //当使命时钟届时,有必要是由定时器减时的才行 { OSRdyTbl |= (0x01<
OSCoreState|=0x02; //要求使命切换的标志位 } } } TCNT0=100; cli(); IntNum–; //中止嵌套-1 IntSwitch(); //进行使命调度 }
unsigned char __attribute__ ((progmem)) proStrA[]=”Task “;
unsigned char strA[20];
SIGNAL(SIG_UART_RECV) //串口接纳中止
{
strA[0]=UDR;
}
/////////////////////////////////////串口发送
unsigned char *pstr_UART_Send;
unsigned int nUART_Sending=0;
void UART_Send(unsigned char *Res,unsigned int Len) //发送字符串数组
{
if(Len>0)
{
pstr_UART_Send=Res; //发送字串的指针
nUART_Sending=Len; //发送字串的长度
UCSRB=0xB8; //发送中止使能
}
}
//SIGNAL在中止期间,其它中止制止
SIGNAL(SIG_UART_DATA) //串口发送数据中止
{
IntNum++; //中止嵌套+1,不充许中止
if(nUART_Sending) //假如未发完
{
UDR=*pstr_UART_Send; //发送字节
pstr_UART_Send++; //发送字串的指针加1
nUART_Sending–; //等候发送的字串数减1
}
if(nUART_Sending==0) //当现已发送完
{
OSSemPost(0);
OSCoreState|=0x02; //要求使命切换的标志位
UCSRB=0x98;
}
cli(); //关发送中止
IntNum–;
IntSwitch(); //进行使命调度
}
void UARTInit() //初始化串口
{
#define fosc 8000000 //晶振8 MHZ UBRRL=(fosc/16/(baud+1))%256;
#define baud 9600 //波特率
OSCCAL=0x97; //串口波特率校正值,从编程器中读出
//UCSRB=(1< UCSRB=0x98; //UCSRB=0x08; UBRRL=(fosc/16/(baud+1))%256; UBRRH=(fosc/16/(baud+1))/256; UCSRC=(1< UCSRB=0xB8; UDR=0; } //打印unsigned int到字符串中00000 void strPUT_uInt(unsigned char *Des,unsigned int i) { unsigned char j; Des=Des+4; for(j=0;j<5;j++) { *Des=i%10+’0’; i=i/10; Des–; } }
void strPUT_Star(unsigned char *Des,unsigned char i)
{
unsigned char j;
for(j=0;j
{
*Des++=’*’;
}
*Des++=13;
}
unsigned int strPUT_TaskState(unsigned char *Des,unsigned char TaskID,unsigned char Num)
{
//unsigned int i=0;
*(Des+4)=’0’+TaskID;
strPUT_uInt(Des+6,Num);
strPUT_Star(Des+12,TaskID);
return 12+TaskID+1;
}
void Task0()
{
unsigned int j=0;
while(1)
{
PORTB=j++;
if(OSTaskSemPend(0,0xffff))
{
unsigned int m;
m=strPUT_TaskState(strA,OSTaskRunningPrio,j);
UART_Send(strA,m);
}
OSTimeDly(200);
}
}
void Task1()
{
unsigned int j=0;
while(1)
{
PORTC=j++;
if(OSTaskSemPend(0,0xffff))
{
unsigned int m;
m=strPUT_TaskState(strA,OSTaskRunningPrio,j);
UART_Send(strA,m);
}
OSTimeDly(100);
}
}
void Task2()
{
unsigned int j=0;
while(1)
{
if(OSTaskSemPend(0,0xffff))
{
unsigned int m;
m=strPUT_TaskState(strA,OSTaskRunningPrio,j);
UART_Send(strA,m);
}
PORTD=j++;
OSTimeDly(50);
}
}
void TaskScheduler()
{
OSSched();
while(1)
{}
}
int main(void)
{
strlcpy_P(strA,proStrA,20);
UARTInit();
TCN0Init();
OSRdyTbl=0;
IntNum=0;
OSTaskCreate(Task0,&Stack[99],0);
OSTaskCreate(Task1,&Stack[199],1);
OSTaskCreate(Task2,&Stack[299],2);
OSTaskCreate(TaskScheduler,&Stack[399],OS_TASKS);
OSStartTask();
}