⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ucos.c

📁 ppc860平台上移植uc OS的实例
💻 C
📖 第 1 页 / 共 5 页
字号:
/*$PAGE*/
/*
*********************************************************************************************************
*                                            INITIALIZE TCB
*********************************************************************************************************
*/

UBYTE OSTCBInit(UBYTE prio, void OS_FAR *stk)
{
    OS_TCB *ptcb;


    OS_ENTER_CRITICAL();
    ptcb = OSTCBFreeList;                                  /* Get a free TCB from the free TCB list    */
    if (ptcb != (OS_TCB *)0) {
        OSTCBFreeList           = ptcb->OSTCBNext;         /* Update pointer to free TCB list          */
        OS_EXIT_CRITICAL();
        ptcb->OSTCBStkPtr       = stk;                     /* Load Stack pointer in TCB                */
        ptcb->OSTCBPrio         = (UBYTE)prio;             /* Load task priority into TCB              */
        ptcb->OSTCBStat         = OS_STAT_RDY;             /* Task is ready to run                     */
        ptcb->OSTCBDly          = 0;
        ptcb->OSTCBDelReq       = OS_NO_ERR;
        ptcb->OSTCBY            = prio >> 3;
        ptcb->OSTCBBitY         = OSMapTbl[ptcb->OSTCBY];
        ptcb->OSTCBX            = prio & 0x07;
        ptcb->OSTCBBitX         = OSMapTbl[ptcb->OSTCBX];

#if     OS_SEM_EN || OS_MBOX_EN || (OS_Q_EN && (OS_MAX_QS > 0))
        ptcb->OSTCBEventPtr     = (OS_EVENT *)0;           /* Task is not pending on an event          */
#endif

#if     OS_MBOX_EN || (OS_Q_EN && (OS_MAX_QS > 0))
        ptcb->OSTCBMsg          = (void *)0;               /* No message received                      */
#endif

        OS_ENTER_CRITICAL();
        OSTCBPrioTbl[prio]      = ptcb;
        ptcb->OSTCBNext         = OSTCBList;               /* Link into TCB chain                      */
        ptcb->OSTCBPrev         = (OS_TCB *)0;
        if (OSTCBList != (OS_TCB *)0) {
            OSTCBList->OSTCBPrev = ptcb;
        }
        OSTCBList               = ptcb;
        OSRdyGrp               |= ptcb->OSTCBBitY;         /* Make task ready to run                   */
        OSRdyTbl[ptcb->OSTCBY] |= ptcb->OSTCBBitX;
        OS_EXIT_CRITICAL();
        return (OS_NO_ERR);
    } else {
        OS_EXIT_CRITICAL();
        return (OS_NO_MORE_TCB);
    }
}
/*$PAGE*/
/*
*********************************************************************************************************
*                                               ENTER ISR
*********************************************************************************************************
*/

void OSIntEnter(void)
{
#ifdef ISR_NOT_CRITICAL
    OS_ENTER_CRITICAL();
#endif
    OSIntNesting++;                              /* Increment ISR nesting level                        */
#ifdef ISR_NOT_CRITICAL
    OS_EXIT_CRITICAL();
#endif
}


/*
*********************************************************************************************************
*                                               EXIT ISR
*********************************************************************************************************
*/

void OSIntExit(void)
{
    OS_ENTER_CRITICAL();
    if ((--OSIntNesting | OSLockNesting) == 0) { /* Reschedule only if all ISRs completed & not locked */
        OSIntExitY   = OSUnMapTbl[OSRdyGrp];
        OSTCBHighRdy = OSTCBPrioTbl[(OSIntExitY << 3) + OSUnMapTbl[OSRdyTbl[OSIntExitY]]];
        if (OSTCBHighRdy != OSTCBCur) {          /* No context switch if current task is highest ready */
            OSCtxSwCtr++;
            OSIntCtxSw();                        /* Perform interrupt level context switch             */
        }
    }
    OS_EXIT_CRITICAL();
}
/*$PAGE*/
#if OS_TASK_CHANGE_PRIO_EN
/*
*********************************************************************************************************
*                                       CHANGE PRIORITY OF A TASK
*********************************************************************************************************
*/

UBYTE OSTaskChangePrio(UBYTE oldprio, UBYTE newprio)
{
    OS_TCB   *ptcb;
    OS_EVENT *pevent;
    UBYTE     x;
    UBYTE     y;
    UBYTE     bitx;
    UBYTE     bity;



    if (oldprio >= OS_MAX_TASKS || newprio >= OS_MAX_TASKS) {
        return (OS_PRIO_INVALID);
    }
    OS_ENTER_CRITICAL();
    if (OSTCBPrioTbl[newprio] != (OS_TCB *)0) {                 /* New priority must not already exist */
        OS_EXIT_CRITICAL();
        return (OS_PRIO_EXIST);
    } else {
        OS_EXIT_CRITICAL();
        y    = newprio >> 3;                                    /* Precompute to reduce INT. latency   */
        bity = OSMapTbl[y];
        x    = newprio & 0x07;
        bitx = OSMapTbl[x];
        OS_ENTER_CRITICAL();
        if ((ptcb = OSTCBPrioTbl[oldprio]) != (OS_TCB *)0) {    /* Task to change must exist           */
            OSTCBPrioTbl[oldprio] = (OS_TCB *)0;                /* Remove TCB from old priority        */
            if (OSRdyTbl[ptcb->OSTCBY] & ptcb->OSTCBBitX) {     /* If task is ready make it not ready  */
                if ((OSRdyTbl[ptcb->OSTCBY] &= ~ptcb->OSTCBBitX) == 0) {
                    OSRdyGrp &= ~ptcb->OSTCBBitY;
                }
                OSRdyGrp    |= bity;                            /* Make new priority ready to run      */
                OSRdyTbl[y] |= bitx;
            } else {
                if ((pevent = ptcb->OSTCBEventPtr) != (OS_EVENT *)0) { /* Remove from event wait list  */
                    if ((pevent->OSEventTbl[ptcb->OSTCBY] &= ~ptcb->OSTCBBitX) == 0) {
                        pevent->OSEventGrp &= ~ptcb->OSTCBBitY;
                    }
                    pevent->OSEventGrp    |= bity;
                    pevent->OSEventTbl[y] |= bitx;
                }
            }
            OSTCBPrioTbl[newprio] = ptcb;                       /* Place pointer to TCB @ new priority */
            ptcb->OSTCBPrio       = newprio;                    /* Set new task priority               */
            ptcb->OSTCBY          = y;
            ptcb->OSTCBX          = x;
            ptcb->OSTCBBitY       = bity;
            ptcb->OSTCBBitX       = bitx;
            OS_EXIT_CRITICAL();
            OSSched();                                          /* Run highest priority task ready     */
            return (OS_NO_ERR);
        } else {
            OS_EXIT_CRITICAL();
            return (OS_PRIO_ERR);                               /* Task to change didn't exist         */
        }
    }
}
#endif
/*$PAGE*/
#if OS_TASK_DEL_EN
/*
*********************************************************************************************************
*                                            DELETE A TASK
*
* Notes:
*    1) To reduce interrupt latency, OSTaskDel() 'disables' the task:
*           a) by making it not ready
*           b) by removing it from any wait lists
*           c) by preventing OSTimeTick() from making the task ready to run.
*       The task can then be 'unlinked' from the miscellaneous structures in uC/OS.
*    2) The function OSDummy() is called after OS_EXIT_CRITICAL() because, on most processors, the next
*       instruction following the enable interrupt instruction is ignored.  You can replace OSDummy()
*       with a macro that basically executes a NO OP (i.e. OS_NOP()).  The NO OP macro would avoid the
*       execution time of the function call and return.
*********************************************************************************************************
*/
/*$PAGE*/
UBYTE OSTaskDel(UBYTE prio)
{
    OS_TCB   *ptcb;
    OS_EVENT *pevent;
    UBYTE     priocur;


    if (prio == OS_LO_PRIO) {                                   /* Not allowed to delete idle task     */
        return (OS_TASK_DEL_IDLE);
    }
    if (prio >= OS_MAX_TASKS && prio != OS_PRIO_SELF) {         /* Make sure task priority is valid    */
        return (OS_PRIO_INVALID);
    }
    OS_ENTER_CRITICAL();
    priocur = OSTCBCur->OSTCBPrio;                              /* Obtain the current priority         */
    if (prio == OS_PRIO_SELF) {                                 /* See if requesting to delete self    */
        prio = priocur;                                         /* Set priority to delete to current   */
    }
    if ((ptcb = OSTCBPrioTbl[prio]) != (OS_TCB *)0) {           /* Task to delete must exist           */
        OSTCBPrioTbl[prio] = (OS_TCB *)0;                       /* Clear old priority entry            */
        if ((OSRdyTbl[ptcb->OSTCBY] &= ~ptcb->OSTCBBitX) == 0) {/* Make task not ready                 */
            OSRdyGrp &= ~ptcb->OSTCBBitY;
        }
        if ((pevent = ptcb->OSTCBEventPtr) != (OS_EVENT *)0) {  /* If task is waiting on event         */
            if ((pevent->OSEventTbl[ptcb->OSTCBY] &= ~ptcb->OSTCBBitX) == 0) { /* ... remove task from */
                pevent->OSEventGrp &= ~ptcb->OSTCBBitY;                        /* ... event ctrl block */
            }
        }
        ptcb->OSTCBDly = 0;                                     /* Prevent OSTimeTick() from updating  */
        OS_EXIT_CRITICAL();                                     /* Enabling INT. ignores next instruc. */
        OSDummy();                                              /* ... Dummy ensures that INTs will be */
        OS_ENTER_CRITICAL();                                    /* ... disabled HERE!                  */
        if (ptcb->OSTCBPrev == (OS_TCB *)0) {                   /* Remove from TCB chain               */
            ptcb->OSTCBNext->OSTCBPrev = (OS_TCB *)0;
            OSTCBList                  = ptcb->OSTCBNext;
        } else {
            ptcb->OSTCBPrev->OSTCBNext = ptcb->OSTCBNext;
            ptcb->OSTCBNext->OSTCBPrev = ptcb->OSTCBPrev;
        }
        ptcb->OSTCBNext = OSTCBFreeList;                        /* Return TCB to free TCB list         */
        OSTCBFreeList   = ptcb;
        if (prio == priocur) {                                  /* Resched. only if deleting self!     */
            OS_EXIT_CRITICAL();
            OSSched();                                          /* Find new highest priority task      */
        } else {
            OS_EXIT_CRITICAL();
        }
        return (OS_NO_ERR);
    } else {
        OS_EXIT_CRITICAL();
        return (OS_TASK_DEL_ERR);
    }
}
/*$PAGE*/
/*
*********************************************************************************************************
*                                   REQUEST THAT A TASK DELETE ITSELF
*
* Description : This function is used to:
*                   a) notify a task to delete itself.
*                   b) to see if a task requested that the current task delete itself.
*               This function is a little tricky to understand.  Basically, you have a task that needs
*               to be deleted however, this task has resources that it has allocated (memory buffers,
*               semaphores, mailboxes, queues etc.).  The task cannot be deleted otherwise these
*               resources would not be freed.  The requesting task calls OSTaskDelReq() to indicate that
*               the task needs to be deleted.  Deleting of the task is however, deferred to the task to
*               be deleted.  For example, suppose that task #10 needs to be deleted.  The requesting task
*               example, task #5, would call OSTaskDelReq(10).  When task #10 gets to execute, it calls
*               this function by specifying OS_PRIO_SELF and monitors the returned value.  If the return
*               value is OS_TASK_DEL_REQ, another task requested a task delete.  Task #10 would look like
*               this:
*
*                   void OS_FAR Task(void *data)
*                   {
*                       .
*                       .
*                       while (1) {
*                           OSTimeDly(1);
*                           if (OSTaskDelReq(OS_PRIO_SELF) == OS_TASK_DEL_REQ) {
*                               Release any owned resources;
*                               De-allocate any dynamic memory;
*                               OSTaskDel(OS_PRIO_SELF);
*                           }
*                       }
*                   }
*
* Arguments : 'prio'   is the priority of the task to request the delete from
*
* Returns   : if 'prio' is OS_PRIO_SELF:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -