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

📄 os-task-switch.c

📁 ucos-ii-at90S-icc-avr
💻 C
📖 第 1 页 / 共 2 页
字号:
{
    OS_ENTER_CRITICAL();
    if ((--OSIntNesting | OSLockNesting) == 0) { /* Reschedule only if all ISRs completed & not locked */
        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)||(0==OSTCBCur)||
			   (OSPrioCur>OS_LOWEST_PRIO)||(OSPrioHighRdy>OS_LOWEST_PRIO))
			  OSError(0);
#endif			
            OSDebug();
            OSIntCtxSw();                        /* Perform interrupt level context switch             */
        }//end OSPrioHighRdy != OSPrioCur
    }//end nesting==0
    OS_EXIT_CRITICAL();
}

void OSDebug(void)
{
INT8U port_a;
            //debug
			DDRA=0;port_a=PORTA;
			DDRA=0xff;PORTA=~port_a;
			//end debug
}

/*
*********************************************************************************************************
*                             MAKE TASK READY TO RUN BASED ON EVENT OCCURING
*
* Description: This function is called by other uC/OS-II services and is used to ready a task that was
*              waiting for an event to occur.
*
* Arguments  : pevent    is a pointer to the event control block corresponding to the event.
*
*              msg       is a pointer to a message.  This pointer is used by message oriented services
*                        such as MAILBOXEs and QUEUEs.  The pointer is not used when called by other
*                        service functions.
*
*              msk       is a mask that is used to clear the status byte of the TCB.  For example,
*                        OSSemPost() will pass OS_STAT_SEM, OSMboxPost() will pass OS_STAT_MBOX etc.
*
* Returns    : none
*
* Note       : This function is INTERNAL to uC/OS-II and your application should not call it.
*********************************************************************************************************
*/
#if OS_EVENT_EN

/*init event structure*/
void  OSEventInit(OS_EVENT *pevent)
{
	INT8U i;
	if(0==pevent)
		return;
    for(i=0;i<OS_TASK_COUNT;i++)
		pevent->OSTaskTbl[i]=0;
    pevent->OSEventCnt=0;
    pevent->OSEventType=OS_EVENT_TYPE_INVALID;                   /* OS_EVENT_TYPE_MBOX, OS_EVENT_TYPE_Q or OS_EVENT_TYPE_SEM */
}

/*get highest prio from event's waiting task list,
  ignore OSTCBStat and OSTCBDly.
  Find first tcb in pevent->OSTaskTbl which is not null*/
INT8U OSEventGetHighPrioRdy(OS_EVENT *pevent)
{
	INT8U i;
	OS_TCB *ptcb;
	if(0==pevent)
		return OS_TASK_COUNT;//error
    for(i=0;i<OS_TASK_COUNT;i++)
	{
		ptcb=pevent->OSTaskTbl[i];
		if(ptcb)
		{
			return ptcb->OSTCBPrio;
		}
	}
	return OS_TASK_COUNT;//error
}

/*when OSSemPend success,call OSEventTaskRdy to remove task from event waitting task list*/
void  OSEventTaskRdy (OS_EVENT *pevent, INT8U msk)
{
    OS_TCB *ptcb;
    INT8U   prio;

	prio=OSEventGetHighPrioRdy(pevent);
#if OS_STRICT
	if(prio>OS_LOWEST_PRIO)
		OSError(0);
#endif
    ptcb                 =  OSpTCBList[prio];       /* Point to this task's OS_TCB                   */
#if OS_STRICT
	if(0==ptcb)
		OSError(0);
#endif
	//clear event flags
    ptcb->OSTCBDly       =  0;                        /* Prevent OSTimeTick() from readying task       */
    ptcb->OSTCBStat     &= ~msk;                      /* Clear bit associated with event type          */
}


/*
*********************************************************************************************************
*                                   MAKE TASK WAIT FOR EVENT TO OCCUR
*
* Description: This function is called by other uC/OS-II services to suspend a task because an event has
*              not occurred.
*
* Arguments  : pevent   is a pointer to the event control block for which the task will be waiting for.
*
* Returns    : none
*
* Note       : This function is INTERNAL to uC/OS-II and your application should not call it.
*********************************************************************************************************
*/
void  OSEventTaskWait (OS_EVENT *pevent)
{
#if OS_STRICT
	if(!OSTCBCur)
		OSError(0);
#endif
}
/*
*********************************************************************************************************
*                              MAKE TASK READY TO RUN BASED ON EVENT TIMEOUT
*
* Description: This function is called by other uC/OS-II services to make a task ready to run because a
*              timeout occurred.
*
* Arguments  : pevent   is a pointer to the event control block which is readying a task.
*
* Returns    : none
*
* Note       : This function is INTERNAL to uC/OS-II and your application should not call it.
*********************************************************************************************************
*/
void  OSEventTO (OS_EVENT *pevent)
{
    OSTCBCur->OSTCBStat     = OS_STAT_RDY;       /* Set status to ready                                */
}
#endif//OS_EVENT_EN

#if OS_SEM_EN
/*
*********************************************************************************************************
*                                           ACCEPT SEMAPHORE
*
* Description: This function checks the semaphore to see if a resource is available or, if an event
*              occurred.  Unlike OSSemPend(), OSSemAccept() does not suspend the calling task if the
*              resource is not available or the event did not occur.
*
* Arguments  : pevent     is a pointer to the event control block
*
* Returns    : >  0       if the resource is available or the event did not occur the semaphore is
*                         decremented to obtain the resource.
*              == 0       if the resource is not available or the event did not occur or,
*                         you didn't pass a pointer to a semaphore
*********************************************************************************************************
*/
INT16U OSSemAccept (OS_EVENT *pevent)
{
    INT16U cnt;

    OS_ENTER_CRITICAL();
    if (pevent->OSEventType != OS_EVENT_TYPE_SEM) {   /* Validate event block type                     */
        OS_EXIT_CRITICAL();
        return (0);
    }
    cnt = pevent->OSEventCnt;
    if (cnt > 0) {                                    /* See if resource is available                  */
        pevent->OSEventCnt--;                         /* Yes, decrement semaphore and notify caller    */
    }
    OS_EXIT_CRITICAL();
    return (cnt);                                     /* Return semaphore count                        */
}

/*
*********************************************************************************************************
*                                           CREATE A SEMAPHORE
*
* Description: This function creates a semaphore.
*
* Arguments  : cnt           is the initial value for the semaphore.  If the value is 0, no resource is
*                            available (or no event has occurred).  You initialize the semaphore to a 
*                            non-zero value to specify how many resources are available (e.g. if you have
*                            10 resources, you would initialize the semaphore to 10).
*
* Returns    : != (void *)0  is a pointer to the event control clock (OS_EVENT) associated with the
*                            created semaphore
*              == (void *)0  if no event control blocks were available
*********************************************************************************************************
*/

OS_EVENT *OSSemCreate (OS_EVENT *pevent,INT16U cnt)
{
    if (pevent != (OS_EVENT *)0) {                         /* Get an event control block               */
        OSEventInit(pevent);
		pevent->OSEventType = OS_EVENT_TYPE_SEM;
        pevent->OSEventCnt  = cnt;                         /* Set semaphore value                      */
    }
    return (pevent);
}
//called by OSSemPend to remove current tcb from event's waitting task list
void OSEventRemoveCurrentTCB(OS_EVENT *pevent)
{
#if OS_STRICT
	if((0==pevent)||(OSPrioCur>OS_LOWEST_PRIO)||(OSTCBCur==0))
		OSError(0);
	if(pevent->OSTaskTbl[OSPrioCur]==0)
		OSError(0);
#endif
	pevent->OSTaskTbl[OSPrioCur]=0;
}

/*
*********************************************************************************************************
*                                           PEND ON SEMAPHORE
*
* Description: This function waits for a semaphore.
*
* Arguments  : pevent        is a pointer to the event control block associated with the desired 
*                            semaphore.
*
*              timeout       is an optional timeout period (in clock ticks).  If non-zero, your task will
*                            wait for the resource up to the amount of time specified by this argument.  
*                            If you specify 0, however, your task will wait forever at the specified 
*                            semaphore or, until the resource becomes available (or the event occurs).
*
*              err           is a pointer to where an error message will be deposited.  Possible error
*                            messages are:
*
*                            OS_NO_ERR          The call was successful and your task owns the resource 
*                                               or, the event you are waiting for occurred.
*                            OS_TIMEOUT         The semaphore was not received within the specified 
*                                               timeout.
*                            OS_ERR_EVENT_TYPE  If you didn't pass a pointer to a semaphore.
*                            OS_ERR_PEND_ISR    If you called this function from an ISR and the result
*                                               would lead to a suspension.
*
* Returns    : none
*********************************************************************************************************
*/

void OSSemPend (OS_EVENT *pevent, INT16U timeout, INT8U *err)
{
    OS_ENTER_CRITICAL();
    if (pevent->OSEventType != OS_EVENT_TYPE_SEM) {   /* Validate event block type                     */
        OS_EXIT_CRITICAL();
        *err = OSERR_EVENT_TYPE;
    }
    if (pevent->OSEventCnt > 0) {                     /* If sem. is positive, resource available ...   */
        pevent->OSEventCnt--;                         /* ... decrement semaphore only if positive.     */
        OS_EXIT_CRITICAL();
        *err = OS_NO_ERR;
    } else if (OSIntNesting > 0) {                    /* See if called from ISR ...                    */
        OS_EXIT_CRITICAL();                           /* ... can't PEND from an ISR                    */
        *err = OSERR_PEND_ISR;
    } else {                                          /* Otherwise, must wait until event occurs       */
        OSTCBCur->OSTCBStat    |= OS_STAT_SEM;        /* Resource not available, pend on semaphore     */
        OSTCBCur->OSTCBDly      = timeout;            /* Store pend timeout in TCB                     */
        OSEventTaskWait(pevent);                      /* Suspend task until event or timeout occurs    */
        OS_EXIT_CRITICAL();
        OSSched();                                    /* Find next highest priority task ready         */
        OS_ENTER_CRITICAL();
        OSEventRemoveCurrentTCB(pevent);
        if (OSTCBCur->OSTCBStat & OS_STAT_SEM) {      /* Must have timed out if still waiting for event*/
            OSEventTO(pevent);
            OS_EXIT_CRITICAL();
            *err = OSERR_TIMEOUT;                        /* Indicate that didn't get event within TO      */
        } else {
            OS_EXIT_CRITICAL();
            *err = OS_NO_ERR;
        }
    }
}

/*
*********************************************************************************************************
*                                         POST TO A SEMAPHORE
*
* Description: This function signals a semaphore
*
* Arguments  : pevent        is a pointer to the event control block associated with the desired 
*                            semaphore.
*
* Returns    : OS_NO_ERR          The call was successful and the semaphore was signaled.
*              OS_SEM_OVF         If the semaphore count exceeded its limit.  In other words, you have 
*                                 signalled the semaphore more often than you waited on it with either
*                                 OSSemAccept() or OSSemPend().
*              OS_ERR_EVENT_TYPE  If you didn't pass a pointer to a semaphore
*********************************************************************************************************
*/

INT8U OSSemPost (OS_EVENT *pevent)
{
	INT8U prio;
    OS_ENTER_CRITICAL();
    if (pevent->OSEventType != OS_EVENT_TYPE_SEM) {        /* Validate event block type                */
        OS_EXIT_CRITICAL();
        return (OSERR_EVENT_TYPE);
    }
    prio=OSEventGetHighPrioRdy(pevent);
    if (prio<=OS_LOWEST_PRIO) {                              /* See if any task waiting for semaphore    */
        OSEventTaskRdy(pevent,OS_STAT_SEM);    /* Ready highest prio task waiting on event */
        OS_EXIT_CRITICAL();
        OSSched();                                    /* Find highest priority task ready to run       */
        return (OS_NO_ERR);
    } else {
        if (pevent->OSEventCnt < 65535) {             /* Make sure semaphore will not overflow         */
            pevent->OSEventCnt++;                     /* Increment semaphore count to register event   */
            OS_EXIT_CRITICAL();
            return (OS_NO_ERR);
        } else {                                      /* Semaphore value has reached its maximum       */
            OS_EXIT_CRITICAL();
            return (OSERR_SEM_OVF);
        }
    }
}

#endif//OS_SEM_EN

⌨️ 快捷键说明

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