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

📄 ucos.c

📁 ppc860平台上移植uc OS的实例
💻 C
📖 第 1 页 / 共 5 页
字号:
/*
*********************************************************************************************************
*                                                 uC/OS
*                                          The Real-Time Kernel
*                                                 KERNEL
*
*                        (c) Copyright 1992-1996, Jean J. Labrosse, Plantation, FL
*                                           All Rights Reserved
*
*                                                  V1.10
*
* File : UCOS.C
* By   : Jean J. Labrosse
*********************************************************************************************************
*/

#define  OS_GLOBALS
#include "INCLUDES.H"

/*
*********************************************************************************************************
*                                              CONSTANTS
*********************************************************************************************************
*/

#define  OS_LO_PRIO          63        /* IDLE task priority                                           */

                                       /* TASK STATUS                                                  */
#define  OS_STAT_RDY       0x00        /* Ready to run                                                 */
#define  OS_STAT_SEM       0x01        /* Pending on semaphore                                         */
#define  OS_STAT_MBOX      0x02        /* Pending on mailbox                                           */
#define  OS_STAT_Q         0x04        /* Pending on queue                                             */
#define  OS_STAT_SUSPEND   0x08        /* Task is suspended                                            */
/*$PAGE*/
/*
*********************************************************************************************************
*                              MAPPING TABLE TO MAP BIT POSITION TO BIT MASK
*
* Note: Index into table is desired bit position, 0..7
*       Indexed value corresponds to bit mask
*********************************************************************************************************
*/

UBYTE const OSMapTbl[]   = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};

/*
*********************************************************************************************************
*                                       PRIORITY RESOLUTION TABLE
*
* Note: Index into table is bit pattern to resolve highest priority
*       Indexed value corresponds to highest priority bit position (i.e. 0..7)
*********************************************************************************************************
*/

UBYTE const OSUnMapTbl[] = {
    0, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
    4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
    5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
    4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
    6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
    4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
    5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
    4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
    7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
    4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
    5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
    4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
    6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
    4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
    5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
    4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
};
/*$PAGE*/
/*
*********************************************************************************************************
*                                            LOCAL VARIABLES
*********************************************************************************************************
*/

static  OS_TCB     *OSTCBList;                 /* Pointer to doubly linked list of TCBs                */
static  UBYTE       OSRdyGrp;                  /* Ready list group                                     */
static  UBYTE       OSRdyTbl[8];               /* Table of tasks which are ready to run                */
static  UBYTE       OSLockNesting;             /* Multitasking lock nesting level                      */
static  OS_TCB     *OSTCBFreeList;             /* Pointer to list of free TCBs                         */
static  OS_EVENT   *OSEventFreeList;           /* Pointer to list of free EVENT control blocks         */

static  ULONG       OSTime;                    /* Current value of system time (in ticks)              */
static  UBYTE       OSIntExitY;                /* Variable used by 'OSIntExit' to prevent using locals */
static  OS_STK_TYPE OSTaskIdleStk[OS_IDLE_TASK_STK_SIZE];  /* Idle task stack                          */
static  OS_TCB      OSTCBTbl[OS_MAX_TASKS+1];              /* Table of TCBs                            */

#if     OS_MAX_EVENTS > 0
static  OS_EVENT    OSEventTbl[OS_MAX_EVENTS];             /* Table of EVENT control blocks            */
#endif

#if     OS_Q_EN && (OS_MAX_QS > 0)
static  OS_Q       *OSQFreeList;               /* Pointer to list of free QUEUE control blocks         */
static  OS_Q        OSQTbl[OS_MAX_QS];         /* Table of QUEUE control blocks                        */
#endif


/*
*********************************************************************************************************
*                                       LOCAL FUNCTION PROTOTYPES
*********************************************************************************************************
*/

static  void OS_FAR OSTaskIdle(void *data);
static  void        OSDummy(void);
/*$PAGE*/
/*
*********************************************************************************************************
*                                         uC/OS INITIALIZATION
*********************************************************************************************************
*/

void OSInit(void)
{
    UBYTE i;


    OSTime        = 0L;
    OSTCBHighRdy  = (OS_TCB *)0;
    OSTCBCur      = (OS_TCB *)0;
    OSTCBList     = (OS_TCB *)0;
    OSIntNesting  = 0;
    OSLockNesting = 0;
    OSRunning     = FALSE;                                 /* Indicate that multitasking not started   */
    OSIdleCtr     = 0L;
    OSCtxSwCtr    = 0;
    OSRdyGrp      = 0;                                     /* Clear the ready list                     */
    for (i = 0; i < 8; i++) {
        OSRdyTbl[i] = 0;
    }
    for (i = 0; i < 64; i++) {                             /* Clear the priority table                 */
        OSTCBPrioTbl[i] = (OS_TCB *)0;
    }
    for (i = 0; i < OS_MAX_TASKS; i++) {                   /* Init. list of free TCBs                  */
        OSTCBTbl[i].OSTCBNext = &OSTCBTbl[i+1];
    }
    OSTCBTbl[OS_MAX_TASKS].OSTCBNext = (OS_TCB *)0;        /* Last OS_TCB is for OSTaskIdle()          */
    OSTCBFreeList                    = &OSTCBTbl[0];
#if OS_MAX_EVENTS > 0
    for (i = 0; i < (OS_MAX_EVENTS - 1); i++) {            /* Init. list of free EVENT control blocks  */
        OSEventTbl[i].OSEventPtr = &OSEventTbl[i+1];
    }
    OSEventTbl[OS_MAX_EVENTS - 1].OSEventPtr = (OS_EVENT *)0;
    OSEventFreeList                          = &OSEventTbl[0];
#else
    OSEventFreeList                          = (OS_EVENT *)0;
#endif

#if OS_Q_EN && (OS_MAX_QS > 0)
    for (i = 0; i < (OS_MAX_QS - 1); i++) {                /* Init. list of free QUEUE control blocks  */
        OSQTbl[i].OSQPtr = &OSQTbl[i+1];
    }
    OSQTbl[OS_MAX_QS - 1].OSQPtr = (OS_Q *)0;
    OSQFreeList                  = &OSQTbl[0];
#endif

    OSTaskCreate(OSTaskIdle, (void *)0, (void *)&OSTaskIdleStk[OS_IDLE_TASK_STK_TOP], OS_LO_PRIO);
}

/*
*********************************************************************************************************
*                                              IDLE TASK
*********************************************************************************************************
*/

static void OS_FAR OSTaskIdle(void *data)
{
    data = data;
    while (1) {
        OS_ENTER_CRITICAL();
        OSIdleCtr++;
        OS_EXIT_CRITICAL();
    }
}
/*$PAGE*/
/*
*********************************************************************************************************
*                                          START MULTITASKING
*********************************************************************************************************
*/

void OSStart(void)
{
    UBYTE y;
    UBYTE x;
    UBYTE prio;


    y             = OSUnMapTbl[OSRdyGrp];        /* Find highest priority's task priority number       */
    x             = OSUnMapTbl[OSRdyTbl[y]];
    prio          = (y << 3) + x;
    OSTCBHighRdy  = OSTCBPrioTbl[prio];          /* Point to highest priority task ready to run        */
    OSTCBCur      = OSTCBHighRdy;
    OSRunning     = TRUE;
    OSStartHighRdy();                            /* Execute target specific code to start task         */
}


/*
*********************************************************************************************************
*                                            uC/OS SCHEDULER
*********************************************************************************************************
*/

void OSSched(void)
{
    UBYTE y;


    OS_ENTER_CRITICAL();
    if ((OSLockNesting | OSIntNesting) == 0) {   /* Task scheduling must be enabled and not ISR level  */
        y             = OSUnMapTbl[OSRdyGrp];    /* Get pointer to highest priority task ready to run  */
        OSTCBHighRdy  = OSTCBPrioTbl[(y << 3) + OSUnMapTbl[OSRdyTbl[y]]];
        if (OSTCBHighRdy != OSTCBCur) {          /* Make sure this is not the current task running     */
            OSCtxSwCtr++;                        /* Increment context switch counter                   */
            OS_TASK_SW();                        /* Perform a context switch                           */
        }
    }
    OS_EXIT_CRITICAL();
}
/*$PAGE*/
/*
*********************************************************************************************************
*                                          PREVENT SCHEDULING
*********************************************************************************************************
*/

void OSSchedLock(void)
{
    if (OSRunning == TRUE) {                     /* Make sure multitasking is running                  */
        OS_ENTER_CRITICAL();
        OSLockNesting++;                         /* Increment lock nesting level                       */
        OS_EXIT_CRITICAL();
    }
}


/*
*********************************************************************************************************
*                                          ENABLE SCHEDULING
*********************************************************************************************************
*/

void OSSchedUnlock(void)
{
    if (OSRunning == TRUE) {                           /* Make sure multitasking is running            */
        OS_ENTER_CRITICAL();
        if (OSLockNesting != 0) {                      /* Do not decrement if already 0                */
            OSLockNesting--;                           /* Decrement lock nesting level                 */
            if ((OSLockNesting | OSIntNesting) == 0) { /* See if scheduling re-enabled and not an ISR  */
                OS_EXIT_CRITICAL();
                OSSched();                             /* See if a higher priority task is ready       */
            } else {
                OS_EXIT_CRITICAL();
            }
        } else {
            OS_EXIT_CRITICAL();
        }
    }
}

⌨️ 快捷键说明

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