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.6 2004/11/16 22:37:14 bernie
19 *#* Replace IPTR with iptr_t.
21 *#* Revision 1.5 2004/08/25 14:12:08 rasky
22 *#* Aggiornato il comment block dei log RCS
24 *#* Revision 1.4 2004/07/30 14:27:49 rasky
25 *#* Aggiornati alcuni file DSP56k per la nuova libreria di IRQ management
27 *#* Revision 1.3 2004/06/06 18:30:34 bernie
28 *#* Import DSP56800 changes from SC.
30 *#* Revision 1.2 2004/06/03 11:27:09 bernie
31 *#* Add dual-license information.
33 *#* Revision 1.1 2004/05/23 18:23:30 bernie
34 *#* Import drv/timer module.
38 #ifndef DRV_TIMER_DSP56K_H
39 #define DRV_TIMER_DSP56K_H
42 #include <DSP56F807.h>
47 // Calculate register pointer and irq vector from hw.h setting
48 #define REG_SYSTEM_TIMER PP_CAT(REG_TIMER_, SYSTEM_TIMER)
49 #define SYSTEM_TIMER_IRQ_VECTOR PP_CAT(IRQ_TIMER_, SYSTEM_TIMER)
51 //! Prescaler for the system timer
52 #define TIMER_PRESCALER 16
54 //! Frequency of the hardware high precision timer
55 #define TIMER_HW_HPTICKS_PER_SEC (IPBUS_FREQ / TIMER_PRESCALER)
57 //! Type of time expressed in ticks of the hardware high precision timer
58 typedef uint16_t hptime_t;
60 static void system_timer_isr(UNUSED(iptr_t, arg));
62 static void timer_hw_init(void)
66 // Clear compare flag status and enable interrupt on compare
67 REG_SYSTEM_TIMER->SCR &= ~REG_TIMER_SCR_TCF;
68 REG_SYSTEM_TIMER->SCR |= REG_TIMER_SCR_TCFIE;
70 // Calculate the compare value needed to generate an interrupt exactly
71 // TICKS_PER_SEC times each second (usually, every millisecond). Check that
72 // the calculation is accurate, otherwise there is a precision error
73 // (probably the prescaler is too big or too small).
74 compare = TIMER_HW_HPTICKS_PER_SEC / TICKS_PER_SEC;
75 ASSERT((uint32_t)compare * TICKS_PER_SEC == IPBUS_FREQ / TIMER_PRESCALER);
76 REG_SYSTEM_TIMER->CMP1 = compare;
78 // The value for reload (at initializationa and after compare is met) is zero
79 REG_SYSTEM_TIMER->LOAD = 0;
81 // Set the interrupt handler and priority
82 irq_install(SYSTEM_TIMER_IRQ_VECTOR, &system_timer_isr, NULL);
83 irq_setpriority(SYSTEM_TIMER_IRQ_VECTOR, IRQ_PRIORITY_SYSTEM_TIMER);
85 // Small preprocessor trick to generate the REG_TIMER_CTRL_PRIMARY_IPBYNN macro
86 // needed to set the prescaler
87 #define REG_CONTROL_PRESCALER PP_CAT(REG_TIMER_CTRL_PRIMARY_IPBY, TIMER_PRESCALER)
89 // Setup the counter and start counting
90 REG_SYSTEM_TIMER->CTRL =
91 REG_TIMER_CTRL_MODE_RISING | // count rising edges (normal)
92 REG_CONTROL_PRESCALER | // frequency (IPbus / TIMER_PRESCALER)
93 REG_TIMER_CTRL_LENGTH; // up to CMP1, then reload
96 INLINE void timer_hw_irq(void)
98 // Clear the overflow flag so that we are ready for another interrupt
99 REG_SYSTEM_TIMER->SCR &= ~REG_TIMER_SCR_TCF;
102 INLINE hptime_t timer_hw_hpread(void)
104 return REG_SYSTEM_TIMER->CNTR;
107 #define DEFINE_TIMER_ISR \
108 static void system_timer_isr(UNUSED(iptr_t, arg))
110 #endif /* DRV_TIMER_DSP56_H */