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

📄 timer.c

📁 FREESCALE的基于802.15.4无线通讯原代码
💻 C
📖 第 1 页 / 共 2 页
字号:

void TMR_StopTimer
  (
  uint8_t userTimerId  /*  IN: */
  )
{
  uint8_t   current   = mTimerHead;
  uint8_t   previous  = mTimerNill_c;
  uint16_t  freeRuningCountValue;
  uint16_t  cmpRegValue;
  uint16_t  firstTimerExpireTime;
  uint8_t   ccr;
  /* Handles start of a timer which is not created  */
  if (userTimerId >= gSoftwareTimerCount_c )
    return;
 
  IrqControlLib_BackupIrqStatus(ccr);/*To protect from re entrant*/
  IrqControlLib_DisableAllIrqs();
      
  /* Handles stoping a timer which is not started  */
  if ((maSoftTimers[ userTimerId ].timerMgmt.status & gUsedTimer_c) == 0x0) {
    IrqControlLib_RestoreIrqStatus(ccr);
    return;
  }
  else  
  {
    maSoftTimers[userTimerId].timerMgmt.status &= (~gUsedTimer_c);
    maSoftTimers[userTimerId].pCallBackFunction = TMR_DefaultTimeoutHandler;
  }

  ReadTpmCntRegister( freeRuningCountValue );
  TPM1C0VReadValue( cmpRegValue );
    
  firstTimerExpireTime =  cmpRegValue - freeRuningCountValue;  	          
  
  /*  Timer to be removed is the timer head  */
  if (userTimerId == mTimerHead) {
    /* This is the only timer running  */
    if (maSoftTimers[ mTimerHead ].timerMgmt.next == mTimerNill_c)  {
      mTimerHead = mTimerNill_c;
      ExitSwTimerMode();
      IrqControlLib_RestoreIrqStatus(ccr);
      return;
    }
    mTimerHead = maSoftTimers[mTimerHead].timerMgmt.next;
    /* Update the timeout */
    maSoftTimers[mTimerHead].timeout += firstTimerExpireTime;
    TMRCalculateAndLoadNextTimeOutCompareValue(mTimerHead);      
  }
  else  {
    /*  Traverse to reach the timer to be removed */
    while( current != userTimerId ) {
      if ( current == mTimerNill_c) {
        break;
      }
      previous = current;
      current = maSoftTimers[ current ].timerMgmt.next; 
    }
    /*  Timer to be removed is amid of list */    
    if( current != mTimerNill_c ) {
      /* Update the timeout */
      maSoftTimers[maSoftTimers[ current ].timerMgmt.next].timeout +=
                                             maSoftTimers[ current ].timeout; 
      maSoftTimers[ previous ].timerMgmt.next = 
                                maSoftTimers[ current ].timerMgmt.next;
    }
    /*  Timer to be removed is the last timer */
    else {
      maSoftTimers[ previous ].timerMgmt.next = mTimerNill_c;
    }
  }
  maSoftTimers[ userTimerId ].timerMgmt.next = mTimerNill_c;
  IrqControlLib_RestoreIrqStatus(ccr);
}
  

/*****************************************************************************
* Checks whether a particular timer is being used or not.
*
* Interface assumptions:
*
* Return value:
* TRUE  - If the timer is available
* FALSE - if it is being used or invalid. 
*
* Revison history:
* date    Author  Comments
* ------  ------  --------
* 071205  LS      Created
*****************************************************************************/
 
bool_t TMR_IsTimerOff
  (
  uint8_t userTimerId /*  IN: */
  )    
{
  bool_t bStatus = FALSE;
  
  /* Handles checking the status of a timer which is not created  */
  if (userTimerId >= gSoftwareTimerCount_c )  {
    bStatus = FALSE;
  } else if ((maSoftTimers[ userTimerId ].timerMgmt.status & gUsedTimer_c)) {
    /* return false if timer is used */
    bStatus = FALSE; 
  } else { 
    /* returns true if timer is free */
    bStatus = TRUE; 
  }
  
  return bStatus;
}


/*****************************************************************************
* Checks whether a particular timer is expired or not. 
*
* Interface assumptions:
*
* Return value:
* TRUE -  if the timer is expired
* FALSE - if the timer is still running 
*
* Revison history:
* date    Author  Comments
* ------  ------  --------
* 071205  LS      Created
*****************************************************************************/

bool_t TMR_TimerStatus
  (
  uint8_t userTimerId /*  IN: */
  )
{
  bool_t bStatus = FALSE;
  
  /* Handles checking the status of a timer which is not created  */
  if (userTimerId >= gSoftwareTimerCount_c )  {
    bStatus = FALSE;
  } else if(maSoftTimers[userTimerId].timerMgmt.status & gExpiredTimer_c)  {
    maSoftTimers[userTimerId].timerMgmt.status &= ~gExpiredTimer_c;
    bStatus = TRUE;
  } else  {
    bStatus = FALSE;
  }
  
  return bStatus;
}


/*****************************************************************************
* This is used to check whether timer module is running or stopped.
* This function is used by system power management decision to go
* either light sleep or deep sleep.
*
* Interface assumptions:
*
* Return value:
* Return TRUE if no timer running else FALSE.
*
* Revison history:
* date    Author  Comments
* ------  ------  --------
* 071205  LS      Created
*****************************************************************************/

bool_t TMR_AreAllTimersOff ()
{
  return ( mTimerHead == mTimerNill_c )?TRUE:FALSE;
}

/*****************************************************************************
* This is used to update the timers after timer expires.Calls callback
* function
*
* Interface assumptions:
*
* Return value:
* None
*
* Revison history:
* date    Author  Comments
* ------  ------  --------
* 071205  LS      Created
*****************************************************************************/
void TMR_Task(
  event_t events /* IN */  
)
{
  
  uint8_t iIndex;
  uint8_t aExpiredTimers[gSoftwareTimerCount_c];
  uint8_t expiredTimerHead;
  uint8_t timerID;
  uint8_t *pSrc = gaExpiredTimers;
  uint8_t *pDst = aExpiredTimers;
  uint32_t tmpRestoreTimeout;
  uint8_t ccr;  
     
  pfTimerCallBackFunction_t pfTempCallBack;
  /* Event posted by ISR  */
  #if (gSchedulerIntegration_d == 1)
    if (events & gTMR_EventTimeOut_c)
  #else
    events = ~events;   /*to remove compiler warning*/
    if(mExpiredTimerHead)
  #endif /*gSchedulerIntegration_d*/
  {
    /*Critical section. to protect mExpiredTimerHead being modified
                                               by Timer ISR.*/
    IrqControlLib_BackupIrqStatus(ccr);
    IrqControlLib_DisableAllIrqs();
    expiredTimerHead = mExpiredTimerHead;
    mExpiredTimerHead = 0;
    for (iIndex = 0; iIndex < expiredTimerHead; iIndex++)   {
      *(pDst++) = *(pSrc++); 
    }
    IrqControlLib_RestoreIrqStatus(ccr);

    for (iIndex = 0; iIndex < expiredTimerHead; iIndex++)  
    {
      timerID = aExpiredTimers[iIndex];
      
      /*reseting timer block*/
      maSoftTimers[timerID].timerMgmt.next = mTimerNill_c; 
  	  maSoftTimers[timerID].timerMgmt.status &= (~gUsedTimer_c);

      /* Assign the callback function pointer to temp variable */
      pfTempCallBack = maSoftTimers[timerID].pCallBackFunction;
      
     
      /*  If interval timer, add the timer again in appropriate place  */  
      if(maSoftTimers[timerID].timerMgmt.timerType == gIntervalTimer_c)
      {
        tmpRestoreTimeout = (uint32_t)maSoftTimers[timerID].restoreTimeout;
        tmpRestoreTimeout <<= 2; /*Converting stored ticks to milli second
        as TMR_SoftTimerInit accepts timout in milli second*/
                
        TMR_SoftTimerInit(gIntervalTimer_c,timerID, 
        tmpRestoreTimeout,
        maSoftTimers[timerID].pCallBackFunction);
      }
      
      /*  Call the callback function  */
      pfTempCallBack((uint16_t)timerID);

    }
  }
}

/*****************************************************************************
******************************************************************************
* Private functions
******************************************************************************
*****************************************************************************/
/*  None  */

/*****************************************************************************
******************************************************************************
* Private Debug stuff
******************************************************************************
*****************************************************************************/

⌨️ 快捷键说明

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