微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 建立一个属于自己的AVR的RTOS

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

时间:04-29 来源:互联网 点击:

第六篇:时间片轮番调度法的内核

Round-RobinSheduling

时间片轮调法是非常有趣的。本篇中的例子,建立了3个任务,任务没有优先级,在时间中断的调度下,每个任务都轮流运行相同的时间。如果在内核中没有加入其它服务,感觉上就好像是有三个大循环在同时运行。

本例只是提供了一个用时间中断进行调度的内核,大家可以根据自己的需要,添加相应的服务。
要注意到:
1,由于在时间中断内调用了任务切换函数,因为在进入中断时,已经将一系列的寄存器入栈。
2,在中断内进行调度,是直接通过RJMPInt_OSSched进入任务切换和调度的,这是GCCAVR的一个特点,为用C编写内核提供了极大的方便。
3,在阅读代码的同时,请对照阅读编译器产生的*.lst文件,会对你理解例子有很大的帮助。

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

registerunsignedcharOSRdyTblasm(r2);//任务运行就绪表
registerunsignedcharOSTaskRunningPrioasm(r3);//正在运行的任务

#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__(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__(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();
}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top