📄 ucos.c
字号:
/*
******************************************************************************
* uCOS
* Microcomputer Real-Time Multitasking Operating System
* KERNEL
*
* (c) Copyright 1992, Jean J. Labrosse, Plantation, FL
* All Rights Reserved
*
*
* File : UCOS.C
* By : Jean J. Labrosse
******************************************************************************
*/
#define UCOS 1
#include "common.h"
#include "196.H"
#include "UCOS.H"
#include "apitsk.h" /* Aplication specific defined stack areas */
#ifdef TURBOC
#pragma inline
#endif
/*
******************************************************************************
* CONSTANTS
******************************************************************************
*/
#define OS_LO_PRIO 63 /* IDLE task priority */
/* TASK STATUS */
#define OS_STAT_RDY 0 /* Ready to run */
#define OS_STAT_SEM 1 /* Pending on semaphore */
#define OS_STAT_MBOX 2 /* Pending on mailbox */
#define OS_STAT_Q 3 /* Pending on queue */
#define OS_STAT_MEVNT 4 /* Pending on sema and mail */
#define OS_STAT_MREC 5 /* Mail received */
#define OS_STAT_SREC 6 /* Sema received */
#define OS_STAT_QREC 7 /* Queue received */
/*$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
******************************************************************************
*/
BYTE 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)
******************************************************************************
*/
BYTE 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*/
/*
******************************************************************************
* GLOBAL VARIABLES
******************************************************************************
*/
#if USE_DEBUG
WORD OSCtxSwCtr; /* Context switch counter */
LONG OSIdleCtr; /* Counter used in OSTaskIdle() */
#endif /*USE_DEBUG*/
BOOLEAN OSRunning;/*Flag indicating that uCOS has been started and is running*/
OS_TCB *OSTCBCur; /* Pointer to current TCB */
OS_TCB *OSTCBHighRdy;/* Pointer to TCB of highest priority task ready to run */
OS_TCB *OSTCBPrioTbl[64]; /* Table of pointers to created TCBs */
/*
******************************************************************************
* LOCAL VARIABLES
******************************************************************************
*/
static OS_TCB *OSTCBList; /* Pointer to doubly linked list of TCBs */
static BYTE OSRdyGrp; /* Ready list group */
static BYTE OSRdyTbl[8]; /* Table of tasks which are ready to run */
static BYTE OSLockNesting; /* Multitasking lock nesting level */
static BYTE OSIntNesting; /* Interrupt nesting level */
static OS_TCB *OSTCBFreeList; /* Pointer to list of free TCBs */
static OS_EVENT *OSEventFreeList;
/* Pointer to list of free EVENT control blocks */
#if USE_QUEUE
static OS_Q *OSQFreeList; /* Pointer to list of free QUEUE control blocks */
#endif /*USE_QUEUE*/
static LONG OSTime; /* Current value of system time (in ticks) */
static BYTE OSIntExitY;/*Variable used by 'OSIntExit' to prevent using locals */
static BYTE OSTaskIdleStk[OS_IDLE_TASK_STK_SIZE]; /* Idle task stack */
static OS_TCB OSTCBTbl[OS_MAX_TASKS+1]; /* Table of TCBs */
static OS_EVENT OSEventTbl[OS_MAX_EVENTS]; /* Table of EVENT control blocks */
#if USE_QUEUE
static OS_Q OSQTbl[OS_MAX_QS]; /* Table of QUEUE control blocks */
#endif /*USE_QUEUE*/
/*
******************************************************************************
* LOCAL FUNCTION PROTOTYPES
******************************************************************************
*/
static void OSTaskIdle(void);
static void OSMakeReady(BYTE prdy);
static void OSEventTaskResume(OS_EVENT *pevent, BYTE status);
/*$PAGE*/
/*
******************************************************************************
* uCOS INITIALIZATION
******************************************************************************
*/
void OSInit(void)
{
BYTE i;
OSTime = 0L;
OSTCBHighRdy = (OS_TCB *)0;
OSTCBCur = (OS_TCB *)0;
OSTCBList = (OS_TCB *)0;
OSIntNesting = 0;
OSLockNesting = 0;
OSRunning = 0;
OSRdyGrp = 0;
#if USE_DEBUG
OSIdleCtr = 0L;
OSCtxSwCtr = 0;
#endif /*USE_DEBUG*/
for (i = 0; i < 8; i++)
{
OSRdyTbl[i] = 0;
}
for (i = 0; i < 64; i++)
{
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];
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];
#if USE_QUEUE
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 /*USE_QUEUE*/
OSTaskCreate((void *)OSTaskIdle, (void *)0,
(void *)&OSTaskIdleStk[OS_IDLE_TASK_STK_SIZE], OS_LO_PRIO);
}
/*
******************************************************************************
* IDLE TASK
******************************************************************************
*/
static void OSTaskIdle(void)
{
while (1)
{
OS_ENTER_CRITICAL();
#if USE_DEBUG
OSIdleCtr++;
#endif /*USE_DEBUG*/
OS_EXIT_CRITICAL();
}
}
/*$PAGE*/
/*
******************************************************************************
* START MULTITASKING
******************************************************************************
*/
void OSStart(void)
{
BYTE y;
BYTE x;
BYTE p;
y = OSUnMapTbl[OSRdyGrp];/* Find highest priority's task priority number */
x = OSUnMapTbl[OSRdyTbl[y]];
p = (y << 3) + x;
OSTCBHighRdy = OSTCBPrioTbl[p];
/* Point to highest priority task ready to run */
OSRunning = 1;
OSStartHighRdy();
}
/*
******************************************************************************
* uCOS SCHEDULER
******************************************************************************
*/
void OSSched(void)
{
BYTE 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 */
#if USE_DEBUG
OSCtxSwCtr++; /* Increment context switch counter */
#endif /*USE_DEBUG*/
OS_TASK_SW(); /* Perform a context switch */
}
}
OS_EXIT_CRITICAL();
}
/*$PAGE*/
/*
******************************************************************************
* PREVENT SCHEDULING
******************************************************************************
*/
void OSSchedLock(void)
{
OS_ENTER_CRITICAL();
OSLockNesting++; /* Increment lock nesting level */
OS_EXIT_CRITICAL();
}
/*
******************************************************************************
* ENABLE SCHEDULING
******************************************************************************
*/
void OSSchedUnlock(void)
{
OS_ENTER_CRITICAL();
if (OSLockNesting != 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
******************************************************************************
*/
BYTE OSTCBInit(BYTE p, void *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 = (BYTE)p; /* Load task priority into TCB */
ptcb->OSTCBStat = OS_STAT_RDY; /* Task is ready to run */
ptcb->OSTCBDly = 0;
ptcb->OSTCBX = p & 0x07;
ptcb->OSTCBBitX = OSMapTbl[p & 0x07];
ptcb->OSTCBY = p >> 3;
ptcb->OSTCBBitY = OSMapTbl[p >> 3];
ptcb->OSTCBEventPtr = (OS_EVENT *)0;/*Task is not pending on an event*/
ptcb->OSTCBEvent2Ptr =(OS_EVENT *)0;/*Task is not pending on an event*/
OS_ENTER_CRITICAL();
OSTCBPrioTbl[p] = ptcb;
ptcb->OSTCBNext = OSTCBList; /* Link into TCB chain */
ptcb->OSTCBPrev = (OS_TCB *)0;
if (OSTCBList != (OS_TCB *)0)
{
OSTCBList->OSTCBPrev = ptcb;
}
OSTCBList = ptcb;
OSRdyGrp |= OSMapTbl[p >> 3]; /* Make task ready to run */
OSRdyTbl[p >> 3] |= OSMapTbl[p & 0x07];
OS_EXIT_CRITICAL();
return (OS_NO_ERR);
} else
{
OS_EXIT_CRITICAL();
return (OS_NO_MORE_TCB);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -