timer_dsp56k.h

Go to the documentation of this file.
00001 #error This code must be revised for the new timer API
00002 
00042 /*#*
00043  *#* $Log$
00044  *#* Revision 1.10  2006/07/19 12:56:26  bernie
00045  *#* Convert to new Doxygen style.
00046  *#*
00047  *#* Revision 1.9  2006/02/21 21:28:02  bernie
00048  *#* New time handling based on TIMER_TICKS_PER_SEC to support slow timers with ticks longer than 1ms.
00049  *#*
00050  *#* Revision 1.8  2005/11/04 16:20:02  bernie
00051  *#* Fix reference to README.devlib in header.
00052  *#*
00053  *#* Revision 1.7  2005/04/11 19:10:28  bernie
00054  *#* Include top-level headers from cfg/ subdir.
00055  *#*
00056  *#* Revision 1.6  2004/11/16 22:37:14  bernie
00057  *#* Replace IPTR with iptr_t.
00058  *#*
00059  *#* Revision 1.5  2004/08/25 14:12:08  rasky
00060  *#* Aggiornato il comment block dei log RCS
00061  *#*
00062  *#* Revision 1.4  2004/07/30 14:27:49  rasky
00063  *#* Aggiornati alcuni file DSP56k per la nuova libreria di IRQ management
00064  *#*
00065  *#* Revision 1.3  2004/06/06 18:30:34  bernie
00066  *#* Import DSP56800 changes from SC.
00067  *#*
00068  *#* Revision 1.2  2004/06/03 11:27:09  bernie
00069  *#* Add dual-license information.
00070  *#*
00071  *#* Revision 1.1  2004/05/23 18:23:30  bernie
00072  *#* Import drv/timer module.
00073  *#*
00074  *#*/
00075 
00076 #ifndef DRV_TIMER_DSP56K_H
00077 #define DRV_TIMER_DSP56K_H
00078 
00079 #include "timer.h"
00080 #include <DSP56F807.h>
00081 #include <cfg/compiler.h>
00082 #include <hw.h>
00083 #include <drv/irq.h>
00084 
00085 // Calculate register pointer and irq vector from hw.h setting
00086 #define REG_SYSTEM_TIMER          PP_CAT(REG_TIMER_, SYSTEM_TIMER)
00087 #define SYSTEM_TIMER_IRQ_VECTOR   PP_CAT(IRQ_TIMER_, SYSTEM_TIMER)
00088 
00090 #define TIMER_PRESCALER           16
00091 
00093 #define TIMER_HW_HPTICKS_PER_SEC           (IPBUS_FREQ / TIMER_PRESCALER)
00094 
00096 typedef uint16_t hptime_t;
00097 
00098 static void system_timer_isr(UNUSED(iptr_t, arg));
00099 
00100 static void timer_hw_init(void)
00101 {
00102     uint16_t compare;
00103 
00104     // Clear compare flag status and enable interrupt on compare
00105     REG_SYSTEM_TIMER->SCR &= ~REG_TIMER_SCR_TCF;
00106     REG_SYSTEM_TIMER->SCR |= REG_TIMER_SCR_TCFIE;
00107 
00108     // Calculate the compare value needed to generate an interrupt exactly
00109     //  TICKS_PER_SEC times each second (usually, every millisecond). Check that
00110     //  the calculation is accurate, otherwise there is a precision error
00111     // (probably the prescaler is too big or too small).
00112     compare = TIMER_HW_HPTICKS_PER_SEC / TICKS_PER_SEC;
00113     ASSERT((uint32_t)compare * TICKS_PER_SEC == IPBUS_FREQ / TIMER_PRESCALER);
00114     REG_SYSTEM_TIMER->CMP1 = compare;
00115 
00116     // The value for reload (at initializationa and after compare is met) is zero
00117     REG_SYSTEM_TIMER->LOAD = 0;
00118 
00119     // Set the interrupt handler and priority
00120     irq_install(SYSTEM_TIMER_IRQ_VECTOR, &system_timer_isr, NULL);
00121     irq_setpriority(SYSTEM_TIMER_IRQ_VECTOR, IRQ_PRIORITY_SYSTEM_TIMER);
00122 
00123     // Small preprocessor trick to generate the REG_TIMER_CTRL_PRIMARY_IPBYNN macro
00124     //  needed to set the prescaler
00125     #define REG_CONTROL_PRESCALER             PP_CAT(REG_TIMER_CTRL_PRIMARY_IPBY, TIMER_PRESCALER)
00126 
00127     // Setup the counter and start counting
00128     REG_SYSTEM_TIMER->CTRL =
00129         REG_TIMER_CTRL_MODE_RISING    |          // count rising edges (normal)
00130         REG_CONTROL_PRESCALER         |          // frequency (IPbus / TIMER_PRESCALER)
00131         REG_TIMER_CTRL_LENGTH;                   // up to CMP1, then reload
00132 }
00133 
00134 INLINE void timer_hw_irq(void)
00135 {
00136     // Clear the overflow flag so that we are ready for another interrupt
00137     REG_SYSTEM_TIMER->SCR &= ~REG_TIMER_SCR_TCF;
00138 }
00139 
00140 INLINE hptime_t timer_hw_hpread(void)
00141 {
00142     return REG_SYSTEM_TIMER->CNTR;
00143 }
00144 
00145 #define DEFINE_TIMER_ISR \
00146     static void system_timer_isr(UNUSED(iptr_t, arg))
00147 
00148 #endif /* DRV_TIMER_DSP56_H */