1 | /* Timer_init() |
---|
2 | * |
---|
3 | * This routine initializes the MC68230 timer on the Motorola IDP board. |
---|
4 | * |
---|
5 | * Input parameters: NONE |
---|
6 | * |
---|
7 | * Output parameters: NONE |
---|
8 | * |
---|
9 | * NOTE: This routine will not work if the optimizer is enabled |
---|
10 | * for some compilers. The multiple writes to the MC68230 |
---|
11 | * may be optimized away. |
---|
12 | * |
---|
13 | * It is important that the timer start/stop overhead be |
---|
14 | * determined when porting or modifying this code. |
---|
15 | * |
---|
16 | * Code Modified for the MC68230 by Doug McBride, Colorado Space Grant College |
---|
17 | * |
---|
18 | * COPYRIGHT (c) 1989, 1990, 1991, 1992, 1993, 1994. |
---|
19 | * On-Line Applications Research Corporation (OAR). |
---|
20 | * All rights assigned to U.S. Government, 1994. |
---|
21 | * |
---|
22 | * This material may be reproduced by or for the U.S. Government pursuant |
---|
23 | * to the copyright license under the clause at DFARS 252.227-7013. This |
---|
24 | * notice must appear in all copies of this file and its derivatives. |
---|
25 | * |
---|
26 | * $Id$ |
---|
27 | */ |
---|
28 | |
---|
29 | |
---|
30 | #include <rtems.h> |
---|
31 | #include <bsp.h> |
---|
32 | #include <mc68230.h> |
---|
33 | |
---|
34 | #define TIMER_VECTOR 0x4D |
---|
35 | |
---|
36 | int Ttimer_val; |
---|
37 | rtems_boolean Timer_driver_Find_average_overhead; |
---|
38 | |
---|
39 | rtems_isr timerisr(); |
---|
40 | |
---|
41 | void Timer_initialize() |
---|
42 | { |
---|
43 | (void) set_vector( timerisr, TIMER_VECTOR, 0 ); /* install ISR */ |
---|
44 | |
---|
45 | Ttimer_val = 0; /* clear timer ISR count */ |
---|
46 | |
---|
47 | /* some PI/T initialization stuff here */ |
---|
48 | /* Set up the interrupt vector on the MC68230 chip: |
---|
49 | TIVR = TIMER_VECTOR; */ |
---|
50 | MC68230_WRITE (TIVR, TIMER_VECTOR); |
---|
51 | |
---|
52 | /* Set CPRH through CPRL to maximum count to reduce interrupt overhead |
---|
53 | CPRH = 0xFF; |
---|
54 | CPRM = 0xFF; |
---|
55 | CPRL = 0xFF; */ |
---|
56 | MC68230_WRITE (CPRH, 0xFF); |
---|
57 | MC68230_WRITE (CPRM, 0xFF); |
---|
58 | MC68230_WRITE (CPRL, 0xFF); |
---|
59 | |
---|
60 | /* Enable timer and use it as an external periodic interrupt generator |
---|
61 | TCR = 0xA1; */ |
---|
62 | MC68230_WRITE (TCR, 0xA1); |
---|
63 | |
---|
64 | } |
---|
65 | |
---|
66 | #define AVG_OVERHEAD 9 /* may not be right -- do this later */ |
---|
67 | #define LEAST_VALID 10 /* Don't trust a value lower than this */ |
---|
68 | |
---|
69 | int Read_timer() |
---|
70 | { |
---|
71 | rtems_unsigned8 data; |
---|
72 | rtems_unsigned8 msb, osb, lsb; |
---|
73 | rtems_unsigned32 remaining, total; |
---|
74 | |
---|
75 | /* Disable timer so that timer can be read |
---|
76 | data = TCR; |
---|
77 | TCR = (data & 0xFE); */ |
---|
78 | MC68230_READ (TCR, data); |
---|
79 | MC68230_WRITE (TCR, (data & 0xFE)); |
---|
80 | |
---|
81 | /* Read the counter value |
---|
82 | msb = CNTRH; |
---|
83 | osb = CNTRM; |
---|
84 | lsb = CNTRL; */ |
---|
85 | MC68230_READ (CNTRH, msb); |
---|
86 | MC68230_READ (CNTRM, osb); |
---|
87 | MC68230_READ (CNTRL, lsb); |
---|
88 | |
---|
89 | /* Calculate the time so far */ |
---|
90 | remaining = 0x1000000 - ((msb << 16) + (osb << 8) + lsb); |
---|
91 | total = (Ttimer_val * 0x1000000) + remaining; |
---|
92 | |
---|
93 | /* Enable timer so that timer can continue |
---|
94 | TCR = 0xA1; */ |
---|
95 | MC68230_WRITE (TCR, 0xA1); |
---|
96 | |
---|
97 | /* do not restore old vector */ |
---|
98 | if ( Timer_driver_Find_average_overhead == 1 ) |
---|
99 | return total; /* in countdown units */ |
---|
100 | |
---|
101 | if ( total < LEAST_VALID ) |
---|
102 | return 0; /* below timer resolution */ |
---|
103 | |
---|
104 | /* Clocked at 6.5 Mhz */ |
---|
105 | /* Avoid floating point problems, be lazy, and return the total minus |
---|
106 | the average overhead */ |
---|
107 | return (total - AVG_OVERHEAD); |
---|
108 | } |
---|
109 | |
---|
110 | rtems_status_code Empty_function( void ) |
---|
111 | { |
---|
112 | return RTEMS_SUCCESSFUL; |
---|
113 | } |
---|
114 | |
---|
115 | void Set_find_average_overhead( |
---|
116 | rtems_boolean find_flag |
---|
117 | ) |
---|
118 | { |
---|
119 | Timer_driver_Find_average_overhead = find_flag; |
---|
120 | } |
---|