4 * Copyright 2004 Giovanni Bajo
5 * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
6 * This file is part of DevLib - See devlib/README for information.
11 * \author Giovanni Bajo <rasky@develer.com>
13 * \brief Driver module for DSP56K
18 * Revision 1.2 2004/06/03 11:27:09 bernie
19 * Add dual-license information.
21 * Revision 1.1 2004/05/23 18:23:30 bernie
22 * Import drv/timer module.
26 #ifndef DRV_TIMER_DSP56K_H
27 #define DRV_TIMER_DSP56K_H
29 #include <DSP56F807.h>
32 //! The system timer for events is currently TMRA0. We prefer A/B over C/D because
33 // A/B share the pin with the quadrature decoder module, and we do not need
34 // pins for our system timer.
35 // If you want to change this setting, you need also to modify the IRQ vector table.
36 #define REG_SYSTEM_TIMER (REG_TIMER_A + 0)
37 #define SYSTEM_TIMER_IRQ_VECTOR 42 /* TMRA0 */
39 #define TIMER_PRESCALER 16
41 //! Frequency of the hardware high precision timer
42 #define TIMER_HW_HPTICKS_PER_SEC (IPBUS_FREQ / TIMER_PRESCALER)
44 //! Type of time expressed in ticks of the hardware high precision timer
45 typedef uint16_t hptime_t;
47 static void timer_hw_init(void)
51 // Clear compare flag status and enable interrupt on compare
52 REG_SYSTEM_TIMER->SCR &= ~REG_TIMER_SCR_TCF;
53 REG_SYSTEM_TIMER->SCR |= REG_TIMER_SCR_TCFIE;
55 // Calculate the compare value needed to generate an interrupt exactly
56 // TICKS_PER_SEC times each second (usually, every millisecond). Check that
57 // the calculation is accurate, otherwise there is a precision error
58 // (probably the prescaler is too big or too small).
59 compare = TIMER_HW_HPTICKS_PER_SEC / TICKS_PER_SEC;
60 ASSERT((uint32_t)compare * TICKS_PER_SEC == IPBUS_FREQ / TIMER_PRESCALER);
61 REG_SYSTEM_TIMER->CMP1 = compare;
63 // The value for reload (at initializationa and after compare is met) is zero
64 REG_SYSTEM_TIMER->LOAD = 0;
66 // Set the interrupt priority
67 irq_setpriority(SYSTEM_TIMER_IRQ_VECTOR, IRQ_PRIORITY_SYSTEM_TIMER);
69 // Small preprocessor trick to generate the REG_TIMER_CTRL_PRIMARY_IPBYNN macro
70 // needed to set the prescaler
71 #define PP_CAT2(x,y) x ## y
72 #define PP_CAT(x,y) PP_CAT2(x,y)
73 #define REG_CONTROL_PRESCALER PP_CAT(REG_TIMER_CTRL_PRIMARY_IPBY, TIMER_PRESCALER)
75 // Setup the counter and start counting
76 REG_SYSTEM_TIMER->CTRL =
77 REG_TIMER_CTRL_MODE_RISING | // count rising edges (normal)
78 REG_CONTROL_PRESCALER | // frequency (IPbus / TIMER_PRESCALER)
79 REG_TIMER_CTRL_LENGTH; // up to CMP1, then reload
82 INLINE void timer_hw_irq(void)
84 // Clear the overflow flag so that we are ready for another interrupt
85 REG_SYSTEM_TIMER->SCR &= ~REG_TIMER_SCR_TCF;
88 INLINE hptime_t timer_hw_hpread(void)
90 return REG_SYSTEM_TIMER->CNTR;
93 void system_timer_isr(void);
95 #define DEFINE_TIMER_ISR \
96 void system_timer_isr(void)
98 #endif /* DRV_TIMER_DSP56_H */