/******************************************************************************
 *
 *   @name        AddTimerQ
 *
 *   @brief       Adds Timer Object to Timer Queue
 *
 *	 @param       pTimerObject	: Pointer to Timer Object
 *
 *   @return      None
 *****************************************************************************
 * Adds Timer Object to Timer Queue
 *****************************************************************************/
uint_8 AddTimerQ(PTIMER_OBJECT pTimerObject)
{
	uint_8 index;
	if(pTimerObject == NULL)
		return (uint_8)ERR_INVALID_PARAM;
	if(pTimerObject->msCount == (unsigned int)INVALID_TIME_COUNT)
		return (uint_8)ERR_INVALID_PARAM;
	
	for(index = 0; index < MAX_TIMER_OBJECTS; index++)
	{
	  /* Disable Timer Interrupts */
    DisableTimerInterrupt();
		
		if(g_TimerObjectArray[index].pfnTimerCallback == NULL)
		{
			(void)memcpy(&g_TimerObjectArray[index], pTimerObject, sizeof(TIMER_OBJECT)); 
			/* Enable Timer Interrupts */
			EnableTimerInterrupt();
			break;
		}
	  /* Enable Timer Interrupts */
		EnableTimerInterrupt();
	}
	if(index == MAX_TIMER_OBJECTS)
		return (uint_8)ERR_TIMER_QUEUE_FULL;
	return index;
}
/******************************************************************************
 *
 *   @name        RemoveTimerQ
 *
 *   @brief       Removes Timer Object from Timer Queue
 *
 *	 @param       index	: Index of Timer Object
 *
 *   @return      None
 *****************************************************************************
 * Removes Timer Object from Timer Queue
 *****************************************************************************/
uint_8 RemoveTimerQ(uint_8 index)
{
	if(index >= MAX_TIMER_OBJECTS)
		return (uint_8)ERR_INVALID_PARAM;
	/* Disable Timer Interrupts */
	DisableTimerInterrupt();
	(void)memset(&g_TimerObjectArray[index], (int)NULL, sizeof(TIMER_OBJECT));
	/* Enable Timer Interrupts */
	EnableTimerInterrupt();
	return (uint_8)ERR_SUCCESS;
}
예제 #3
0
/*FUNCTION*-------------------------------------------------------------------
*
* Function Name    : time_delay 
* Returned Value   :
* Comments         : Wait until interrupt of timer occur
*    
*
*END*----------------------------------------------------------------------*/
void time_delay(uint_32 delay) 
{
  delay_count = delay * 10;
  EnableTimerInterrupt();
  TPM2CNT = 0;
  while(1){    
    if(0 >= delay_count){
      DisableTimerInterrupt();
      break;
    }
  }
}
/******************************************************************************
 *   @name        Timer_ISR
 *
 *   @brief       This routine services RTC Interrupt
 *
 *	 @param       None
 *
 *   @return      None
 *
 ******************************************************************************
 * Services RTC Interrupt. If a Timer Object expires, then removes the object 
 * from Timer Queue and Calls the callback function (if registered)
 *****************************************************************************/
void Timer_ISR(void)
{
	uint_8 index;
	if(PIT_TFLG0 & PIT_TFLG_TIF_MASK)
    {
		/* Clear RTC Interrupt */
		PIT_TFLG0 |= PIT_TFLG_TIF_MASK;
		
		DisableTimerInterrupt();
		NVICICPR2|=(1<<4);   /* Clear any pending interrupts on LPT */
	    NVICISER2|=(1<<4);   /* Enable interrupts from LPT module */
    	
		/* Call Pending Timer CallBacks */
		for (index = 0; index < MAX_TIMER_OBJECTS; index++)
		{
			PTIMER_OBJECT ptemp = &g_TimerObjectArray[index];
			if(ptemp->pfnTimerCallback == NULL)
			{
				continue;
			}
			ptemp->msCount--;
			if (ptemp->msCount < 0) 
			{
			    PFNTIMER_CALLBACK pfnTimerCallback = ptemp->pfnTimerCallback;
#ifdef TIMER_CALLBACK_ARG
			    void *parg = ptemp->arg;
#endif
			    (void)RemoveTimerQ(index);
#ifdef TIMER_CALLBACK_ARG
				pfnTimerCallback(parg);
#else
				pfnTimerCallback();
#endif
			}
		}
	}
	EnableTimerInterrupt();
}