📄 timerdrv.c
字号:
/**************************************************
* prototypes
***************************************************/
void timer_get_counters(measure_t* );
short time_test_cascade(volatile arch_sTimerChannel * gTimerOut, volatile arch_sTimerChannel * gTimerIn);
/*******************************************************************************
*
* Function: time_test_cascade
*
* Description: test cascede timers on belomging to one groop
*
* Arguments:
* gTimerOut
* gTimerIn - arch of timers
*
*
* Returns: -1 wrong cascade timers(timers cannot connect in cascade)
* correct cascade InputSource
* Range Issues: none
*
* Special Issues: none
*
*******************************************************************************/
short time_test_cascade(volatile arch_sTimerChannel * gTimerOut, volatile arch_sTimerChannel * gTimerIn)
{
unsigned short tmpShift = (unsigned short) gTimerIn;
if( gTimerOut == gTimerIn ) return -1;
switch ( (unsigned short) gTimerOut - (unsigned short) &ArchIO.TimerA.Channel0 )
{
case 0 : /* Cascade consist of timers from groop A */
case 8 :
case 16 :
case 24 : tmpShift -= (unsigned short) &ArchIO.TimerA.Channel0;
break;
#if 0 /* B timer-groop reserved */
case 64 : /* Cascade consist of timers from groop B */
case 72 :
case 80 :
case 88 : tmpShift -= (unsigned short) &ArchIO.TimerB.Channel0;
break;
#endif
case 128 : /* Cascade consist of timers from groop C */
case 136 :
case 144 :
case 152 : tmpShift -= (unsigned short) &ArchIO.TimerC.Channel0;
break;
case 192 : /* Cascade consist of timers from groop D */
case 200 :
case 208 :
case 216 : tmpShift -= (unsigned short) &ArchIO.TimerD.Channel0;
break;
default : return -1;
}
switch ( tmpShift )
{
case 0 : return qtCounter0Output;break;
case 8 : return qtCounter1Output;break;
case 16 : return qtCounter2Output;break;
case 24 : return qtCounter3Output;break;
default : return -1;
}
return -1;
}
/*******************************************************************************
*
* Function: timer_Init_Measure
*
* Description: initialize measure library
*
* Arguments:
* _gArchQtmer1
* _gArchQtmer2 - arch of timers
*
*
* Returns: calibrate value
*
* Range Issues: none
*
* Special Issues: none
*
*******************************************************************************/
unsigned long timer_Init_Measure(volatile arch_sTimerChannel * gArchQtmer1, volatile arch_sTimerChannel * gArchQtmer2)
{
short InputSourceOut;
if ( ( gArchQtmer1 == NULL ) && ( gArchQtmer2 == NULL ) )
{ /* default timers for measure are C0,C1 */
gArchQtmer1 = &ArchIO.TimerC.Channel1;
gArchQtmer2 = &ArchIO.TimerC.Channel0;
}
InputSourceOut = time_test_cascade(gArchQtmer1, gArchQtmer2);
if ( InputSourceOut != -1 )
{
measure_t measureDesc;
static qt_sQuadState gQtimerParamIn = {
/* unsigned int OutputMode :3; 1, */ qtDeassertOnCompare,
/* unsigned int CoChannelInitialize :1; */ 0,
/* unsigned int CountDirection :1; 1, */ qtDown,
/* unsigned int CountLength :1; 1, */ qtUntilCompare,
/* unsigned int CountFrequency :1; 0, */ qtRepeatedly,
/* unsigned int SecondaryInputSource :2; 0, */ qtSISCounter0Input,
/* unsigned int InputSource :4; 8, */ qtPrescalerDiv1,
/* unsigned int Mode :3; 2, */ qtCountBothEdges,
/* unsigned int OutputEnable :1; */ 0,
/* unsigned int OutputPolarity :1; 0, */ qtNormal,
/* unsigned int :1; */
/* unsigned int :1; */
/* unsigned int OutputOnMaster :1; */ 0,
/* unsigned int Master :1; */ 0,
/* unsigned int CaptureMode :2; 2, */ qtDisabled,
/* unsigned int :1; */
/* unsigned int InputPolarity :1; 0, */ qtNormal
/* unsigned int :6; */
};
static qt_sQuadState gQtimerParamOut = {
/* unsigned int OutputMode :3; 1, */ qtDeassertOnCompare,
/* unsigned int CoChannelInitialize :1; */ 0,
/* unsigned int CountDirection :1; 1, */ qtDown,
/* unsigned int CountLength :1; 1, */ qtUntilCompare,
/* unsigned int CountFrequency :1; 0, */ qtRepeatedly,
/* unsigned int SecondaryInputSource :2; 0, */ qtSISCounter0Input,
/* unsigned int InputSource :4; */ 0,
/* unsigned int Mode :3; 7, */ qtCascadeCount,
/* unsigned int OutputEnable :1; */ 0,
/* unsigned int OutputPolarity :1; 0, */ qtNormal,
/* unsigned int :1; */
/* unsigned int :1; */
/* unsigned int OutputOnMaster :1; */ 0,
/* unsigned int Master :1; */ 0,
/* unsigned int CaptureMode :2; 2, */ qtDisabled,
/* unsigned int :1; */
/* unsigned int InputPolarity :1; 0, */ qtNormal
/* unsigned int :6; */
};
gQtimerParamOut.bits.InputSource = InputSourceOut;
/* quad timer initialize */
gArchQtmerOut = gArchQtmer1;
gArchQtmerIn = gArchQtmer2;
/* run timers */
timer_qt_writeCompareReg1( gArchQtmerOut, 0);
timer_qt_writeCompareReg1( gArchQtmerIn , 0);
timer_qt_writeCompareReg2( gArchQtmerOut, 0);
timer_qt_writeCompareReg2( gArchQtmerIn , 0);
timer_qt_writeLoadReg ( gArchQtmerOut, InitialLoadValue);
timer_qt_writeLoadReg ( gArchQtmerIn , InitialLoadValue);
timer_qt_start (gArchQtmerOut, gQtimerParamOut.value);
timer_qt_start (gArchQtmerIn , gQtimerParamIn.value);
/* get calibrate value */
timer_begin_measure ( &measureDesc );
timer_end_measure ( &measureDesc );
gCalibrate = measureDesc;
return gCalibrate;
}
return -1;
}
/*******************************************************************************
*
* Function: timer_Uninit_Measure
*
* Description: uninitialize measure library
*
* Arguments:
* none
*
* Returns: none
*
* Range Issues: none
*
* Special Issues: none
*
*******************************************************************************/
void timer_Uninit_Measure(void)
{
timer_qt_disable ( gArchQtmerOut );
timer_qt_disable ( gArchQtmerIn );
}
/*******************************************************************************
*
* Function: timer_get_counters
*
* Description: gets current value real-time timer
*
* Arguments:
* pTime - pointer to measurenment value in ticks (measure_t)
*
* Returns: none
*
* Range Issues: none
*
* Special Issues: none
*
*******************************************************************************/
void timer_get_counters(measure_t* pTime)
{
*pTime = timer_qt_readCounterReg(gArchQtmerIn);
*pTime |= (unsigned long) timer_qt_readHoldReg(gArchQtmerOut) << 16;
}
/*******************************************************************************
*
* Function: timer_begin_measure
*
* Description: starts measure procedure
*
* Arguments:
* pTime - pointer to measurenment value in ticks (measure_t)
*
* Returns: none
*
* Range Issues: none
*
* Special Issues: none
*
*******************************************************************************/
void timer_begin_measure(measure_t* pTime)
{
timer_get_counters ( pTime );
}
/*******************************************************************************
*
* Function: timer_end_measure
*
* Description: finishes measure procedure and calculate expired time
*
* Arguments:
* pTime - pointer to measurenment value in ticks (measure_t)
*
* Returns: none
*
* Range Issues: none
*
* Special Issues: none
*
*******************************************************************************/
void timer_end_measure( measure_t* pTime)
{
measure_t tEnd;
measure_t timeRez;
timer_get_counters( &tEnd );
timeRez = *pTime;
/* calculate expired time */
if( ( ( timeRez & HIGH_BITS_VALUE ) ) < ( tEnd & HIGH_BITS_VALUE ) )
{
timeRez += ( ( InitialLoadValue << 16 ) - ( tEnd & HIGH_BITS_VALUE ) + 0x10000 );
}
else timeRez -= tEnd & HIGH_BITS_VALUE;
if ( ( timeRez & LOW_BITS_VALUE ) < ( tEnd & LOW_BITS_VALUE ) )
{
/* validate */
timeRez += ( InitialLoadValue - ( tEnd & LOW_BITS_VALUE ) + 1 );
timeRez -= 0x10000;
}
else timeRez -= tEnd & LOW_BITS_VALUE;
if ( InitialLoadValue != 0xFFFF )
{
U64 tmp;
tmp.w[0] = ( ( timeRez & HIGH_BITS_VALUE ) >> 16 );
tmp.w[1] = 0;
U64mulU32(&tmp, InitialLoadValue, &tmp);
/* get real size */
timeRez = tmp.w[0] + (timeRez & LOW_BITS_VALUE);
}
/* calibrate */
if( timeRez >= gCalibrate ) timeRez -= gCalibrate;
else timeRez = 0;
*pTime = timeRez;
}
/*******************************************************************************
*
* Function: timer_resume
*
* Description: continue measure procedure. This function can be used for multiple measurenment with one descriptor
*
* Arguments:
* pTime - pointer to measurenment value in ticks (measure_t)
*
* Returns: none
*
* Range Issues: none
*
* Special Issues: none
*
*******************************************************************************/
void timer_resume(measure_t* pTime)
{
timer_get_counters( pTime );
}
/*******************************************************************************
*
* Function: timer_measure_ticks_to_us
*
* Description: convert ticks meaning to us meaaning
*
* Arguments:
* pTime - pointer to measurenment value in ticks (measure_t)
*
* Returns: meaning in us.
*
* Range Issues: none
*
* Special Issues: none
*
*******************************************************************************/
unsigned long timer_measure_ticks_to_us(measure_t* pTime)
{
/* us = tick * 10^6 / IP_BUS_FREQUENCY */
U64 us;
U64 tmp;
us.w[0] = *pTime;
us.w[1] = 0;
U64mulU32(&us, 1000000, &tmp);
U64divU16(&tmp, IP_BUS_FREQUENCY_2, &us);
U64divU16(&us, IP_BUS_FREQUENCY_1, &tmp);
return tmp.w[0];
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -