1 | /* |
---|
2 | * Cogent CSB337 Timer driver |
---|
3 | * |
---|
4 | * This uses timer 0 for timing measurments. |
---|
5 | * |
---|
6 | * Copyright (c) 2004 by Jay Monkman <jtm@lopingdog.com> |
---|
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.com/license/LICENSE. |
---|
11 | * |
---|
12 | * Notes: |
---|
13 | * This file manages the benchmark timer used by the RTEMS Timing Test |
---|
14 | * Suite. Each measured time period is demarcated by calls to |
---|
15 | * benchmark_timer_initialize() and benchmark_timer_read(). benchmark_timer_read() usually returns |
---|
16 | * the number of microseconds since benchmark_timer_initialize() exitted. |
---|
17 | * |
---|
18 | * It is important that the timer start/stop overhead be determined |
---|
19 | * when porting or modifying this code. |
---|
20 | * |
---|
21 | * $Id$ |
---|
22 | */ |
---|
23 | |
---|
24 | #include <rtems.h> |
---|
25 | #include <bsp.h> |
---|
26 | #include <at91rm9200.h> |
---|
27 | #include <at91rm9200_pmc.h> |
---|
28 | |
---|
29 | uint16_t tstart; |
---|
30 | bool benchmark_timer_find_average_overhead; |
---|
31 | uint32_t tick_time; |
---|
32 | /* |
---|
33 | * Set up TC0 - |
---|
34 | * timer_clock2 (MCK/8) |
---|
35 | * capture mode - this shouldn't matter |
---|
36 | */ |
---|
37 | void benchmark_timer_initialize( void ) |
---|
38 | { |
---|
39 | uint32_t tmr_freq; |
---|
40 | |
---|
41 | /* since we are using timer_clock2, divide mck by 8 */ |
---|
42 | tmr_freq = at91rm9200_get_mck() / 8; |
---|
43 | |
---|
44 | TC_TC0_REG(TC_CMR) = TC_CMR_TCCLKS(1); /* timer_clock2 */ |
---|
45 | TC_TC0_REG(TC_CCR) = (TC_CCR_CLKEN /* enable the counter */ |
---|
46 | | TC_CCR_SWTRG); /* start it up */ |
---|
47 | |
---|
48 | /* tick time in nanoseconds */ |
---|
49 | tick_time = 1000000000/tmr_freq; |
---|
50 | |
---|
51 | } |
---|
52 | |
---|
53 | /* |
---|
54 | * The following controls the behavior of benchmark_timer_read(). |
---|
55 | * |
---|
56 | * AVG_OVEREHAD is the overhead for starting and stopping the timer. It |
---|
57 | * is usually deducted from the number returned. |
---|
58 | * |
---|
59 | * LEAST_VALID is the lowest number this routine should trust. Numbers |
---|
60 | * below this are "noise" and zero is returned. |
---|
61 | */ |
---|
62 | |
---|
63 | #define AVG_OVERHEAD 0 /* It typically takes X.X microseconds */ |
---|
64 | /* (Y countdowns) to start/stop the timer. */ |
---|
65 | /* This value is in microseconds. */ |
---|
66 | #define LEAST_VALID 1 /* Don't trust a clicks value lower than this */ |
---|
67 | |
---|
68 | int benchmark_timer_read( void ) |
---|
69 | { |
---|
70 | uint16_t t; |
---|
71 | uint32_t total; |
---|
72 | t = TC_TC0_REG(TC_CV); |
---|
73 | |
---|
74 | /* |
---|
75 | * Total is calculated by taking into account the number of timer overflow |
---|
76 | * interrupts since the timer was initialized and clicks since the last |
---|
77 | * interrupts. |
---|
78 | */ |
---|
79 | |
---|
80 | total = t * tick_time; |
---|
81 | |
---|
82 | if ( benchmark_timer_find_average_overhead == 1 ) |
---|
83 | return total; /* in nanosecond units */ |
---|
84 | else { |
---|
85 | if ( total < LEAST_VALID ) |
---|
86 | return 0; /* below timer resolution */ |
---|
87 | /* |
---|
88 | * Somehow convert total into microseconds |
---|
89 | */ |
---|
90 | return (total - AVG_OVERHEAD); |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | void benchmark_timer_disable_subtracting_average_overhead(bool find_flag) |
---|
95 | { |
---|
96 | benchmark_timer_find_average_overhead = find_flag; |
---|
97 | } |
---|
98 | |
---|