新聞中心

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

        建立一個屬于自己的AVR的RTOS

        作者: 時間: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)先級的任務(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);
        //開中斷,因為如果因中斷在任務(wù)調(diào)度中進(jìn)行,要重新進(jìn)行調(diào)度時,已經(jīng)關(guān)中斷
        //根據(jù)中斷時保存寄存器的次序入棧,模擬一次中斷后,入棧的情況
        __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);
        //開中斷,因為如果因中斷在任務(wù)調(diào)度中進(jìn)行,已經(jīng)關(guān)中斷
        __asm____volatile__(PUSHR28nt);//R28與R29用于在堆棧上的指針
        __asm____volatile__(PUSHR29nt);//入棧完成

        TCB[OSTaskRunningPrio].OSTaskStackTop=SP;//將正在運行的任務(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ù)中斷時的出棧次序
        __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出棧
        //中斷時出棧完成
        __asm____volatile__(CLInt);//關(guān)中斷
        __asm____volatile__(SBRCR16,1nt);//SBRC當(dāng)寄存器位為0剛跳過下一條指令
        //檢查是在調(diào)度時,是否有中斷要求任務(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)入中斷時,已經(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è)為0,到時
        if(OSTaskRunningPrio>prio)//當(dāng)要當(dāng)前任務(wù)的優(yōu)先級低于重置位的任務(wù)的優(yōu)先級
        OSSched();//從新調(diào)度//從新調(diào)度
        }

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


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

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

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

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



        //發(fā)送信號量,可以從任務(wù)或中斷發(fā)送
        voidOSSemPost(unsignedcharIndex)
        {
        if(Sem[Index].OSEventType)//當(dāng)要求的信號量是共享型
        {
        Sem[Index].OSEventState=0x01;//使信號量有效
        OSRdyTbl|=Sem[Index].OSTaskPendTbl;//使在等待該信號的所有任務(wù)就緒
        Sem[Index].OSTaskPendTbl=0;//清空所有等待該信號的等待任務(wù)
        }
        else//當(dāng)要求的信號量為獨占型
        {
        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;//使信號量有效
        }
        }
        }

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

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

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



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


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

        評論


        相關(guān)推薦

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

        關(guān)閉
        主站蜘蛛池模板: 武隆县| 曲麻莱县| 内黄县| 运城市| 莒南县| 黄大仙区| 彰化县| 湄潭县| 商洛市| 鄂托克前旗| 乌鲁木齐县| 宜兰县| 渭源县| 新源县| 江达县| 呼图壁县| 临潭县| 东乌| 巴塘县| 横峰县| 广平县| 乐亭县| 台东市| 南澳县| 广德县| 玉溪市| 资溪县| 榆中县| 来安县| 贵定县| 邯郸县| 孟州市| 都兰县| 安国市| 兴国县| 福贡县| 弥渡县| 靖远县| 元氏县| 阿巴嘎旗| 望江县|