source: rtems/c/src/lib/libcpu/m68k/mcf5272/timer/timer.c @ cfaa366

4.115
Last change on this file since cfaa366 was cfaa366, checked in by Joel Sherrill <joel.sherrill@…>, on 05/03/12 at 17:55:58

General - Remove extraneous blank line in license message

Many files had an extra blank line in the license text
found in the file header. This patch removes that line.

The script that did this also turned off execute permission
when it was turned on incorrectly.

  • Property mode set to 100644
File size: 4.5 KB
Line 
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 *  timer.c,v 1.1 2001/10/26 19:32:40 joel Exp
22 */
23
24#include <rtems.h>
25#include <bsp.h>
26#include <mcf5272/mcf5272.h>
27
28#define TRR2_VAL 65530
29
30uint32_t Timer_interrupts;
31
32bool benchmark_timer_find_average_overhead;
33
34/* External assembler interrupt handler routine */
35extern rtems_isr timerisr(rtems_vector_number vector);
36
37
38/* benchmark_timer_initialize --
39 *     Initialize timer 2 for accurate time measurement.
40 *
41 * PARAMETERS:
42 *     none
43 *
44 * RETURNS:
45 *     none
46 */
47void
48benchmark_timer_initialize(void)
49{
50    uint32_t icr;
51    /* Catch timer2 interrupts */
52    set_vector(timerisr, BSP_INTVEC_TMR2, 0);
53
54    /* Reset Timer */
55    g_timer_regs->tmr2 = MCF5272_TMR_RST;
56    g_timer_regs->tmr2 = MCF5272_TMR_CLK_STOP;
57    g_timer_regs->tmr2 = MCF5272_TMR_RST;
58    g_timer_regs->tcn2 = 0;  /* reset counter */
59    Timer_interrupts   = 0;  /* Clear timer ISR counter */
60    g_timer_regs->ter2 = MCF5272_TER_REF | MCF5272_TER_CAP;
61    g_timer_regs->trr2 = TRR2_VAL -1 ;
62
63
64    /* Set Timer 2 prescaler so that it counts in microseconds */
65    g_timer_regs->tmr2 = (
66        (((BSP_SYSTEM_FREQUENCY / 1000000) - 1) << MCF5272_TMR_PS_SHIFT) |
67        MCF5272_TMR_CE_DISABLE                                           |
68        MCF5272_TMR_ORI                                                  |
69        MCF5272_TMR_FRR                                                  |
70        MCF5272_TMR_CLK_MSTR                                             |
71        MCF5272_TMR_RST);
72
73    /* Initialize interrupts for timer2 */
74    icr = g_intctrl_regs->icr1;
75    icr = icr & ~(MCF5272_ICR1_TMR2_MASK | MCF5272_ICR1_TMR2_PI);
76    icr |= (MCF5272_ICR1_TMR2_IPL(BSP_INTLVL_TMR2) | MCF5272_ICR1_TMR2_PI);
77    g_intctrl_regs->icr1 = icr;
78
79}
80
81/*
82 *  The following controls the behavior of benchmark_timer_read().
83 *
84 *  FIND_AVG_OVERHEAD *  instructs the routine to return the "raw" count.
85 *
86 *  AVG_OVEREHAD is the overhead for starting and stopping the timer.  It
87 *  is usually deducted from the number returned.
88 *
89 *  LEAST_VALID is the lowest number this routine should trust.  Numbers
90 *  below this are "noise" and zero is returned.
91 */
92
93#define AVG_OVERHEAD      0  /* It typically takes 2.0 microseconds */
94                             /* (Y countdowns) to start/stop the timer. */
95                             /* This value is in microseconds. */
96#define LEAST_VALID       1  /* Don't trust a clicks value lower than this */
97
98/* benchmark_timer_read --
99 *     Read timer value in microsecond units since timer start.
100 *
101 * PARAMETERS:
102 *     none
103 *
104 * RETURNS:
105 *     number of microseconds since timer has been started
106 */
107int
108benchmark_timer_read( void )
109{
110    uint16_t clicks;
111    uint32_t total;
112
113    /*
114     *  Read the timer and see how many clicks it has been since counter
115     *  rolled over.
116     */
117    clicks = g_timer_regs->tcn2;
118
119    /* Stop Timer... */
120    g_timer_regs->tmr2 = MCF5272_TMR_CLK_STOP | MCF5272_TMR_RST;
121
122    /*
123     *  Total is calculated by taking into account the number of timer
124     *  overflow interrupts since the timer was initialized and clicks
125     *  since the last interrupts.
126     */
127
128    total = (Timer_interrupts * TRR2_VAL) + clicks;
129
130    if ( benchmark_timer_find_average_overhead == 1 )
131        return total;          /* in XXX microsecond units */
132
133    if ( total < LEAST_VALID )
134        return 0;            /* below timer resolution */
135
136    /*
137     *  Return the count in microseconds
138     */
139    return (total - AVG_OVERHEAD);
140}
141
142/* benchmark_timer_disable_subtracting_average_overhead --
143 *     This routine is invoked by the "Check Timer" (tmck) test in the
144 *     RTEMS Timing Test Suite. It makes the benchmark_timer_read routine not
145 *     subtract the overhead required to initialize and read the benchmark
146 *     timer.
147 *
148 * PARAMETERS:
149 *     find_flag - bool flag, true if overhead must not be subtracted.
150 *
151 * RETURNS:
152 *     none
153 */
154void
155benchmark_timer_disable_subtracting_average_overhead(bool find_flag)
156{
157  benchmark_timer_find_average_overhead = find_flag;
158}
Note: See TracBrowser for help on using the repository browser.