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