1 | /* |
---|
2 | * Timer Init |
---|
3 | * |
---|
4 | * This module initializes TIMER 2 for on the MCF5272 for benchmarks. |
---|
5 | * |
---|
6 | * Copyright (C) 2000 OKTET Ltd., St.-Petersburg, Russia |
---|
7 | * Author: Victor V. Vengerov <vvv@oktet.ru> |
---|
8 | * |
---|
9 | * Based on work: |
---|
10 | * Author: |
---|
11 | * David Fiddes, D.J@fiddes.surfaid.org |
---|
12 | * http://www.calm.hw.ac.uk/davidf/coldfire/ |
---|
13 | * |
---|
14 | * COPYRIGHT (c) 1989-1998. |
---|
15 | * On-Line Applications Research Corporation (OAR). |
---|
16 | * |
---|
17 | * The license and distribution terms for this file may be |
---|
18 | * found in the file LICENSE in this distribution or at |
---|
19 | * http://www.rtems.com/license/LICENSE. |
---|
20 | */ |
---|
21 | |
---|
22 | #include <rtems.h> |
---|
23 | #include <bsp.h> |
---|
24 | #include <mcf5272/mcf5272.h> |
---|
25 | |
---|
26 | #define TRR2_VAL 65530 |
---|
27 | |
---|
28 | uint32_t Timer_interrupts; |
---|
29 | |
---|
30 | bool benchmark_timer_find_average_overhead; |
---|
31 | |
---|
32 | /* External assembler interrupt handler routine */ |
---|
33 | extern rtems_isr timerisr(rtems_vector_number vector); |
---|
34 | |
---|
35 | |
---|
36 | /* benchmark_timer_initialize -- |
---|
37 | * Initialize timer 2 for accurate time measurement. |
---|
38 | * |
---|
39 | * PARAMETERS: |
---|
40 | * none |
---|
41 | * |
---|
42 | * RETURNS: |
---|
43 | * none |
---|
44 | */ |
---|
45 | void |
---|
46 | benchmark_timer_initialize(void) |
---|
47 | { |
---|
48 | uint32_t icr; |
---|
49 | /* Catch timer2 interrupts */ |
---|
50 | set_vector(timerisr, BSP_INTVEC_TMR2, 0); |
---|
51 | |
---|
52 | /* Reset Timer */ |
---|
53 | g_timer_regs->tmr2 = MCF5272_TMR_RST; |
---|
54 | g_timer_regs->tmr2 = MCF5272_TMR_CLK_STOP; |
---|
55 | g_timer_regs->tmr2 = MCF5272_TMR_RST; |
---|
56 | g_timer_regs->tcn2 = 0; /* reset counter */ |
---|
57 | Timer_interrupts = 0; /* Clear timer ISR counter */ |
---|
58 | g_timer_regs->ter2 = MCF5272_TER_REF | MCF5272_TER_CAP; |
---|
59 | g_timer_regs->trr2 = TRR2_VAL -1 ; |
---|
60 | |
---|
61 | |
---|
62 | /* Set Timer 2 prescaler so that it counts in microseconds */ |
---|
63 | g_timer_regs->tmr2 = ( |
---|
64 | (((BSP_SYSTEM_FREQUENCY / 1000000) - 1) << MCF5272_TMR_PS_SHIFT) | |
---|
65 | MCF5272_TMR_CE_DISABLE | |
---|
66 | MCF5272_TMR_ORI | |
---|
67 | MCF5272_TMR_FRR | |
---|
68 | MCF5272_TMR_CLK_MSTR | |
---|
69 | MCF5272_TMR_RST); |
---|
70 | |
---|
71 | /* Initialize interrupts for timer2 */ |
---|
72 | icr = g_intctrl_regs->icr1; |
---|
73 | icr = icr & ~(MCF5272_ICR1_TMR2_MASK | MCF5272_ICR1_TMR2_PI); |
---|
74 | icr |= (MCF5272_ICR1_TMR2_IPL(BSP_INTLVL_TMR2) | MCF5272_ICR1_TMR2_PI); |
---|
75 | g_intctrl_regs->icr1 = icr; |
---|
76 | |
---|
77 | } |
---|
78 | |
---|
79 | /* |
---|
80 | * The following controls the behavior of benchmark_timer_read(). |
---|
81 | * |
---|
82 | * FIND_AVG_OVERHEAD * instructs the routine to return the "raw" count. |
---|
83 | * |
---|
84 | * AVG_OVEREHAD is the overhead for starting and stopping the timer. It |
---|
85 | * is usually deducted from the number returned. |
---|
86 | * |
---|
87 | * LEAST_VALID is the lowest number this routine should trust. Numbers |
---|
88 | * below this are "noise" and zero is returned. |
---|
89 | */ |
---|
90 | |
---|
91 | #define AVG_OVERHEAD 0 /* It typically takes 2.0 microseconds */ |
---|
92 | /* (Y countdowns) to start/stop the timer. */ |
---|
93 | /* This value is in microseconds. */ |
---|
94 | #define LEAST_VALID 1 /* Don't trust a clicks value lower than this */ |
---|
95 | |
---|
96 | /* benchmark_timer_read -- |
---|
97 | * Read timer value in microsecond units since timer start. |
---|
98 | * |
---|
99 | * PARAMETERS: |
---|
100 | * none |
---|
101 | * |
---|
102 | * RETURNS: |
---|
103 | * number of microseconds since timer has been started |
---|
104 | */ |
---|
105 | int |
---|
106 | benchmark_timer_read( void ) |
---|
107 | { |
---|
108 | uint16_t clicks; |
---|
109 | uint32_t total; |
---|
110 | |
---|
111 | /* |
---|
112 | * Read the timer and see how many clicks it has been since counter |
---|
113 | * rolled over. |
---|
114 | */ |
---|
115 | clicks = g_timer_regs->tcn2; |
---|
116 | |
---|
117 | /* Stop Timer... */ |
---|
118 | g_timer_regs->tmr2 = MCF5272_TMR_CLK_STOP | MCF5272_TMR_RST; |
---|
119 | |
---|
120 | /* |
---|
121 | * Total is calculated by taking into account the number of timer |
---|
122 | * overflow interrupts since the timer was initialized and clicks |
---|
123 | * since the last interrupts. |
---|
124 | */ |
---|
125 | |
---|
126 | total = (Timer_interrupts * TRR2_VAL) + clicks; |
---|
127 | |
---|
128 | if ( benchmark_timer_find_average_overhead == 1 ) |
---|
129 | return total; /* in XXX microsecond units */ |
---|
130 | |
---|
131 | if ( total < LEAST_VALID ) |
---|
132 | return 0; /* below timer resolution */ |
---|
133 | |
---|
134 | /* |
---|
135 | * Return the count in microseconds |
---|
136 | */ |
---|
137 | return (total - AVG_OVERHEAD); |
---|
138 | } |
---|
139 | |
---|
140 | /* benchmark_timer_disable_subtracting_average_overhead -- |
---|
141 | * This routine is invoked by the "Check Timer" (tmck) test in the |
---|
142 | * RTEMS Timing Test Suite. It makes the benchmark_timer_read routine not |
---|
143 | * subtract the overhead required to initialize and read the benchmark |
---|
144 | * timer. |
---|
145 | * |
---|
146 | * PARAMETERS: |
---|
147 | * find_flag - bool flag, true if overhead must not be subtracted. |
---|
148 | * |
---|
149 | * RETURNS: |
---|
150 | * none |
---|
151 | */ |
---|
152 | void |
---|
153 | benchmark_timer_disable_subtracting_average_overhead(bool find_flag) |
---|
154 | { |
---|
155 | benchmark_timer_find_average_overhead = find_flag; |
---|
156 | } |
---|