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

📄 timer.c

📁 FREESCALE的基于802.15.4无线通讯原代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************************
* This module is used to meet the timer requirement for BeeStack project.
*
* BUILD OPTIONS:
* gInterruptDrivenTimers_d - Uses interrupts to run the software timer.
* gPolledTimers_d - Does not use interrupts but required to call the TMR_Main
*                at regular interval.
* gSoftwareTimerCount_c - Specify the number of software timer required.
*                (in TMR_Interface.h)
*  
* Author(s):  Lakshmi (LS)
*             
* (c) Copyright 2005, Freescale, Inc. All rights reserved.
*
* Freescale Semiconductor Confidential Proprietary
*
* No part of this document must be reproduced in any form - including copied,
* transcribed, printed or by any electronic means - without specific written
* permission from Freescale Semiconductor Danmark A/S.
*
* $Date: 9/14/06 12:18p $
* $Author: Amarjeet $
* $Revision: 110 $
* $Workfile: Timer.c $
*****************************************************************************/

#include "IrqControlLib.h"
#include "Timer.h"
#include "TimerHAL.h"
#include "TMR_Interface.h"
#include "TS_Interface.h"

/*****************************************************************************
******************************************************************************
* Private macros
******************************************************************************
*****************************************************************************/
/*  None  */

/*****************************************************************************
******************************************************************************
* Private prototypes
******************************************************************************
*****************************************************************************/
/*  None  */

/*****************************************************************************
******************************************************************************
* Private type definitions
******************************************************************************
*****************************************************************************/
typedef void ( *pfTimerCallBackFunction_t )( uint8_t timerID);

/*****************************************************************************
******************************************************************************
* Private memory declarations
******************************************************************************
*****************************************************************************/
TimerRecord_t maSoftTimers[gSoftwareTimerCount_c]; /*  Array of timers */
uint8_t mTimerHead = mTimerNill_c;  /*  Holds the least timeout timerID */
read16bitReg_t  mRead16bitReg; /*  used to access 16 bit register  */ 

/*****************************************************************************
******************************************************************************
* Public functions
******************************************************************************
*****************************************************************************/

/*****************************************************************************
* Default timeout handler.This function does nothing. Used for initializing 
* software timeout hander in TMR_Init.
*
* Interface assumptions:
*
* Return value:
* None
*
* Revison history:
* date    Author  Comments
* ------  ------  --------
* 071205  LS      Created
*****************************************************************************/
void TMR_DefaultTimeoutHandler
  ( 
    uint8_t timerId 
  )
{
  timerId |= TRUE; /*to avoid compiler warning*/
  return;
}

/*****************************************************************************
* Reads the  Free running counter, calculates the count to be loaded to
* compare register
*
* Interface assumptions:
*
* Return value:
* None
*
* Revison history:
* date    Author  Comments
* ------  ------  --------
* 071205  LS      Created
*****************************************************************************/

void TMRCalculateAndLoadNextTimeOutCompareValue
  (
  uint8_t userTimerId   /*  IN: */
  )
{
  uint16_t freeRunningCounter;
  uint16_t timeout = maSoftTimers[userTimerId].timeout;
  
  /* Read Free running counter  */  
  ReadTpmCntRegister( freeRunningCounter );
  
  /*  compare register = free running counter + timeout  */
  if (timeout)  {
    freeRunningCounter += timeout;
    maSoftTimers[mTimerHead].timeout = 0;
  }
  /*  compare register = free running counter + latency(1)  */    
  else  if (maSoftTimers[mTimerHead].timerMgmt.status & gUsedTimer_c) {
    freeRunningCounter +=1;
  }
    
  TMRInitCompareRegister( freeRunningCounter ); /* Load the value to the
                                                   Compare Register  */
}

/*****************************************************************************
* It adds the timer to the timer list at appropriate place.Sets the timer. 
*
* Interface assumptions:
*
* Return value:
* None
*
* Revison history:
* date    Author  Comments
* ------  ------  --------
* 071205  LS      Created
*****************************************************************************/

void TMR_SoftTimerInit
  (
  uint8_t timerType,                  /*  IN: Normal of Interval  */                      
  uint8_t userTimerId,                /*  IN: */
  timer_t timerTickInMiliSec,         /*  IN: */
  void (*pTimeoutHandler)( uint8_t )  /*  IN: callback function */   
  ) 
{
  uint8_t   current;
  uint8_t   previous;
  uint16_t  count;
  uint16_t  freeRuningCountValue;
  uint16_t  cmpRegValue;
  uint16_t  firstTimerExpireTime; /* Stores elapsed time  */
  uint8_t   ccr;
  
  /* Handles start of a timer which is not created  */
  if (userTimerId >= gSoftwareTimerCount_c )
    return;
  
  /*  Handles overwriting of timer */
  if (maSoftTimers[ userTimerId ].timerMgmt.status & gUsedTimer_c) {
    TMR_StopTimer(userTimerId);
  }
  
  /* assign timer structure members */ 
  maSoftTimers[ userTimerId ].pCallBackFunction = pTimeoutHandler;
  maSoftTimers[ userTimerId ].timerMgmt.timerType = timerType;
  maSoftTimers[ userTimerId ].timerMgmt.status |= gUsedTimer_c; 
  
  /*  Handles if timer is started for less than 4 millisec  */
  if(timerTickInMiliSec < 4)  { 
   
     timerTickInMiliSec = 4;
    
  }  
    /* Convert the time given in milli sec to timer ticks */        
    count = (uint16_t)TMR_ConversionOfMilliSecToTicks (timerTickInMiliSec);
    maSoftTimers[ userTimerId ].restoreTimeout = count;
  

  IrqControlLib_BackupIrqStatus(ccr);/*To protect from re entrant*/
  IrqControlLib_DisableAllIrqs();
    
  /* Read the free running counter */  
  ReadTpmCntRegister( freeRuningCountValue );

  /* If timer is not the first one  */
  if ( mTimerHead != mTimerNill_c)  {
    /* Reads the free running register and subtract the value from the value 
    stored in the  Compare register */ 
    TPM1C0VReadValue( cmpRegValue );
      
    /* Get the Elapsed time for the first timer before adding current timer */
    firstTimerExpireTime =  cmpRegValue - freeRuningCountValue;  	          
  
    /*  Current timer is less than mTimerHead  */
    if(count < firstTimerExpireTime )  {
      maSoftTimers[userTimerId].timeout = count;/* assign the timeout */  
      maSoftTimers[userTimerId ].timerMgmt.next = mTimerHead;
      /* reduce the timeout of the first timer by the new timers timeout  */
      maSoftTimers[ mTimerHead ].timeout = firstTimerExpireTime - count;
      /* Make the new timer as the mTimerHead */ 
      mTimerHead = userTimerId; 
      TMRCalculateAndLoadNextTimeOutCompareValue(mTimerHead);/*Load count*/
    }
      
    /*  find out the correct position to insert for the timer  */    
    else {
        current = mTimerHead;
        previous = mTimerNill_c;
        count -= (firstTimerExpireTime-1);/*Subtracts first timer timeout*/
        
      /*  Traverse to reach the correct position  */
       while( maSoftTimers[ current ].timeout <= count)  {
        if ( current == mTimerNill_c) { /*  Last timer  */
          break;
        }
        count -= maSoftTimers[ current ].timeout; /*  Reduce the timeout by
                  previous timer timeout  */ 
        previous = current;
        current = maSoftTimers[ current ].timerMgmt.next; 
      }
     
      maSoftTimers[userTimerId].timeout = count;
      /* timer has to be inserted last but one,subtract next timer timeout */
      if (current != mTimerNill_c)  {
        maSoftTimers[current].timeout -= count; 
      }
      maSoftTimers[ userTimerId ].timerMgmt.next =  current;
      maSoftTimers[ previous ].timerMgmt.next = userTimerId;
      
    }
  }
  /*  if it is the first timer  */
  else  {
    maSoftTimers[userTimerId].timeout = count;
    mTimerHead = userTimerId; 
    TMRCalculateAndLoadNextTimeOutCompareValue(userTimerId);/*load the count*/
    EnterSwTimerMode();
  }
  IrqControlLib_RestoreIrqStatus(ccr);
}


/*****************************************************************************
* Removes the timer from the timer list 
*
* Interface assumptions:
*
* Return value:
* None
*
* Revison history:
* date    Author  Comments
* ------  ------  --------
* 071205  LS      Created
*****************************************************************************/

⌨️ 快捷键说明

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