收藏 分享(赏)

μCOS-II中OS_FLAG.C源码中文注释版.doc

上传人:jinchen 文档编号:6612652 上传时间:2019-04-18 格式:DOC 页数:40 大小:138.50KB
下载 相关 举报
μCOS-II中OS_FLAG.C源码中文注释版.doc_第1页
第1页 / 共40页
μCOS-II中OS_FLAG.C源码中文注释版.doc_第2页
第2页 / 共40页
μCOS-II中OS_FLAG.C源码中文注释版.doc_第3页
第3页 / 共40页
μCOS-II中OS_FLAG.C源码中文注释版.doc_第4页
第4页 / 共40页
μCOS-II中OS_FLAG.C源码中文注释版.doc_第5页
第5页 / 共40页
点击查看更多>>
资源描述

1、xilentz 的网络文摘博客园 首页 新随笔 联系 订阅 管理 随笔 - 204 文章 - 0 评论 - 10 trackbacks - 0 OS_FLAG.C/* uC/OS-II* The Real-Time Kernel* EVENT FLAG MANAGEMENT* (c) Copyright 2001-2002, Jean J. Labrosse, Weston, FL* All Rights Reserved* File : OS_FLAG.C* By : Jean J. Labrosse*/ #ifndef OS_MASTER_FILE#include “INCLUDES.H“

2、#endif #if (OS_VERSION = 251) static BOOLEAN OS_FlagTaskRdy(OS_FLAG_NODE *pnode, OS_FLAGS flags_rdy); /*$PAGE*/ /* CHECK THE STATUS OF FLAGS IN AN EVENT FLAG GROUP* Description: This function is called to check the status of a combination of bits to be set or cleared* in an event flag group. Your ap

3、plication can check for ANY bit to be set/cleared or ALL* bits to be set/cleared.* This call does not block if the desired flags are not present.* 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 check.* The bits

4、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/cleared or ANY of the bits* to be set/cleared.* You can specify the following argument:

5、* OS_FLAG_WAIT_CLR_ALL You will check ALL bits in flags to be clear (0)* OS_FLAG_WAIT_CLR_ANY You will check ANY bit in flags to be clear (0)* OS_FLAG_WAIT_SET_ALL You will check ALL bits in flags to be set (1)* OS_FLAG_WAIT_SET_ANY You will check ANY bit in flags to be set (1)* NOTE: Add OS_FLAG_CO

6、NSUME 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* err is a pointer to an error code and can be:* OS_NO_ERR No error* OS_ERR_EVENT_TYPE You are n

7、ot pointing to an event flag group* OS_FLAG_ERR_WAIT_TYPE You didnt specify a proper wait_type argument.* OS_FLAG_INVALID_PGRP You passed a NULL pointer instead of the event flag* group handle.* OS_FLAG_ERR_NOT_RDY The desired flags you are waiting for are not* available.* Returns : The state of the

8、 flags in the event flag group.* Called from: Task or ISR无等待地获得事件标志组中的事件标志描述:去检查事件标志组中结合位的状态是置位还是被清除,你能检查任何将被置位或者清除的位或者全部位如果等待事件不发生,调用事件并不挂起。与 OSFlagPend()唯一不同点参数:pgrp:指向目标事件标志组的指针flags:是一个位的模式显示要检查的位,比如:你要检测 0 位和 1 位,那么你将它设置为 0x03wait_type :标记你想全部检测还是只想检测其实一部分。你能标记如下参数:* OS_FLAG_WAIT_CLR_ALL 你将检测 f

9、lags 中全部的清零位* OS_FLAG_WAIT_CLR_ANY 你将检测 flags 中任何清零位* OS_FLAG_WAIT_SET_ALL 你将检测 flags 中全部的置一位* OS_FLAG_WAIT_SET_ANY 你将检测 flags 中任何置一位如果想事件标志被调用函数清除的话,要加上 OS_FLAG_CONSUME,比如:如果想要组与后清除,那么将 wait_type 设置成 OS_FLAG_WAIT_SET_ANY + OS_FLAG_CONSUMEerr 指向错误代码的指针,可以为:* OS_NO_ERR 无误* OS_ERR_EVENT_TYPE 没有指向任务事件标

10、志组* OS_FLAG_ERR_WAIT_TYPE 你没有设置正确的 wait_type 参数* OS_FLAG_INVALID_PGRP 你传送了零指针而不是事件标志组操作* OS_FLAG_ERR_NOT_RDY 你等待的目标标志不合理返回:事件标志组的标志状态从任务和 ISR 中调用*/ #if OS_FLAG_ACCEPT_EN 0OS_FLAGS OSFlagAccept (OS_FLAG_GRP *pgrp, OS_FLAGS flags, INT8U wait_type, INT8U *err)#if OS_CRITICAL_METHOD = 3 /* Allocate stor

11、age for CPU status register */OS_CPU_SR cpu_sr;#endifOS_FLAGS flags_cur;OS_FLAGS flags_rdy;BOOLEAN consume; #if OS_ARG_CHK_EN 0if (pgrp = (OS_FLAG_GRP *)0) /* Validate pgrp */*err = OS_FLAG_INVALID_PGRP;return (OS_FLAGS)0);/非空的 pgrpif (pgrp-OSFlagType != OS_EVENT_TYPE_FLAG) /* Validate event block t

12、ype */*err = OS_ERR_EVENT_TYPE;return (OS_FLAGS)0);/合理的事件控制块类型#endifif (wait_type consume = TRUE; else consume = FALSE;/是否需要清除位/*$PAGE*/ *err = OS_NO_ERR; /* Assume NO error until proven otherwise. */初始化无错OS_ENTER_CRITICAL();switch (wait_type) case OS_FLAG_WAIT_SET_ALL: /* See if all required flags

13、are set */flags_rdy = pgrp-OSFlagFlags /* 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 /* Clear ONLY the flags that we wanted */是否要清除 else *err = OS_FLAG_ERR_NOT

14、_RDY;/没有我们想要的,返回不合理flags_cur = pgrp-OSFlagFlags; /* Will return the state of the group */返回标志OS_EXIT_CRITICAL();break; case OS_FLAG_WAIT_SET_ANY:flags_rdy = pgrp-OSFlagFlags /* Extract only the bits we want */if (flags_rdy != (OS_FLAGS)0) /* See if any flag set */是否有标志置位if (consume = TRUE) /* See if

15、 we need to consume the flags */pgrp-OSFlagFlags /* Clear ONLY the flags that we got */是否需要清除位 else /没有标志置位*err = OS_FLAG_ERR_NOT_RDY;flags_cur = pgrp-OSFlagFlags; /* Will return the state of the group */返回组状态,OS_EXIT_CRITICAL();break; #if OS_FLAG_WAIT_CLR_EN 0case OS_FLAG_WAIT_CLR_ALL: /* See if al

16、l required flags are cleared */flags_rdy = pgrp-OSFlagFlags /* 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

17、 */是否要设置位 else *err = OS_FLAG_ERR_NOT_RDY;/不是所有位都匹配flags_cur = pgrp-OSFlagFlags; /* Will return the state of the group */返回标志组状态OS_EXIT_CRITICAL();break; case OS_FLAG_WAIT_CLR_ANY:flags_rdy = pgrp-OSFlagFlags /* Extract only the bits we want */if (flags_rdy != (OS_FLAGS)0) /* See if any flag cleared

18、 */if (consume = TRUE) /* See if we need to consume the flags */pgrp-OSFlagFlags |= flags_rdy; /* Set ONLY the flags that we got */ else *err = OS_FLAG_ERR_NOT_RDY;flags_cur = pgrp-OSFlagFlags; /* Will return the state of the group */OS_EXIT_CRITICAL();break;#endif default:/其它异常情况OS_EXIT_CRITICAL();

19、flags_cur = (OS_FLAGS)0;*err = OS_FLAG_ERR_WAIT_TYPE;break;return (flags_cur);/返回状态#endif /*$PAGE*/ /* CREATE AN EVENT FLAG* Description: This function is called to create an event flag group.* Arguments : flags Contains the initial value to store in the event flag group.* err is a pointer to an err

20、or code which will be returned to your application:* OS_NO_ERR if the call was successful.* OS_ERR_CREATE_ISR if you attempted to create an Event Flag from an* ISR.* OS_FLAG_GRP_DEPLETED if there are no more event flag groups* Returns : A pointer to an event flag group or a NULL pointer if no more g

21、roups are available.* Called from: Task ONLY建立一个事件标志组描述:建立一个事件标志组参数:flags:包含存储在事件标志组中的初始值err:将返回到你应用程序的错误信息* OS_NO_ERR 如果成功* OS_ERR_CREATE_ISR 如果你想从 ISR 中建立* OS_FLAG_GRP_DEPLETED 如果没有多余的事件标志组了*/ OS_FLAG_GRP *OSFlagCreate (OS_FLAGS flags, INT8U *err)#if OS_CRITICAL_METHOD = 3 /* Allocate storage for

22、CPU status register */OS_CPU_SR cpu_sr;#endifOS_FLAG_GRP *pgrp; if (OSIntNesting 0) /* See if called from ISR . */*err = OS_ERR_CREATE_ISR; /* . cant CREATE from an ISR */return (OS_FLAG_GRP *)0);/不能从 ISR 中建立OS_ENTER_CRITICAL();pgrp = OSFlagFreeList; /* Get next free event flag */指向空链表的空地址 ,取得一个空闲事件

23、标志if (pgrp != (OS_FLAG_GRP *)0) /* See if we have event flag groups available */* Adjust free list */如果为 0,表明没有空闲的事件标志组了OSFlagFreeList = (OS_FLAG_GRP *)OSFlagFreeList-OSFlagWaitList;/调整系统的空闲事件标志组链表指针,使之指向新的表头pgrp-OSFlagType = OS_EVENT_TYPE_FLAG; /* Set to event flag group type */分配它是事件标志组,确保能系统正常运行p

24、grp-OSFlagFlags = flags; /* Set to desired initial value */将初始化值传入这个事件标志组pgrp-OSFlagWaitList = (void *)0; /* Clear list of tasks waiting on flags */因为是刚刚建立的事件标志组,没有任何任务等待这个事件标志组,/所以等待任务链表指针初始化为 0OS_EXIT_CRITICAL();*err = OS_NO_ERR; else /如果没有空闲的事件标志组了OS_EXIT_CRITICAL();*err = OS_FLAG_GRP_DEPLETED;re

25、turn (pgrp); /* Return pointer to event flag group */返回刚刚建立的事件标志组指针,如果没有空闲的事件标志组,/将返回 NULL 指针 /*$PAGE*/ /* DELETE AN EVENT FLAG GROUP* Description: This function deletes an event flag group and readies all tasks pending on the event flag* group.* Arguments : pgrp is a pointer to the desired event fl

26、ag group.* opt determines delete options as follows:* opt = OS_DEL_NO_PEND Deletes the event flag group ONLY if no task pending* opt = OS_DEL_ALWAYS Deletes the event flag group even if tasks are* waiting. In this case, all the tasks pending will be* readied.* err is a pointer to an error code that

27、can contain one of the following values:* OS_NO_ERR The call was successful and the event flag group was* deleted* OS_ERR_DEL_ISR If you attempted to delete the event flag group from* an ISR* OS_FLAG_INVALID_PGRP If pgrp is a NULL pointer.* OS_ERR_EVENT_TYPE If you didnt pass a pointer to an event f

28、lag group* OS_ERR_INVALID_OPT An invalid option was specified* OS_ERR_TASK_WAITING One or more tasks were waiting on the event flag* group.* Returns : pevent upon error* (OS_EVENT *)0 if the semaphore was successfully deleted.* Note(s) : 1) This function must be used with care. Tasks that would norm

29、ally expect the presence of* the event flag group MUST check the return code of OSFlagAccept() and OSFlagPend().* 2) This call can potentially disable interrupts for a long time. The interrupt disable* time is directly proportional to the number of tasks waiting on the event flag group.删除一个事件标志组描述:删

30、除一个事件标志组,将事件标志组中所有挂起的任务就绪。参数:pgrp:指向目标事件标志组的指针opt:决定以下删除选项:* opt = OS_DEL_NO_PEND 没有任务挂起的时候才删事件标志组* opt = OS_DEL_ALWAYS 即使有任务等待也删除,这个,所有挂起的任务将就绪。* err 包含以下错误信息之一的指针* OS_NO_ERR 调用成功,事件标志组成功删除* OS_ERR_DEL_ISR 如果想从 ISR 中删除事件标志组* OS_FLAG_INVALID_PGRP 如果 pgrp 是一个空指针* OS_ERR_EVENT_TYPE 如果没有传递指针到事件标志组* OS_

31、ERR_INVALID_OPT 有非法选项* OS_ERR_TASK_WAITING 一个或多个任务在事件标志组中等待*返回 : pevent 有错* (OS_EVENT *)0 如果成功删除 */ #if OS_FLAG_DEL_EN 0OS_FLAG_GRP *OSFlagDel (OS_FLAG_GRP *pgrp, INT8U opt, INT8U *err)#if OS_CRITICAL_METHOD = 3 /* Allocate storage for CPU status register */OS_CPU_SR cpu_sr;#endifBOOLEAN tasks_waiti

32、ng;OS_FLAG_NODE *pnode; if (OSIntNesting 0) /* See if called from ISR . */*err = OS_ERR_DEL_ISR; /* . cant DELETE from an ISR */return (pgrp);/不允许从 ISR 中删除#if OS_ARG_CHK_EN 0if (pgrp = (OS_FLAG_GRP *)0) /* Validate pgrp */*err = OS_FLAG_INVALID_PGRP;return (pgrp);/pgrp 必须有效。不能为零,并指向事件标志组if (pgrp-OSF

33、lagType != OS_EVENT_TYPE_FLAG) /* Validate event group type */*err = OS_ERR_EVENT_TYPE;return (pgrp);/如果不是事件组类型#endifOS_ENTER_CRITICAL();if (pgrp-OSFlagWaitList != (void *)0) /* See if any tasks waiting on event flags */是否有任务在此等待?tasks_waiting = TRUE; /* Yes */ else /tasks_waiting = FALSE; /* No */s

34、witch (opt) case OS_DEL_NO_PEND: /* Delete group if no task waiting */如果没有任务等待才删除if (tasks_waiting = FALSE) /没有任务等待pgrp-OSFlagType = OS_EVENT_TYPE_UNUSED;/将它标记为未使用pgrp-OSFlagWaitList = (void *)OSFlagFreeList; /* Return group to free list */将其返回到空闲链表中OSFlagFreeList = pgrp;OS_EXIT_CRITICAL();*err = OS

35、_NO_ERR;/无错return (OS_FLAG_GRP *)0); /* Event Flag Group has been deleted */ else /有任务等待OS_EXIT_CRITICAL();*err = OS_ERR_TASK_WAITING;return (pgrp);/返回事件标志组指针 case OS_DEL_ALWAYS: /* Always delete the event flag group */有任务等待也删除pnode = (OS_FLAG_NODE *)pgrp-OSFlagWaitList;/指向事件标志中等待任务结点的指针while (pnode

36、 != (OS_FLAG_NODE *)0) /* Ready ALL tasks waiting for flags */就绪所有的任务OS_FlagTaskRdy(pnode, (OS_FLAGS)0);/使任务就绪,事件发生pnode = (OS_FLAG_NODE *)pnode-OSFlagNodeNext;/只要不为零,就继续指向下一个结点。继续就绪pgrp-OSFlagType = OS_EVENT_TYPE_UNUSED;/标记为未用pgrp-OSFlagWaitList = (void *)OSFlagFreeList;/* Return group to free list

37、 */OSFlagFreeList = pgrp;/将其返回到空闲链表OS_EXIT_CRITICAL();if (tasks_waiting = TRUE) /* Reschedule only if task(s) were waiting */有任务等待的情况下,要重新任务调度。因为挂起的任务就绪了。OS_Sched(); /* Find highest priority task ready to run */*err = OS_NO_ERR;/无错return (OS_FLAG_GRP *)0); /* Event Flag Group has been deleted */ def

38、ault:/其它异常情况OS_EXIT_CRITICAL();*err = OS_ERR_INVALID_OPT;return (pgrp);#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.* Arg

39、uments : 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.* wai

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报