📄 i8253timer.c
字号:
if (!sysAuxClkRunning) { tc = PIT_CLOCK / sysAuxClkTicksPerSecond; sysOutByte (PIT_CMD (PIT_BASE_ADR), 0x76); sysOutByte (PIT_CNT1 (PIT_BASE_ADR), LSB(tc)); sysOutByte (PIT_CNT1 (PIT_BASE_ADR), MSB(tc)); /* enable clock interrupt */ sysIntEnablePIC (PIT1_INT_LVL); sysAuxClkRunning = TRUE; } }/********************************************************************************* sysAuxClkRateGet - get the auxiliary clock rate** This routine returns the interrupt rate of the auxiliary clock.** RETURNS: The number of ticks per second of the auxiliary clock.** SEE ALSO: sysAuxClkEnable(), sysAuxClkRateSet()*/int sysAuxClkRateGet (void) { return (sysAuxClkTicksPerSecond); }/********************************************************************************* sysAuxClkRateSet - set the auxiliary clock rate** This routine sets the interrupt rate of the auxiliary clock. It does not* enable auxiliary clock interrupts.** RETURNS: OK, or ERROR if the tick rate is invalid or the timer cannot be set.** SEE ALSO: sysAuxClkEnable(), sysAuxClkRateGet()*/STATUS sysAuxClkRateSet ( int ticksPerSecond /* number of clock interrupts per second */ ) { if (ticksPerSecond < AUX_CLK_RATE_MIN || ticksPerSecond > AUX_CLK_RATE_MAX) return (ERROR); sysAuxClkTicksPerSecond = ticksPerSecond; if (sysAuxClkRunning) { sysAuxClkDisable (); sysAuxClkEnable (); } return (OK); }#else /*PIT1_FOR_AUX*//********************************************************************************* sysAuxClkInt - handle an auxiliary clock interrupt** This routine handles an auxiliary clock interrupt. It acknowledges the* interrupt and calls the routine installed by sysAuxClkConnect().** RETURNS: N/A*/void sysAuxClkInt (void) { int oldLevel; /* acknowledge the interrupt */ oldLevel = intLock (); /* LOCK INTERRUPT */ sysOutByte (RTC_INDEX, MC146818_STATUS_C); sysInByte (RTC_DATA); intUnlock (oldLevel); /* UNLOCK INTERRUPT */ /* call auxiliary clock service routine */ if (sysAuxClkRoutine != NULL) (*sysAuxClkRoutine) (sysAuxClkArg); }/********************************************************************************* sysAuxClkConnect - connect a routine to the auxiliary clock interrupt** This routine specifies the interrupt service routine to be called at each* auxiliary clock interrupt. It does not enable auxiliary clock interrupts.** RETURNS: OK, or ERROR if the routine cannot be connected to the interrupt.** SEE ALSO: intConnect(), sysAuxClkEnable()*/STATUS sysAuxClkConnect ( FUNCPTR routine, /* routine called at each aux clock interrupt */ int arg /* argument to auxiliary clock interrupt routine */ ) { sysAuxClkRoutine = routine; sysAuxClkArg = arg; return (OK); }/********************************************************************************* sysAuxClkDisable - turn off auxiliary clock interrupts** This routine disables auxiliary clock interrupts.** RETURNS: N/A** SEE ALSO: sysAuxClkEnable()*/void sysAuxClkDisable (void) { if (sysAuxClkRunning) { /* disable interrupts */ sysOutByte (RTC_INDEX, MC146818_STATUS_B); sysOutByte (RTC_DATA, MC146818_24); sysIntDisablePIC (RTC_INT_LVL); sysAuxClkRunning = FALSE; } }/********************************************************************************* sysAuxClkEnable - turn on auxiliary clock interrupts** This routine enables auxiliary clock interrupts.** RETURNS: N/A** SEE ALSO: sysAuxClkConnect(), sysAuxClkDisable(), sysAuxClkRateSet()*/void sysAuxClkEnable (void) { int ix; char statusA; int oldLevel; if (!sysAuxClkRunning) { /* set an interrupt rate */ for (ix = 0; ix < NELEMENTS (auxTable); ix++) { if (auxTable [ix].rate == sysAuxClkTicksPerSecond) { sysOutByte (RTC_INDEX, MC146818_STATUS_A); statusA = sysInByte (RTC_DATA) & ~MC146818_RS_BITS; sysOutByte (RTC_INDEX, MC146818_STATUS_A); sysOutByte (RTC_DATA, statusA | auxTable [ix].bits); break; } } /* start the timer */ oldLevel = intLock (); /* LOCK INTERRUPT */ sysOutByte (RTC_INDEX, MC146818_STATUS_C); /* clear Int Flags */ sysInByte (RTC_DATA); sysOutByte (RTC_INDEX, MC146818_STATUS_B); /* set PIE */ sysOutByte (RTC_DATA, MC146818_PIE | MC146818_24); sysIntEnablePIC (RTC_INT_LVL); sysAuxClkRunning = TRUE; intUnlock (oldLevel); /* UNLOCK INTERRUPT */ } }/********************************************************************************* sysAuxClkRateGet - get the auxiliary clock rate** This routine returns the interrupt rate of the auxiliary clock.** RETURNS: The number of ticks per second of the auxiliary clock.** SEE ALSO: sysAuxClkEnable(), sysAuxClkRateSet()*/int sysAuxClkRateGet (void) { return (sysAuxClkTicksPerSecond); }/********************************************************************************* sysAuxClkRateSet - set the auxiliary clock rate** This routine sets the interrupt rate of the auxiliary clock. It does not* enable auxiliary clock interrupts.** RETURNS: OK, or ERROR if the tick rate is invalid or the timer cannot be set.** SEE ALSO: sysAuxClkEnable(), sysAuxClkRateGet()*/STATUS sysAuxClkRateSet ( int ticksPerSecond /* number of clock interrupts per second */ ) { int ix; /* hold temporary variable */ BOOL match; /* hold the match status */ match = FALSE; /* initialize to false */ if (ticksPerSecond < AUX_CLK_RATE_MIN || ticksPerSecond > AUX_CLK_RATE_MAX) return (ERROR); for (ix = 0; ix < NELEMENTS (auxTable); ix++) { if (auxTable [ix].rate == ticksPerSecond) { sysAuxClkTicksPerSecond = ticksPerSecond; match = TRUE; break; } } if (!match) /* ticksPerSecond not matching the values in table */ return (ERROR); if (sysAuxClkRunning) { sysAuxClkDisable (); sysAuxClkEnable (); } return (OK); }#endif /* PIT1_FOR_AUX */#if defined(INCLUDE_TIMESTAMP_PIT2) || defined (INCLUDE_TIMESTAMP_TSC)/********************************************************************************* sysTimestampConnect - connect a user routine to the timestamp timer interrupt** This routine specifies the user interrupt routine to be called at each* timestamp timer interrupt. It does not enable the timestamp timer itself.** RETURNS: OK, or ERROR if sysTimestampInt() interrupt handler is not used.*/ STATUS sysTimestampConnect ( FUNCPTR routine, /* routine called at each timestamp timer interrupt */ int arg /* argument with which to call routine */ ) { return (ERROR); }/********************************************************************************* sysTimestampEnable - initialize and enable the timestamp timer** This routine connects the timestamp timer interrupt and initializes the* counter registers. If the timestamp timer is already running, this routine* merely resets the timer counter.** The rate of the timestamp timer should be set explicitly within the BSP,* in the sysHwInit() routine. This routine does not intialize the timer* rate. ** RETURNS: OK, or ERROR if the timestamp timer cannot be enabled.*/ STATUS sysTimestampEnable (void) { if (sysTimestampRunning) return (OK); sysTimestampRunning = TRUE; return (OK); }/********************************************************************************* sysTimestampDisable - disable the timestamp timer** This routine disables the timestamp timer. Interrupts are not disabled,* although the tick counter will not increment after the timestamp timer* is disabled, thus interrupts will no longer be generated.** RETURNS: OK, or ERROR if the timestamp timer cannot be disabled.*/ STATUS sysTimestampDisable (void) { if (sysTimestampRunning) { sysTimestampRunning = FALSE; } return (ERROR); }/********************************************************************************* sysTimestampPeriod - get the timestamp timer period** This routine returns the period of the timestamp timer in ticks.* The period, or terminal count, is the number of ticks to which the timestamp* timer will count before rolling over and restarting the counting process.** RETURNS: The period of the timestamp timer in counter ticks.*/ UINT32 sysTimestampPeriod (void) { sysTimestampPeriodValue = sysTimestampFreq () / sysClkTicksPerSecond; return (sysTimestampPeriodValue); }/********************************************************************************* sysTimestampFreq - get the timestamp timer clock frequency** This routine returns the frequency of the timer clock, in ticks per second.* The rate of the timestamp timer should be set explicitly within the BSP,* in the sysHwInit() routine.** RETURNS: The timestamp timer clock frequency, in ticks per second.*/ UINT32 sysTimestampFreq (void) {#ifdef INCLUDE_TIMESTAMP_TSC return (sysTimestampFreqValue);#else return (PIT_CLOCK * 2);#endif /* INCLUDE_TIMESTAMP_TSC */ } #ifdef INCLUDE_TIMESTAMP_TSCLOCAL void sysTimestampFreqGet (void) { if (sysTimestampTickCount == sysClkTicksPerSecond) { sysTimestampFreqValue = pentiumTscGet32 (); sysTimestampTickCount = 0; } if (sysTimestampTickCount == 0) pentiumTscReset (); sysTimestampTickCount++; }#endif /* INCLUDE_TIMESTAMP_TSC *//********************************************************************************* sysTimestamp - get the timestamp timer tick count** This routine returns the current value of the timestamp timer tick counter.* The tick count can be converted to seconds by dividing by the return of* sysTimestampFreq().** This routine should be called with interrupts locked. If interrupts are* not already locked, sysTimestampLock() should be used instead.** RETURNS: The current timestamp timer tick count.** SEE ALSO: sysTimestampLock()*/ UINT32 sysTimestamp (void) {#ifdef INCLUDE_TIMESTAMP_TSC return (pentiumTscGet32 ());#else union uc uc; sysOutByte (PIT_CMD (PIT_BASE_ADR), 0x80); uc.count08[0] = sysInByte (PIT_CNT2 (PIT_BASE_ADR)); uc.count08[1] = sysInByte (PIT_CNT2 (PIT_BASE_ADR)); return (sysTimestampPeriodValue - uc.count16);#endif /* INCLUDE_TIMESTAMP_TSC */ }/********************************************************************************* sysTimestampLock - get the timestamp timer tick count** This routine returns the current value of the timestamp timer tick counter.* The tick count can be converted to seconds by dividing by the return of* sysTimestampFreq().** This routine locks interrupts for cases where it is necessary to stop the* tick counter in order to read it, or when two independent counters must* be read. If interrupts are already locked, sysTimestamp() should be* used instead.** RETURNS: The current timestamp timer tick count.** SEE ALSO: sysTimestamp()*/ UINT32 sysTimestampLock (void) {#ifdef INCLUDE_TIMESTAMP_TSC /* pentiumTscGet32() has just one instruction "rdtsc", so locking * the interrupt is not necessary. */ return (pentiumTscGet32 ());#else int oldLevel; union uc uc; oldLevel = intLock (); sysOutByte (PIT_CMD (PIT_BASE_ADR), 0x80); uc.count08[0] = sysInByte (PIT_CNT2 (PIT_BASE_ADR)); uc.count08[1] = sysInByte (PIT_CNT2 (PIT_BASE_ADR)); intUnlock (oldLevel); return (sysTimestampPeriodValue - uc.count16);#endif /* INCLUDE_TIMESTAMP_TSC */ }#endif /* INCLUDE_TIMESTAMP_PIT2 || INCLUDE_TIMESTAMP_TSC */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -