New time handling based on TIMER_TICKS_PER_SEC to support slow timers with ticks...
[bertos.git] / drv / timer_dsp56k.h
1 #error This code must be revised for the new timer API
2 /*!
3  * \file
4  * <!--
5  * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
6  * Copyright 2004 Giovanni Bajo
7  * This file is part of DevLib - See README.devlib for information.
8  * -->
9  *
10  * \version $Id$
11  *
12  * \author Giovanni Bajo <rasky@develer.com>
13  *
14  * \brief Driver module for DSP56K
15  */
16
17 /*#*
18  *#* $Log$
19  *#* Revision 1.9  2006/02/21 21:28:02  bernie
20  *#* New time handling based on TIMER_TICKS_PER_SEC to support slow timers with ticks longer than 1ms.
21  *#*
22  *#* Revision 1.8  2005/11/04 16:20:02  bernie
23  *#* Fix reference to README.devlib in header.
24  *#*
25  *#* Revision 1.7  2005/04/11 19:10:28  bernie
26  *#* Include top-level headers from cfg/ subdir.
27  *#*
28  *#* Revision 1.6  2004/11/16 22:37:14  bernie
29  *#* Replace IPTR with iptr_t.
30  *#*
31  *#* Revision 1.5  2004/08/25 14:12:08  rasky
32  *#* Aggiornato il comment block dei log RCS
33  *#*
34  *#* Revision 1.4  2004/07/30 14:27:49  rasky
35  *#* Aggiornati alcuni file DSP56k per la nuova libreria di IRQ management
36  *#*
37  *#* Revision 1.3  2004/06/06 18:30:34  bernie
38  *#* Import DSP56800 changes from SC.
39  *#*
40  *#* Revision 1.2  2004/06/03 11:27:09  bernie
41  *#* Add dual-license information.
42  *#*
43  *#* Revision 1.1  2004/05/23 18:23:30  bernie
44  *#* Import drv/timer module.
45  *#*
46  *#*/
47
48 #ifndef DRV_TIMER_DSP56K_H
49 #define DRV_TIMER_DSP56K_H
50
51 #include "timer.h"
52 #include <DSP56F807.h>
53 #include <cfg/compiler.h>
54 #include <hw.h>
55 #include <drv/irq.h>
56
57 // Calculate register pointer and irq vector from hw.h setting
58 #define REG_SYSTEM_TIMER          PP_CAT(REG_TIMER_, SYSTEM_TIMER)
59 #define SYSTEM_TIMER_IRQ_VECTOR   PP_CAT(IRQ_TIMER_, SYSTEM_TIMER)
60
61 //! Prescaler for the system timer
62 #define TIMER_PRESCALER           16
63
64 //! Frequency of the hardware high precision timer
65 #define TIMER_HW_HPTICKS_PER_SEC           (IPBUS_FREQ / TIMER_PRESCALER)
66
67 //! Type of time expressed in ticks of the hardware high precision timer
68 typedef uint16_t hptime_t;
69
70 static void system_timer_isr(UNUSED(iptr_t, arg));
71
72 static void timer_hw_init(void)
73 {
74         uint16_t compare;
75
76         // Clear compare flag status and enable interrupt on compare
77         REG_SYSTEM_TIMER->SCR &= ~REG_TIMER_SCR_TCF;
78         REG_SYSTEM_TIMER->SCR |= REG_TIMER_SCR_TCFIE;
79
80         // Calculate the compare value needed to generate an interrupt exactly
81         //  TICKS_PER_SEC times each second (usually, every millisecond). Check that
82         //  the calculation is accurate, otherwise there is a precision error
83         // (probably the prescaler is too big or too small).
84         compare = TIMER_HW_HPTICKS_PER_SEC / TICKS_PER_SEC;
85         ASSERT((uint32_t)compare * TICKS_PER_SEC == IPBUS_FREQ / TIMER_PRESCALER);
86         REG_SYSTEM_TIMER->CMP1 = compare;
87
88         // The value for reload (at initializationa and after compare is met) is zero
89         REG_SYSTEM_TIMER->LOAD = 0;
90
91         // Set the interrupt handler and priority
92         irq_install(SYSTEM_TIMER_IRQ_VECTOR, &system_timer_isr, NULL);
93         irq_setpriority(SYSTEM_TIMER_IRQ_VECTOR, IRQ_PRIORITY_SYSTEM_TIMER);
94
95         // Small preprocessor trick to generate the REG_TIMER_CTRL_PRIMARY_IPBYNN macro
96         //  needed to set the prescaler
97         #define REG_CONTROL_PRESCALER             PP_CAT(REG_TIMER_CTRL_PRIMARY_IPBY, TIMER_PRESCALER)
98
99         // Setup the counter and start counting
100         REG_SYSTEM_TIMER->CTRL =
101                 REG_TIMER_CTRL_MODE_RISING    |          // count rising edges (normal)
102                 REG_CONTROL_PRESCALER         |          // frequency (IPbus / TIMER_PRESCALER)
103                 REG_TIMER_CTRL_LENGTH;                   // up to CMP1, then reload
104 }
105
106 INLINE void timer_hw_irq(void)
107 {
108         // Clear the overflow flag so that we are ready for another interrupt
109         REG_SYSTEM_TIMER->SCR &= ~REG_TIMER_SCR_TCF;
110 }
111
112 INLINE hptime_t timer_hw_hpread(void)
113 {
114         return REG_SYSTEM_TIMER->CNTR;
115 }
116
117 #define DEFINE_TIMER_ISR \
118         static void system_timer_isr(UNUSED(iptr_t, arg))
119
120 #endif /* DRV_TIMER_DSP56_H */