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

📄 ucos_cl.c

📁 uCOS的源码例子
💻 C
📖 第 1 页 / 共 5 页
字号:
/*
*********************************************************************************************************
*                                                 uC/OS
*                                          The Real-Time Kernel
*                                                 KERNEL
*
*                        (c) Copyright 1992-1996, Jean J. Labrosse, Plantation, FL
*                                           All Rights Reserved
*
*                                                  V1.09
*
* File : UCOS_CL.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
*********************************************************************************************************
*/

code UBYTE 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)
*********************************************************************************************************
*/

code UBYTE 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
*********************************************************************************************************
*/

far OS_TCB     * far OSTCBList;                /* Pointer to doubly linked list of TCBs                */
far UBYTE            OSRdyGrp;                 /* Ready list group                                     */
far UBYTE            OSRdyTbl[8];              /* Table of tasks which are ready to run                */
far UBYTE            OSLockNesting;            /* Multitasking lock nesting level                      */
far OS_TCB     * far OSTCBFreeList;            /* Pointer to list of free TCBs                         */
far OS_EVENT   * far OSEventFreeList;          /* Pointer to list of free EVENT control blocks         */
far OS_Q       *     OSQFreeList;              /* Pointer to list of free QUEUE control blocks         */
far ULONG            OSTime;                   /* Current value of system time (in ticks)              */
far UBYTE            OSIntExitY;               /* Variable used by 'OSIntExit' to prevent using locals */
far OS_STK_TYPE  far OSTaskIdleStk[OS_IDLE_TASK_STK_SIZE]; /* Idle task stack                          */
far OS_TCB       far OSTCBTbl[OS_MAX_TASKS+1];             /* Table of TCBs                            */
far OS_EVENT     far OSEventTbl[OS_MAX_EVENTS];            /* Table of EVENT control blocks            */
far OS_Q         far OSQTbl[OS_MAX_QS];                    /* Table of QUEUE control blocks            */

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

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

void OSInit(void)
{
    UBYTE i;


    OSTime        = 0L;
    OSTCBHighRdy  = (far OS_TCB *)0;
    OSTCBCur      = (far OS_TCB *)0;
    OSTCBList     = (far 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] = (far OS_TCB *)0;
    }
    for (i = 0; i < OS_MAX_TASKS; i++) {                   /* Init. list of free TCBs                  */
        OSTCBTbl[i].OSTCBNext = (far OS_TCB *)&OSTCBTbl[i+1];
    }
    OSTCBTbl[OS_MAX_TASKS].OSTCBNext = (far OS_TCB *)0;    /* Last OS_TCB is for OSTaskIdle()          */
    OSTCBFreeList                    = (far OS_TCB *)&OSTCBTbl[0];
    for (i = 0; i < (OS_MAX_EVENTS - 1); i++) {            /* Init. list of free EVENT control blocks  */
        OSEventTbl[i].OSEventPtr = (far OS_EVENT *)&OSEventTbl[i+1];
    }
    OSEventTbl[OS_MAX_EVENTS - 1].OSEventPtr = (far OS_EVENT *)0;
    OSEventFreeList                          = (far OS_EVENT *)&OSEventTbl[0];
    for (i = 0; i < (OS_MAX_QS - 1); i++) {                /* Init. list of free QUEUE control blocks  */
        OSQTbl[i].OSQPtr = (far OS_Q *)&OSQTbl[i+1];
    }
    OSQTbl[OS_MAX_QS - 1].OSQPtr = (far OS_Q *)0;
    OSQFreeList                  = (far OS_Q *)&OSQTbl[0];
    OSTaskCreate(OSTaskIdle, (far void *)0x12345678L, (far void *)&OSTaskIdleStk[OS_IDLE_TASK_STK_TOP], OS_LO_PRIO);
}

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

static void OSTaskIdle(far 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  = (far OS_TCB *)OSTCBPrioTbl[prio]; /* Point to highest priority task ready to run   */
    OSTCBCur      = (far OS_TCB *)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  = (far OS_TCB *)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();
        }
    }
}
/*$PAGE*/
/*
*********************************************************************************************************
*                                            INITIALIZE TCB
*********************************************************************************************************
*/

UBYTE OSTCBInit(UBYTE prio, far void *stk)
{

⌨️ 快捷键说明

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