色婷婷AⅤ一区二区三区|亚洲精品第一国产综合亚AV|久久精品官方网视频|日本28视频香蕉

          新聞中心

          EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 建立一個(gè)屬于自己的AVR的RTOS

          建立一個(gè)屬于自己的AVR的RTOS

          作者: 時(shí)間:2011-04-29 來(lái)源:網(wǎng)絡(luò) 收藏

          unsignedintOSWaitTick;//任務(wù)延時(shí)時(shí)鐘
          }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);


          //任務(wù)
          voidOSTaskCreate(void(*Task)(void),unsignedchar*Stack,unsignedcharTaskID)
          {
          unsignedchari;
          *Stack--=(unsignedint)Task>>8;//將任務(wù)的地址高位壓入堆棧,
          *Stack--=(unsignedint)Task;//將任務(wù)的地址低位壓入堆棧,

          *Stack--=0x00;//R1__zero_reg__
          *Stack--=0x00;//R0__tmp_reg__
          *Stack--=0x80;//SREG在任務(wù)中,開(kāi)啟全局中斷
          for(i=0;i14;i++)//在avr-libc中的FAQ中的WhatregistersareusedbytheCcompiler?
          *Stack--=i;//描述了寄存器的作用
          TCB[TaskID].OSTaskStackTop=(unsignedint)Stack;//將人工堆棧的棧頂,保存到堆棧的數(shù)組中
          OSRdyTbl|=0x01TaskID;//任務(wù)就緒表已經(jīng)準(zhǔn)備好
          }

          //開(kāi)始任務(wù)調(diào)度,從最低優(yōu)先級(jí)的任務(wù)的開(kāi)始
          voidOSStartTask()
          {
          OSTaskRunningPrio=OS_TASKS;
          SP=TCB[OS_TASKS].OSTaskStackTop+17;
          __asm____volatile__(retint);
          }

          //進(jìn)行任務(wù)調(diào)度
          voidOSSched(void)
          {
          //根據(jù)中斷時(shí)保存寄存器的次序入棧,模擬一次中斷后,入棧的情況
          __asm____volatile__(PUSH__zero_reg__nt);//R1
          __asm____volatile__(PUSH__tmp_reg__nt);//R0
          __asm____volatile__(IN__tmp_reg__,__SREG__nt);//保存狀態(tài)寄存器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__(PUSHR28nt);//R28與R29用于在堆棧上的指針
          __asm____volatile__(PUSHR29nt);//入棧完成

          TCB[OSTaskRunningPrio].OSTaskStackTop=SP;//將正在運(yùn)行的任務(wù)的堆棧底保存


          unsignedcharOSNextTaskID;//在現(xiàn)有堆棧上開(kāi)設(shè)新的空間
          for(OSNextTaskID=0;//進(jìn)行任務(wù)調(diào)度
          OSNextTaskIDOS_TASKS!(OSRdyTbl(0x01OSNextTaskID));
          OSNextTaskID++);
          OSTaskRunningPrio=OSNextTaskID;

          cli();//保護(hù)堆棧轉(zhuǎn)換
          SP=TCB[OSTaskRunningPrio].OSTaskStackTop;
          sei();

          //根據(jù)中斷時(shí)的出棧次序
          __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出棧并恢復(fù)
          __asm____volatile__(OUT__SREG__,__tmp_reg__nt);//
          __asm____volatile__(POP__tmp_reg__nt);//R0出棧
          __asm____volatile__(POP__zero_reg__nt);//R1出棧
          //中斷時(shí)出棧完成
          }

          voidOSTimeDly(unsignedintticks)
          {
          if(ticks)//當(dāng)延時(shí)有效
          {
          OSRdyTbl=~(0x01OSTaskRunningPrio);
          TCB[OSTaskRunningPrio].OSWaitTick=ticks;
          OSSched();//從新調(diào)度
          }
          }


          voidTCN0Init(void)//計(jì)時(shí)器0
          {
          TCCR0=0;
          TCCR0|=(1CS02);//256預(yù)分頻
          TIMSK|=(1TOIE0);//T0溢出中斷允許
          TCNT0=100;//置計(jì)數(shù)起始值

          }


          SIGNAL(SIG_OVERFLOW0)
          {
          unsignedchari;
          for(i=0;iOS_TASKS;i++)//任務(wù)時(shí)鐘
          {
          if(TCB[i].OSWaitTick)
          {
          TCB[i].OSWaitTick--;
          if(TCB[i].OSWaitTick==0)//當(dāng)任務(wù)時(shí)鐘到時(shí),必須是由定時(shí)器減時(shí)的才行
          {
          OSRdyTbl|=(0x01i);//使任務(wù)在就緒表中置位
          }
          }
          }
          TCNT0=100;
          }

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

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

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



          voidTaskScheduler()
          {
          while(1)
          {
          OSSched();//反復(fù)進(jìn)行調(diào)度
          }
          }


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

          在上面的例子中,一切變得很簡(jiǎn)單,三個(gè)正在運(yùn)行的主任務(wù),都通過(guò)延時(shí)服務(wù),主動(dòng)放棄對(duì)CPU的控制權(quán)。
          在時(shí)間中斷中,對(duì)各個(gè)任務(wù)的的延時(shí)進(jìn)行計(jì)時(shí),如果某個(gè)任務(wù)的延時(shí)結(jié)束,將任務(wù)重新在就緒表中置位。
          最低級(jí)的系統(tǒng)任務(wù)TaskScheduler(),在三個(gè)主任務(wù)在放棄對(duì)CPU的控制權(quán)后開(kāi)始不斷地進(jìn)行調(diào)度。如果某個(gè)任務(wù)在就緒表中置位,通過(guò)調(diào)度,進(jìn)入最高級(jí)別的任務(wù)中繼續(xù)運(yùn)行。

          第五篇:完善的協(xié)作式的內(nèi)核

          現(xiàn)在為上面的協(xié)作式內(nèi)核添加一些OS中所必須的服務(wù):
          1掛起和重新運(yùn)行任務(wù)
          2信號(hào)量(在必要時(shí)候,可以擴(kuò)展成郵箱和信息隊(duì)列)
          3延時(shí)


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

          registerunsignedcharOSRdyTblasm(r2);//任務(wù)運(yùn)行就緒表
          registerunsignedcharOSTaskRunningPrioasm(r3);//正在運(yùn)行的任務(wù)

          #defineOS_TASKS3//設(shè)定運(yùn)行任務(wù)的數(shù)量
          structTaskCtrBlock
          {
          unsignedintOSTaskStackTop;//保存任務(wù)的堆棧頂
          unsignedintOSWaitTick;//任務(wù)延時(shí)時(shí)鐘
          }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);


          //任務(wù)
          voidOSTaskCreate(void(*Task)(void),unsignedchar*Stack,unsignedcharTaskID)
          {
          unsignedchari;
          *Stack--=(unsignedint)Task>>8;//將任務(wù)的地址高位壓入堆棧,
          *Stack--=(unsignedint)Task;//將任務(wù)的地址低位壓入堆棧,

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

          //SREG在任務(wù)中,開(kāi)啟全局中斷
          for(i=0;i14;i++)//在avr-libc中的FAQ中的WhatregistersareusedbytheCcompiler?
          *Stack--=i;//描述了寄存器的作用
          TCB[TaskID].OSTaskStackTop=(unsignedint)Stack;//將人工堆棧的棧頂,保存到堆棧的數(shù)組中
          OSRdyTbl|=0x01TaskID;//任務(wù)就緒表已經(jīng)準(zhǔn)備好
          }

          //開(kāi)始任務(wù)調(diào)度,從最低優(yōu)先級(jí)的任務(wù)的開(kāi)始
          voidOSStartTask()
          {
          OSTaskRunningPrio=OS_TASKS;
          SP=TCB[OS_TASKS].OSTaskStackTop+17;
          __asm____volatile__(retint);
          }

          //進(jìn)行任務(wù)調(diào)度
          voidOSSched(void)
          {
          //根據(jù)中斷時(shí)保存寄存器的次序入棧,模擬一次中斷后,入棧的情況
          __asm____volatile__(PUSH__zero_reg__nt);//R1
          __asm____volatile__(PUSH__tmp_reg__nt);//R0
          __asm____volatile__(IN__tmp_reg__,__SREG__nt);//保存狀態(tài)寄存器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__(PUSHR28nt);//R28與R29用于建立在堆棧上的指針


          關(guān)鍵詞: AVR RTOS 自己 屬于 一個(gè) 建立

          評(píng)論


          相關(guān)推薦

          技術(shù)專區(qū)

          關(guān)閉