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

4.104.115
Last change on this file since 023f1dd9 was 023f1dd9, checked in by Ralf Corsepius <ralf.corsepius@…>, on 11/30/09 at 05:27:08

Whitespace removal.

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