📄 os_core.c
字号:
/*
*********************************************************************************************************
* uC/OS-II
* The Real-Time Kernel
* CORE FUNCTIONS
*
* (c) Copyright 1992-1998, Jean J. Labrosse, Plantation, FL
* All Rights Reserved
*
* V2.00
*
* File : OS_CORE.C
* By : Jean J. Labrosse
*********************************************************************************************************
*/
#define OS_GLOBALS
#include "INCLUDES.H"
/*
*********************************************************************************************************
* LOCAL GLOBAL VARIABLES
*********************************************************************************************************
*/
static INT8U OSIntExitY;
/*$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
*********************************************************************************************************
*/
INT8U const code 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)
*********************************************************************************************************
*/
INT8U const code 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
};
//事件时一个任务进入就绪状态
#if (OS_Q_EN && (OS_MAX_QS >= 2)) || OS_MBOX_EN || OS_Sem_EN
void OSEventTaskRdy (OS_EVENT *pevent, void *msg, INT8U msk)reentrant
{
OS_TCB *ptcb;
INT8U x;
INT8U y;
INT8U bitx;
INT8U bity;
INT8U prio;
y = OSUnMapTbl[pevent->OSEventGrp]; /* Find highest prio. task waiting for message */
bity = OSMapTbl[y];
x = OSUnMapTbl[pevent->OSEventTbl[y]];
bitx = OSMapTbl[x];
prio = (INT8U)((y << 3) + x); /* Find priority of task getting the msg */
if ((pevent->OSEventTbl[y] &= ~bitx) == 0) { /* Remove this task from the waiting list */
pevent->OSEventGrp &= ~bity;
}
ptcb = OSTCBPrioTbl[prio]; /* Point to this task's OS_TCB */
ptcb->OSTCBDly = 0; /* Prevent OSTimeTick() from readying task */
ptcb->OSTCBEventPtr = (OS_EVENT *)0; /* Unlink ECB from this task */
#if (OS_Q_EN && (OS_MAX_QS >= 2)) || OS_MBOX_EN
ptcb->OSTCBMsg = msg; /* Send message directly to waiting task */
#else
msg = msg; /* Prevent compiler warning if not used */
#endif
ptcb->OSTCBStat &= ~msk; /* Clear bit associated with event type */
if (ptcb->OSTCBStat == OS_STAT_RDY) { /* See if task is ready (could be susp'd) */
OSRdyGrp |= bity; /* Put task in the ready to run list */
OSRdyTbl[y] |= bitx;
}
}
#endif
//任务等待事件发生
#if (OS_Q_EN && (OS_MAX_QS >= 2)) || OS_MBOX_EN || OS_Sem_EN
void OSEventTaskWait (OS_EVENT *pevent)reentrant
{
OSTCBCur->OSTCBEventPtr = pevent; /* control block in TCB */
if ((OSRdyTbl[OSTCBCur->OSTCBY] &= ~OSTCBCur->OSTCBBitX) == 0) { /* Task no longer ready */
OSRdyGrp &= ~OSTCBCur->OSTCBBitY;
}
pevent->OSEventTbl[OSTCBCur->OSTCBY] |= OSTCBCur->OSTCBBitX; /* Put task in waiting list */
pevent->OSEventGrp |= OSTCBCur->OSTCBBitY;
}
#endif
//等到超时而使一个任务进入就绪状态
#if (OS_Q_EN && (OS_MAX_QS >= 2)) || OS_MBOX_EN || OS_Sem_EN
void OSEventTO (OS_EVENT *pevent)reentrant
{
if ((pevent->OSEventTbl[OSTCBCur->OSTCBY] &= ~OSTCBCur->OSTCBBitX) == 0) {
pevent->OSEventGrp &= ~OSTCBCur->OSTCBBitY;
}
OSTCBCur->OSTCBStat = OS_STAT_RDY; /* Set status to ready */
OSTCBCur->OSTCBEventPtr = (OS_EVENT *)0; /* No longer waiting for event */
}
#endif
//初始化一个事件控制快
#if (OS_Q_EN && (OS_MAX_QS >= 2)) || OS_MBOX_EN || OS_Sem_EN
void OSEventWaitListInit (OS_EVENT *pevent)reentrant
{
INT8U i;
pevent->OSEventGrp = 0x00; /* No task waiting on event */
for (i = 0; i < OS_EVENT_TBL_SIZE; i++) {
pevent->OSEventTbl[i] = 0x00;
}
}
#endif
//系统初始化
void OSInit (void)
{
INT8U i;
OSTime = 0;
OSIntNesting = 0;
OSLockNesting = 0;
#if OS_TASK_CREATE_EN || OS_TASK_CREATE_EXT_EN || OS_TASK_DEL_EN
OSTaskCtr = 0;
#endif
OSRunning = FALSE;
OSIdleCtr = 0;
#if OS_TASK_STAT_EN && OS_TASK_CREATE_EXT_EN
OSIdleCtrRun = 0;
OSIdleCtrMax = 0;
OSStatRdy = FALSE;
#endif
OSCtxSwCtr = 0;
OSRdyGrp = 0;
for (i = 0; i < OS_RDY_TBL_SIZE; i++) {
OSRdyTbl[i] = 0;
}
OSPrioCur = 0;
OSPrioHighRdy = 0;
OSTCBHighRdy = (OS_TCB *)0;
OSTCBCur = (OS_TCB *)0;
OSTCBList = (OS_TCB *)0;
//初始化优先级任务控制块列表
for (i = 0; i < (OS_LOWEST_PRIO + 1);i++){
OSTCBPrioTbl[i] = (OS_TCB *)0;
}
//连接空闲OS_TCB
for (i = 0; i < OS_MAX_TASKS + OS_N_SYS_TASKS - 1; i++) {
OSTCBTbl[i].OSTCBNext = &OSTCBTbl[i + 1];
}
OSTCBTbl[OS_MAX_TASKS + OS_N_SYS_TASKS - 1].OSTCBNext = (OS_TCB *)0;
OSTCBFreeList = &OSTCBTbl[0];
#if OS_STK_GROWTH==1
//连接OS_STKCB并指向各自的堆栈首地址
for (i = 0; i < OS_MAX_TASKS + OS_N_SYS_TASKS - 1; i++) {
OSTKCBTbl[i].OSTKCBNext = &OSTKCBTbl[i + 1];
OSTKCBTbl[i].OSTKBtomPtr = &OSTaskStk[i][0];
}
OSTKCBTbl[OS_MAX_TASKS + OS_N_SYS_TASKS - 1].OSTKCBNext = (OS_STKCB *)0;
OSTKCBTbl[OS_MAX_TASKS + OS_N_SYS_TASKS - 1].OSTKBtomPtr = &OSTaskStk[OS_MAX_TASKS + OS_N_SYS_TASKS - 1][0];
OSTKCBFreeList = &OSTKCBTbl[0];
#else
//连接OS_STKCB并指向各自的堆栈尾地址
for (i = 0; i < OS_MAX_TASKS + OS_N_SYS_TASKS - 1; i++) {
OSTKCBTbl[i].OSTKCBNext = &OSTKCBTbl[i + 1];
OSTKCBTbl[i].OSTKBtomPtr= &OSTaskStk[i][OS_STK_SIZE-1];
}
OSTKCBTbl[OS_MAX_TASKS + OS_N_SYS_TASKS - 1].OSTKCBNext = (OS_STKCB *)0;
OSTKCBTbl[OS_MAX_TASKS + OS_N_SYS_TASKS - 1].OSTKBtomPtr = &OSTaskStk[OS_MAX_TASKS + OS_N_SYS_TASKS - 1][OS_STK_SIZE-1];
OSTKCBFreeList = &OSTKCBTbl[0];
#endif
#if OS_MAX_EVENTS >= 2
for (i = 0; i < (OS_MAX_EVENTS - 1); i++) {
OSEventTbl[i].OSEventPtr = (OS_EVENT *)&OSEventTbl[i + 1];
}
OSEventTbl[OS_MAX_EVENTS - 1].OSEventPtr = (OS_EVENT *)0;
OSEventFreeList = &OSEventTbl[0];
#endif
#if OS_Q_EN && (OS_MAX_QS >= 2)
OSQInit();
#endif
#if OS_MEM_EN && OS_MAX_MEM_PART >= 2
OSMemInit();
#endif
//建立IDLE任务
OSTaskIdleCreate();
//建立STAT任务
#if OS_TASK_STAT_EN
OSTaskStatCreate();
#endif
}
//进入中断
void OSIntEnter (void)
{
OS_ENTER_CRITICAL();
OSIntNesting++; /* Increment ISR nesting level */
OS_EXIT_CRITICAL();
}
//退出中断
void OSIntExit (void)
{
OS_ENTER_CRITICAL();
if ((--OSIntNesting | OSLockNesting) == 0) { /* Reschedule only if all ISRs completed & not locked */
OSIntExitY = OSUnMapTbl[OSRdyGrp];
OSPrioHighRdy = (INT8U)((OSIntExitY << 3) + OSUnMapTbl[OSRdyTbl[OSIntExitY]]);
if (OSPrioHighRdy != OSPrioCur) { /* No context switch if current task is highest ready */
OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy];
OSCtxSwCtr++; /* Keep track of the number of context switches */
OSIntCtxSw(); /* Perform interrupt level context switch */
}
}
OS_EXIT_CRITICAL();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -