⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 event.c

📁 基于MDK的LPC1100处理器开发应用例程
💻 C
📖 第 1 页 / 共 2 页
字号:
/**
 *******************************************************************************
 * @file       event.c
 * @version    V1.13    
 * @date       2010.04.26
 * @brief      event management implementation code of CooCox CoOS kernel.	
 *******************************************************************************
 * @copy
 *
 * INTERNAL FILE,DON'T PUBLIC.
 * 
 * <h2><center>&copy; COPYRIGHT 2009 CooCox </center></h2>
 *******************************************************************************
 */ 


/*---------------------------- Include ---------------------------------------*/
#include <coocox.h>


/*---------------------------- Variable Define -------------------------------*/
#if CFG_EVENT_EN > 0

ECB    EventTbl[CFG_MAX_EVENT]= {{0}};/*!< Table which save event control block.*/
P_ECB  FreeEventList = NULL;        /*!< Pointer to free event control block. */


/**
 *******************************************************************************
 * @brief      Create a empty list of event control block 	   
 * @param[in]  None 	 
 * @param[out] None  	 
 * @retval     None	 
 *
 * @par Description
 * @details    This function is called by OSInit() API to create a ECB list,supply
 *             a  pointer to next event control block that not used.	 				
 *******************************************************************************
 */
void CreateEventList(void)
{	
    U8  i;
    P_ECB pecb1;
#if CFG_MAX_EVENT > 1
    P_ECB pecb2;
#endif
    i=0;
    pecb1 = &EventTbl[0];               /* Get first item                     */
#if CFG_MAX_EVENT == 1                  /* Build event list for only one item */									   
    pecb1->eventPtr  = NULL;
    pecb1->id        = i;               /* Assign ID.                         */
    pecb1->eventType = EVENT_TYPE_INVALID;  /* Sign that not to use.          */
#endif
    
#if CFG_MAX_EVENT > 1             /* Build event list for more than one item  */								   
    pecb2 = &EventTbl[1];
    for(;i< (CFG_MAX_EVENT-1);i++ )
    {
        pecb1->eventPtr  = (void*)pecb2;      /* Set link for list            */
        pecb1->id        = i;                 /* Assign ID.                   */
        pecb1->eventType = EVENT_TYPE_INVALID;/* Sign that not to use.        */
        pecb1++;                              /* Get next item                */
        pecb2++;	
    }
	pecb1->eventType = EVENT_TYPE_INVALID;    /* Sign that not to use.        */
    pecb1->eventPtr  = NULL;                  /* Set link for last item       */
    pecb1->id        = i;	
#endif
    
    FreeEventList    = &EventTbl[0];          /* Set free event item          */	
}



/**
 *******************************************************************************
 * @brief      Release a ECB	 
 * @param[in]  pecb     A pointer to event control block which be released.	 
 * @param[out] None 
 * @retval     None	 
 *
 * @par Description
 * @details    This function is called to release a event control block when a 
 *             event be deleted.
 *******************************************************************************
 */
static void ReleaseECB(P_ECB pecb)
{
    pecb->eventType = EVENT_TYPE_INVALID;     /* Sign that not to use.        */ 
    OsSchedLock();                            /* Lock schedule                */
    pecb->eventPtr  = FreeEventList;          /* Release ECB that event hold  */
    FreeEventList   = pecb;                   /* Reset free event item        */
    OsSchedUnlock();                          /* Unlock schedule              */
}



/**
 *******************************************************************************
 * @brief      Create a event	  
 * @param[in]  eventType       The type of event which	being created.
 * @param[in]  eventSortType   Event sort type.
 * @param[in]  eventCounter    Event counter,ONLY for EVENT_TYPE_SEM.
 * @param[in]  eventPtr        Event struct pointer,ONLY for Queue.NULL for other 
 *                             event type.		
 * @param[out] None  
 * @retval     NULL     Invalid pointer,create event fail.					 
 * @retval     others   Pointer to event control block which had assigned right now.
 *
 * @par Description
 * @details    This function is called by CreateSem(),...
 *             to get a event control block and initial the event content. 
 *
 * @note       This is a internal function of CooCox CoOS,User can't call.
 *******************************************************************************
 */
P_ECB CreatEvent(U8 eventType,U8 eventSortType,void* eventPtr)
{
    P_ECB pecb;
    
    OsSchedLock();                      /* Lock schedule                      */
    if(FreeEventList == NULL)           /* Is there no free evnet item        */
    {
        OsSchedUnlock();                /* Yes,unlock schedule                */
        return NULL;                    /* Return error                       */
    }
    pecb          = FreeEventList;/* Assign the free event item to this event */
    FreeEventList = FreeEventList->eventPtr;  /* Reset free event item        */
    OsSchedUnlock();                    /* Unlock schedul                     */
    
    pecb->eventType     = eventType;    /* Initialize event item as user set  */
    pecb->eventSortType = eventSortType;
    pecb->eventPtr      = eventPtr;
    pecb->eventTCBList  = NULL;
    return pecb;                        /* Return event item pointer          */
}


/**
 *******************************************************************************
 * @brief      Delete a event	  
 * @param[in]  pecb     Pointer to event control block which will be deleted. 
 * @param[in]  opt      Delete option.
 * @arg        == OPT_DEL_ANYWAY     Delete event always   
 * @arg        == OPT_DEL_NO_PEND	 Delete event only when no task pending on.
 * @param[out] None  	 
 * @retval     E_INVALID_PARAMETER   Parameter passed is invalid,deleted fail.
 * @retval     E_TASK_WAITTING       These are one more tasks waitting event.  
 * @retval     E_OK                  Delete event control block successful.
 *		  	
 * @par Description
 * @details    This function is called to delete a event from the event wait list
 *             use specify option.
 *
 * @note       This is a internal function of Coocox CoOS,user can't call.		
 *******************************************************************************
 */
StatusType DeleteEvent(P_ECB pecb,U8 opt)
{
    P_OSTCB ptcb;
    if(opt == OPT_DEL_NO_PEND)          /* Do delete event when no task pend? */
    {
        if(pecb->eventTCBList != NULL)  /* Yes,is there task pend this event? */
        {
            return E_TASK_WAITING;      /* Yes,error return                   */
        }
        else
        {
            ReleaseECB(pecb);           /* No,release resource that event hold*/
        }
    }
    else if(opt == OPT_DEL_ANYWAY)      /* Do delete event anyway?            */
    {
        OsSchedLock();                      /* Lock schedule                  */
        while(pecb->eventTCBList != NULL)   /* Is there task pend this event? */
        {                                   /* Yes,remove it                  */
            ptcb = pecb->eventTCBList;/* Get first task in event waiting list */
            if(ptcb->delayTick != INVALID_VALUE) /* Is task in delay list?    */
            {
                RemoveDelayList(ptcb);    /* Yes,remove task from delay list  */
            }

            /* Set next item as event waiting list head */
            pecb->eventTCBList = ptcb->waitNext; 
            ptcb->waitNext     = NULL;  /* Clear link for event waiting list  */
            ptcb->eventID      = INVALID_ID;  /* Sign that not to use.        */

			InsertToTCBRdyList(ptcb);         /* Insert task into ready list  */
        }
        OsSchedUnlock();                  /* Unlock schedule                  */
        ReleaseECB(pecb);                 /* Release resource that event hold */
    }
    return E_OK;                          /* Return OK                        */
}


/**
 *******************************************************************************
 * @brief      Insert a task to event wait list 	  						  
 * @param[in]  pecb    Pointer to event control block corresponding to the event. 	
 * @param[in]  ptcb    Pointer to task that will be insert to event wait list.	 
 * @param[out] None   
 * @retval     None	 
 *
 * @par Description
 * @details   This function is called to insert a task by fllowing manner:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -