📄 os_flag.c
字号:
参数:pgrp:指向目标事件标志组的指针
flags:是一个位的模式显示要检查的位,比如:你要检测0位和1位,那么你将它
设置为0x03
wait_type :标记你想全部检测还是只想检测其实一部分。你能标记如下参数:
* OS_FLAG_WAIT_CLR_ALL 等待所有指定事件标志组中的事件标志位清0
* OS_FLAG_WAIT_CLR_ANY 等待任意一个指定事件标志组中的事件标志位置1
* OS_FLAG_WAIT_SET_ALL 等待所有指定事件标志组中的事件标志位置1
* OS_FLAG_WAIT_SET_ANY 等待任意一个指定事件标志组中的事件标志位置1
如果想事件标志被调用函数清除的话,要加上OS_FLAG_CONSUME,比如:如果
想要组与后清除,那么将wait_type设置成OS_FLAG_WAIT_SET_ANY + OS_FLAG_CONSUME
timeout:你的任务等待目标结合位超时,如果你标志零,你的任务将在指定
事件标志组永久等待直到一条消息到来
err 指向错误代码的指针,可以为:
* OS_NO_ERR 无误
* OS_ERR_PEND_ISR 如果想从ISR中挂起
* OS_FLAG_INVALID_PGRP 如果 'pgrp' 是NULL指针
* OS_ERR_EVENT_TYPE 你没有指向任务事件控制组
* OS_TIMEOUT 在指定时间内位没有被置位
* OS_FLAG_ERR_WAIT_TYPE 你没有指定合适的 'wait_type' 参数
返回:当任务继续时,标志任务新状态,错误或者超时返回零
只能从任务中调用
*********************************************************************************************************
*/
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);//看pgrp是不是一个NULL指针
}
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 */
//看是否需要局部布尔变量,OSFlagPend 允许指定在任务等待事件发生后,重新
//置位或清除相应的事件标志位,方法是在调用此函数时,将一个常量与参数
//wait_type相加
wait_type &= ~OS_FLAG_CONSUME;
consume = TRUE;
} 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 */
//如果wait_type为OS_FLAG_WAIT_SET_ALL或者OS_FLAG_WAIT_SET_AND时,函数取出由flags参数指定
//的事件标志位
if (flags_rdy == flags) { /* Must match ALL the bits that we want */
//如果取出标志位状态恰好符合预期状态,说明任务需要等待的事件标志位都已经置位
//此情况下,PEND函数立即返回调用函数
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_FlagBlock()返回时,会进行任务调度
//是因为调用此任务的函数因为事件没有发生而不能继续运行
OS_ENTER_CRITICAL();
if (OSTCBCur->OSTCBStat & OS_STAT_FLAG) { /* Have we timed-out? */
OS_FlagUnlink(&node);//超时了吗?如果超时,就从双向等待列表中删除
OSTCBCur->OSTCBStat = OS_STAT_RDY;/* Yes, make task ready-to-run */
//超时就使任务进入就绪状态
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 */
//是否要清除参数
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;//此函数获取事件标志组的当前事件标志状态,
//并返回给调用状态
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.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -