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

📄 os_core.c

📁 基于51单片机来实现UCOS用一个串口来看到实现阶段
💻 C
📖 第 1 页 / 共 2 页
字号:

//任务调度
void OSSched (void)reentrant
{
    INT8U y;


    OS_ENTER_CRITICAL();
    if ((OSLockNesting | OSIntNesting) == 0) {   /* Task scheduling must be enabled and not ISR level  */
        y             = OSUnMapTbl[OSRdyGrp];    /* Get pointer to highest priority task ready to run  */
        OSPrioHighRdy = (INT8U)((y << 3) + OSUnMapTbl[OSRdyTbl[y]]);
        if (OSPrioHighRdy != OSPrioCur) {         /* No context switch if current task is highest ready */
            OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy];
            OSCtxSwCtr++;                        /* Increment context switch counter                   */
            OSCtxSw();                        /* Perform a context switch                           */
        }
    }
    OS_EXIT_CRITICAL();
}

//任务调度锁
#if  OSSCHED_LOCK_EN
void OSSchedLock (void)reentrant
{
    if (OSRunning == TRUE) {                     /* Make sure multitasking is running                  */
        OS_ENTER_CRITICAL();
        OSLockNesting++;                         /* Increment lock nesting level                       */
        OS_EXIT_CRITICAL();
    }
}

//任务调度开锁
void OSSchedUnlock (void)reentrant
{
    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 | OSIntNesting) == 0) { /* See if scheduling re-enabled and not an ISR  */
                OS_EXIT_CRITICAL();
                OSSched();                             /* See if a higher priority task is ready       */
            } else {
                OS_EXIT_CRITICAL();
            }
        } else {
            OS_EXIT_CRITICAL();
        }
    }
}
#endif

//系统开始运行
void OSStart (void)
{
    INT8U y;
    INT8U x;

    if (OSRunning == FALSE) {
        y             = OSUnMapTbl[OSRdyGrp];        /* Find highest priority's task priority number   */
        x             = OSUnMapTbl[OSRdyTbl[y]];
        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     */
    }
}

//空闲任务例程
void OSTaskIdle (void *dataptr)reentrant
{ 
    dataptr = dataptr; 
	while(1){
	    OS_ENTER_CRITICAL();        
	    OSIdleCtr++;
	    OS_EXIT_CRITICAL();
	}
}
#if OS_TASK_STAT_EN
//统计任务初始化
void OSStatInit (void)reentrant
{
    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();
}

//统计任务例程
void OSTaskStat(void *dataptr)reentrant
{
    INT32U run;
    INT8S  usage;

	OSStatInit();
    dataptr = dataptr;                               /* Prevent compiler warning for not using 'dataptr'     */
    while (OSStatRdy == FALSE) {
        OSTimeDly(2 * OS_TICKS_PER_SEC);             /* Wait until statistic task is ready           */
    }
    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

//任务控制块初始化
static OS_TCB *ptcb11;
INT8U OSTCBInit (INT8U prio, 
				 OS_STKCB *stkcb,
		         INT16U id, 
		         void *pext, 
		         INT16U opt)reentrant
{
    OS_ENTER_CRITICAL();
    ptcb11 = OSTCBFreeList;                                 
    if (ptcb11 != (OS_TCB *)0) {
        OSTCBFreeList        = ptcb11->OSTCBNext;            
        OS_EXIT_CRITICAL();
		ptcb11->OSTCBStkCb     = stkcb;
        ptcb11->OSTCBStkPtr    = stkcb->OSTKBtomPtr ;                      
        ptcb11->OSTCBPrio      = (INT8U)prio;               
        ptcb11->OSTCBStat      = OS_STAT_RDY;               
        ptcb11->OSTCBDly       = 0;                        

#if OS_TASK_CREATE_EXT_EN        
        ptcb11->OSTCBExtPtr    = pext;                      
        ptcb11->OSTCBStkSize   = OS_STK_SIZE;               
        ptcb11->OSTCBStkBottom = stkcb->OSTKBtomPtr;                       
        ptcb11->OSTCBOpt       = opt;                    
        ptcb11->OSTCBId        = id;                         
#else
        pext                 = pext;                      
        stk_size             = OS_STK_SIZE;
        opt                  = opt;
        id                   = id;
#endif

#if OS_TASK_DEL_EN        
        ptcb11->OSTCBDelReq    = OS_NO_ERR;
#endif

        ptcb11->OSTCBY         = prio >> 3;                  /* Pre-compute X, Y, BitX and BitY          */
        ptcb11->OSTCBBitY      = OSMapTbl[ptcb11->OSTCBY];
        ptcb11->OSTCBX         = prio & 0x07;
        ptcb11->OSTCBBitX      = OSMapTbl[ptcb11->OSTCBX];

#if     OS_MBOX_EN || (OS_Q_EN && (OS_MAX_QS >= 2)) || OS_Sem_EN
        ptcb11->OSTCBEventPtr  = (OS_EVENT *)0;              /* Task is not pending on an event          */
#endif

#if     OS_MBOX_EN || (OS_Q_EN && (OS_MAX_QS >= 2))
        ptcb11->OSTCBMsg       = (void *)0;                  /* No message received                      */
#endif

        OS_ENTER_CRITICAL();
        OSTCBPrioTbl[prio]   = ptcb11;
        ptcb11->OSTCBNext      = OSTCBList;                  /* Link into TCB chain                      */
        ptcb11->OSTCBPrev      = (OS_TCB *)0;
        if (OSTCBList != (OS_TCB *)0) {
            OSTCBList->OSTCBPrev = ptcb11;
        }
        OSTCBList               = ptcb11;
        OSRdyGrp               |= ptcb11->OSTCBBitY;         /* Make task ready to run                   */
        OSRdyTbl[ptcb11->OSTCBY] |= ptcb11->OSTCBBitX;
        OS_EXIT_CRITICAL();
        return (OS_NO_ERR);
    } else {
        OS_EXIT_CRITICAL();
        return (OS_NO_MORE_TCB);
    }
}

//要求分配堆栈控制块
OS_STKCB *OStkAsk(void)reentrant{
		    OS_STKCB * stkcb;
			if(OSTKCBFreeList==0){
			  return(OS_NO_MORE_OSTK);
			}else{
	         stkcb=OSTKCBFreeList;
			 OSTKCBFreeList=OSTKCBFreeList->OSTKCBNext;
			return(stkcb);
	        }  
}

//时间片中断处理
void OSTimeTick (void)
{
    OS_TCB *ptcb;

    OSTimeTickHook();                                      /* Call user definable hook                 */
    ptcb = OSTCBList;                                      /* Point at first TCB in TCB list           */
    while (ptcb->OSTCBPrio != OS_IDLE_PRIO) {              /* Go through all TCBs in TCB list          */
        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)) {    /* Is task suspended?                   */
                    OSRdyGrp               |= ptcb->OSTCBBitY; /* No,  Make task Rdy to Run (timed out)*/
                    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            */
        OS_EXIT_CRITICAL();
    }
    OS_ENTER_CRITICAL();                                   /* Update the 32-bit tick counter           */
    OSTime++;
    OS_EXIT_CRITICAL();
}

//任务版本查询
#if OS_VERSION_CHK_EN
INT16U OSVersion (void)reentrant
{
    return (OS_VERSION);
}
#endif

⌨️ 快捷键说明

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