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