在线看毛片网站电影-亚洲国产欧美日韩精品一区二区三区,国产欧美乱夫不卡无乱码,国产精品欧美久久久天天影视,精品一区二区三区视频在线观看,亚洲国产精品人成乱码天天看,日韩久久久一区,91精品国产91免费

<menu id="6qfwx"><li id="6qfwx"></li></menu>
    1. <menu id="6qfwx"><dl id="6qfwx"></dl></menu>

      <label id="6qfwx"><ol id="6qfwx"></ol></label><menu id="6qfwx"></menu><object id="6qfwx"><strike id="6qfwx"><noscript id="6qfwx"></noscript></strike></object>
        1. <center id="6qfwx"><dl id="6qfwx"></dl></center>

            新聞中心

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

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

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

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

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

            //進(jìn)行任務(wù)調(diào)度
            voidOSSched(void)
            {

            __asm____volatile__(LDIR16,0x01nt);
            //清除中斷要求任務(wù)切換的標(biāo)志位,設(shè)置正在任務(wù)切換標(biāo)志位
            __asm____volatile__(SEInt);
            //開中斷,因?yàn)槿绻蛑袛嘣谌蝿?wù)調(diào)度中進(jìn)行,要重新進(jìn)行調(diào)度時(shí),已經(jīng)關(guān)中斷
            //根據(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__(Int_OSSched:nt);//當(dāng)中斷要求調(diào)度,直接進(jìn)入這里
            __asm____volatile__(SEInt);
            //開中斷,因?yàn)槿绻蛑袛嘣谌蝿?wù)調(diào)度中進(jìn)行,已經(jīng)關(guān)中斷
            __asm____volatile__(PUSHR28nt);//R28與R29用于在堆棧上的指針
            __asm____volatile__(PUSHR29nt);//入棧完成

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

            unsignedcharOSNextTaskPrio;//在現(xiàn)有堆棧上開設(shè)新的空間
            for(OSNextTaskPrio=0;//進(jìn)行任務(wù)調(diào)度
            OSNextTaskPrioOS_TASKS!(OSRdyTbl(0x01OSNextTaskPrio));
            OSNextTaskPrio++);
            OSTaskRunningPrio=OSNextTaskPrio;

            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í)出棧完成
            __asm____volatile__(CLInt);//關(guān)中斷
            __asm____volatile__(SBRCR16,1nt);//SBRC當(dāng)寄存器位為0剛跳過下一條指令
            //檢查是在調(diào)度時(shí),是否有中斷要求任務(wù)調(diào)度0x02是中斷要求調(diào)度的標(biāo)志位
            __asm____volatile__(RJMPOSSchednt);//重新調(diào)度
            __asm____volatile__(LDIR16,0x00nt);
            //清除中斷要求任務(wù)切換的標(biāo)志位,清除正在任務(wù)切換標(biāo)志位
            __asm____volatile__(RETInt);//返回并開中斷
            }


            //從中斷退出并進(jìn)行調(diào)度
            voidIntSwitch(void)
            {
            //當(dāng)中斷無嵌套,并且沒有在切換任務(wù)的過程中,直接進(jìn)行任務(wù)切換
            if(OSCoreState==0x02IntNum==0)
            {
            //進(jìn)入中斷時(shí),已經(jīng)保存了SREG和R0,R1,R18~R27,R30,R31
            __asm____volatile__(POPR31nt);//去除因調(diào)用子程序而入棧的PC
            __asm____volatile__(POPR31nt);
            __asm____volatile__(LDIR16,0x01nt);
            //清除中斷要求任務(wù)切換的標(biāo)志位,設(shè)置正在任務(wù)切換標(biāo)志位
            __asm____volatile__(RJMPInt_OSSchednt);//重新調(diào)度
            }
            }
            ////////////////////////////////////////////任務(wù)處理
            //掛起任務(wù)
            voidOSTaskSuspend(unsignedcharprio)
            {
            TCB[prio].OSWaitTick=0;
            OSRdyTbl=~(0x01prio);//從任務(wù)就緒表上去除標(biāo)志位
            if(OSTaskRunningPrio==prio)//當(dāng)要掛起的任務(wù)為當(dāng)前任務(wù)
            OSSched();//從新調(diào)度
            }

            //恢復(fù)任務(wù)可以讓被OSTaskSuspend或OSTimeDly暫停的任務(wù)恢復(fù)
            voidOSTaskResume(unsignedcharprio)
            {
            OSRdyTbl|=0x01prio;//從任務(wù)就緒表上重置標(biāo)志位
            TCB[prio].OSWaitTick=0;//將時(shí)間計(jì)時(shí)設(shè)為0,到時(shí)
            if(OSTaskRunningPrio>prio)//當(dāng)要當(dāng)前任務(wù)的優(yōu)先級(jí)低于重置位的任務(wù)的優(yōu)先級(jí)
            OSSched();//從新調(diào)度//從新調(diào)度
            }

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


            //信號(hào)量
            structSemBlk
            {
            unsignedcharOSEventType;//型號(hào)0,信號(hào)量獨(dú)占型;1信號(hào)量共享型
            unsignedcharOSEventState;//狀態(tài)0,不可用;1,可用
            unsignedcharOSTaskPendTbl;//等待信號(hào)量的任務(wù)列表
            }Sem[10];

            //初始化信號(hào)量
            voidOSSemCreat(unsignedcharIndex,unsignedcharType)
            {
            Sem[Index].OSEventType=Type;//型號(hào)0,信號(hào)量獨(dú)占型;1信號(hào)量共享型
            Sem[Index].OSTaskPendTbl=0;
            Sem[Index].OSEventState=0;
            }

            //任務(wù)等待信號(hào)量,掛起
            //當(dāng)Timeout==0xffff時(shí),為無限延時(shí)
            unsignedcharOSTaskSemPend(unsignedcharIndex,unsignedintTimeout)
            {

            //unsignedchari=0;
            if(Sem[Index].OSEventState)//信號(hào)量有效
            {
            if(Sem[Index].OSEventType==0)//如果為獨(dú)占型
            Sem[Index].OSEventState=0x00;//信號(hào)量被獨(dú)占,不可用
            }
            else
            {//加入信號(hào)的任務(wù)等待表
            Sem[Index].OSTaskPendTbl|=0x01OSTaskRunningPrio;
            TCB[OSTaskRunningPrio].OSWaitTick=Timeout;//如延時(shí)為0,剛無限等待
            OSRdyTbl=~(0x01OSTaskRunningPrio);//從任務(wù)就緒表中去除
            OSSched();//從新調(diào)度
            if(TCB[OSTaskRunningPrio].OSWaitTick==0)//超時(shí),未能拿到資源
            return0;
            }
            return1;
            }



            //發(fā)送信號(hào)量,可以從任務(wù)或中斷發(fā)送
            voidOSSemPost(unsignedcharIndex)
            {
            if(Sem[Index].OSEventType)//當(dāng)要求的信號(hào)量是共享型
            {
            Sem[Index].OSEventState=0x01;//使信號(hào)量有效
            OSRdyTbl|=Sem[Index].OSTaskPendTbl;//使在等待該信號(hào)的所有任務(wù)就緒
            Sem[Index].OSTaskPendTbl=0;//清空所有等待該信號(hào)的等待任務(wù)
            }
            else//當(dāng)要求的信號(hào)量為獨(dú)占型
            {
            unsignedchari;
            for(i=0;iOS_TASKS!(Sem[Index].OSTaskPendTbl(0x01i));i++);
            if(iOS_TASKS)//如果有任務(wù)需要
            {
            Sem[Index].OSTaskPendTbl=~(0x01i);//從等待表中去除
            OSRdyTbl|=0x01i;//任務(wù)就緒
            }
            else
            {
            Sem[Index].OSEventState=1;//使信號(hào)量有效
            }
            }
            }

            //從任務(wù)發(fā)送信號(hào)量,并進(jìn)行調(diào)度
            voidOSTaskSemPost(unsignedcharIndex)
            {
            OSSemPost(Index);
            OSSched();
            }

            //清除信號(hào)量,只對共享型的有用。
            //對于獨(dú)占型的信號(hào)量,在任務(wù)占用后,就交得不可以用了。

            voidOSSemClean(unsignedcharIndex)
            {
            Sem[Index].OSEventState=0;//要求的信號(hào)量無效
            }



            voidTCN0Init(void)//計(jì)時(shí)器0
            {
            TCCR0=0;
            TCCR0|=(1CS02);//256預(yù)分頻


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

            評(píng)論


            相關(guān)推薦

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

            關(guān)閉