ucos ii 源代码中文注释详解: OS_FLAG.c



*********************************************************************************************************

*                                                uC/OS-II

*                                          The Real-Time Kernel

*                                         EVENT FLAG  MANAGEMENT

*

*                            (c) Copyright 2001, Jean J. Labrosse, Weston, FL

*                                           All Rights Reserved

*

* File : OS_FLAG.C

* By   : Jean J. Labrosse

*********************************************************************************************************

*/

 

#ifndef  OS_MASTER_FILE

#include "INCLUDES.H"

#endif

 

#if (OS_VERSION >= 251) && (OS_FLAG_EN > 0) && (OS_MAX_FLAGS > 0)

/*

*********************************************************************************************************

*                                            LOCAL PROTOTYPES

*********************************************************************************************************

*/

 

static  void     OS_FlagBlock(OS_FLAG_GRP *pgrp, OS_FLAG_NODE *pnode, OS_FLAGS flags, INT8U wait_type, INT16U timeout);

static  BOOLEAN  OS_FlagTaskRdy(OS_FLAG_NODE *pnode, OS_FLAGS flags_rdy);

 

/*$PAGE*/

/*

*********************************************************************************************************

*                              CHECK THE STATUS OF FLAGS IN AN EVENT FLAG GROUP

*

* Description: This function is called to check the status of a combination of bits to be set or cleared

*              in an event flag group.  Your application can check for ANY bit to be set/cleared or ALL 

*              bits to be set/cleared.

*

*              This call does not block if the desired flags are not present.

*

* Arguments  : pgrp          is a pointer to the desired event flag group.

*

*              flags         Is a bit pattern indicating which bit(s) (i.e. flags) you wish to check.  

*                            The bits you want are specified by setting the corresponding bits in 

*                            'flags'.  e.g. if your application wants to wait for bits 0 and 1 then 

*                            'flags' would contain 0x03.

*

*              wait_type     specifies whether you want ALL bits to be set/cleared or ANY of the bits 

*                            to be set/cleared.

*                            You can specify the following argument:

*

*                            OS_FLAG_WAIT_CLR_ALL   You will check ALL bits in 'flags' to be clear (0)

*                            OS_FLAG_WAIT_CLR_ANY   You will check ANY bit  in 'flags' to be clear (0)

*                            OS_FLAG_WAIT_SET_ALL   You will check ALL bits in 'flags' to be set   (1)

*                            OS_FLAG_WAIT_SET_ANY   You will check ANY bit  in 'flags' to be set   (1)

*

*                            NOTE: Add OS_FLAG_CONSUME if you want the event flag to be 'consumed' by

*                                  the call.  Example, to wait for any flag in a group AND then clear

*                                  the flags that are present, set 'wait_type' to:

*

*                                  OS_FLAG_WAIT_SET_ANY + OS_FLAG_CONSUME

*

*              err           is a pointer to an error code and can be:

*                            OS_NO_ERR              No error

*                            OS_ERR_EVENT_TYPE      You are not pointing to an event flag group

*                            OS_FLAG_ERR_WAIT_TYPE  You didn't specify a proper 'wait_type' argument.

*                            OS_FLAG_INVALID_PGRP   You passed a NULL pointer instead of the event flag

*                                                   group handle.

*                            OS_FLAG_ERR_NOT_RDY    The desired flags you are waiting for are not 

*                                                   available.

*

* Returns    : The state of the flags in the event flag group.

*

* Called from: Task or ISR

*********************************************************************************************************

*/

 

#if OS_FLAG_ACCEPT_EN > 0

//无等待地获得事件标志组中的事件标志

OS_FLAGS  OSFlagAccept (OS_FLAG_GRP *pgrp, OS_FLAGS flags, INT8U wait_type, INT8U *err)

{

#if OS_CRITICAL_METHOD == 3                                /* Allocate storage for CPU status register */

    OS_CPU_SR     cpu_sr;

#endif    

    OS_FLAGS      flags_cur;

    OS_FLAGS      flags_rdy;

    BOOLEAN       consume;

 

 

#if OS_ARG_CHK_EN > 0

    if (pgrp == (OS_FLAG_GRP *)0) {                        /* Validate 'pgrp'                          */

        *err = OS_FLAG_INVALID_PGRP;

        return ((OS_FLAGS)0);

		//如果事件组无效,发送错误码并返回0

    }

    if (pgrp->OSFlagType != OS_EVENT_TYPE_FLAG) {          /* Validate event block type                */

        *err = OS_ERR_EVENT_TYPE;

        return ((OS_FLAGS)0);

		//如果不是事件类型,发送错误码并返回0

    }

#endif

    if (wait_type & OS_FLAG_CONSUME) {                     /* See if we need to consume the flags      */

	//判断是否为恢复状态

        wait_type &= ~OS_FLAG_CONSUME;

		//wait_type清空为0

        consume    = TRUE;

		//恢复标志位置位

    } else {

        consume    = FALSE;

    }

/*$PAGE*/

    *err = OS_NO_ERR;                                      /* Assume NO error until proven otherwise.  */

	//返回错误码

    OS_ENTER_CRITICAL();

	//进入临界区

    switch (wait_type) {

		//判断wait_type状态

        case OS_FLAG_WAIT_SET_ALL:                         /* See if all required flags are set        */

		//所有的请求标志位都置位

             flags_rdy = pgrp->OSFlagFlags & flags;        /* Extract only the bits we want            */

			 //提取出我们需要的那一位

             if (flags_rdy == flags) {                     /* Must match ALL the bits that we want     */

			 //需要所有的位都匹配

                 if (consume == TRUE) {                    /* See if we need to consume the flags      

				 //如果是恢复状态

                     pgrp->OSFlagFlags &= ~flags_rdy;      /* Clear ONLY the flags that we wanted      */

					 //清除我们提取的那位

                 }

             } else {

                 *err  = OS_FLAG_ERR_NOT_RDY;

				 //返回错误码

             }

             flags_cur = pgrp->OSFlagFlags;                /* Will return the state of the group       */

			 //返回组的状态

             OS_EXIT_CRITICAL();

			 //退出临界区

             break;

 

        case OS_FLAG_WAIT_SET_ANY:

		//置其中任意一位

             flags_rdy = pgrp->OSFlagFlags & flags;        /* Extract only the bits we want            */

			 //提取我们需要的那一位

             if (flags_rdy != (OS_FLAGS)0) {               /* See if any flag set                      */

			 //判断是否有位置位

                 if (consume == TRUE) {                    /* See if we need to consume the flags      */

				 //判断是否需要恢复状态

                     pgrp->OSFlagFlags &= ~flags_rdy;      /* Clear ONLY the flags that we got         */

					 //清除我们获得那一位

                 }

             } else {

                 *err  = OS_FLAG_ERR_NOT_RDY;

				 //返回错误码

             }

             flags_cur = pgrp->OSFlagFlags;                /* Will return the state of the group       */

			 //返回错误码

             OS_EXIT_CRITICAL();

			 //退出临界区

             break;

 

#if OS_FLAG_WAIT_CLR_EN > 0

        case OS_FLAG_WAIT_CLR_ALL: 

		//清除所有位。

		/* See if all required flags are cleared    */

             flags_rdy = ~pgrp->OSFlagFlags & flags;       /* Extract only the bits we want            */

			//提取我们需要的那一位

             if (flags_rdy == flags) {                     /* Must match ALL the bits that we want     */

			 //需要匹配我们需要的每一位

                 if (consume == TRUE) {                    /* See if we need to consume the flags      */

				 //如果是恢复状态 

                     pgrp->OSFlagFlags |= flags_rdy;       /* Set ONLY the flags that we wanted        */

					 //把我们需要的每一位都置位

                 }

             } else {

                 *err  = OS_FLAG_ERR_NOT_RDY;

				 //返货错误码

             }

             flags_cur = pgrp->OSFlagFlags;                /* Will return the state of the group       */

			 //返回错误码

             OS_EXIT_CRITICAL();

			 //退出临界区

             break;

 

        case OS_FLAG_WAIT_CLR_ANY:

		//清除其中任意一位,和上面的分析类似。

           flags_rdy = ~pgrp->OSFlagFlags & flags;       /* Extract only the bits we want            */

		   

             if (flags_rdy != (OS_FLAGS)0) {               /* See if any flag cleared                  */

                 if (consume == TRUE) {                    /* See if we need to consume the flags      */

                     pgrp->OSFlagFlags |= flags_rdy;       /* Set ONLY the flags that we got           */

                 }

             } else {

                 *err  = OS_FLAG_ERR_NOT_RDY;

             }

             flags_cur = pgrp->OSFlagFlags;                /* Will return the state of the group       */

             OS_EXIT_CRITICAL();

             break;

#endif

 

        default:

             OS_EXIT_CRITICAL();

             flags_cur = (OS_FLAGS)0;

             *err      = OS_FLAG_ERR_WAIT_TYPE;

             break;

    }

    return (flags_cur);

	、、   //返回flags_cur

    

	}

#endif    

 

/*$PAGE*/

/*

*********************************************************************************************************

*                                           CREATE AN EVENT FLAG

*

* Description: This function is called to create an event flag group.

*

* Arguments  : flags         Contains the initial value to store in the event flag group.

*

*              err           is a pointer to an error code which will be returned to your application:

*                               OS_NO_ERR                if the call was successful.

*                               OS_ERR_CREATE_ISR        if you attempted to create an Event Flag from an 

*                                                        ISR.

*                               OS_FLAG_GRP_DEPLETED     if there are no more event flag groups

*

* Returns    : A pointer to an event flag group or a NULL pointer if no more groups are available.

*

* Called from: Task ONLY

*********************************************************************************************************

*/

//事件标志组事件标志组创建函数

OS_FLAG_GRP  *OSFlagCreate (OS_FLAGS flags, INT8U *err)

{

#if OS_CRITICAL_METHOD == 3                         /* Allocate storage for CPU status register        */

    OS_CPU_SR    cpu_sr;

#endif    

    OS_FLAG_GRP *pgrp;

 

 

    if (OSIntNesting > 0) {                         /* See if called from ISR ...                      */

        *err = OS_ERR_CREATE_ISR;                   /* ... can't CREATE from an ISR                    */

        return ((OS_FLAG_GRP *)0);      

     //如果有中断发生,返回错误码		

    }

    OS_ENTER_CRITICAL();

	//进入临界区

    pgrp = OSFlagFreeList;                          /* Get next free event flag                        */

    //获取下一个事件标志

    if (pgrp != (OS_FLAG_GRP *)0) {                 /* See if we have event flag groups available      */

                                                    /* Adjust free list                                */

	  //判断组是否有效

        OSFlagFreeList       = (OS_FLAG_GRP *)OSFlagFreeList->OSFlagWaitList;

		//OSFlagFreeList赋值为OSFlagWaitList

        pgrp->OSFlagType     = OS_EVENT_TYPE_FLAG;  /* Set to event flag group type                    */

		     //类型为事件

        pgrp->OSFlagFlags    = flags;               /* Set to desired initial value                    */

		//初始化事件

        pgrp->OSFlagWaitList = (void *)0;           /* Clear list of tasks waiting on flags            */

		//OSFlagWaitList清空

        OS_EXIT_CRITICAL();

               //退出临界区

        *err                 = OS_NO_ERR;

		//退出临界区

    } else {

        OS_EXIT_CRITICAL();

        *err                 = OS_FLAG_GRP_DEPLETED;

    }

    return (pgrp);                                  /* Return pointer to event flag group              */

}

 

/*$PAGE*/

/*

*********************************************************************************************************

*                                     DELETE AN EVENT FLAG GROUP

*

* Description: This function deletes an event flag group and readies all tasks pending on the event flag

*              group.

*

* Arguments  : pgrp          is a pointer to the desired event flag group.

*

*              opt           determines delete options as follows:

*                            opt == OS_DEL_NO_PEND   Deletes the event flag group ONLY if no task pending

*                            opt == OS_DEL_ALWAYS    Deletes the event flag group even if tasks are 

*                                                    waiting.  In this case, all the tasks pending will be 

*                                                    readied.

*

*              err           is a pointer to an error code that can contain one of the following values:

*                            OS_NO_ERR               The call was successful and the event flag group was 

*                                                    deleted

*                            OS_ERR_DEL_ISR          If you attempted to delete the event flag group from 

*                                                    an ISR

*                            OS_FLAG_INVALID_PGRP    If 'pgrp' is a NULL pointer.

*                            OS_ERR_EVENT_TYPE       You are not pointing to an event flag group

*                            OS_ERR_EVENT_TYPE       If you didn't pass a pointer to an event flag group

*                            OS_ERR_INVALID_OPT      An invalid option was specified

*                            OS_ERR_TASK_WAITING     One or more tasks were waiting on the event flag 

*                                                    group.

*

* Returns    : pevent        upon error

*              (OS_EVENT *)0 if the semaphore was successfully deleted.

*

* Note(s)    : 1) This function must be used with care.  Tasks that would normally expect the presence of

*                 the event flag group MUST check the return code of OSFlagAccept() and OSFlagPend().

*              2) This call can potentially disable interrupts for a long time.  The interrupt disable

*                 time is directly proportional to the number of tasks waiting on the event flag group.

*********************************************************************************************************

*/

 

#if OS_FLAG_DEL_EN > 0

 

 

//删除事件标志组函数

OS_FLAG_GRP  *OSFlagDel (OS_FLAG_GRP *pgrp, INT8U opt, INT8U *err)

{

#if OS_CRITICAL_METHOD == 3                                /* Allocate storage for CPU status register */

    OS_CPU_SR     cpu_sr;

#endif    

    BOOLEAN       tasks_waiting;

    OS_FLAG_NODE *pnode;

 

 

    if (OSIntNesting > 0) {                                /* See if called from ISR ...               */

        *err = OS_ERR_DEL_ISR;                             /* ... can't DELETE from an ISR             */

        return (pgrp);

	           //如果有中断发生,返回错误码

    }

#if OS_ARG_CHK_EN > 0

    if (pgrp == (OS_FLAG_GRP *)0) {                        /* Validate 'pgrp'                          */

        *err = OS_FLAG_INVALID_PGRP;

        return (pgrp);

	        	//【判断如果不是有效的组,返回错误码

    }

    if (pgrp->OSFlagType != OS_EVENT_TYPE_FLAG) {          /* Validate event group type                */

        *err = OS_ERR_EVENT_TYPE;

        return (pgrp);

		//判断不是事件组类型,返回错误码

    }

#endif

    OS_ENTER_CRITICAL();

	//进入临界区

    if (pgrp->OSFlagWaitList != (void *)0) {               /* See if any tasks waiting on event flags  */

        tasks_waiting = TRUE;                              /* Yes                                      */

	//判断事件等待列表如果不为零,tasks_waiting置位

    } else {

        tasks_waiting = FALSE;                             /* No                                       */

    }

    switch (opt) {

        case OS_DEL_NO_PEND:                               /* Delete group if no task waiting          */

	//没有任务在等待时删除组

             if (tasks_waiting == FALSE) {

				//先判断tasks_waiting是FALSE

                 pgrp->OSFlagType     = OS_EVENT_TYPE_UNUSED;

				 //事件的类型是OS_EVENT_TYPE_UNUSED

                 pgrp->OSFlagWaitList = (void *)OSFlagFreeList; /* Return group to free list           */

				 //OSFlagWaitList指针指向空链表

                 OSFlagFreeList       = pgrp;

				 //空链表指向新的列表

                 OS_EXIT_CRITICAL();

				 //退出临界区

                 *err                 = OS_NO_ERR;

				 //返回错误码

                 return ((OS_FLAG_GRP *)0);                /* Event Flag Group has been deleted        */

		       //返回0

             } else { //有任务在等待

                 OS_EXIT_CRITICAL();

				 //退出临界区

				 

                 *err                 = OS_ERR_TASK_WAITING;

				 //返回错误码

                 return (pgrp);

				 //返回pgrp

             }

 

        case OS_DEL_ALWAYS:                                /* Always delete the event flag group       */

		//不管有没有任务直接删除

             pnode = pgrp->OSFlagWaitList;

			 //*指向等待事件组的节点链表*/  

             while (pnode != (OS_FLAG_NODE *)0) {          /* Ready ALL tasks waiting for flags        */

			///遍历该链表

                 OS_FlagTaskRdy(pnode, (OS_FLAGS)0);

				 //将该链表中的节点指向的任务转为就绪状态

                 pnode = pnode->OSFlagNodeNext;

				 //查找下一个节点

             }

             pgrp->OSFlagType     = OS_EVENT_TYPE_UNUSED;

			    //标志类型为OS_EVENT_TYPE_UNUSED

             pgrp->OSFlagWaitList = (void *)OSFlagFreeList;/* Return group to free list                */

			    // 将该组返回到空闲列表中

             OSFlagFreeList       = pgrp;

			 //OSFlagFreeList指向pgrp

             OS_EXIT_CRITICAL();

              //退出临界区

             if (tasks_waiting == TRUE) {                  /* Reschedule only if task(s) were waiting  */

                 OS_Sched();                               /* Find highest priority task ready to run  */

				  //如果任务等待是真。进行一次任务调度

             }

             *err = OS_NO_ERR;

             return ((OS_FLAG_GRP *)0);                    /* Event Flag Group has been deleted        */

			  //返回错误码和OS_FLAG_GRP为0

 

        default:

             OS_EXIT_CRITICAL();

			 //退出临界区

             *err = OS_ERR_INVALID_OPT;

             return (pgrp);

			 //返回错误码和pgrp

    }

}

#endif

/*$PAGE*/

/*

*********************************************************************************************************

*                                        WAIT ON AN EVENT FLAG GROUP

*

* Description: This function is called to wait for a combination of bits to be set in an event flag 

*              group.  Your application can wait for ANY bit to be set or ALL bits to be set.

*

* Arguments  : pgrp          is a pointer to the desired event flag group.

*

*              flags         Is a bit pattern indicating which bit(s) (i.e. flags) you wish to wait for.  

*                            The bits you want are specified by setting the corresponding bits in 

*                            'flags'.  e.g. if your application wants to wait for bits 0 and 1 then 

*                            'flags' would contain 0x03.

*

*              wait_type     specifies whether you want ALL bits to be set or ANY of the bits to be set.

*                            You can specify the following argument:

*

*                            OS_FLAG_WAIT_CLR_ALL   You will wait for ALL bits in 'mask' to be clear (0)

*                            OS_FLAG_WAIT_SET_ALL   You will wait for ALL bits in 'mask' to be set   (1)

*                            OS_FLAG_WAIT_CLR_ANY   You will wait for ANY bit  in 'mask' to be clear (0)

*                            OS_FLAG_WAIT_SET_ANY   You will wait for ANY bit  in 'mask' to be set   (1)

*

*                            NOTE: Add OS_FLAG_CONSUME if you want the event flag to be 'consumed' by

*                                  the call.  Example, to wait for any flag in a group AND then clear

*                                  the flags that are present, set 'wait_type' to:

*

*                                  OS_FLAG_WAIT_SET_ANY + OS_FLAG_CONSUME

*

*              timeout       is an optional timeout (in clock ticks) that your task will wait for the

*                            desired bit combination.  If you specify 0, however, your task will wait

*                            forever at the specified event flag group or, until a message arrives.

*

*              err           is a pointer to an error code and can be:

*                            OS_NO_ERR              The desired bits have been set within the specified

*                                                   'timeout'.

*                            OS_ERR_PEND_ISR        If you tried to PEND from an ISR

*                            OS_FLAG_INVALID_PGRP   If 'pgrp' is a NULL pointer.

*                            OS_ERR_EVENT_TYPE      You are not pointing to an event flag group

*                            OS_TIMEOUT             The bit(s) have not been set in the specified 

*                                                   'timeout'.

*                            OS_FLAG_ERR_WAIT_TYPE  You didn't specify a proper 'wait_type' argument.

*

* Returns    : The new state of the flags in the event flag group when the task is resumed or,

*              0 if a timeout or an error occurred.

*

* Called from: Task ONLY

*********************************************************************************************************

*/

//等待事件标志组的事件标志位

OS_FLAGS  OSFlagPend (OS_FLAG_GRP *pgrp, OS_FLAGS flags, INT8U wait_type, INT16U timeout, INT8U *err)

{

#if OS_CRITICAL_METHOD == 3                                /* Allocate storage for CPU status register */

    OS_CPU_SR     cpu_sr;

#endif    

    OS_FLAG_NODE  node;

    OS_FLAGS      flags_cur;

    OS_FLAGS      flags_rdy;

    BOOLEAN       consume;

 

 

    if (OSIntNesting > 0) {                                /* See if called from ISR ...               */

        *err = OS_ERR_PEND_ISR;                            /* ... can't PEND from an ISR               */

        return ((OS_FLAGS)0);

	         //如果有中断事件发生,返回错误码

#if OS_ARG_CHK_EN > 0

    if (pgrp == (OS_FLAG_GRP *)0) {                        /* Validate 'pgrp'                          */

        *err = OS_FLAG_INVALID_PGRP;

        return ((OS_FLAGS)0);

		  //如果不是有效的组,返回错误码

    }

    if (pgrp->OSFlagType != OS_EVENT_TYPE_FLAG) {          /* Validate event block type                */

        *err = OS_ERR_EVENT_TYPE;

        return ((OS_FLAGS)0);

		    //如果不是有效的事件类型,返回错误码

    }

#endif

    if (wait_type & OS_FLAG_CONSUME) {                     /* See if we need to consume the flags      */

        wait_type &= ~OS_FLAG_CONSUME;

        consume    = TRUE;

		//如果有consume事件发生,进行置位

    } else {

        consume    = FALSE;

		

    }

/*$PAGE*/

    OS_ENTER_CRITICAL();

	         //进入临界区

    switch (wait_type) {

        case OS_FLAG_WAIT_SET_ALL:                         /* See if all required flags are set        */

		//置位所有的等待,见上面函数分析,不展开

             flags_rdy = pgrp->OSFlagFlags & flags;        /* Extract only the bits we want            */

             if (flags_rdy == flags) {                     /* Must match ALL the bits that we want     */

                 if (consume == TRUE) {                    /* See if we need to consume the flags      */

                     pgrp->OSFlagFlags &= ~flags_rdy;      /* Clear ONLY the flags that we wanted      */

                 }

                 flags_cur = pgrp->OSFlagFlags;            /* Will return the state of the group       */

                 OS_EXIT_CRITICAL();                       /* Yes, condition met, return to caller     */

                 *err      = OS_NO_ERR;

                 return (flags_cur);

             } else {                                      /* Block task until events occur or timeout */

                 OS_FlagBlock(pgrp, &node, flags, wait_type, timeout); 

                 OS_EXIT_CRITICAL();

             }

             break;

 

        case OS_FLAG_WAIT_SET_ANY:

		 //置位任意一个等待

             flags_rdy = pgrp->OSFlagFlags & flags;        /* Extract only the bits we want            */

             if (flags_rdy != (OS_FLAGS)0) {               /* See if any flag set                      */

                 if (consume == TRUE) {                    /* See if we need to consume the flags      */

                     pgrp->OSFlagFlags &= ~flags_rdy;      /* Clear ONLY the flags that we got         */

                 }

                 flags_cur = pgrp->OSFlagFlags;            /* Will return the state of the group       */

                 OS_EXIT_CRITICAL();                       /* Yes, condition met, return to caller     */

                 *err      = OS_NO_ERR;

                 return (flags_cur);

             } else {                                      /* Block task until events occur or timeout */

                 OS_FlagBlock(pgrp, &node, flags, wait_type, timeout); 

                 OS_EXIT_CRITICAL();

             }

             break;

 

#if OS_FLAG_WAIT_CLR_EN > 0

        case OS_FLAG_WAIT_CLR_ALL:                         /* See if all required flags are cleared    *

		//清除所有的等待

             flags_rdy = ~pgrp->OSFlagFlags & flags;       /* Extract only the bits we want            */

             if (flags_rdy == flags) {                     /* Must match ALL the bits that we want     */

                 if (consume == TRUE) {                    /* See if we need to consume the flags      */

                     pgrp->OSFlagFlags |= flags_rdy;       /* Set ONLY the flags that we wanted        */

                 }

                 flags_cur = pgrp->OSFlagFlags;            /* Will return the state of the group       */

                 OS_EXIT_CRITICAL();                       /* Yes, condition met, return to caller     */

                 *err      = OS_NO_ERR;

                 return (flags_cur);

             } else {                                      /* Block task until events occur or timeout */

                 OS_FlagBlock(pgrp, &node, flags, wait_type, timeout); 

                 OS_EXIT_CRITICAL();

             }

             break;

 

        case OS_FLAG_WAIT_CLR_ANY:

		//清除任意一个等待

             flags_rdy = ~pgrp->OSFlagFlags & flags;       /* Extract only the bits we want            */

             if (flags_rdy != (OS_FLAGS)0) {               /* See if any flag cleared                  */

                 if (consume == TRUE) {                    /* See if we need to consume the flags      */

                     pgrp->OSFlagFlags |= flags_rdy;       /* Set ONLY the flags that we got           */

                 }

                 flags_cur = pgrp->OSFlagFlags;            /* Will return the state of the group       */

                 OS_EXIT_CRITICAL();                       /* Yes, condition met, return to caller     */

                 *err      = OS_NO_ERR;

                 return (flags_cur);

             } else {                                      /* Block task until events occur or timeout */

                 OS_FlagBlock(pgrp, &node, flags, wait_type, timeout); 

                 OS_EXIT_CRITICAL();

             }

             break;

#endif

 

        default:

             OS_EXIT_CRITICAL();

             flags_cur = (OS_FLAGS)0;

             *err      = OS_FLAG_ERR_WAIT_TYPE;

             return (flags_cur);

    }

    OS_Sched();                                            /* Find next HPT ready to run               */

	//进行一次调度

    OS_ENTER_CRITICAL();

      //进入临界区

    if (OSTCBCur->OSTCBStat & OS_STAT_FLAG) {              /* Have we timed-out?                       */

	//*当前TCB挂起状态为取消或者超时状态*/  

        OS_FlagUnlink(&node);

		//删除节点

        OSTCBCur->OSTCBStat = OS_STAT_RDY;                 /* Yes, make task ready-to-run              */

	      //TCB的状态为OS_STAT_RDY

        OS_EXIT_CRITICAL();

		//退出临界区

        flags_cur           = (OS_FLAGS)0;

        *err                = OS_TIMEOUT;                  /* Indicate that we timed-out waiting       */

		  //返回错误码

    } else {

		//没有超时发生

        if (consume == TRUE) {                             /* See if we need to consume the flags      */

		       //consume为真

            switch (wait_type) {

                case OS_FLAG_WAIT_SET_ALL:

                case OS_FLAG_WAIT_SET_ANY:                 /* Clear ONLY the flags we got              */

                     pgrp->OSFlagFlags &= ~OSTCBCur->OSTCBFlagsRdy;

					 //清除我们得到的位

                     break;

                     

#if OS_FLAG_WAIT_CLR_EN > 0

                case OS_FLAG_WAIT_CLR_ALL:

                case OS_FLAG_WAIT_CLR_ANY:                 /* Set   ONLY the flags we got              */

                     pgrp->OSFlagFlags |= OSTCBCur->OSTCBFlagsRdy;

					 //置位我们得到的位

                     break;

#endif

            }

        }

        flags_cur = pgrp->OSFlagFlags;

		  //返回flags_cur

        OS_EXIT_CRITICAL();

		//退出临界区

        *err      = OS_NO_ERR;                             /* Event(s) must have occurred              */

    }  //返回错误码

    return (flags_cur);

}

/*$PAGE*/

/*

*********************************************************************************************************

*                                         POST EVENT FLAG BIT(S)

*

* Description: This function is called to set or clear some bits in an event flag group.  The bits to 

*              set or clear are specified by a 'bit mask'.

*

* Arguments  : pgrp          is a pointer to the desired event flag group.

*

*              flags         If 'opt' (see below) is OS_FLAG_SET, each bit that is set in 'flags' will 

*                            set the corresponding bit in the event flag group.  e.g. to set bits 0, 4 

*                            and 5 you would set 'flags' to:

*

*                                0x31     (note, bit 0 is least significant bit)

*

*                            If 'opt' (see below) is OS_FLAG_CLR, each bit that is set in 'flags' will 

*                            CLEAR the corresponding bit in the event flag group.  e.g. to clear bits 0, 

*                            4 and 5 you would specify 'flags' as:

*

*                                0x31     (note, bit 0 is least significant bit)

*

*              opt           indicates whether the flags will be:

*                                set     (OS_FLAG_SET) or 

*                                cleared (OS_FLAG_CLR)

*

*              err           is a pointer to an error code and can be:

*                            OS_NO_ERR              The call was successfull

*                            OS_FLAG_INVALID_PGRP   You passed a NULL pointer

*                            OS_ERR_EVENT_TYPE      You are not pointing to an event flag group

*                            OS_FLAG_INVALID_OPT    You specified an invalid option

*

* Returns    : the new value of the event flags bits that are still set.

*

* Called From: Task or ISR

*

* WARNING(s) : 1) The execution time of this function depends on the number of tasks waiting on the event 

*                 flag group.

*              2) The amount of time interrupts are DISABLED depends on the number of tasks waiting on

*                 the event flag group.        

*********************************************************************************************************

*/

//设置或清除事件标志组

OS_FLAGS  OSFlagPost (OS_FLAG_GRP *pgrp, OS_FLAGS flags, INT8U opt, INT8U *err)

{

#if OS_CRITICAL_METHOD == 3                          /* Allocate storage for CPU status register       */

    OS_CPU_SR     cpu_sr;

#endif    

    OS_FLAG_NODE *pnode;

    BOOLEAN       sched;

    OS_FLAGS      flags_cur;

    OS_FLAGS      flags_rdy;

 

 

#if OS_ARG_CHK_EN > 0

    if (pgrp == (OS_FLAG_GRP *)0) {                  /* Validate 'pgrp'                                */

        *err = OS_FLAG_INVALID_PGRP;

        return ((OS_FLAGS)0);

		// 判断是否是有效的组

    }

    if (pgrp->OSFlagType != OS_EVENT_TYPE_FLAG) {    /* Make sure we are pointing to an event flag grp */

        *err = OS_ERR_EVENT_TYPE;

        return ((OS_FLAGS)0);

		   //判断是否是有效的事件类型

    }

#endif

/*$PAGE*/

    OS_ENTER_CRITICAL();

	      //进入临界区

    switch (opt) 

	         

        case OS_FLAG_CLR

		        

             pgrp->OSFlagFlags &= ~flags;            /* Clear the flags specified in the group         */

             break;

			    //*清除在事件标志组中设置的位*/ 

             

        case OS_FLAG_SET:

             pgrp->OSFlagFlags |=  flags;            /* Set   the flags specified in the group         */

             break;

			 //置位在事件标志组中设置的位

          

        default:

             OS_EXIT_CRITICAL();                     /* INVALID option                                 */

             *err = OS_FLAG_INVALID_OPT;

             return ((OS_FLAGS)0);

			 //退出临界区并返回错误码

    }

    sched = FALSE;                                   /* Indicate that we don't need rescheduling       */

	//不进行调度

    pnode = pgrp->OSFlagWaitList; 

      //获得OSFlagWaitList指针

    while (pnode != (OS_FLAG_NODE *)0) {             /* Go through all tasks waiting on event flag(s)  */

	        //轮询事件组

        switch (pnode->OSFlagNodeWaitType) {

            case OS_FLAG_WAIT_SET_ALL:               /* See if all req. flags are set for current node */

			      //所有的位都置位

                 flags_rdy = pgrp->OSFlagFlags & pnode->OSFlagNodeFlags;

				       //得到事件中就绪表

                 if (flags_rdy == pnode->OSFlagNodeFlags) {     

				   //判断是否就绪

                     if (OS_FlagTaskRdy(pnode, flags_rdy) == TRUE) { /* Make task RTR, event(s) Rx'd   */

                         sched = TRUE;

?				 /* When done we will reschedule   */

                         //如果就绪,进行调度

                     }

                 }

                 break;

 

            case OS_FLAG_WAIT_SET_ANY:               /* See if any flag set                            */

			      //任意一个位就置位

                 flags_rdy = pgrp->OSFlagFlags & pnode->OSFlagNodeFlags;

                 if (flags_rdy != (OS_FLAGS)0) {    

                     if (OS_FlagTaskRdy(pnode, flags_rdy) == TRUE) { /* Make task RTR, event(s) Rx'd   */

                         sched = TRUE;                               /* When done we will reschedule   */

                     }

                 }

                 break;

 

#if OS_FLAG_WAIT_CLR_EN > 0

            case OS_FLAG_WAIT_CLR_ALL:               /* See if all req. flags are set for current node */

			         //所有的位都清零

                 flags_rdy = ~pgrp->OSFlagFlags & pnode->OSFlagNodeFlags;

                 if (flags_rdy == pnode->OSFlagNodeFlags) {     

                     if (OS_FlagTaskRdy(pnode, flags_rdy) == TRUE) { /* Make task RTR, event(s) Rx'd   */

                         sched = TRUE;                               /* When done we will reschedule   */

                     }

                 }

                 break;

 

            case OS_FLAG_WAIT_CLR_ANY:               /* See if any flag set                            */

			      //任意一个位就清零

                 flags_rdy = ~pgrp->OSFlagFlags & pnode->OSFlagNodeFlags;

                 if (flags_rdy != (OS_FLAGS)0) {    

                     if (OS_FlagTaskRdy(pnode, flags_rdy) == TRUE) { /* Make task RTR, event(s) Rx'd   */

                         sched = TRUE;                               /* When done we will reschedule   */

                     }

                 }

                 break;

#endif                 

        }

        pnode = pnode->OSFlagNodeNext;               /* Point to next task waiting for event flag(s)   */

		   //指向下一个节点

    }

    OS_EXIT_CRITICAL();

	     //退出临界区

    if (sched == TRUE) {

        OS_Sched();

		     //存在调度就进行调度

    }

    OS_ENTER_CRITICAL();

	        //进入临界区

    flags_cur = pgrp->OSFlagFlags;

	   //返回flags_cur

    OS_EXIT_CRITICAL();

    *err      = OS_NO_ERR;

    return (flags_cur);

}

/*$PAGE*/

/*

*********************************************************************************************************

*                                           QUERY EVENT FLAG 

*

* Description: This function is used to check the value of the event flag group.

*

* Arguments  : pgrp         is a pointer to the desired event flag group.

*

*              err           is a pointer to an error code returned to the called:

*                            OS_NO_ERR              The call was successfull

*                            OS_FLAG_INVALID_PGRP   You passed a NULL pointer

*                            OS_ERR_EVENT_TYPE      You are not pointing to an event flag group

*

* Returns    : The current value of the event flag group.

*

* Called From: Task or ISR

*********************************************************************************************************

*/

 

#if OS_FLAG_QUERY_EN > 0 

     

        //该功能用来检测事件标志组的值

OS_FLAGS  OSFlagQuery (OS_FLAG_GRP *pgrp, INT8U *err)

{

#if OS_CRITICAL_METHOD == 3                       /* Allocate storage for CPU status register          */

    OS_CPU_SR  cpu_sr;

#endif    

    OS_FLAGS   flags;

 

 

#if OS_ARG_CHK_EN > 0

    if (pgrp == (OS_FLAG_GRP *)0) {               /* Validate 'pgrp'                                   */

        *err = OS_FLAG_INVALID_PGRP;

        return ((OS_FLAGS)0);

		    //判断是否是有效的组 

    }

    if (pgrp->OSFlagType != OS_EVENT_TYPE_FLAG) { /* Validate event block type                         */

        *err = OS_ERR_EVENT_TYPE;

        return ((OS_FLAGS)0);

		     //判断是否是有效的类型

    }

#endif    

    OS_ENTER_CRITICAL(); 

	 //进入临界区

    flags = pgrp->OSFlagFlags;

	  //返回事件类型

    OS_EXIT_CRITICAL();

	   

    *err = OS_NO_ERR;

	     //退出临界区并返回错误码

    return (flags);                               /* Return the current value of the event flags       */

	 //返回flags

}

#endif

 

/*$PAGE*/

/*

*********************************************************************************************************

*                         SUSPEND TASK UNTIL EVENT FLAG(s) RECEIVED OR TIMEOUT OCCURS

*

* Description: This function is internal to uC/OS-II and is used to put a task to sleep until the desired

*              event flag bit(s) are set.

*

* Arguments  : pgrp          is a pointer to the desired event flag group.

*

*              pnode         is a pointer to a structure which contains data about the task waiting for 

*                            event flag bit(s) to be set.

*

*              flags         Is a bit pattern indicating which bit(s) (i.e. flags) you wish to check.  

*                            The bits you want are specified by setting the corresponding bits in 

*                            'flags'.  e.g. if your application wants to wait for bits 0 and 1 then 

*                            'flags' would contain 0x03.

*

*              wait_type     specifies whether you want ALL bits to be set/cleared or ANY of the bits 

*                            to be set/cleared.

*                            You can specify the following argument:

*

*                            OS_FLAG_WAIT_CLR_ALL   You will check ALL bits in 'mask' to be clear (0)

*                            OS_FLAG_WAIT_CLR_ANY   You will check ANY bit  in 'mask' to be clear (0)

*                            OS_FLAG_WAIT_SET_ALL   You will check ALL bits in 'mask' to be set   (1)

*                            OS_FLAG_WAIT_SET_ANY   You will check ANY bit  in 'mask' to be set   (1)

*

*              timeout       is the desired amount of time that the task will wait for the event flag 

*                            bit(s) to be set.

*

* Returns    : none

*

* Called by  : OSFlagPend()  OS_FLAG.C

*

* Note(s)    : This function is INTERNAL to uC/OS-II and your application should not call it.

*********************************************************************************************************

*/

//OS_FLAG.C文件中的OSFlagPend()调用

static  void  OS_FlagBlock (OS_FLAG_GRP *pgrp, OS_FLAG_NODE *pnode, OS_FLAGS flags, INT8U wait_type, INT16U timeout)

{

    OS_FLAG_NODE  *pnode_next;

 

 

    OSTCBCur->OSTCBStat      |= OS_STAT_FLAG;         

	 //TCB的状态为OS_STAT_FLAG

    OSTCBCur->OSTCBDly        = timeout;              /* Store timeout in task's TCB                   */

	//OSTCBDly 为timeout

#if OS_TASK_DEL_EN > 0

    OSTCBCur->OSTCBFlagNode   = pnode;                /* TCB to link to node                           */】

	       /*以下几行代码都是将pnode设置为链表的新的头节点*/

#endif    

    pnode->OSFlagNodeFlags    = flags;                /* Save the flags that we need to wait for       */

	        //获得flags存到pnode

    pnode->OSFlagNodeWaitType = wait_type;            /* Save the type of wait we are doing            */

	             //赋值wait_type为等待状态

    pnode->OSFlagNodeTCB      = (void *)OSTCBCur;     /* Link to task's TCB                            */

	          //和TCB建立联系

    pnode->OSFlagNodeNext     = pgrp->OSFlagWaitList; /* Add node at beginning of event flag wait list */

	        //设置下一个节点的值

    pnode->OSFlagNodePrev     = (void *)0;

    pnode->OSFlagNodeFlagGrp  = (void *)pgrp;         /* Link to Event Flag Group                      */

	                 //链接到事件组

    pnode_next                = pgrp->OSFlagWaitList;

    if (pnode_next != (void *)0) {                    /* Is this the first NODE to insert?             */

        pnode_next->OSFlagNodePrev = pnode;           /* No, link in doubly linked list                */

    } 

	          //空地址,只有一个节点(就是本节点),如果非空,那么将该链表的头节点改为现在这个节点

    pgrp->OSFlagWaitList = (void *)pnode;

                                                      /* Suspend current task until flag(s) received   */

    if ((OSRdyTbl[OSTCBCur->OSTCBY] &= ~OSTCBCur->OSTCBBitX) == 0) {

        OSRdyGrp &= ~OSTCBCur->OSTCBBitY;

    }

	  //挂起现在的任务直到收到任务

}

 

/*$PAGE*/

/*

*********************************************************************************************************

*                                    INITIALIZE THE EVENT FLAG MODULE

*

* Description: This function is called by uC/OS-II to initialize the event flag module.  Your application

*              MUST NOT call this function.  In other words, this function is internal to uC/OS-II.

*

* Arguments  : none

*

* Returns    : none

*

* WARNING    : You MUST NOT call this function from your code.  This is an INTERNAL function to uC/OS-II.

*********************************************************************************************************

*/

//该功能用来初始化事件标志模块

void  OS_FlagInit (void)

{

#if OS_MAX_FLAGS == 1

    OSFlagFreeList                 = (OS_FLAG_GRP *)&OSFlagTbl[0];  /* Only ONE event flag group!      */

	// 只有一个事件标志组,空闲列表指针指向标志表中第0号内容

    OSFlagFreeList->OSFlagType     = OS_EVENT_TYPE_UNUSED;

	//将标志类型设置为未使用类型

    OSFlagFreeList->OSFlagWaitList = (void *)0;

	//*将等待列表指针置为空*/  

#endif

 

#if OS_MAX_FLAGS >= 2

    INT8U        i;

    OS_FLAG_GRP *pgrp1;// 第一个指向事件标志组的指针

    OS_FLAG_GRP *pgrp2;// 第一个指向事件标志组的指针

 

 

    pgrp1 = &OSFlagTbl[0];

    pgrp2 = &OSFlagTbl[1];

    for (i = 0; i < (OS_MAX_FLAGS - 1); i++) {                      /* Init. list of free EVENT FLAGS  */

        pgrp1->OSFlagType     = OS_EVENT_TYPE_UNUSED;

        pgrp1->OSFlagWaitList = (void *)pgrp2;

        pgrp1++;

        pgrp2++;

    }

	//遍历该表,初始化所有的事件标志

    pgrp1->OSFlagWaitList = (void *)0;

    OSFlagFreeList        = (OS_FLAG_GRP *)&OSFlagTbl[0];

	

#endif    

}

 

/*$PAGE*/

/*

*********************************************************************************************************

*                              MAKE TASK READY-TO-RUN, EVENT(s) OCCURRED

*

* Description: This function is internal to uC/OS-II and is used to make a task ready-to-run because the

*              desired event flag bits have been set.

*

* Arguments  : pnode         is a pointer to a structure which contains data about the task waiting for 

*                            event flag bit(s) to be set.

*

*              flags_rdy     contains the bit pattern of the event flags that cause the task to become

*                            ready-to-run.

*

* Returns    : none

*

* Called by  : OSFlagsPost() OS_FLAG.C

*

* Note(s)    : 1) This function assumes that interrupts are disabled.

*              2) This function is INTERNAL to uC/OS-II and your application should not call it.

*********************************************************************************************************

*/

    //是否可以使任务变为就绪态 

static  BOOLEAN  OS_FlagTaskRdy (OS_FLAG_NODE *pnode, OS_FLAGS flags_rdy)

{

    OS_TCB   *ptcb;

    BOOLEAN   sched;

          

                                                        

    ptcb                = (OS_TCB *)pnode->OSFlagNodeTCB;  /* Point to TCB of waiting task             */

    //指向TCB

    ptcb->OSTCBDly      = 0;

	//等待时间设置为0

    ptcb->OSTCBFlagsRdy = flags_rdy;

	//    //OSTCBFlagsRdy块中设置为flags_rdy

    ptcb->OSTCBStat    &= ~OS_STAT_FLAG;

	         //OSTCBStat设置为0

    if (ptcb->OSTCBStat == OS_STAT_RDY) {                  /* Put task into ready list                 */

	     //如果OSTCBStat为就绪状态

        OSRdyGrp               |= ptcb->OSTCBBitY;

        OSRdyTbl[ptcb->OSTCBY] |= ptcb->OSTCBBitX;

		//设置对应的优先级

        sched                   = TRUE;

		//调度开启

    } else {

        sched                   = FALSE;

    }

    OS_FlagUnlink(pnode);

	//删除节点

    return (sched);

}

 

/*$PAGE*/

/*

*********************************************************************************************************

*                                  UNLINK EVENT FLAG NODE FROM WAITING LIST

*

* Description: This function is internal to uC/OS-II and is used to unlink an event flag node from a

*              list of tasks waiting for the event flag.

*

* Arguments  : pnode         is a pointer to a structure which contains data about the task waiting for 

*                            event flag bit(s) to be set.

*

* Returns    : none

*

* Called by  : OS_FlagTaskRdy() OS_FLAG.C

*              OSFlagPend()     OS_FLAG.C

*              OSTaskDel()      OS_TASK.C

*

* Note(s)    : 1) This function assumes that interrupts are disabled.

*              2) This function is INTERNAL to uC/OS-II and your application should not call it.

*********************************************************************************************************

*/

//待事件标志任务列表中解除事件标志节点

void  OS_FlagUnlink (OS_FLAG_NODE *pnode)

{

    OS_TCB       *ptcb;

    OS_FLAG_GRP  *pgrp;

    OS_FLAG_NODE *pnode_prev;

    OS_FLAG_NODE *pnode_next;

    

    

    pnode_prev = pnode->OSFlagNodePrev;

	      //前一个节点指针

	          

    pnode_next = pnode->OSFlagNodeNext;

	      //后一个节点指针

    if (pnode_prev == (OS_FLAG_NODE *)0) {                      /* Is it first node in wait list?      */

	    ///如果前一个节点为空。那么pnode指向的是等待列表的首节点(相当于删除首节点)

        pgrp                 = pnode->OSFlagNodeFlagGrp;        /* Yes, Point to event flag group      */

		        //指向所在的组

        pgrp->OSFlagWaitList = (void *)pnode_next;              /*      Update list for new 1st node   */

		      //指向下一个节点 

        if (pnode_next != (OS_FLAG_NODE *)0) {       

            pnode_next->OSFlagNodePrev = (OS_FLAG_NODE *)0;     /*      Link new 1st node PREV to NULL */

			  //判断下一个节点是否有效,有效把前一个节点设置为0

        }

    } else {                                                    /* No,  A node somewhere in the list   */

        pnode_prev->OSFlagNodeNext = pnode_next;                /*      Link around the node to unlink */

		   //指向下一个节点

        if (pnode_next != (OS_FLAG_NODE *)0) {                  /*      Was this the LAST node?        */

            pnode_next->OSFlagNodePrev = pnode_prev;            /*      No, Link around current node   */

			    //如果下一个节点有效,链接当前节点

        }

    }

    ptcb                = (OS_TCB *)pnode->OSFlagNodeTCB;

	   //点指向的TCB节点赋给ptcb指针*/  

#if OS_TASK_DEL_EN > 0

    ptcb->OSTCBFlagNode = (void *)0;

	  //OSTCBFlagNode赋值为0

#endif

}

#endif

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值