📄 os_core.c
字号:
#if OS_TASK_STAT_EN
void OSStatInit (void)
{
OSTimeDly(2); /* Synchronize with clock tick */
OS_ENTER_CRITICAL();
OSIdleCtr = 0L; /* Clear idle counter */
OS_EXIT_CRITICAL();
OSTimeDly(OS_TICKS_PER_SEC); /* Determine MAX. idle counter value for 1 second */
OS_ENTER_CRITICAL();
OSIdleCtrMax = OSIdleCtr; /* Store maximum idle counter count in 1 second */
OSStatRdy = TRUE;
OS_EXIT_CRITICAL();
}
#endif
/*
*********************************************************************************************************
* IDLE TASK
*
* Description: This task is internal to uC/OS-II and executes whenever no other higher priority tasks
* executes because they are waiting for event(s) to occur.
*
* Arguments : none
*
* Returns : none
*********************************************************************************************************
*/
void OSTaskIdle (void *pdata)
{
pdata = pdata; // 防止编译器警告
for (;;)
{
OS_ENTER_CRITICAL();
OSIdleCtr++;
OS_EXIT_CRITICAL();
}
}
/*
*********************************************************************************************************
* STATISTICS TASK
*
* Description: This task is internal to uC/OS-II and is used to compute some statistics about the
* multitasking environment. Specifically, OSTaskStat() 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.
*********************************************************************************************************
*/
#if OS_TASK_STAT_EN
void OSTaskStat (void *pdata)
{
INT32U run;
INT8S usage;
pdata = pdata; // 防止编译器警告
while (OSStatRdy == FALSE)
OSTimeDly(2 * OS_TICKS_PER_SEC); // 等待统计任务就绪
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 */
OS_EXIT_CRITICAL();
if (OSIdleCtrMax > 0L) {
usage = (INT8S)(100L - 100L * run / OSIdleCtrMax);
if (usage > 100) {
OSCPUUsage = 100;
} else if (usage < 0) {
OSCPUUsage = 0;
} else {
OSCPUUsage = usage;
}
} else {
OSCPUUsage = 0;
}
OSTaskStatHook(); /* Invoke user definable hook */
OSTimeDly(OS_TICKS_PER_SEC); /* Accumulate OSIdleCtr for the next second */
}
}
#endif
/*
*********************************************************************************************************
* 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.
*********************************************************************************************************
*/
INT8U OSTCBInit (INT8U prio, OS_STK *ptos, OS_STK *pbos, INT16U id, INT16U stk_size, void *pext, INT16U opt)
{
OS_TCB *ptcb;
OS_ENTER_CRITICAL();
ptcb = OSTCBFreeList; // 从获取空闲 TCB 列表中获取一个空闲 TCB
if (ptcb != (OS_TCB *)0) // 获取成功
{
OSTCBFreeList = ptcb->OSTCBNext; // 刷新指向空闲 TCB 列表的指针
OS_EXIT_CRITICAL();
ptcb->OSTCBStkPtr = ptos; // 加载 TCB 栈指针
ptcb->OSTCBPrio = (INT8U)prio; // 加载 TCB 任务优先级
ptcb->OSTCBStat = OS_STAT_RDY; // 使任务就绪
ptcb->OSTCBDly = 0; // 任务不被延迟
#if OS_TASK_CREATE_EXT_EN
ptcb->OSTCBExtPtr = pext; // 保存指针到 TCB 扩展块
ptcb->OSTCBStkSize = stk_size; // 保存栈大小
ptcb->OSTCBStkBottom = pbos; // 保存指针到栈尾
ptcb->OSTCBOpt = opt; // 保存任务选项
ptcb->OSTCBId = id; // 保存任务 ID
#else
pext = pext; // 防止编译器警告
stk_size = stk_size;
pbos = pbos;
opt = opt;
id = id;
#endif
#if OS_TASK_DEL_EN
ptcb->OSTCBDelReq = OS_NO_ERR;
#endif
ptcb->OSTCBY = prio >> 3; /* Pre-compute X, Y, BitX and BitY */
ptcb->OSTCBBitY = OSMapTbl[ptcb->OSTCBY];
ptcb->OSTCBX = prio & 0x07;
ptcb->OSTCBBitX = OSMapTbl[ptcb->OSTCBX];
#if OS_MBOX_EN || (OS_Q_EN && (OS_MAX_QS >= 2)) || OS_SEM_EN
ptcb->OSTCBEventPtr = (OS_EVENT *)0; // 任务不悬挂在事件上
#endif
#if OS_MBOX_EN || (OS_Q_EN && (OS_MAX_QS >= 2))
ptcb->OSTCBMsg = (void *)0; // 没有接收到消息
#endif
OS_ENTER_CRITICAL();
OSTCBPrioTbl[prio] = ptcb;
ptcb->OSTCBNext = OSTCBList; // 链接到 TCB 链
ptcb->OSTCBPrev = (OS_TCB *)0;
if (OSTCBList != (OS_TCB *)0)
OSTCBList->OSTCBPrev = ptcb;
OSTCBList = ptcb;
OSRdyGrp |= ptcb->OSTCBBitY; // 运行就绪任务
OSRdyTbl[ptcb->OSTCBY] |= ptcb->OSTCBBitX;
OS_EXIT_CRITICAL();
return (OS_NO_ERR);
}
else
{
OS_EXIT_CRITICAL();
return (OS_NO_MORE_TCB);
}
}
//--------------------------------------------------------------------------------------------
// 处理系统时钟 Tick
// 该函数将在 Ticker ISR 中被调用,但同时也可以由更高优先级的任务调用
//--------------------------------------------------------------------------------------------
void OSTimeTick (void)
{
OS_TCB *ptcb;
OSTimeTickHook(); // 调用用户定义的 HOOK
// 遍历 TCB 列表中的所有 TCB
ptcb = OSTCBList; // 指向 TCB 列表中的第一个 TCB
while (ptcb->OSTCBPrio != OS_IDLE_PRIO)
{
// OS_ENTER_CRITICAL();
if (ptcb->OSTCBDly != 0) { /* Delayed or waiting for event with TO */
if (--ptcb->OSTCBDly == 0) /* Decrement nbr of ticks to end of delay */
{
if (!(ptcb->OSTCBStat & OS_STAT_SUSPEND)) // 如果任务未被挂起,则使就绪任务执行
{
OSRdyGrp |= ptcb->OSTCBBitY;
OSRdyTbl[ptcb->OSTCBY] |= ptcb->OSTCBBitX;
}
else // 如果任务被挂起...
ptcb->OSTCBDly = 1; // 则等待 1 个 Tick 以防止在撤消挂起时任务丢失
}
}
ptcb = ptcb->OSTCBNext; // 指向下一个 TCB
// OS_EXIT_CRITICAL();
}
// 刷新 32 位 Tick 计数器
// OS_ENTER_CRITICAL();
OSTime++;
// OS_EXIT_CRITICAL();
}
//--------------------------------------------------------------------------------------------
// 返回当前 uC/OS-II 操作系统的版本号
//
// 返回值: 返回值为实际的版本号乘以100 (如: uC/OS-II 2.00 返回值为 200)
//--------------------------------------------------------------------------------------------
INT16U OSVersion (void)
{
return (OS_VERSION);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -