μC/OS-II---事件标志组管理2(os_flag.c)

目录

事件标志组标志置1或清0

cpp 复制代码
OS_FLAGS  OSFlagPost (OS_FLAG_GRP  *pgrp,
											OS_FLAGS      flags,
											INT8U         opt,
											INT8U        *perr)
{
	OS_FLAG_NODE *pnode;
	BOOLEAN       sched;
	OS_FLAGS      flags_cur;
	OS_FLAGS      flags_rdy;
	BOOLEAN       rdy;
#if OS_CRITICAL_METHOD == 3u                         /* Allocate storage for CPU status register       */
	OS_CPU_SR     cpu_sr = 0u;
#endif
#ifdef OS_SAFETY_CRITICAL
	
	if (perr == (INT8U *)0)
	{
		OS_SAFETY_CRITICAL_EXCEPTION();
		return ((OS_FLAGS)0);
	}
	
#endif
#if OS_ARG_CHK_EN > 0u
	
	if (pgrp == (OS_FLAG_GRP *)0)                    /* Validate 'pgrp'                                */
	{
		*perr = OS_ERR_FLAG_INVALID_PGRP;
		return ((OS_FLAGS)0);
	}
	
#endif
	
	if (pgrp->OSFlagType != OS_EVENT_TYPE_FLAG)      /* Make sure we are pointing to an event flag grp */
	{
		*perr = OS_ERR_EVENT_TYPE;
		return ((OS_FLAGS)0);
	}
	
	/*$PAGE*/
	OS_ENTER_CRITICAL();
	
	switch (opt)
	{
		case OS_FLAG_CLR:
			pgrp->OSFlagFlags &= (OS_FLAGS)~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                                 */
			*perr = OS_ERR_FLAG_INVALID_OPT;
			return ((OS_FLAGS)0);
	}
	
	sched = OS_FALSE;                                /* Indicate that we don't need rescheduling       */
	pnode = (OS_FLAG_NODE *)pgrp->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 = (OS_FLAGS) (pgrp->OSFlagFlags & pnode->OSFlagNodeFlags);
				
				if (flags_rdy == pnode->OSFlagNodeFlags)     /* Make task RTR, event(s) Rx'd          */
				{
					rdy = OS_FlagTaskRdy (pnode, flags_rdy, OS_STAT_PEND_OK);
					
					if (rdy == OS_TRUE)
					{
						sched = OS_TRUE;                     /* When done we will reschedule          */
					}
				}
				
				break;
				
			case OS_FLAG_WAIT_SET_ANY:               /* See if any flag set                            */
				flags_rdy = (OS_FLAGS) (pgrp->OSFlagFlags & pnode->OSFlagNodeFlags);
				
				if (flags_rdy != (OS_FLAGS)0)                /* Make task RTR, event(s) Rx'd          */
				{
					rdy = OS_FlagTaskRdy (pnode, flags_rdy, OS_STAT_PEND_OK);
					
					if (rdy == OS_TRUE)
					{
						sched = OS_TRUE;                     /* When done we will reschedule          */
					}
				}
				
				break;
#if OS_FLAG_WAIT_CLR_EN > 0u
				
			case OS_FLAG_WAIT_CLR_ALL:               /* See if all req. flags are set for current node */
				flags_rdy = (OS_FLAGS)~pgrp->OSFlagFlags & pnode->OSFlagNodeFlags;
				
				if (flags_rdy == pnode->OSFlagNodeFlags)     /* Make task RTR, event(s) Rx'd          */
				{
					rdy = OS_FlagTaskRdy (pnode, flags_rdy, OS_STAT_PEND_OK);
					
					if (rdy == OS_TRUE)
					{
						sched = OS_TRUE;                     /* When done we will reschedule          */
					}
				}
				
				break;
				
			case OS_FLAG_WAIT_CLR_ANY:               /* See if any flag set                            */
				flags_rdy = (OS_FLAGS)~pgrp->OSFlagFlags & pnode->OSFlagNodeFlags;
				
				if (flags_rdy != (OS_FLAGS)0)                /* Make task RTR, event(s) Rx'd          */
				{
					rdy = OS_FlagTaskRdy (pnode, flags_rdy, OS_STAT_PEND_OK);
					
					if (rdy == OS_TRUE)
					{
						sched = OS_TRUE;                     /* When done we will reschedule          */
					}
				}
				
				break;
#endif
				
			default:
				OS_EXIT_CRITICAL();
				*perr = OS_ERR_FLAG_WAIT_TYPE;
				return ((OS_FLAGS)0);
		}
		
		pnode = (OS_FLAG_NODE *)pnode->OSFlagNodeNext; /* Point to next task waiting for event flag(s) */
	}
	
	OS_EXIT_CRITICAL();
	
	if (sched == OS_TRUE)
	{
		OS_Sched();
	}
	
	OS_ENTER_CRITICAL();
	flags_cur = pgrp->OSFlagFlags;
	OS_EXIT_CRITICAL();
	*perr     = OS_ERR_NONE;
	return (flags_cur);
}

事件标志组获取/无等待

cpp 复制代码
#if OS_FLAG_ACCEPT_EN > 0u
OS_FLAGS  OSFlagAccept (OS_FLAG_GRP  *pgrp,
												OS_FLAGS      flags,
												INT8U         wait_type,
												INT8U        *perr)
{
	OS_FLAGS      flags_rdy;
	INT8U         result;
	BOOLEAN       consume;
#if OS_CRITICAL_METHOD == 3u                               /* Allocate storage for CPU status register */
	OS_CPU_SR     cpu_sr = 0u;
#endif
#ifdef OS_SAFETY_CRITICAL
	
	if (perr == (INT8U *)0)
	{
		OS_SAFETY_CRITICAL_EXCEPTION();
		return ((OS_FLAGS)0);
	}
	
#endif
#if OS_ARG_CHK_EN > 0u
	
	if (pgrp == (OS_FLAG_GRP *)0)                          /* Validate 'pgrp'                          */
	{
		*perr = OS_ERR_FLAG_INVALID_PGRP;
		return ((OS_FLAGS)0);
	}
	
#endif
	
	if (pgrp->OSFlagType != OS_EVENT_TYPE_FLAG)            /* Validate event block type                */
	{
		*perr = OS_ERR_EVENT_TYPE;
		return ((OS_FLAGS)0);
	}
	
	result = (INT8U) (wait_type & OS_FLAG_CONSUME);
	
	if (result != (INT8U)0)                                /* See if we need to consume the flags      */
	{
		wait_type &= ~OS_FLAG_CONSUME;
		consume    = OS_TRUE;
	}
	
	else
	{
		consume    = OS_FALSE;
	}
	
	/*$PAGE*/
	*perr = OS_ERR_NONE;                                   /* Assume NO error until proven otherwise.  */
	OS_ENTER_CRITICAL();
	
	switch (wait_type)
	{
		case OS_FLAG_WAIT_SET_ALL:                         /* See if all required flags are set        */
			flags_rdy = (OS_FLAGS) (pgrp->OSFlagFlags & flags);    /* Extract only the bits we want   */
			
			if (flags_rdy == flags)                       /* Must match ALL the bits that we want     */
			{
				if (consume == OS_TRUE)                   /* See if we need to consume the flags      */
				{
					pgrp->OSFlagFlags &= (OS_FLAGS)~flags_rdy;     /* Clear ONLY the flags we wanted  */
				}
			}
			
			else
			{
				*perr = OS_ERR_FLAG_NOT_RDY;
			}
			
			OS_EXIT_CRITICAL();
			break;
			
		case OS_FLAG_WAIT_SET_ANY:
			flags_rdy = (OS_FLAGS) (pgrp->OSFlagFlags & flags);    /* Extract only the bits we want   */
			
			if (flags_rdy != (OS_FLAGS)0)                 /* See if any flag set                      */
			{
				if (consume == OS_TRUE)                   /* See if we need to consume the flags      */
				{
					pgrp->OSFlagFlags &= (OS_FLAGS)~flags_rdy;     /* Clear ONLY the flags we got     */
				}
			}
			
			else
			{
				*perr = OS_ERR_FLAG_NOT_RDY;
			}
			
			OS_EXIT_CRITICAL();
			break;
#if OS_FLAG_WAIT_CLR_EN > 0u
			
		case OS_FLAG_WAIT_CLR_ALL:                         /* See if all required flags are cleared    */
			flags_rdy = (OS_FLAGS)~pgrp->OSFlagFlags & flags;    /* Extract only the bits we want     */
			
			if (flags_rdy == flags)                       /* Must match ALL the bits that we want     */
			{
				if (consume == OS_TRUE)                   /* See if we need to consume the flags      */
				{
					pgrp->OSFlagFlags |= flags_rdy;       /* Set ONLY the flags that we wanted        */
				}
			}
			
			else
			{
				*perr = OS_ERR_FLAG_NOT_RDY;
			}
			
			OS_EXIT_CRITICAL();
			break;
			
		case OS_FLAG_WAIT_CLR_ANY:
			flags_rdy = (OS_FLAGS)~pgrp->OSFlagFlags & flags;   /* Extract only the bits we want      */
			
			if (flags_rdy != (OS_FLAGS)0)                 /* See if any flag cleared                  */
			{
				if (consume == OS_TRUE)                   /* See if we need to consume the flags      */
				{
					pgrp->OSFlagFlags |= flags_rdy;       /* Set ONLY the flags that we got           */
				}
			}
			
			else
			{
				*perr = OS_ERR_FLAG_NOT_RDY;
			}
			
			OS_EXIT_CRITICAL();
			break;
#endif
			
		default:
			OS_EXIT_CRITICAL();
			flags_rdy = (OS_FLAGS)0;
			*perr     = OS_ERR_FLAG_WAIT_TYPE;
			break;
	}
	
	return (flags_rdy);
}
#endif

事件标志组状态查询

cpp 复制代码
#if OS_FLAG_QUERY_EN > 0u
OS_FLAGS  OSFlagQuery (OS_FLAG_GRP  *pgrp,
											 INT8U        *perr)
{
	OS_FLAGS   flags;
#if OS_CRITICAL_METHOD == 3u                      /* Allocate storage for CPU status register          */
	OS_CPU_SR  cpu_sr = 0u;
#endif
#ifdef OS_SAFETY_CRITICAL
	
	if (perr == (INT8U *)0)
	{
		OS_SAFETY_CRITICAL_EXCEPTION();
		return ((OS_FLAGS)0);
	}
	
#endif
#if OS_ARG_CHK_EN > 0u
	
	if (pgrp == (OS_FLAG_GRP *)0)                 /* Validate 'pgrp'                                   */
	{
		*perr = OS_ERR_FLAG_INVALID_PGRP;
		return ((OS_FLAGS)0);
	}
	
#endif
	
	if (pgrp->OSFlagType != OS_EVENT_TYPE_FLAG)   /* Validate event block type                         */
	{
		*perr = OS_ERR_EVENT_TYPE;
		return ((OS_FLAGS)0);
	}
	
	OS_ENTER_CRITICAL();
	flags = pgrp->OSFlagFlags;
	OS_EXIT_CRITICAL();
	*perr = OS_ERR_NONE;
	return (flags);                               /* Return the current value of the event flags       */
}
#endif

设置事件标志组命名

cpp 复制代码
#if OS_FLAG_NAME_EN > 0u
void  OSFlagNameSet (OS_FLAG_GRP  *pgrp,
										 INT8U        *pname,
										 INT8U        *perr)
{
#if OS_CRITICAL_METHOD == 3u                     /* Allocate storage for CPU status register           */
	OS_CPU_SR  cpu_sr = 0u;
#endif
#ifdef OS_SAFETY_CRITICAL
	
	if (perr == (INT8U *)0)
	{
		OS_SAFETY_CRITICAL_EXCEPTION();
		return;
	}
	
#endif
#if OS_ARG_CHK_EN > 0u
	
	if (pgrp == (OS_FLAG_GRP *)0)                /* Is 'pgrp' a NULL pointer?                          */
	{
		*perr = OS_ERR_FLAG_INVALID_PGRP;
		return;
	}
	
	if (pname == (INT8U *)0)                     /* Is 'pname' a NULL pointer?                         */
	{
		*perr = OS_ERR_PNAME_NULL;
		return;
	}
	
#endif
	
	if (OSIntNesting > 0u)                       /* See if trying to call from an ISR                  */
	{
		*perr = OS_ERR_NAME_SET_ISR;
		return;
	}
	
	OS_ENTER_CRITICAL();
	
	if (pgrp->OSFlagType != OS_EVENT_TYPE_FLAG)
	{
		OS_EXIT_CRITICAL();
		*perr = OS_ERR_EVENT_TYPE;
		return;
	}
	
	pgrp->OSFlagName = pname;
	OS_EXIT_CRITICAL();
	*perr            = OS_ERR_NONE;
	return;
}
#endif

获取事件标志组命名

cpp 复制代码
#if OS_FLAG_NAME_EN > 0u
INT8U  OSFlagNameGet (OS_FLAG_GRP   *pgrp,
											INT8U        **pname,
											INT8U         *perr)
{
	INT8U      len;
#if OS_CRITICAL_METHOD == 3u                     /* Allocate storage for CPU status register           */
	OS_CPU_SR  cpu_sr = 0u;
#endif
#ifdef OS_SAFETY_CRITICAL
	
	if (perr == (INT8U *)0)
	{
		OS_SAFETY_CRITICAL_EXCEPTION();
		return (0u);
	}
	
#endif
#if OS_ARG_CHK_EN > 0u
	
	if (pgrp == (OS_FLAG_GRP *)0)                /* Is 'pgrp' a NULL pointer?                          */
	{
		*perr = OS_ERR_FLAG_INVALID_PGRP;
		return (0u);
	}
	
	if (pname == (INT8U **)0)                     /* Is 'pname' a NULL pointer?                         */
	{
		*perr = OS_ERR_PNAME_NULL;
		return (0u);
	}
	
#endif
	
	if (OSIntNesting > 0u)                       /* See if trying to call from an ISR                  */
	{
		*perr = OS_ERR_NAME_GET_ISR;
		return (0u);
	}
	
	OS_ENTER_CRITICAL();
	
	if (pgrp->OSFlagType != OS_EVENT_TYPE_FLAG)
	{
		OS_EXIT_CRITICAL();
		*perr = OS_ERR_EVENT_TYPE;
		return (0u);
	}
	
	*pname = pgrp->OSFlagName;
	len    = OS_StrLen (*pname);
	OS_EXIT_CRITICAL();
	*perr  = OS_ERR_NONE;
	return (len);
}
#endif
相关推荐
析木不会编程44 分钟前
【C语言】动态内存管理:详解malloc和free函数
c语言·开发语言
达帮主1 小时前
7.C语言 宏(Macro) 宏定义,宏函数
linux·c语言·算法
茶猫_1 小时前
力扣面试题 39 - 三步问题 C语言解法
c语言·数据结构·算法·leetcode·职场和发展
初学者丶一起加油1 小时前
C语言基础:指针(数组指针与指针数组)
linux·c语言·开发语言·数据结构·c++·算法·visual studio
半盏茶香3 小时前
C语言勘破之路-最终篇 —— 预处理(上)
c语言·开发语言·数据结构·c++·算法
2401_858286113 小时前
118.【C语言】数据结构之排序(堆排序和冒泡排序)
c语言·数据结构·算法
Zer0_on11 小时前
数据结构栈和队列
c语言·开发语言·数据结构
一只小bit11 小时前
数据结构之栈,队列,树
c语言·开发语言·数据结构·c++
马浩同学11 小时前
【GD32】从零开始学GD32单片机 | DAC数模转换器 + 三角波输出例程
c语言·单片机·嵌入式硬件·mcu
一个没有本领的人12 小时前
win11+matlab2021a配置C-COT
c语言·开发语言·matlab·目标跟踪