1 | /* |
---|
2 | * |
---|
3 | * This routine initializes TIMER 2 for an MC68302. |
---|
4 | * |
---|
5 | * COPYRIGHT (c) 1989-1999. |
---|
6 | * On-Line Applications Research Corporation (OAR). |
---|
7 | * |
---|
8 | * The license and distribution terms for this file may be |
---|
9 | * found in the file LICENSE in this distribution or at |
---|
10 | * http://www.rtems.org/license/LICENSE. |
---|
11 | */ |
---|
12 | |
---|
13 | #include <rtems.h> |
---|
14 | #include <bsp.h> |
---|
15 | #include <rtems/btimer.h> |
---|
16 | #include <rtems/m68k/m68302.h> |
---|
17 | |
---|
18 | #define TMR2_VAL 0x071b /* Timer mode register |
---|
19 | * (section 3.5.2.1 in 68302 manual) |
---|
20 | * 15-8: "7" prescaler divide by 8 (x+1) |
---|
21 | * 7-6: 00 dis. intr. on capture event |
---|
22 | * 5: 0 active-low pulse |
---|
23 | * 4: 1 intr. on reaching reference |
---|
24 | * 3: 1 restart counter on reference |
---|
25 | * 2-1: 01 master clock input source |
---|
26 | * 0: 1 enable timer |
---|
27 | */ |
---|
28 | #define TRR2_VAL 2000 /* Timer reference register |
---|
29 | * (section 3.5.2.2 in 68302 manual) |
---|
30 | * 2000 ticks @ (16MHz/1)/8 = 1-ms count |
---|
31 | */ |
---|
32 | |
---|
33 | uint32_t Timer_interrupts; |
---|
34 | |
---|
35 | bool benchmark_timer_find_average_overhead; |
---|
36 | |
---|
37 | rtems_isr timerisr(void); |
---|
38 | |
---|
39 | void benchmark_timer_initialize( void ) |
---|
40 | { |
---|
41 | m302.reg.tmr2 = 0; /* disable timer */ |
---|
42 | |
---|
43 | Timer_interrupts = 0; /* clear timer ISR count */ |
---|
44 | |
---|
45 | m302.reg.trr2 = TRR2_VAL; /* set timer reference register */ |
---|
46 | m302.reg.tmr2 = TMR2_VAL; /* set timer mode register */ |
---|
47 | m302.reg.imr |= RBIT_IMR_TIMER2; /* set 68302 int-mask to allow ints */ |
---|
48 | } |
---|
49 | |
---|
50 | /* |
---|
51 | * The following controls the behavior of benchmark_timer_read(). |
---|
52 | * |
---|
53 | * FIND_AVG_OVERHEAD * instructs the routine to return the "raw" count. |
---|
54 | * |
---|
55 | * AVG_OVEREHAD is the overhead for starting and stopping the timer. It |
---|
56 | * is usually deducted from the number returned. |
---|
57 | * |
---|
58 | * LEAST_VALID is the lowest number this routine should trust. Numbers |
---|
59 | * below this are "noise" and zero is returned. |
---|
60 | */ |
---|
61 | |
---|
62 | #define AVG_OVERHEAD 0 /* It typically takes X.X microseconds */ |
---|
63 | /* (Y countdowns) to start/stop the timer. */ |
---|
64 | /* This value is in microseconds. */ |
---|
65 | #define LEAST_VALID 1 /* Don't trust a clicks value lower than this */ |
---|
66 | |
---|
67 | /* |
---|
68 | * Return timer value in 1/2-microsecond units |
---|
69 | */ |
---|
70 | uint32_t benchmark_timer_read( void ) |
---|
71 | { |
---|
72 | uint16_t clicks; |
---|
73 | uint32_t total; |
---|
74 | |
---|
75 | /* |
---|
76 | * Read the timer and see how many clicks it has been since counter |
---|
77 | * rolled over. |
---|
78 | */ |
---|
79 | |
---|
80 | clicks = m302.reg.tcn2; |
---|
81 | |
---|
82 | /* |
---|
83 | * Total is calculated by taking into account the number of timer overflow |
---|
84 | * interrupts since the timer was initialized and clicks since the last |
---|
85 | * interrupts. |
---|
86 | */ |
---|
87 | |
---|
88 | total = (Timer_interrupts * TRR2_VAL) + clicks; |
---|
89 | |
---|
90 | if ( benchmark_timer_find_average_overhead == true ) |
---|
91 | return total; /* in XXX microsecond units */ |
---|
92 | |
---|
93 | if ( total < LEAST_VALID ) |
---|
94 | return 0; /* below timer resolution */ |
---|
95 | |
---|
96 | /* |
---|
97 | * Convert 1/2-microsecond count into microseconds |
---|
98 | */ |
---|
99 | |
---|
100 | return (total - AVG_OVERHEAD) >> 1; |
---|
101 | } |
---|
102 | |
---|
103 | void benchmark_timer_disable_subtracting_average_overhead( |
---|
104 | bool find_flag |
---|
105 | ) |
---|
106 | { |
---|
107 | benchmark_timer_find_average_overhead = find_flag; |
---|
108 | } |
---|