📄 os_flag.c
字号:
* OS_ERR_FLAG_INVALID_PGRP if you passed a NULL pointer for 'pgrp'
* OS_ERR_NAME_GET_ISR if you called this function from an ISR
*
* Returns : The length of the string or 0 if the 'pgrp' is a NULL pointer.
*********************************************************************************************************
*/
#if OS_FLAG_NAME_SIZE > 1
INT8U OSFlagNameGet(OS_FLAG_GRP *pgrp, INT8U *pname, INT8U *perr)
{
INT8U len;
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr = 0;
#endif
#if OS_ARG_CHK_EN > 0
if (perr == (INT8U *) 0)
{
/* Validate 'perr' */
return (0);
}
if (pgrp == (OS_FLAG_GRP *) 0)
{
/* Is 'pgrp' a NULL pointer? */
*perr = OS_ERR_FLAG_INVALID_PGRP;
return (0);
}
if (pname == (INT8U *) 0)
{
/* Is 'pname' a NULL pointer? */
*perr = OS_ERR_PNAME_NULL;
return (0);
}
#endif
if (OSIntNesting > 0)
{
/* See if trying to call from an ISR */
*perr = OS_ERR_NAME_GET_ISR;
return (0);
}
OS_ENTER_CRITICAL();
if (pgrp->OSFlagType != OS_EVENT_TYPE_FLAG)
{
OS_EXIT_CRITICAL();
*perr = OS_ERR_EVENT_TYPE;
return (0);
}
len = OS_StrCopy(pname, pgrp->OSFlagName); /* Copy name from OS_FLAG_GRP */
OS_EXIT_CRITICAL();
*perr = OS_ERR_NONE;
return (len);
}
#endif
/*$PAGE*/
/*
*********************************************************************************************************
* ASSIGN A NAME TO AN EVENT FLAG GROUP
*
* Description: This function assigns a name to an event flag group.
*
* Arguments : pgrp is a pointer to the event flag group.
*
* pname is a pointer to an ASCII string that will be used as the name of the event flag
* group. The string must be able to hold at least OS_FLAG_NAME_SIZE characters.
*
* perr is a pointer to an error code that can contain one of the following values:
*
* OS_ERR_NONE if the requested task is resumed
* OS_ERR_EVENT_TYPE if 'pevent' is not pointing to an event flag group
* OS_ERR_PNAME_NULL You passed a NULL pointer for 'pname'
* OS_ERR_FLAG_INVALID_PGRP if you passed a NULL pointer for 'pgrp'
* OS_ERR_NAME_SET_ISR if you called this function from an ISR
*
* Returns : None
*********************************************************************************************************
*/
#if OS_FLAG_NAME_SIZE > 1
void OSFlagNameSet(OS_FLAG_GRP *pgrp, INT8U *pname, INT8U *perr)
{
INT8U len;
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr = 0;
#endif
#if OS_ARG_CHK_EN > 0
if (perr == (INT8U *) 0)
{
/* Validate 'perr' */
return;
}
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 > 0)
{
/* 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;
}
len = OS_StrLen(pname); /* Can we fit the string in the storage area? */
if (len > (OS_FLAG_NAME_SIZE - 1))
{
/* No */
OS_EXIT_CRITICAL();
*perr = OS_ERR_FLAG_NAME_TOO_LONG;
return;
}
(void) OS_StrCopy(pgrp->OSFlagName, pname); /* Yes, copy name from OS_FLAG_GRP */
OS_EXIT_CRITICAL();
*perr = OS_ERR_NONE;
return;
}
#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.
*
* perr is a pointer to an error code and can be:
* OS_ERR_NONE The desired bits have been set within the specified
* 'timeout'.
* OS_ERR_PEND_ISR If you tried to PEND from an ISR
* OS_ERR_FLAG_INVALID_PGRP If 'pgrp' is a NULL pointer.
* OS_ERR_EVENT_TYPE You are not pointing to an event flag group
* OS_ERR_TIMEOUT The bit(s) have not been set in the specified
* 'timeout'.
* OS_ERR_PEND_ABORT The wait on the flag was aborted.
* OS_ERR_FLAG_WAIT_TYPE You didn't specify a proper 'wait_type' argument.
*
* Returns : The flags in the event flag group that made the task ready or, 0 if a timeout or an error
* occurred.
*
* Called from: Task ONLY
*
* Note(s) : 1) IMPORTANT, the behavior of this function has changed from PREVIOUS versions. The
* function NOW returns the flags that were ready INSTEAD of the current state of the
* event flags.
*********************************************************************************************************
*/
OS_FLAGS OSFlagPend(OS_FLAG_GRP *pgrp, OS_FLAGS flags, INT8U wait_type, INT16U timeout, INT8U *perr)
{
OS_FLAG_NODE node;
OS_FLAGS flags_rdy;
INT8U result;
INT8U pend_stat;
BOOLEAN consume;
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr = 0;
#endif
#if OS_ARG_CHK_EN > 0
if (perr == (INT8U *) 0)
{
/* Validate 'perr' */
return ((OS_FLAGS) 0);
}
if (pgrp == (OS_FLAG_GRP *) 0)
{
/* Validate 'pgrp' */
*perr = OS_ERR_FLAG_INVALID_PGRP;
return ((OS_FLAGS) 0);
}
#endif
if (OSIntNesting > 0)
{
/* See if called from ISR ... */
*perr = OS_ERR_PEND_ISR; /* ... can't PEND from an ISR */
return ((OS_FLAGS) 0);
}
if (OSLockNesting > 0)
{
/* See if called with scheduler locked ... */
*perr = OS_ERR_PEND_LOCKED; /* ... can't PEND when locked */
return ((OS_FLAGS) 0);
}
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 &= ~(INT8U) OS_FLAG_CONSUME;
consume = OS_TRUE;
}
else
{
consume = OS_FALSE;
}
/*$PAGE*/
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 &= ~flags_rdy; /* Clear ONLY the flags that we wanted */
}
OSTCBCur->OSTCBFlagsRdy = flags_rdy; /* Save flags that were ready */
OS_EXIT_CRITICAL(); /* Yes, condition met, return to caller */
*perr = OS_ERR_NONE;
return (flags_rdy);
}
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 = (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 &= ~flags_rdy; /* Clear ONLY the flags that we got */
}
OSTCBCur->OSTCBFlagsRdy = flags_rdy; /* Save flags that were ready */
OS_EXIT_CRITICAL(); /* Yes, condition met, return to caller */
*perr = OS_ERR_NONE;
return (flags_rdy);
}
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 = (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 */
}
OSTCBCur->OSTCBFlagsRdy = flags_rdy; /* Save flags that were ready */
OS_EXIT_CRITICAL(); /* Yes, condition met, return to caller */
*perr = OS_ERR_NONE;
return (flags_rdy);
}
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 = (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 */
}
OSTCBCur->OSTCBFlagsRdy = flags_rdy; /* Save flags that were ready */
OS_EXIT_CRITICAL(); /* Yes, condition met, return to caller */
*perr = OS_ERR_NONE;
return (flags_rdy);
}
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_rdy = (OS_FLAGS) 0;
*perr = OS_ERR_FLAG_WAIT_TYPE;
return (flags_rdy);
}
/*$PAGE*/
OS_Sched(); /* Find next HPT ready to run */
OS_ENTER_CRITICAL();
if (OSTCBCur->OSTCBStatPend != OS_STAT_PEND_OK)
{
/* Have we timed-out or aborted? */
pend_stat = OSTCBCur->OSTCBStatPend;
OSTCBCur->OSTCBStatPend = OS_STAT_PEND_OK;
OS_FlagUnlink(&node);
OSTCBCur->OSTCBStat = OS_STAT_RDY; /* Yes, make task ready-to-run */
OS_EXIT_CRITICAL();
flags_rdy = (OS_FLAGS) 0;
switch (pend_stat)
{
case OS_STAT_PEND_TO:
default:
*perr = OS_ERR_TIMEOUT; /* Indicate that we timed-out waiting */
break;
case OS_STAT_PEND_ABORT:
*perr = OS_ERR_PEND_ABORT; /* Indicate that we aborted waiting */
break;
}
return (flags_rdy);
}
flags_rdy = OSTCBCur->OSTCBFlagsRdy;
if (consume == OS_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 &= ~flags_rdy;
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 |= flags_rdy;
break;
#endif
default:
OS_EXIT_CRITICAL();
*perr = OS_ERR_FLAG_WAIT_TYPE;
return ((OS_FLAGS) 0);
}
}
OS_EXIT_CRITICAL();
*perr = OS_ERR_NONE; /* Event(s) must have occurred */
return (flags_rdy);
}
/*$PAGE*/
/*
*********************************************************************************************************
* GET FLAGS WHO CAUSED TASK TO BECOME READY
*
* Description: This function is called to obtain the flags that caused the task to become ready to run.
* In other words, this function allows you to tell "Who done it!".
*
* Arguments : None
*
* Returns : The flags that caused the task to be ready.
*
* Called from: Task ONLY
*********************************************************************************************************
*/
OS_FLAGS OSFlagPendGetFlagsRdy(void)
{
OS_FLAGS flags;
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr = 0;
#endif
OS_ENTER_CRITICAL();
flags = OSTCBCur->OSTCBFlagsRdy;
OS_EXIT_CRITICAL();
return (flags);
}
/*$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)
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -