μCOS-II中OS_FLAG.C源码中文注释版.doc_第1页
μCOS-II中OS_FLAG.C源码中文注释版.doc_第2页
μCOS-II中OS_FLAG.C源码中文注释版.doc_第3页
μCOS-II中OS_FLAG.C源码中文注释版.doc_第4页
μCOS-II中OS_FLAG.C源码中文注释版.doc_第5页
已阅读5页,还剩35页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

xilentz的网络文摘博客园 首页 新随笔 联系 订阅 管理 随笔 - 204 文章 - 0评论 - 10trackbacks - 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#endif #if (OS_VERSION = 251) & (OS_FLAG_EN 0) & (OS_MAX_FLAGS 0)/* LOCAL PROTOTYPES 局部定义*/ static void OS_FlagBlock(OS_FLAG_GRP *pgrp, OS_FLAG_NODE *pnode, OS_FLAGS flags, INT8U wait_type, INT16U timeout);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 application 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 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:* 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_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* err is a pointer to an error code and can be:* OS_NO_ERR No error* OS_ERR_EVENT_TYPE You are not 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 flags in the event flag group.* Called from: Task or ISR 无等待地获得事件标志组中的事件标志描述:去检查事件标志组中结合位的状态是置位还是被清除,你能 检查任何将被置位或者清除的位或者全部位 如果等待事件不发生,调用事件并不挂起。与OSFlagPend()唯一不同点参数:pgrp:指向目标事件标志组的指针 flags:是一个位的模式显示要检查的位,比如:你要检测0位和1位,那么你将它 设置为0x03 wait_type :标记你想全部检测还是只想检测其实一部分。你能标记如下参数:* OS_FLAG_WAIT_CLR_ALL 你将检测flags中全部的清零位* 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_CONSUME err 指向错误代码的指针,可以为:* OS_NO_ERR 无误* OS_ERR_EVENT_TYPE 没有指向任务事件标志组* 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 storage for CPU status register */ OS_CPU_SR cpu_sr;#endif OS_FLAGS flags_cur; OS_FLAGS flags_rdy; BOOLEAN consume; #if OS_ARG_CHK_EN 0 if (pgrp = (OS_FLAG_GRP *)0) /* Validate pgrp */ *err = OS_FLAG_INVALID_PGRP; return (OS_FLAGS)0);/非空的pgrp 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 */ wait_type &= OS_FLAG_CONSUME; 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 are set */ 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; /* Clear ONLY the flags that we wanted */ /是否要清除 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_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 */ /是否需要清除位 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 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 */ /是否要设置位 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 & 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 */ 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(); 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 error 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 groups 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 CPU status register */ OS_CPU_SR cpu_sr;#endif OS_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 */ /指向空链表的空地址 ,取得一个空闲事件标志 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 */ /分配它是事件标志组,确保能系统正常运行 pgrp-OSFlagFlags = flags; /* Set to desired initial value */ /将初始化值传入这个事件标志组 pgrp-OSFlagWaitList = (void *)0; /* Clear list of tasks waiting on flags */ /因为是刚刚建立的事件标志组,没有任何任务等待这个事件标志组, /所以等待任务链表指针初始化为0 OS_EXIT_CRITICAL(); *err = OS_NO_ERR; else /如果没有空闲的事件标志组了 OS_EXIT_CRITICAL(); *err = OS_FLAG_GRP_DEPLETED; return (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 flag 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 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 flag 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 normally 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. 删除一个事件标志组描述:删除一个事件标志组,将事件标志组中所有挂起的任务就绪。参数: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_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;#endif BOOLEAN tasks_waiting; 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 0 if (pgrp = (OS_FLAG_GRP *)0) /* Validate pgrp */ *err = OS_FLAG_INVALID_PGRP; return (pgrp);/pgrp必须有效。不能为零,并指向事件标志组 if (pgrp-OSFlagType != OS_EVENT_TYPE_FLAG) /* Validate event group type */ *err = OS_ERR_EVENT_TYPE; return (pgrp);/如果不是事件组类型 #endif OS_ENTER_CRITICAL(); if (pgrp-OSFlagWaitList != (void *)0) /* See if any tasks waiting on event flags */ /是否有任务在此等待? tasks_waiting = TRUE; /* Yes */ else / tasks_waiting = FALSE; /* No */ switch (opt) case OS_DEL_NO_PEND: /* Delete group if no task waiting */ /如果没有任务等待才删除 if (tasks_wai

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论