μ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
相关推荐
QAQ小菜鸟28 分钟前
一、初识C语言(1)
c语言
何曾参静谧43 分钟前
「C/C++」C/C++ 之 变量作用域详解
c语言·开发语言·c++
互联网打工人no11 小时前
每日一题——第一百二十一题
c语言
朱一头zcy2 小时前
C语言复习第9章 字符串/字符/内存函数
c语言
此生只爱蛋2 小时前
【手撕排序2】快速排序
c语言·c++·算法·排序算法
何曾参静谧3 小时前
「C/C++」C/C++ 指针篇 之 指针运算
c语言·开发语言·c++
lulu_gh_yu3 小时前
数据结构之排序补充
c语言·开发语言·数据结构·c++·学习·算法·排序算法
~yY…s<#>5 小时前
【刷题17】最小栈、栈的压入弹出、逆波兰表达式
c语言·数据结构·c++·算法·leetcode
EricWang13587 小时前
[OS] 项目三-2-proc.c: exit(int status)
服务器·c语言·前端
我是谁??7 小时前
C/C++使用AddressSanitizer检测内存错误
c语言·c++