两种同步机制
“或”同步
“与”同步
使能
#define OS_CFG_FLAG_EN 1u /* Enable (1) or Disable (0) code generation for EVENT FLAGS */
主结构体
typedef struct os_flag_grp OS_FLAG_GRP;
struct os_flag_grp { /* Event Flag Group */
OS_OBJ_TYPE Type; /* Should be set to OS_OBJ_TYPE_FLAG */
CPU_CHAR *NamePtr; /* Pointer to Event Flag Name (NUL terminated ASCII) */
OS_PEND_LIST PendList; /* List of tasks waiting on event flag group */
#if OS_CFG_DBG_EN > 0u
OS_FLAG_GRP *DbgPrevPtr;
OS_FLAG_GRP *DbgNextPtr;
CPU_CHAR *DbgNamePtr;
#endif
OS_FLAGS Flags; /* 8, 16 or 32 bit flags */
CPU_TS TS; /* Timestamp of when last post occurred */
};
API函数
//创建
void OSFlagCreate (OS_FLAG_GRP *p_grp,
CPU_CHAR *p_name,
OS_FLAGS flags,
OS_ERR *p_err);
#if OS_CFG_FLAG_DEL_EN > 0u
//删除
OS_OBJ_QTY OSFlagDel (OS_FLAG_GRP *p_grp,
OS_OPT opt,
OS_ERR *p_err);
#endif
/*
* opt specifies whether you want ALL bits to be set or ANY of the bits to be set.
* You can specify the 'ONE' of the following arguments:
*
* OS_OPT_PEND_FLAG_CLR_ALL You will wait for ALL bits in 'flags' to be clear (0)
* OS_OPT_PEND_FLAG_CLR_ANY You will wait for ANY bit in 'flags' to be clear (0)
* OS_OPT_PEND_FLAG_SET_ALL You will wait for ALL bits in 'flags' to be set (1)
* OS_OPT_PEND_FLAG_SET_ANY You will wait for ANY bit in 'flags' to be set (1)
*
* You can 'ADD' OS_OPT_PEND_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_opt' to:
*
* OS_OPT_PEND_FLAG_SET_ANY + OS_OPT_PEND_FLAG_CONSUME
*
* You can also 'ADD' the type of pend with 'ONE' of the two option:
*
* OS_OPT_PEND_NON_BLOCKING Task will NOT block if flags are not available
* OS_OPT_PEND_BLOCKING Task will block if flags are not available
*/
//等待-1
OS_FLAGS OSFlagPend (OS_FLAG_GRP *p_grp,
OS_FLAGS flags,
OS_TICK timeout,
OS_OPT opt,
CPU_TS *p_ts,
OS_ERR *p_err);
#if OS_CFG_FLAG_PEND_ABORT_EN > 0u
//取消等待
OS_OBJ_QTY OSFlagPendAbort (OS_FLAG_GRP *p_grp,
OS_OPT opt,
OS_ERR *p_err);
#endif
//获取使任务就绪的事件标志
OS_FLAGS OSFlagPendGetFlagsRdy (OS_ERR *p_err);
//发送+1
OS_FLAGS OSFlagPost (OS_FLAG_GRP *p_grp,
OS_FLAGS flags,
OS_OPT opt,
OS_ERR *p_err);
应用举例
#define FLAG1 (1 << 0)
#define FLAG2 (1 << 1)
OS_FLAG_GRP EventFlags; //定义一个事件标志组
void task1(void *p_arg)
{
while(1)
{
//向事件标志组EventFlags发送标志
flags_num=OSFlagPost((OS_FLAG_GRP*)&EventFlags,
(OS_FLAGS )FLAG1,
(OS_OPT )OS_OPT_POST_FLAG_SET,
(OS_ERR* )&err);
printf("task1事件标志组EventFlags的值:%d
",flags_num);
OSTimeDlyHMSM(0,0,2,0,OS_OPT_TIME_PERIODIC,&err);
//向事件标志组EventFlags发送标志
flags_num=OSFlagPost((OS_FLAG_GRP*)&EventFlags,
(OS_FLAGS )FLAG2,
(OS_OPT )OS_OPT_POST_FLAG_SET,
(OS_ERR* )&err);
printf("task1事件标志组EventFlags的值:%d
",flags_num);
OSTimeDlyHMSM(0,0,2,0,OS_OPT_TIME_PERIODIC,&err);
}
}
void task2(void *p_arg)
{
while(1)
{
//等待事件标志组
OSFlagPend((OS_FLAG_GRP*)&EventFlags,
(OS_FLAGS )FLAG1|FLAG2,
(OS_TICK )0,
(OS_OPT )OS_OPT_PEND_FLAG_SET_ALL+OS_OPT_PEND_FLAG_CONSUME,
(CPU_TS* )0,
(OS_ERR* )&err);
printf("task2事件标志组EventFlags的值:%d
",EventFlags.Flags);
}
}
实验现象