新闻中心

EEPW首页 > 嵌入式系统 > 设计应用 > 建立一个属于自己的AVR的RTOS

建立一个属于自己的AVR的RTOS

作者:时间:2011-04-29来源:网络收藏
__asm____volatile__(PUSH__tmp_reg__nt);//R0
__asm____volatile__(IN__tmp_reg__,__SREG__nt);//保存状态寄存器SREG
__asm____volatile__(PUSH__tmp_reg__nt);
__asm____volatile__(CLR__zero_reg__nt);//R0重新清零
__asm____volatile__(PUSHR18nt);
__asm____volatile__(PUSHR19nt);
__asm____volatile__(PUSHR20nt);
__asm____volatile__(PUSHR21nt);
__asm____volatile__(PUSHR22nt);
__asm____volatile__(PUSHR23nt);
__asm____volatile__(PUSHR24nt);
__asm____volatile__(PUSHR25nt);
__asm____volatile__(PUSHR26nt);
__asm____volatile__(PUSHR27nt);
__asm____volatile__(PUSHR30nt);
__asm____volatile__(PUSHR31nt);

__asm____volatile__(Int_OSSched:nt);//当中断要求调度,直接进入这里
__asm____volatile__(PUSHR28nt);//R28与R29用于在堆栈上的指针
__asm____volatile__(PUSHR29nt);//入栈完成

TCB[OSTaskRunningPrio].OSTaskStackTop=SP;//将正在运行的任务的堆栈底保存

if(++OSTaskRunningPrio>=OS_TASKS)//轮流运行各个任务,没有优先级
OSTaskRunningPrio=0;

//cli();//保护堆栈转换
SP=TCB[OSTaskRunningPrio].OSTaskStackTop;
//sei();

//根据中断时的出栈次序
__asm____volatile__(POPR29nt);
__asm____volatile__(POPR28nt);
__asm____volatile__(POPR31nt);
__asm____volatile__(POPR30nt);
__asm____volatile__(POPR27nt);
__asm____volatile__(POPR26nt);
__asm____volatile__(POPR25nt);
__asm____volatile__(POPR24nt);
__asm____volatile__(POPR23nt);
__asm____volatile__(POPR22nt);
__asm____volatile__(POPR21nt);
__asm____volatile__(POPR20nt);
__asm____volatile__(POPR19nt);
__asm____volatile__(POPR18nt);
__asm____volatile__(POP__tmp_reg__nt);//SERG出栈并恢复
__asm____volatile__(OUT__SREG__,__tmp_reg__nt);//
__asm____volatile__(POP__tmp_reg__nt);//R0出栈
__asm____volatile__(POP__zero_reg__nt);//R1出栈
__asm____volatile__(RETInt);//返回并开中断
//中断时出栈完成
}


voidIntSwitch(void)
{
__asm____volatile__(POPR31nt);//去除因调用子程序而入栈的PC
__asm____volatile__(POPR31nt);
__asm____volatile__(RJMPInt_OSSchednt);//重新调度
}




voidTCN0Init(void)//计时器0
{
TCCR0=0;
TCCR0|=(1CS02);//256预分频
TIMSK|=(1TOIE0);//T0溢出中断允许
TCNT0=100;//置计数起始值
}


SIGNAL(SIG_OVERFLOW0)
{
TCNT0=100;
IntSwitch();//任务调度
}

voidTask0()
{
unsignedintj=0;
while(1)
{
PORTB=j++;
//OSTimeDly(50);
}
}

voidTask1()
{
unsignedintj=0;
while(1)
{
PORTC=j++;
//OSTimeDly(5);
}
}

voidTask2()
{
unsignedintj=0;
while(1)
{
PORTD=j++;
//OSTimeDly(5);
}
}



voidTaskScheduler()
{
while(1)
{
OSSched();//反复进行调度
}
}


intmain(void)
{
TCN0Init();
OSRdyTbl=0;
OSTaskCreate(Task0,Stack[99],0);
OSTaskCreate(Task1,Stack[199],1);
OSTaskCreate(Task2,Stack[299],2);
OSTaskCreate(TaskScheduler,Stack[399],OS_TASKS);
OSStartTask();
}

本文引用地址:http://www.eepw.com.cn/article/172857.htm

第七篇:占先式内核(只带延时服务)

PreemptiveMultitasking
当大家理解时间片轮番调度法的任务调度方式后,占先式的内核的原理,已经伸手可及了。
先想想,占先式内核是在什么地方实现任务调度的呢?对了,它在可以在任务中进行调度,这个在协作式的内核中已经做到了;同时,它也可以在中断结束后进行调度,这个问题,已经在时间片轮番调度法中已经做到了。

由于中断是可以嵌套的,只有当各层嵌套中要求调度,并且中断嵌套返回到最初进入的中断的那一层时,才能进行任务调度。

#includeavr/io.h>
#includeavr/Interrupt.h>
#includeavr/signal.h>
unsignedcharStack[400];

registerunsignedcharOSRdyTblasm(r2);//任务运行就绪表
registerunsignedcharOSTaskRunningPrioasm(r3);//正在运行的任务
registerunsignedcharIntNumasm(r4);//中断嵌套计数器
//只有当中断嵌套数为0,并且有中断要求时,才能在退出中断时,进行任务调度
registerunsignedcharOSCoreStateasm(r16);//系统核心标志位,R16编译器没有使用
//只有大于R15的寄存器才能直接赋值例LDIR16,0x01
//0x01正在任务切换0x02有中断要求切换

#defineOS_TASKS3//设定运行任务的数量
structTaskCtrBlock
{
unsignedintOSTaskStackTop;//保存任务的堆栈顶
unsignedintOSWaitTick;//任务延时时钟
}TCB[OS_TASKS+1];

//防止被编译器占用
//registerunsignedchartempR4asm(r4);
registerunsignedchartempR5asm(r5);
registerunsignedchartempR6asm(r6);
registerunsignedchartempR7asm(r7);
registerunsignedchartempR8asm(r8);
registerunsignedchartempR9asm(r9);
registerunsignedchartempR10asm(r10);
registerunsignedchartempR11asm(r11);
registerunsignedchartempR12asm(r12);
registerunsignedchartempR13asm(r13);
registerunsignedchartempR14asm(r14);
registerunsignedchartempR15asm(r15);
//registerunsignedchartempR16asm(r16);
registerunsignedchartempR16asm(r17);


//任务
voidOSTaskCreate(void(*Task)(void),unsignedchar*Stack,unsignedcharTaskID)
{
unsignedchari;
*Stack--=(unsignedint)Task>>8;//将任务的地址高位压入堆栈,
*Stack--=(unsignedint)Task;//将任务的地址低位压入堆栈,

*Stack--=0x00;//R1__zero_reg__
*Stack--=0x00;//R0__tmp_reg__
*Stack--=0x80;

//SREG在任务中,开启全局中断
for(i=0;i14;i++)//在avr-libc中的FAQ中的WhatregistersareusedbytheCcompiler?
*Stack--=i;//描述了寄存器的作用
TCB[TaskID].OSTaskStackTop=(unsignedint)Stack;//将人工堆栈的栈顶,保存到堆栈的数组中
OSRdyTbl|=0x01TaskID;//任务就绪表已经准备好
}

//开始任务调度,从最低优先级的任务的开始
voidOSStartTask()
{
OSTaskRunningPrio=OS_TASKS;
SP=TCB[OS_TASKS].OSTaskStackTop+17;
__asm____volatile__(retint);
}

//进行任务调度
voidOSSched(void)
{

__asm____volatile__(LDIR16,0x01nt);
//清除中断要求任务切换的标志位,设置正在任务切换标志位
__asm____volatile__(SEInt);
//开中断,因为如果因中断在任务调度中进行,要重新进行调度时,已经关中断
//根据中断时保存寄存器的次序入栈,模拟一次中断后,入栈的情况
__asm____volatile__(PUSH__zero_reg__nt);//R1
__asm____volatile__(PUSH__tmp_reg__nt);//R0
__asm____volatile__(IN__tmp_reg__,__SREG__nt);//保存状态寄存器SREG
__asm____volatile__(PUSH__tmp_reg__nt);
__asm____volatile__(CLR__zero_reg__nt);//R0重新清零
__asm____volatile__(PUSHR18nt);
__asm____volatile__(PUSHR19nt);
__asm____volatile__(PUSHR20nt);
__asm____volatile__(PUSHR21nt);
__asm____volatile__(PUSHR22nt);
__asm____volatile__(PUSHR23nt);
__asm____volatile__(PUSHR24nt);
__asm____volatile__(PUSHR25nt);
__asm____volatile__(PUSHR26nt);
__asm____volatile__(PUSHR27nt);
__asm____volatile__(PUSHR30nt);
__asm____volatile__(PUSHR31nt);

__asm____volatile__(Int_OSSched:nt);//当中断要求调度,直接进入这里
__asm____volatile__(SEInt);
//开中断,因为如果因中断在任务调度中进行,已经关中断
__asm____volatile__(PUSHR28nt);//R28与R29用于在堆栈上的指针
__asm____volatile__(PUSHR29nt);//入栈完成

TCB[OSTaskRunningPrio].OSTaskStackTop=SP;//将正在运行的任务的堆栈底保存

unsignedcharOSNextTaskPrio;//在现有堆栈上开设新的空间
for(OSNextTaskPrio=0;//进行任务调度
OSNextTaskPrioOS_TASKS!(OSRdyTbl(0x01OSNextTaskPrio));
OSNextTaskPrio++);
OSTaskRunningPrio=OSNextTaskPrio;

cli();//保护堆栈转换
SP=TCB[OSTaskRunningPrio].OSTaskStackTop;
sei();

//根据中断时的出栈次序
__asm____volatile__(POPR29nt);
__asm____volatile__(POPR28nt);
__asm____volatile__(POPR31nt);
__asm____volatile__(POPR30nt);
__asm____volatile__(POPR27nt);
__asm____volatile__(POPR26nt);
__asm____volatile__(POPR25nt);
__asm____volatile__(POPR24nt);
__asm____volatile__(POPR23nt);
__asm____volatile__(POPR22nt);
__asm____volatile__(POPR21nt);
__asm____volatile__(POPR20nt);
__asm____volatile__(POPR19nt);
__asm____volatile__(POPR18nt);
__asm____volatile__(POP__tmp_reg__nt);//SERG出栈并恢复
__asm____volatile__(OUT__SREG__,__tmp_reg__nt);//
__asm____volatile__(POP__tmp_reg__nt);//R0出栈
__asm____volatile__(POP__zero_reg__nt);//R1出栈
//中断时出栈完成
__asm____volatile__(CLInt);//关中断
__asm____volatile__(SBRCR16,1nt);//SBRC当寄存器位为0刚跳过下一条指令
//检查是在调度时,是否有中断要求任务调度0x02是中断要求调度的标志位
__asm____volatile__(RJMPOSSchednt);//重新调度
__asm____volatile__(LDIR16,0x00nt);
//清除中断要求任务切换的标志位,清除正在任务切换标志位
__asm____volatile__(RETInt);//返回并开中断
}


//从中断退出并进行调度
voidIntSwitch(void)
{
//当中断无嵌套,并且没有在切换任务的过程中,直接进行任务切换
if(OSCoreState==0x02IntNum==0)
{
//进入中断时,已经保存了SREG和R0,R1,R18~R27,R30,R31
__asm____volatile__(POPR31nt);//去除因调用子程序而入栈的PC
__asm____volatile__(POPR31nt);
__asm____volatile__(LDIR16,0x01nt);
//清除中断要求任务切换的标志位,设置正在任务切换标志位
__asm____volatile__(RJMPInt_OSSchednt);//重新调度
}
}

//任务延时
voidOSTimeDly(unsignedintticks)
{
if(ticks)//当延时有效
{
OSRdyTbl=~(0x01OSTaskRunningPrio);
TCB[OSTaskRunningPrio].OSWaitTick=ticks;
OSSched();//从新调度
}
}



voidTCN0Init(void)//计时器0
{
TCCR0=0;
TCCR0|=(1CS02);//256预分频
TIMSK|=(1TOIE0);//T0溢出中断允许
TCNT0=100;//置计数起始值

}

SIGNAL(SIG_OVERFLOW0)
{
IntNum++;//中断嵌套+1
sei();//在中断中,重开中断

unsignedchari,j=0;
for(i=0;iOS_TASKS;i++)//任务时钟
{
if(TCB[i].OSWaitTick)
{
TCB[i].OSWaitTick--;
if(TCB[i].OSWaitTick==0)//当任务时钟到时,必须是由定时器减时的才行
{
OSRdyTbl|=(0x01i);//使任务可以重新运行
OSCoreState|=0x02;//要求任务切换的标志位
}
}
}
TCNT0=100;
cli();
IntNum--;//中断嵌套-1
IntSwitch();//进行任务调度
}

voidTask0()
{
unsignedintj=0;
while(1)
{
PORTB=j++;
OSTimeDly(50);
}
}

voidTask1()
{
unsignedintj=0;
while(1)
{
PORTC=j++;
OSTimeDly(20);
}
}

voidTask2()
{
unsignedintj=0;
while(1)
{
PORTD=j++;
OSTimeDly(5);
}
}



voidTaskScheduler()
{
OSSched();
while(1)
{
//OSSched();//反复进行调度
}
}


intmain(void)
{
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();
}

第八篇:占先式内核(完善的服务)

如果将前面所提到的占先式内核和协作式内核组合在一起,很容易就可以得到功能较为完善的占先式内核,它的功能有:
1,挂起和恢复任务
2,任务延时
3,信号量(包括共享型和独占型)
另外,在本例中,在各个任务中加入了从串口发送任务状态的功能。


#includeavr/io.h>
#includeavr/Interrupt.h>
#includeavr/signal.h>
unsignedcharStack[400];

registerunsignedcharOSRdyTblasm(r2);//任务运行就绪表
registerunsignedcharOSTaskRunningPrioasm(r3);//正在运行的任务
registerunsignedcharIntNumasm(r4);//中断嵌套计数器
//只有当中断嵌套数为0,并且有中断要求时,才能在退出中断时,进行任务调度
registerunsignedcharOSCoreStateasm(r16);//系统核心标志位,R16编译器没有使用
//只有大于R15的寄存器才能直接赋值例LDIR16,0x01
//0x01正在任务切换0x02有中断要求切换

#defineOS_TASKS3//设定运行任务的数量
structTaskCtrBlock
{
unsignedintOSTaskStackTop;//保存任务的堆栈顶
unsignedintOSWaitTick;//任务延时时钟
}TCB[OS_TASKS+1];

//防止被编译器占用



评论


相关推荐

技术专区

关闭