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