📄 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
*********************************************************************************************************
*/
#include "DEFS.H"
#include "UCOS.H"
#ifdef TURBOC
#pragma inline
#endif
/******* Note that all references to "near" and "far" were deleted */
/*
*********************************************************************************************************
* CONSTANTS
*********************************************************************************************************
*/
#define OS_LO_PRIO 63 /* IDLE task priority */
#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 */
/*
*********************************************************************************************************
* 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*/
/*
*********************************************************************************************************
* GLOBAL VARIABLES
*********************************************************************************************************
*/
UWORD OSCtxSwCtr; /* Context switch counter */
ULONG OSIdleCtr; /* Counter used in OSTaskIdle() */
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 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 UBYTE 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 */
static OS_Q *OSQFreeList; /* Pointer to list of free QUEUE control blocks */
static ULONG OSTime; /* Current value of system time (in ticks) */
static UBYTE OSIntExitY; /* Variable used by 'OSIntExit' to prevent using locals */
static UBYTE 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 */
static OS_Q OSQTbl[OS_MAX_QS]; /* Table of QUEUE control blocks */
/*
*********************************************************************************************************
* LOCAL FUNCTION PROTOTYPES
*********************************************************************************************************
*/
static void OSTaskIdle(void);
static void OSEventTaskResume(OS_EVENT *pevent);
/*
*********************************************************************************************************
* uCOS 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 = 0;
OSRdyGrp = 0;
OSIdleCtr = 0L;
OSCtxSwCtr = 0;
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];
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];
OSTaskCreate(OSTaskIdle, (void *)&OSTaskIdleStk[OS_IDLE_TASK_STK_SIZE], OS_LO_PRIO);
}
/*
*********************************************************************************************************
* IDLE TASK
*********************************************************************************************************
*/
static void OSTaskIdle(void)
{
while (1) {
OS_ENTER_CRITICAL();
OSIdleCtr++;
OS_EXIT_CRITICAL();
}
}
/*
*********************************************************************************************************
* START MULTITASKING
*********************************************************************************************************
*/
void OSStart(void)
{
UBYTE y;
UBYTE x;
UBYTE 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)
{
register 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();
}
/*
*********************************************************************************************************
* 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();
}
}
/*
*********************************************************************************************************
* INITIALIZE TCB
*********************************************************************************************************
*/
UBYTE OSTCBInit(UBYTE 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 = (UBYTE)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 */
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 + -