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

📄 os-task-switch.c

📁 ucos-ii-at90S-icc-avr
💻 C
📖 第 1 页 / 共 2 页
字号:
//os主要代码
#define  OS_GLOBALS                           /* Declare GLOBAL variables                              */
#include "includes.h"

#define  OS_MASTER_FILE                       /* Prevent the following files from including includes.h */

void        OSInit(void)
{
  INT8U i;

  OSIntNesting=0;             /* Interrupt nesting level                              */
  OSLockNesting=0;            /* Multitasking lock nesting level                      */
  OSPrioCur=0xff;                /* Priority of current task                             */
  OSPrioHighRdy=0xff;            /* Priority of highest priority task                    */
  OSRunning = FALSE;

  OSTCBCur=0;                        /* Pointer to currently running TCB              */
  OSTCBHighRdy=0;                    /* Pointer to highest priority TCB ready to run  */
/*init task tcb pointers list*/
  for(i=0;i<OS_TASK_COUNT;i++)
    OSpTCBList[i]=(OS_TCB*)0;
}
/*
OS_STK *	OSTaskStkInit (void (*task)(void *pd),void *pdata, OS_STK *ptos);
INT8U       OSTCBInit(OS_TCB *ptcb,INT8U prio, OS_STK *ptos,INT16U stk_size);
*/

INT8U       OSTaskCreate(OS_TCB *ptcb,void (*task)(void *pd), void *pdata, OS_STK *ptos,INT16U stk_size, INT8U prio)
{
    void   *psp;
    INT8U   err;

    if (prio > OS_LOWEST_PRIO) {             /* Make sure priority is within allowable range           */
        return (OSERR_PRIO_INVALID);
    }
    OS_ENTER_CRITICAL();
    if (OSpTCBList[prio] == (OS_TCB *)0) { /* Make sure task doesn't already exist at this priority  */
        OSpTCBList[prio] = ptcb;    /* Reserve the priority to prevent others from doing ...  */
        OS_EXIT_CRITICAL();
        psp = (void *)OSTaskStkInit(task, pdata, ptos, stk_size); /* Initialize the task's stack              */
        err = OSTCBInit(ptcb,prio, psp,stk_size);         
        if (err == OS_NO_ERR) {
            if (OSRunning) {                 /* Find highest priority task if multitasking has started */
                OSSched();
            }
        } else {
        }
        return (err);
    } else {
        OS_EXIT_CRITICAL();
        return (OSERR_PRIO_EXIST);
    }
}

/*
*********************************************************************************************************
*                                            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 (OS_TCB *ptcb,INT8U prio, OS_STK *ptos, INT16U stk_size)
{
#if OS_STRICT
  if((0==ptcb)||(prio>OS_LOWEST_PRIO)||((INT16U)ptos<OS_HARDWARE_STACK_SIZE+40)||(stk_size<OS_HARDWARE_STACK_SIZE+40))
    OSError(0);
#endif
    OS_ENTER_CRITICAL();
    if ((OS_TCB *)0!=ptcb) {
        OS_EXIT_CRITICAL();
        ptcb->OSTCBStkPtr    = ptos;                       /* Load Stack pointer in TCB                */
        ptcb->OSTCBPrio      = (INT8U)prio;                /* Load task priority into TCB              */
        ptcb->OSTCBStat      = OS_STAT_RDY;                /* Task is ready to run                     */
        ptcb->OSTCBDly       = 0;                          /* Task is not delayed                      */

        return (OS_NO_ERR);
    } else {
        OS_EXIT_CRITICAL();
        return (OSERR_TCB_INVALID);
    }
}

/*
*********************************************************************************************************
*                                              SCHEDULER
*
* Description: This function is called by other uC/OS-II services to determine whether a new, high
*              priority task has been made ready to run.  This function is invoked by TASK level code
*              and is not used to reschedule tasks from ISRs (see OSIntExit() for ISR rescheduling).
*
* Arguments  : none
*
* Returns    : none
*
* Notes      : 1) This function is INTERNAL to uC/OS-II and your application should not call it.
*              2) Rescheduling is prevented when the scheduler is locked (see OSSchedLock())
*********************************************************************************************************
*/

void OSSched (void)
{
    OS_ENTER_CRITICAL();
    if ((OSLockNesting | OSIntNesting) == 0) {   /* Task scheduling must be enabled and not ISR level  */
        OSPrioHighRdy=OSGetPrioRdy();
		if(OSPrioHighRdy>OS_LOWEST_PRIO)
		{
#if OS_STRICT
		  OSError(0);
#endif
		}else if (OSPrioHighRdy != OSPrioCur) {         /* No context switch if current task is highest ready */
            OSTCBHighRdy = OSpTCBList[OSPrioHighRdy];
#if OS_STRICT
		  	if(0==OSTCBHighRdy)
			  OSError(0);
#endif			
			OS_TASK_SW();                        /* Perform a context switch                           */
        }//end OSPrioHighRdy != OSPrioCur
    }//end nesting==0
    OS_EXIT_CRITICAL();
}

/*
*********************************************************************************************************
*                                          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.
*********************************************************************************************************
*/

void OSStart (void)
{
    if (OSRunning == FALSE) {
        OSPrioHighRdy = 0;
        OSPrioCur     = 0;
        OSTCBHighRdy  = OSpTCBList[OSPrioHighRdy]; /* Point to highest priority task ready to run    */
        OSTCBCur      = OSTCBHighRdy;
#if OS_STRICT
		if(0==OSTCBCur) 
		  OSError(0);
#endif		
        OSStartHighRdy();                            /* Execute target specific code to start task     */
    }
#if OS_STRICT
	else OSError(0);
#endif
}

/*
*********************************************************************************************************
*                                DELAY TASK 'n' TICKS   (n from 0 to 65535)
*
* Description: This function is called to delay execution of the currently running task until the 
*              specified number of system ticks expires.  This, of course, directly equates to delaying
*              the current task for some time to expire.  No delay will result If the specified delay is 
*              0.  If the specified delay is greater than 0 then, a context switch will result.
*
* Arguments  : ticks     is the time delay that the task will be suspended in number of clock 'ticks'.  
*                        Note that by specifying 0, the task will not be delayed.
*
* Returns    : none
*********************************************************************************************************
*/

void OSTimeDly (INT16U ticks)
{
    if (ticks > 0) {                                                      /* 0 means no delay!         */
        OS_ENTER_CRITICAL();
        OSTCBCur->OSTCBDly = ticks;                                       /* Load ticks in TCB         */
        OS_EXIT_CRITICAL();
        OSSched();                                                        /* Find next task to run!    */
    }
}
/*return tcb's prio which OSTCBDly==0*/
INT8U OSGetPrioRdy(void)
{
  INT8U i,ret=0xff;
  OS_TCB *ptcb;
  for(i=0;i<OS_TASK_COUNT;i++)
  {
    ptcb=OSpTCBList[i];
    //OS_ENTER_CRITICAL();
    if(ptcb)
	{
	  if(0==ptcb->OSTCBDly)
	  {
	  if(OS_STAT_RDY==ptcb->OSTCBStat)
	  {
#if OS_STRICT
	    if(i!=ptcb->OSTCBPrio)
		  OSError(0);
#endif  
        ret=ptcb->OSTCBPrio;     
#if OS_STRICT
		if(ret>OS_LOWEST_PRIO)
		  OSError(0);
#endif  		
	    break;//for
	  }//end OS_STAT_RDY==ptcb->OSTCBStat
#if OS_SEM_EN
	  else{
		  if(ptcb->OSTCBStat & OS_STAT_SEM)
		  {
#if OS_STRICT
			  if(i!=ptcb->OSTCBPrio)
				  OSError(0);
#endif  
			  ret=ptcb->OSTCBPrio;     
#if OS_STRICT
			  if(ret>OS_LOWEST_PRIO)
				  OSError(0);
#endif  		
	    break;//for
		  }
	  }//end (ptcb->OSTCBStat & OS_STAT_SEM)
#endif//OS_SEM_EN
	  }//end (0==ptcb->OSTCBDly)
	}
    //OS_EXIT_CRITICAL();
  }
  return ret;
}

/*
*********************************************************************************************************
*                                         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
*********************************************************************************************************
*/

void OSTimeTick (void)
{
    INT8U i,ret;
    OS_TCB *ptcb;
	//skip last task,last task must be idle task
	for(i=0;i<OS_TASK_COUNT-1;i++)
	{
	ptcb=OSpTCBList[i];
//    OS_ENTER_CRITICAL();
    //check ptcb != 0
	if(ptcb){              /* Go through all TCBs in TCB list          */
        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.           */
			}//end (--ptcb->OSTCBDly==0)         
			}//end OSTCBDly!=0
        }//end if(ptcb)
  //  OS_EXIT_CRITICAL();
    }//end for
}

/*
*********************************************************************************************************
*                                               EXIT ISR
*
* Description: This function is used to notify uC/OS-II that you have completed serviving an ISR.  When 
*              the last nested ISR has completed, uC/OS-II will call the scheduler to determine whether
*              a new, high-priority task, is ready to run.
*
* Arguments  : none
*
* Returns    : none
*
* Notes      : 1) You MUST invoke OSIntEnter() and OSIntExit() in pair.  In other words, for every call
*                 to OSIntEnter() at the beginning of the ISR you MUST have a call to OSIntExit() at the
*                 end of the ISR.
*              2) Rescheduling is prevented when the scheduler is locked (see OSSchedLock())
*********************************************************************************************************
*/

void OSIntExit(void)

⌨️ 快捷键说明

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