📄 sh7615timer.c
字号:
void sysAuxClkDisable (void) { if (sysAuxClkRunning) { /* disable system timer interrupts */ SH7615TPU_WRITE (TPU_TIER1, 0); 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) { static BOOL connected = FALSE; if (!connected) { UINT16 tmp; /* Disable interrupts until it's safe */ SH7615TPU_WRITE (TPU_TIER1, 0); SH7615TPU_READ (TPU_TSR1, tmp); SH7615TPU_WRITE (TPU_TSR1, 0); /* Connect handler */ intConnect (IV_TPU_TGI1A, sysAuxClkInt, 0); /* Set AUX clock interrupt priority */ SH7615TPU_READ (INTC_IPRD, tmp); SH7615TPU_WRITE (INTC_IPRD, (tmp & 0xf0ff) | (INT_LVL_AUXCLK << 8)); connected = TRUE; sysAuxClkRunning = FALSE; } if (!sysAuxClkRunning) { /* select counter at F/64 , clear with TGRA */ SH7615TPU_WRITE (TPU_TCR1, TCR_DIVIDE | TCR_TGRA); /* set period */ SH7615TPU_WRITE (TPU_TGR1A, (TICK_FREQ / sysAuxClkTicksPerSecond)); /* Reset counter */ SH7615TPU_WRITE (TPU_TCNT1, 0); /* enable interrupt */ SH7615TPU_WRITE (TPU_TIER1, TIER_TGIEA); /* start counter */ SH7615TPU_SET (TPU_TSTR, TSTR_CST1); 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); }#ifdef INCLUDE_TIMESTAMP/********************************************************************************* sysTimestampInt - timestamp timer interrupt handler** This rountine handles the timestamp timer interrupt. A user routine is* called, if one was connected by sysTimestampConnect().** RETURNS: N/A** SEE ALSO: sysTimestampConnect()*/void sysTimestampInt (void) { UINT8 value; if (SH7615TPU_READ (TPU_TSR2, value), (value & TSR_TGFA) == TSR_TGFA) { /* clear the interrupt */ SH7615TPU_WRITE (TPU_TSR2, (value & ~TSR_TGFA)); /* call system clock service routine */ if (sysTimestampRunning && sysTimestampRoutine != NULL) (*sysTimestampRoutine)(sysTimestampArg); } }/********************************************************************************* 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 */ ) { sysTimestampRoutine = NULL; /* set to NULL to avoid calling old */ /* routine with new argument */ sysTimestampArg = arg; sysTimestampRoutine = routine; return OK; }/********************************************************************************* 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 hardware cannot be enabled.*/STATUS sysTimestampEnable (void) { static BOOL connected = FALSE; if (!connected) { UINT16 tmp; /* Disable interrupts until it's safe */ SH7615TPU_WRITE (TPU_TIER2, 0); SH7615TPU_READ (TPU_TSR2, tmp); SH7615TPU_WRITE (TPU_TSR2, 0); /* connect sysTimestamp to the appropriate interrupt */ if (intConnect(IV_TPU_TGI2A, sysTimestampInt, 0) != OK) return (ERROR); /* Set timestamp interrupt priority */ SH7615TPU_READ (INTC_IPRD, tmp); SH7615TPU_WRITE (INTC_IPRD, (tmp & 0xff0f) | (INT_LVL_TSTAMP << 4)); connected = TRUE; } if (!sysTimestampRunning) { /* select counter at F/64 , clear with TGRA */ SH7615TPU_WRITE (TPU_TCR2, TCR_DIVIDE | TCR_TGRA); /* set rate */ SH7615TPU_WRITE (TPU_TGR2A, 0xffff); /* Reset counter */ SH7615TPU_WRITE (TPU_TCNT2, 0); /* enable interrupt */ SH7615TPU_WRITE (TPU_TIER2, TIER_TGIEA); /* start counter */ SH7615TPU_SET (TPU_TSTR, TSTR_CST2); sysTimestampRunning = TRUE; } return (OK); }/********************************************************************************* sysTimestampDisable - disable the timestamp timer** This routine disables the timestamp timer. ** RETURNS: OK, or ERROR if timer cannot be disabled.*/STATUS sysTimestampDisable (void) { if (sysTimestampRunning) { /* Stop timer */ SH7615TPU_CLEAR (TPU_TSTR, TSTR_CST2); /* disable system timer interrupts */ SH7615TPU_WRITE (TPU_TIER2, 0); sysTimestampRunning = FALSE; } return (OK); }/********************************************************************************* 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) { return (UINT32)(0xffff); /* highest period -> freerunning */ }/********************************************************************************* sysTimestampFreq - get the timestamp timer clock frequency** This routine returns the frequency of the timer clock, in ticks per second.** RETURNS: The timestamp timer clock frequency, in ticks per second.*/UINT32 sysTimestampFreq (void) { return (UINT32)(TICK_FREQ); }/********************************************************************************* 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) { UINT16 result; SH7615TPU_READ (TPU_TCNT2, result); return (UINT32) result; }/********************************************************************************* 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) { UINT16 result; SH7615TPU_READ (TPU_TCNT2, result); return (UINT32) result; }#endif /* INCLUDE_TIMESTAMP */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -