?? os_core.c
字號(hào):
返回:無(wú)
備注:這個(gè)函數(shù)是ucosII內(nèi)部的,在應(yīng)用程序中不能調(diào)用
當(dāng)調(diào)度程序鎖定的話,重新調(diào)度將被禁止。(見OS_SchedLock())
**************************************************************************************************
*/
void OS_Sched (void)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;//為CPU狀態(tài)寄存器分配存儲(chǔ)空間
#endif
INT8U y;
OS_ENTER_CRITICAL();//進(jìn)入臨界狀態(tài)
if ((OSIntNesting == 0) && (OSLockNesting == 0)) { /* Sched. only if all ISRs done & not locked */
//如果中斷嵌套層為零,多任務(wù)處理鎖定嵌套層為零
//即只有在所以ISR完成,且沒(méi)有鎖定的請(qǐng)況下調(diào)度
y = OSUnMapTbl[OSRdyGrp]; /* Get pointer to HPT ready to run */
//得到高優(yōu)先級(jí)就緒態(tài)的任務(wù)指針
OSPrioHighRdy = (INT8U)((y << 3) + OSUnMapTbl[OSRdyTbl[y]]);
//最高優(yōu)先級(jí)任務(wù)的優(yōu)先數(shù)算法,有時(shí)間要研究一下,現(xiàn)在看不懂
if (OSPrioHighRdy != OSPrioCur) { /* No Ctx Sw if current task is highest rdy */
//如果最高優(yōu)先級(jí)任務(wù)不是當(dāng)前任務(wù)
OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy];//將最高優(yōu)先級(jí)任務(wù)調(diào)到即將運(yùn)行指針
OSCtxSwCtr++; /* Increment context switch counter */
//上下文轉(zhuǎn)換數(shù)加一
OS_TASK_SW(); /* Perform a context switch */
//運(yùn)行上下文轉(zhuǎn)換
}
}
OS_EXIT_CRITICAL();//退出臨界狀態(tài)
}
/*$PAGE*/
/*
*********************************************************************************************************
* IDLE TASK
*
* Description: This task is internal to uC/OS-II and executes whenever no other higher priority tasks
* executes because they are ALL waiting for event(s) to occur.
*
* Arguments : none
*
* Returns : none
*
* Note(s) : 1) OSTaskIdleHook() is called after the critical section to ensure that interrupts will be
* enabled for at least a few instructions. On some processors (ex. Philips XA), enabling
* and then disabling interrupts didn't allow the processor enough time to have interrupts
* enabled before they were disabled again. uC/OS-II would thus never recognize
* interrupts.
* 2) This hook has been added to allow you to do such things as STOP the CPU to conserve
* power.
空閑任務(wù):
描述:這個(gè)任務(wù)是ucos內(nèi)部任務(wù),由于其它任務(wù)都在等事件發(fā)生,
沒(méi)有高優(yōu)先級(jí)任務(wù)運(yùn)行的時(shí)候它就運(yùn)行
參數(shù):無(wú)
返回:無(wú)
備注:1、出臨界后要調(diào)用OSTaskIdleHook()保證中斷真正開啟。
2、這個(gè)能加擴(kuò)展允許我們做一些事情,如:為了節(jié)能,讓CPU
停止工作
*********************************************************************************************************
*/
void OS_TaskIdle (void *pdata)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
pdata = pdata; /* Prevent compiler warning for not using 'pdata' */
for (;;) {
OS_ENTER_CRITICAL();
OSIdleCtr++;//加一前后中斷先關(guān)后開,是因?yàn)?位或者十六位處理器加一
//需要多條指令,防止中斷打入。
OS_EXIT_CRITICAL();
OSTaskIdleHook(); /* Call user definable HOOK */
}
}
/*$PAGE*/
/*
*********************************************************************************************************
* STATISTICS TASK
*
* Description: This task is internal to uC/OS-II and is used to compute some statistics about the
* multitasking environment. Specifically, OS_TaskStat() computes the CPU usage.
* CPU usage is determined by:
*
* OSIdleCtr
* OSCPUUsage = 100 * (1 - ------------) (units are in %)
* OSIdleCtrMax
*
* Arguments : pdata this pointer is not used at this time.
*
* Returns : none
*
* Notes : 1) This task runs at a priority level higher than the idle task. In fact, it runs at the
* next higher priority, OS_IDLE_PRIO-1.
* 2) You can disable this task by setting the configuration #define OS_TASK_STAT_EN to 0.
* 3) We delay for 5 seconds in the beginning to allow the system to reach steady state and
* have all other tasks created before we do statistics. You MUST have at least a delay
* of 2 seconds to allow for the system to establish the maximum value for the idle
* counter.
統(tǒng)計(jì)任務(wù):
描述:作多任務(wù)處理的一些統(tǒng)計(jì),一般計(jì)算CPU使用率,公式如下:
* OSIdleCtr
* OSCPUUsage = 100 * (1 - ------------) (units are in %)
* OSIdleCtrMax
參數(shù):pdata:暫時(shí)沒(méi)有用到
返回:無(wú)
備注:1、此任務(wù)優(yōu)先級(jí)只比idle高,實(shí)際上,它運(yùn)行在進(jìn)一步高的優(yōu)先
級(jí)上,OS_IDLE_PRIO-1
2、通過(guò)設(shè)置OS_TASK_STAT_EN為零來(lái)禁止此任務(wù)
3、我們延時(shí)5秒讓系統(tǒng)穩(wěn)定,統(tǒng)計(jì)前我們建立其它任務(wù),
我們至少有兩秒去讓空閑任務(wù)建立最大值。
*********************************************************************************************************
*/
#if OS_TASK_STAT_EN > 0
void OS_TaskStat (void *pdata)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
INT32U run;
INT32U max;
INT8S usage;
pdata = pdata; /* Prevent compiler warning for not using 'pdata' */
while (OSStatRdy == FALSE) {
OSTimeDly(2 * OS_TICKS_PER_SEC); /* Wait until statistic task is ready */
//延時(shí)兩秒OSIdleCr不會(huì)像沒(méi)有什么應(yīng)用任務(wù)運(yùn)行時(shí)那樣有那么多計(jì)數(shù),
//它最大計(jì)數(shù)值是OSStatInit()在初始化時(shí),保存在空閑計(jì)數(shù)器最大值OSIdleCtr中的
}
max = OSIdleCtrMax / 100L;
for (;;) {
OS_ENTER_CRITICAL();
OSIdleCtrRun = OSIdleCtr; /* Obtain the of the idle counter for the past second */
run = OSIdleCtr;
OSIdleCtr = 0L; /* Reset the idle counter for the next second */
//清除,用于下一次測(cè)量
OS_EXIT_CRITICAL();
if (max > 0L) {
usage = (INT8S)(100L - run / max);
if (usage >= 0) { /* Make sure we don't have a negative percentage */
OSCPUUsage = usage;
} else {
OSCPUUsage = 0;
}
} else {
OSCPUUsage = 0;
max = OSIdleCtrMax / 100L;
}
OSTaskStatHook(); /* Invoke user definable hook */
//一旦完成,就調(diào)用外界接入函數(shù)OSTaskStatHook(),能使統(tǒng)計(jì)任務(wù)得到擴(kuò)展,
//這樣用戶可以計(jì)算并顯示所有任務(wù)總執(zhí)行時(shí)間,每個(gè)任務(wù)執(zhí)行的百分比等。
OSTimeDly(OS_TICKS_PER_SEC); /* Accumulate OSIdleCtr for the next second */
//為下一秒作準(zhǔn)備
}
}
#endif
/*$PAGE*/
/*
*********************************************************************************************************
* INITIALIZE TCB
*
* Description: This function is internal to uC/OS-II and is used to initialize a Task Control Block when
* a task is created (see OSTaskCreate() and OSTaskCreateExt()).
*
* Arguments : prio is the priority of the task being created
*
* ptos is a pointer to the task's top-of-stack assuming that the CPU registers
* have been placed on the stack. Note that the top-of-stack corresponds to a
* 'high' memory location is OS_STK_GROWTH is set to 1 and a 'low' memory
* location if OS_STK_GROWTH is set to 0. Note that stack growth is CPU
* specific.
*
* pbos is a pointer to the bottom of stack. A NULL pointer is passed if called by
* 'OSTaskCreate()'.
*
* id is the task's ID (0..65535)
*
* stk_size is the size of the stack (in 'stack units'). If the stack units are INT8Us
* then, 'stk_size' contains the number of bytes for the stack. If the stack
* units are INT32Us then, the stack contains '4 * stk_size' bytes. The stack
* units are established by the #define constant OS_STK which is CPU
* specific. 'stk_size' is 0 if called by 'OSTaskCreate()'.
*
* pext is a pointer to a user supplied memory area that is used to extend the task
* control block. This allows you to store the contents of floating-point
* registers, MMU registers or anything else you could find useful during a
* context switch. You can even assign a name to each task and store this name
* in this TCB extension. A NULL pointer is passed if called by OSTaskCreate().
*
* opt options as passed to 'OSTaskCreateExt()' or,
* 0 if called from 'OSTaskCreate()'.
*
* Returns : OS_NO_ERR if the call was successful
* OS_NO_MORE_TCB if there are no more free TCBs to be allocated and thus, the task cannot
* be created.
*
* Note : This function is INTERNAL to uC/OS-II and your application should not call it.
*********************************************************************************************************
*/
/*
*******************************************************************************
初始化任務(wù)控制塊
參數(shù):prio:任務(wù)創(chuàng)建時(shí)的優(yōu)先級(jí)
ptos:假定CPU寄存器放置于堆棧中指向堆棧棧頂?shù)闹羔槨m敭?dāng)OS_STK_GROWTH為1時(shí)是寄存器的
高位、當(dāng)OS_STK_GROWTH為0時(shí)是寄存器的低位,堆棧增長(zhǎng)是CPU的特權(quán)。
pbos:棧底指針。由OSTaskCreate()調(diào)用時(shí)傳入空指針。
id: 任務(wù)的ID
stk_size:堆棧大小。 當(dāng)堆棧單位是int8us時(shí),堆棧大小包含堆棧數(shù)量個(gè)字節(jié),當(dāng)堆棧單位是
int32us時(shí),堆棧大小包含“4*stk_size”個(gè)字節(jié)。堆棧單位由“#define constant
OS_STK”建立,它是CPU特有。如果被OSTaskCreate()調(diào)用stk_size為0。
pext:用戶提供存儲(chǔ)器空間的指針,用于任務(wù)控制塊。允許存儲(chǔ)浮點(diǎn)寄存器常量,MMU寄存器或
者其它在內(nèi)容轉(zhuǎn)換時(shí)有用的東西。甚至在TCB擴(kuò)展中為每個(gè)任務(wù)指定一個(gè)名字存到這個(gè)名 字里面。
當(dāng)被OSTaskCreate()調(diào)用的時(shí)候?yàn)榭罩羔槨? opt :傳到OSTaskCreateExt()時(shí)可以選擇,被OSTaskCreate()調(diào)用的時(shí)候?yàn)?。
返回:OS_NO_ERR :如果調(diào)用成功。
OS_NO_MORE_TCB:如果沒(méi)有多余TCB安排,所以任務(wù)不能被創(chuàng)建。
備注:這個(gè)函數(shù)對(duì)uC/OS-II來(lái)說(shuō)是內(nèi)部的,自己的應(yīng)用程序不能夠調(diào)用它。
*************************************************************************
*/
INT8U OS_TCBInit (INT8U prio, OS_STK *ptos, OS_STK *pbos, INT16U id, INT32U stk_size, void *pext, INT16U opt)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
//為CPU狀態(tài)寄存器分配存儲(chǔ)空間
OS_CPU_SR cpu_sr;//#define OS_CPU_SR unsigned int
#endif
OS_TCB *ptcb;//定義任務(wù)控制塊
OS_ENTER_CRITICAL();//進(jìn)入臨界狀態(tài)
ptcb = OSTCBFreeList; /* Get a free TCB from the free TCB list */
//從空TCB列表中得到一塊空TCB
if (ptcb != (OS_TCB *)0) {//分配空TCB成功
OSTCBFreeList = ptcb->OSTCBNext; /* Update pointer to free TCB list */
//更新空TCB列表,即減掉一塊
OS_EXIT_CRITICAL();//退出臨界狀態(tài)
ptcb->OSTCBStkPtr = ptos; /* Load Stack pointer in TCB */
//裝載TCB中的堆棧指針
ptcb->OSTCBPrio = (INT8U)prio; /* Load task priority into TCB */
//裝載TCB中任務(wù)優(yōu)先級(jí)
ptcb->OSTCBStat = OS_STAT_RDY; /* Task is ready to run */
//任務(wù)狀態(tài)設(shè)為就緒
ptcb->OSTCBDly = 0; /* Task is not delayed */
//任務(wù)不延時(shí)
#if OS_TASK_CREATE_EXT_EN > 0//如果能使
ptcb->OSTCBExtPtr = pext; /* Store pointer to TCB extension */
//存儲(chǔ)TCB擴(kuò)展指針
ptcb->OSTCBStkSize = stk_size; /* Store stack size */
//存儲(chǔ)堆棧大小
ptcb->OSTCBStkBottom = pbos; /* Store pointer to bottom of stack */
//存儲(chǔ)棧底指針
ptcb->OSTCBOpt = opt; /* Store task options */
//存儲(chǔ)任務(wù)選項(xiàng)
ptcb->OSTCBId = id; /* Store task ID */
//保存任務(wù)ID
#else//如果不能使
pext = pext; /* Prevent compiler warning if not used */
stk_size = stk_size;
pbos
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -