1 | /* Timer_init() |
---|
2 | * |
---|
3 | * This routine initializes a timer in efi68k's DP8570A TCP |
---|
4 | * |
---|
5 | * Input parameters: NONE |
---|
6 | * |
---|
7 | * Output parameters: NONE |
---|
8 | * |
---|
9 | * NOTE: It is important that the timer start/stop overhead be |
---|
10 | * determined when porting or modifying this code. |
---|
11 | * |
---|
12 | * COPYRIGHT (c) 1989-1998. |
---|
13 | * On-Line Applications Research Corporation (OAR). |
---|
14 | * Copyright assigned to U.S. Government, 1994. |
---|
15 | * |
---|
16 | * The license and distribution terms for this file may be |
---|
17 | * found in the file LICENSE in this distribution or at |
---|
18 | * http://www.OARcorp.com/rtems/license.html. |
---|
19 | * |
---|
20 | * $Id$ |
---|
21 | */ |
---|
22 | |
---|
23 | |
---|
24 | #include <bsp.h> |
---|
25 | |
---|
26 | rtems_boolean Timer_driver_Find_average_overhead; |
---|
27 | |
---|
28 | extern rtems_isr Clock_isr(); |
---|
29 | |
---|
30 | void Timer_initialize( void ) |
---|
31 | { |
---|
32 | /* stop counter */ |
---|
33 | *MSR = 0; |
---|
34 | *T0CR = 0; |
---|
35 | |
---|
36 | /* |
---|
37 | * Make sure isr is installed |
---|
38 | */ |
---|
39 | |
---|
40 | set_vector( Clock_isr, TCP_ISR_LEVEL+24, 1); |
---|
41 | |
---|
42 | /* clear timer ISR count */ |
---|
43 | Timer_interrupts = 0; |
---|
44 | |
---|
45 | /* load count (count down timer) */ |
---|
46 | *MSR = 0; |
---|
47 | *T0_MSB = 0xff; |
---|
48 | *T0_LSB = 0xff; |
---|
49 | |
---|
50 | /* clear old interrupts */ |
---|
51 | *MSR = T0; |
---|
52 | |
---|
53 | /* enable timer 0 interrupt */ |
---|
54 | *MSR = RS; |
---|
55 | *ICR0 |= T0E; |
---|
56 | |
---|
57 | /* |
---|
58 | TSS = 1 starts the timer (timer resets on start) |
---|
59 | M1/0 = 0/1 rate generator |
---|
60 | C2/1/0 = 0/0/0 external clock (8MHz) (1/8 usec resolution) |
---|
61 | RD = 0 read data (latchs count) |
---|
62 | CHG = 0 hold |
---|
63 | */ |
---|
64 | *MSR = 0; |
---|
65 | *T0CR = (TSS | M0); |
---|
66 | } |
---|
67 | |
---|
68 | /* |
---|
69 | * The following controls the behavior of Read_timer(). |
---|
70 | * |
---|
71 | * FIND_AVG_OVERHEAD * instructs the routine to return the "raw" count. |
---|
72 | * |
---|
73 | * AVG_OVEREHAD is the overhead for starting and stopping the timer. It |
---|
74 | * is usually deducted from the number returned. |
---|
75 | * |
---|
76 | * LEAST_VALID is the lowest number this routine should trust. Numbers |
---|
77 | * below this are "noise" and zero is returned. |
---|
78 | */ |
---|
79 | |
---|
80 | #define AVG_OVERHEAD 0 /* It typically takes X.X microseconds */ |
---|
81 | /* (Y countdowns) to start/stop the timer. */ |
---|
82 | /* This value is in microseconds. */ |
---|
83 | #define LEAST_VALID 1 /* Don't trust a clicks value lower than this */ |
---|
84 | |
---|
85 | /* |
---|
86 | * Return timer value in 1/2-microsecond units |
---|
87 | */ |
---|
88 | int Read_timer( void ) |
---|
89 | { |
---|
90 | rtems_unsigned16 clicks; |
---|
91 | rtems_unsigned32 total; |
---|
92 | rtems_unsigned32 msb, lsb; |
---|
93 | |
---|
94 | |
---|
95 | /* |
---|
96 | * Read the timer and see how many clicks it has been since counter |
---|
97 | * rolled over. |
---|
98 | */ |
---|
99 | |
---|
100 | *MSR = 0; |
---|
101 | *T0CR |= RD; |
---|
102 | /* must read MSB first */ |
---|
103 | msb = *T0_MSB; |
---|
104 | lsb = *T0_LSB; |
---|
105 | clicks = 0xffff - ((msb << 8) | lsb); |
---|
106 | |
---|
107 | /* |
---|
108 | * Total is calculated by taking into account the number of timer overflow |
---|
109 | * interrupts since the timer was initialized and clicks since the last |
---|
110 | * interrupts. |
---|
111 | */ |
---|
112 | |
---|
113 | total = (Timer_interrupts * 0x10000 + clicks + 4)/8; /* in micoseconds */ |
---|
114 | /* ^^^ round to nearest int */ |
---|
115 | |
---|
116 | if ( Timer_driver_Find_average_overhead == 1 ) |
---|
117 | return total; /* in XXX microsecond units */ |
---|
118 | |
---|
119 | if ( total < LEAST_VALID ) |
---|
120 | return 0; /* below timer resolution */ |
---|
121 | |
---|
122 | return (total - AVG_OVERHEAD); |
---|
123 | } |
---|
124 | |
---|
125 | |
---|
126 | /* |
---|
127 | * Empty function call used in loops to measure basic cost of looping |
---|
128 | * in Timing Test Suite. |
---|
129 | */ |
---|
130 | |
---|
131 | rtems_status_code Empty_function(void) |
---|
132 | { |
---|
133 | return RTEMS_SUCCESSFUL; |
---|
134 | } |
---|
135 | |
---|
136 | void Set_find_average_overhead( |
---|
137 | rtems_boolean find_flag |
---|
138 | ) |
---|
139 | { |
---|
140 | Timer_driver_Find_average_overhead = find_flag; |
---|
141 | } |
---|