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

📄 os_core.c

📁 UCOSII源代码中文注释版
💻 C
📖 第 1 页 / 共 5 页
字号:
描述:阻止再次调度发生,它让你准备执行任务切换的时候才进行任务切换
//参数:无
返回:无
备注:1、必须调用OSSchedLock() and OSSchedUnlock()成对
*********************************************************************************************************
*/

#if OS_SCHED_LOCK_EN > 0
void  OSSchedLock (void)
{
#if OS_CRITICAL_METHOD == 3                      /* Allocate storage for CPU status register           */
    OS_CPU_SR  cpu_sr;
#endif    
    
    
    if (OSRunning == TRUE) {                     /* Make sure multitasking is running                  */
//保证多任务在运行
        OS_ENTER_CRITICAL();
        if (OSLockNesting < 255) {               /* Prevent OSLockNesting from wrapping back to 0      */
			//防止嵌套数返回到0
            OSLockNesting++;                     /* Increment lock nesting level                       */
        }
        OS_EXIT_CRITICAL();
    }
}
#endif    

/*$PAGE*/
/*
*********************************************************************************************************
*                                          ENABLE SCHEDULING
*
* Description: This function is used to re-allow rescheduling.
*
* Arguments  : none
*
* Returns    : none
*
* Notes      : 1) You MUST invoke OSSchedLock() and OSSchedUnlock() in pair.  In other words, for every
*                 call to OSSchedLock() you MUST have a call to OSSchedUnlock().
                                                  能使任务调度
描述:用于再次允许任务调度
参数:无
返回:无
备注:1、必须将OSSchedLock() and OSSchedUnlock()成对调用
*********************************************************************************************************
*/

#if OS_SCHED_LOCK_EN > 0//能使包含代码OSSchedLock() and OSSchedUnlock()
void  OSSchedUnlock (void)
{
#if OS_CRITICAL_METHOD == 3                                /* Allocate storage for CPU status register */
    OS_CPU_SR  cpu_sr;
#endif    
    
    
    if (OSRunning == TRUE) {                                   /* Make sure multitasking is running    */
		//保证多任务运行
        OS_ENTER_CRITICAL();
        if (OSLockNesting > 0) {                               /* Do not decrement if already 0        */
			//如果是零的话就不要再减了
            OSLockNesting--;                                   /* Decrement lock nesting level         */
            if ((OSLockNesting == 0) && (OSIntNesting == 0)) { /* See if sched. enabled and not an ISR */
				//如果解锁,且中断嵌套数为零,由需要任务调度,看高优先
				//级任务是否就绪
                OS_EXIT_CRITICAL();
                OS_Sched();                                    /* See if a HPT is ready                */
            } else {
                OS_EXIT_CRITICAL();//否则就不要任务调度了
            }
        } else {
            OS_EXIT_CRITICAL();//如果锁定的话,就开中断了。
        }
    }
}
#endif    

/*$PAGE*/
/*
*********************************************************************************************************
*                                          START MULTITASKING
*
* Description: This function is used to start the multitasking process which lets uC/OS-II manages the
*              task that you have created.  Before you can call OSStart(), you MUST have called OSInit()
*              and you MUST have created at least one task.
*
* Arguments  : none
*
* Returns    : none
*
* Note       : OSStartHighRdy() MUST:
*                 a) Call OSTaskSwHook() then,
*                 b) Set OSRunning to TRUE.
*                 c) Load the context of the task pointed to by OSTCBHighRdy.
*                 d_ Execute the task.
                                                  开始多任务处理
描述:开始多任务处理,使ucos管理你建立的任务,在调用OSStart()前,你必须
                 先调用OSInit(),且至少建立了一个任务
参数:无
返回;无
备注:OSStartHighRdy()必须
                 1、先调用Call OSTaskSwHook()
                 2、再设置OSRunning为真
                 3、装载指向OSTCBHighRdy的内容的指针
                 4、执行任务
*********************************************************************************************************
*/

void  OSStart (void)
{
    INT8U y;
    INT8U x;


    if (OSRunning == FALSE) {//如果没有运行
        y             = OSUnMapTbl[OSRdyGrp];        /* Find highest priority's task priority number   */
        x             = OSUnMapTbl[OSRdyTbl[y]];//又遇到你们,真是FUCK。
        OSPrioHighRdy = (INT8U)((y << 3) + x);
        OSPrioCur     = OSPrioHighRdy;//高优先级的任务作为当前任务
        OSTCBHighRdy  = OSTCBPrioTbl[OSPrioHighRdy]; /* Point to highest priority task ready to run    */
		//下一个任务作为即将运行的任务
        OSTCBCur      = OSTCBHighRdy;
        OSStartHighRdy();                            /* Execute target specific code to start task     */
		//执行特定代码去开始任务
    }
}
/*$PAGE*/
/*
*********************************************************************************************************
*                                        STATISTICS INITIALIZATION
*
* Description: This function is called by your application to establish CPU usage by first determining
*              how high a 32-bit counter would count to in 1 second if no other tasks were to execute
*              during that time.  CPU usage is then determined by a low priority task which keeps track
*              of this 32-bit counter every second but this time, with other tasks running.  CPU usage is
*              determined by:
*
*                                                            OSIdleCtr
*                 CPU Usage (%) = 100 * (1 - ————)
*                                                          OSIdleCtrMax
*
* Arguments  : none
*
* Returns    : none
                                                 统计任务初始化
描述:假如没有其它任务在这个时候运行,以在一秒内一个32位计数器能
                 计到多少数来建立CPU使用率CPU使用率由一个每秒跟踪32位计数器的低优先
                 级任务决定,但如果其它任务运行,CPU使用率由下面函数决定:
  *                                                            OSIdleCtr
*                 CPU Usage (%) = 100 * (1 - ————)
*                                                          OSIdleCtrMax      
参数:无
返回:无
*********************************************************************************************************
*/

#if OS_TASK_STAT_EN > 0
void  OSStatInit (void)
{
#if OS_CRITICAL_METHOD == 3                      /* Allocate storage for CPU status register           */
    OS_CPU_SR  cpu_sr;
#endif    
    
    
    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
/*$PAGE*/
/*
*********************************************************************************************************
*                                         PROCESS SYSTEM TICK
*
* Description: This function is used to signal to uC/OS-II the occurrence of a 'system tick' (also known
*              as a 'clock tick').  This function should be called by the ticker ISR but, can also be
*              called by a high priority task.
*
* Arguments  : none
*
* Returns    : none
                                               建立系统时钟
描述:这个函数向ucos发信号产生时钟节拍,它能被ISR节拍调用,也可以由
                 高优先级任务调用
参数:无
返回:无
*********************************************************************************************************
*/

void  OSTimeTick (void)
{
#if OS_CRITICAL_METHOD == 3                                /* Allocate storage for CPU status register */
    OS_CPU_SR  cpu_sr;
#endif    
    OS_TCB    *ptcb;


    OSTimeTickHook();                                      /* Call user definable hook                 */
	//用户定义的时钟节拍外连函数,,可将时钟节拍函数OSTimeTick扩展,调用此
	//函数是打算在中断一开始给用户一个可以做点什么的机会,
#if OS_TIME_GET_SET_EN > 0   //能使包含代码OSTimeGet() and OSTimeSet()
    OS_ENTER_CRITICAL();                                   /* Update the 32-bit tick counter           */
    OSTime++;//系统节拍现阶段值,计算自系统上电以来的时钟节拍数
    OS_EXIT_CRITICAL();
#endif
    if (OSRunning == TRUE) {    
        ptcb = OSTCBList;                                  /* Point at first TCB in TCB list           */
		//指向PCB双向链表中的第一个
        while (ptcb->OSTCBPrio != OS_IDLE_PRIO) {          /* Go through all TCBs in TCB list          */
			//将PCB链表中的TCB遍历一遍,一直做到空闲任务
            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) == OS_STAT_RDY) { /* Is task suspended?    */
						//确实被挂起的任务不会进入就绪态
                        OSRdyGrp               |= ptcb->OSTCBBitY; /* No,  Make task R-to-R (timed out)*/
			//如果某任务的TCB中的时间延时项OSTCBDly减为0时,这个任务就进入了就绪态
                        OSRdyTbl[ptcb->OSTCBY] |= ptcb->OSTCBBitX;
                    } else {                               /* Yes, Leave 1 tick to prevent ...         */
   //保留一个节拍防止去除挂起的时候任务不稳定
                        ptcb->OSTCBDly = 1;                /* ... loosing the task when the ...        */
                    }                                      /* ... suspension is removed.               */
                }
            }
            ptcb = ptcb->OSTCBNext;                        /* Point at next TCB in TCB list            */
			//指向TCB链表的下一块
            OS_EXIT_CRITICAL();
        }
    }
}
/*$PAGE*/
/*
*********************************************************************************************************
*                                             GET VERSION
*
* Description: This function is used to return the version number of uC/OS-II.  The returned value
*              corresponds to uC/OS-II's version number multiplied by 100.  In other words, version 2.00
*              would be returned as 200.
*
* Arguments  : none
*
* Returns    : the version number of uC/OS-II multiplied by 100.
                                                  版本号
描述:返回UCOS版本号号,返回值为UCOS版本*100,换句话说,2.00版本将
                 返回200
 参数:无
 返回:版本号*100
*********************************************************************************************************
*/

INT16U  OSVersion (void)
{
    return (OS_VERSION);
}

/*$PAGE*/
/*
*********************************************************************************************************
*                                            DUMMY FUNCTION
*
* Description: This function doesn't do anything.  It is called by OSTaskDel().

⌨️ 快捷键说明

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