⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 os_core.c

📁 UCOSII源代码中文注释版
💻 C
📖 第 1 页 / 共 5 页
字号:
返回:无
备注:这个函数是ucosII内部的,在应用程序中不能调用
                 当调度程序锁定的话,重新调度将被禁止。(见OS_SchedLock())

**************************************************************************************************
*/

void  OS_Sched (void)
{
#if OS_CRITICAL_METHOD == 3                            /* Allocate storage for CPU status register     */
    OS_CPU_SR  cpu_sr;//为CPU状态寄存器分配存储空间
#endif    
    INT8U      y;


    OS_ENTER_CRITICAL();//进入临界状态
    if ((OSIntNesting == 0) && (OSLockNesting == 0)) { /* Sched. only if all ISRs done & not locked    */
		//如果中断嵌套层为零,多任务处理锁定嵌套层为零
		//即只有在所以ISR完成,且没有锁定的请况下调度
        y             = OSUnMapTbl[OSRdyGrp];          /* Get pointer to HPT ready to run              */
		//得到高优先级就绪态的任务指针
        OSPrioHighRdy = (INT8U)((y << 3) + OSUnMapTbl[OSRdyTbl[y]]);
		//最高优先级任务的优先数算法,有时间要研究一下,现在看不懂
        if (OSPrioHighRdy != OSPrioCur) {              /* No Ctx Sw if current task is highest rdy     */
			//如果最高优先级任务不是当前任务
            OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy];//将最高优先级任务调到即将运行指针
            OSCtxSwCtr++;                              /* Increment context switch counter             */
			//上下文转换数加一
            OS_TASK_SW();                              /* Perform a context switch                     */
            //运行上下文转换
        }
    }
    OS_EXIT_CRITICAL();//退出临界状态
}
/*$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.
                                           空闲任务:
描述:这个任务是ucos内部任务,由于其它任务都在等事件发生,
                没有高优先级任务运行的时候它就运行
参数:无
返回:无
备注:1、出临界后要调用OSTaskIdleHook()保证中断真正开启。
                2、这个能加扩展允许我们做一些事情,如:为了节能,让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++;//加一前后中断先关后开,是因为8位或者十六位处理器加一
        //需要多条指令,防止中断打入。
        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.
                                                         统计任务:
描述:作多任务处理的一些统计,一般计算CPU使用率,公式如下:
*                                                      OSIdleCtr
*                 OSCPUUsage = 100 * (1 - ------------)     (units are in %)
*                                                     OSIdleCtrMax
参数:pdata:暂时没有用到
返回:无
备注:1、此任务优先级只比idle高,实际上,它运行在进一步高的优先
                       级上,OS_IDLE_PRIO-1
                2、通过设置OS_TASK_STAT_EN为零来禁止此任务
                3、我们延时5秒让系统稳定,统计前我们建立其它任务,
                      我们至少有两秒去让空闲任务建立最大值。

*********************************************************************************************************
*/

#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   */
		//延时两秒OSIdleCr不会像没有什么应用任务运行时那样有那么多计数,
		//它最大计数值是OSStatInit()在初始化时,保存在空闲计数器最大值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         */
		//清除,用于下一次测量
        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           */
		//一旦完成,就调用外界接入函数OSTaskStatHook(),能使统计任务得到扩展,
		//这样用户可以计算并显示所有任务总执行时间,每个任务执行的百分比等。
        OSTimeDly(OS_TICKS_PER_SEC); /* Accumulate OSIdleCtr for the next second           */
		//为下一秒作准备
    }
}
#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.
*********************************************************************************************************
*/
/*
*******************************************************************************
                                           初始化任务控制块
参数:prio:任务创建时的优先级
      ptos:假定CPU寄存器放置于堆栈中指向堆栈栈顶的指针。栈顶当OS_STK_GROWTH为1时是寄存器的  

         高位、当OS_STK_GROWTH为0时是寄存器的低位,堆栈增长是CPU的特权。
      pbos:栈底指针。由OSTaskCreate()调用时传入空指针。
      id: 任务的ID
      stk_size:堆栈大小。 当堆栈单位是int8us时,堆栈大小包含堆栈数量个字节,当堆栈单位是
               int32us时,堆栈大小包含“4*stk_size”个字节。堆栈单位由“#define constant
               OS_STK”建立,它是CPU特有。如果被OSTaskCreate()调用stk_size为0。
      pext:用户提供存储器空间的指针,用于任务控制块。允许存储浮点寄存器常量,MMU寄存器或
            者其它在内容转换时有用的东西。甚至在TCB扩展中为每个任务指定一个名字存到这个名 字里面。
            当被OSTaskCreate()调用的时候为空指针。
      opt :传到OSTaskCreateExt()时可以选择,被OSTaskCreate()调用的时候为0。
返回:OS_NO_ERR :如果调用成功。
      OS_NO_MORE_TCB:如果没有多余TCB安排,所以任务不能被创建。
备注:这个函数对uC/OS-II来说是内部的,自己的应用程序不能够调用它。

*************************************************************************
*/

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状态寄存器分配存储空间
    OS_CPU_SR  cpu_sr;//#define OS_CPU_SR    unsigned int 
#endif    
    OS_TCB    *ptcb;//定义任务控制块


    OS_ENTER_CRITICAL();//进入临界状态
    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();//退出临界状态
        ptcb->OSTCBStkPtr    = ptos;                       /* Load Stack pointer in TCB                */
		//装载TCB中的堆栈指针
        ptcb->OSTCBPrio      = (INT8U)prio;                /* Load task priority into TCB              */
		//装载TCB中任务优先级
        ptcb->OSTCBStat      = OS_STAT_RDY;                /* Task is ready to run                     */
		//任务状态设为就绪
        ptcb->OSTCBDly       = 0;                          /* Task is not delayed                      */
		//任务不延时

#if OS_TASK_CREATE_EXT_EN > 0//如果能使
        ptcb->OSTCBExtPtr    = pext;                       /* Store pointer to TCB extension           */
        //存储TCB扩展指针
        ptcb->OSTCBStkSize   = stk_size;                   /* Store stack size */
		//存储堆栈大小
        ptcb->OSTCBStkBottom = pbos;                       /* Store pointer to bottom of stack         */
		//存储栈底指针
        ptcb->OSTCBOpt       = opt;                        /* Store task options                       */
		//存储任务选项
        ptcb->OSTCBId        = id;                         /* Store task ID                            */
		//保存任务ID
#else//如果不能使
        pext                 = pext;                       /* Prevent compiler warning if not used     */
        stk_size             = stk_size;
        pbos     

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -