?? 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
*********************************************************************************************************
*/
#ifndef OS_MASTER_FILE //若未定義主文件宏
#define OS_GLOBALS //定義全局變量宏
#include "includes.h" //包括頭文件
#endif
/*
*********************************************************************************************************
* 變量定義區
*********************************************************************************************************
*/
static INT8U OSIntExitY; //定義退出中斷后的未映射消息表
static OS_STK OSTaskIdleStk[OS_TASK_IDLE_STK_SIZE]; //定義空閑任務堆棧大小未OS_TASK_IDLE_STK_SIZE個字節
#if OS_TASK_STAT_EN //若允許使用靜態任務功能
static OS_STK OSTaskStatStk[OS_TASK_STAT_STK_SIZE]; //定義靜態任務堆棧的大小
#endif
static OS_TCB OSTCBTbl[OS_MAX_TASKS + OS_N_SYS_TASKS]; //定義任務控制塊的字節個數
/*
*********************************************************************************************************
* 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 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 OSUnMapTbl[] = { //定義未映射消息字節 256個字節
0, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x00 to 0x0F */
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x10 to 0x1F */
5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x20 to 0x2F */
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x30 to 0x3F */
6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x40 to 0x4F */
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x50 to 0x5F */
5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x60 to 0x6F */
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x70 to 0x7F */
7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x80 to 0x8F */
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x90 to 0x9F */
5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0xA0 to 0xAF */
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0xB0 to 0xBF */
6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0xC0 to 0xCF */
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0xD0 to 0xDF */
5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0xE0 to 0xEF */
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 /* 0xF0 to 0xFF */
};
/*$PAGE*/
/*
*********************************************************************************************************
* MAKE TASK READY TO RUN BASED ON EVENT OCCURING
*
* Description: This function is called by other uC/OS-II services and is used to ready a task that was
* waiting for an event to occur.
*
* Arguments : pevent is a pointer to the event control block corresponding to the event.
*
* msg is a pointer to a message. This pointer is used by message oriented services
* such as MAILBOXEs and QUEUEs. The pointer is not used when called by other
* service functions.
*
* msk is a mask that is used to clear the status byte of the TCB. For example,
* OSSemPost() will pass OS_STAT_SEM, OSMboxPost() will pass OS_STAT_MBOX etc.
*
* Returns : none
*
* Note : This function is INTERNAL to uC/OS-II and your application should not call it.
*********************************************************************************************************
*/
#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
/*$PAGE*/
/*
*********************************************************************************************************
* MAKE TASK WAIT FOR EVENT TO OCCUR
*
* Description: This function is called by other uC/OS-II services to suspend a task because an event has
* not occurred.
*
* Arguments : pevent is a pointer to the event control block for which the task will be waiting for.
*
* Returns : none
*
* Note : This function is INTERNAL to uC/OS-II and your application should not call it.
*********************************************************************************************************
*/
#if (OS_Q_EN && (OS_MAX_QS >= 2)) || OS_MBOX_EN || OS_SEM_EN
void OSEventTaskWait (OS_EVENT *pevent) reentrant
{
OSTCBCur->OSTCBEventPtr = pevent; /* Store pointer to event 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
/*$PAGE*/
/*
*********************************************************************************************************
* MAKE TASK READY TO RUN BASED ON EVENT TIMEOUT
*
* Description: This function is called by other uC/OS-II services to make a task ready to run because a
* timeout occurred.
*
* Arguments : pevent is a pointer to the event control block which is readying a task.
*
* Returns : none
*
* Note : This function is INTERNAL to uC/OS-II and your application should not call it.
*********************************************************************************************************
*/
#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
/*$PAGE*/
/*
*********************************************************************************************************
* INITIALIZE EVENT CONTROL BLOCK'S WAIT LIST
*
* Description: This function is called by other uC/OS-II services to initialize the event wait list.
*
* Arguments : pevent is a pointer to the event control block allocated to the event.
*
* Returns : none
*
* Note : This function is INTERNAL to uC/OS-II and your application should not call it.
*********************************************************************************************************
*/
#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
/*$PAGE*/
/*
*********************************************************************************************************
* INITIALIZATION
*
* Description: This function is used to initialize the internals of uC/OS-II and MUST be called prior to
* creating any uC/OS-II object and, prior to calling OSStart().
*
* Arguments : none
*
* Returns : none
*********************************************************************************************************
*/
void OSInit (void) reentrant //操作系統初始化
{
INT16U i; //定義變量
OSTime = 0L;
//初始化時間變量為0
OSIntNesting = 0; //初始化中斷進入遞增 中斷退出遞減 變量為0
OSLockNesting = 0; //初始化任務鎖住時遞增 任務未鎖住時遞減變量為0
#if OS_TASK_CREATE_EN || OS_TASK_CREATE_EXT_EN || OS_TASK_DEL_EN
//若允許任務創建 或者 創建擴展任務 或者允許任務刪除
OSTaskCtr = 0; //任務創建時遞增 任務刪除時遞減變量為0 /* Clear the number of tasks */
#endif
OSRunning = FALSE; //初始化操作系統為未運行標志
OSIdleCtr = 0L; //初始化空閑任務變量 空閑任務執行時變量遞增
#if OS_TASK_STAT_EN && OS_TASK_CREATE_EXT_EN
//若運行使用靜態任務 并且可以創建擴展任務功能
OSIdleCtrRun = 0L; //初始化空閑任務運行標志
OSIdleCtrMax = 0L;//初始化空閑任務最大運行標志
OSStatRdy = FALSE; //初始化靜態任務未在準備運行狀態在靜態任務開始初始化時為TRUE
#endif
OSCtxSwCtr = 0; //任務在調度時 并且此任務是高優先級時遞增
OSRdyGrp = 0; //初始化保存任務就緒的組號變量
for (i = 0; i < OS_RDY_TBL_SIZE; i++) {
OSRdyTbl[i] = 0; //初始化就緒任務組表為0
}
OSPrioCur = 0;//初始化當前的任務優先級為0
OSPrioHighRdy = 0; //最高任務優先級初始化
OSTCBHighRdy = (OS_TCB *)0; //初始化最高就緒任務指針為0
OSTCBCur = (OS_TCB *)0; //初始化當前任務控制塊指針為0
OSTCBList = (OS_TCB *)0; //初始化任務控制塊指針為0
for (i = 0; i < (OS_LOWEST_PRIO + 1); i++) {
OSTCBPrioTbl[i] = (OS_TCB *)0;//初始化任務控制塊優先級表指針數組為0
}
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;
//初始化最后一個任務表的下一個控制塊為0
OSTCBFreeList = &OSTCBTbl[0];
//初始化釋放任務控制塊的指針指向第一個任務的起始地址
#if OS_MAX_EVENTS >= 2 //若最大事件數大于2個以上
for (i = 0; i < (OS_MAX_EVENTS - 1); i++) { //從0到事件總個數
OSEventTbl[i].OSEventPtr = (OS_EVENT *)&OSEventTbl[i + 1];
//初始化化每個事件的事件指針的下一個指針指向表的下一個地址
}
OSEventTbl[OS_MAX_EVENTS - 1].OSEventPtr = (OS_EVENT *)0;
//最后一個事件的指針的下一個地址為0
OSEventFreeList = &OSEventTbl[0];
//初始化釋放事件列表指針指向數組鏈表的第一個地址
#endif
//初始化事件
#if OS_Q_EN && (OS_MAX_QS >= 2)
//若運行使用隊列并且隊列數大于2個
OSQInit(); //初始化隊列
#endif
#if OS_MEM_EN && OS_MAX_MEM_PART >= 2
//若允許使用內存 并且內存個數 大于等于2個
OSMemInit(); //初始化內存函數
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -