source: rtems/c/src/lib/libcpu/m68k/mcf5206/timer/timer.c @ 1857b00

4.104.115
Last change on this file since 1857b00 was 1857b00, checked in by Ralf Corsepius <ralf.corsepius@…>, on 11/30/09 at 17:14:20

Whitespace removal.

  • Property mode set to 100644
File size: 4.4 KB
RevLine 
[00717a3]1/*
2 *  Timer Init
3 *
4 *  This module initializes TIMER 2 for on the MCF5206E 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 *
[57e9580f]20 *  http://www.rtems.com/license/LICENSE.
[1857b00]21 *
[00717a3]22 *  $Id$
23 */
24
25#include <rtems.h>
26#include <bsp.h>
27#include "mcf5206/mcf5206e.h"
28
[1857b00]29#define TRR2_VAL 65530
[00717a3]30
[8bc4430b]31uint32_t   Timer_interrupts;
[00717a3]32
[3551166d]33bool benchmark_timer_find_average_overhead;
[00717a3]34
35/* External assembler interrupt handler routine */
36extern rtems_isr timerisr(rtems_vector_number vector);
37
38
[a5cd2271]39/* benchmark_timer_initialize --
[00717a3]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)
[00717a3]50{
51    /* Catch timer2 interrupts */
52    set_vector(timerisr, BSP_INTVEC_TIMER2, 0);
[1857b00]53
[00717a3]54    /* Initialize interrupts for timer2 */
55    *MCF5206E_ICR(MBAR, MCF5206E_INTR_TIMER_2) =
56        MCF5206E_ICR_AVEC |
57        ((BSP_INTLVL_TIMER2 << MCF5206E_ICR_IL_S) & MCF5206E_ICR_IL) |
58        ((BSP_INTPRIO_TIMER2 << MCF5206E_ICR_IP_S) & MCF5206E_ICR_IP);
[1857b00]59
[00717a3]60    /* Enable interrupts from timer2 */
61    *MCF5206E_IMR(MBAR) &= ~MCF5206E_INTR_BIT(MCF5206E_INTR_TIMER_2);
[1857b00]62
[00717a3]63    /* Reset Timer */
64    *MCF5206E_TMR(MBAR, 2) = MCF5206E_TMR_RST;
65    *MCF5206E_TMR(MBAR, 2) = MCF5206E_TMR_ICLK_STOP;
66    *MCF5206E_TMR(MBAR, 2) = MCF5206E_TMR_RST;
67    *MCF5206E_TCN(MBAR, 2) = 0; /* Zero timer counter */
68    Timer_interrupts = 0; /* Clear timer ISR counter */
69    *MCF5206E_TER(MBAR, 2) = MCF5206E_TER_REF | MCF5206E_TER_CAP; /*clr pend*/
70    *MCF5206E_TRR(MBAR, 2) = TRR2_VAL - 1;
71    *MCF5206E_TMR(MBAR, 2) =
72        (((BSP_SYSTEM_FREQUENCY / 1000000) << MCF5206E_TMR_PS_S) &
73          MCF5206E_TMR_PS) |
74        MCF5206E_TMR_CE_NONE | MCF5206E_TMR_ORI | MCF5206E_TMR_FRR |
75        MCF5206E_TMR_RST;
76    *MCF5206E_TMR(MBAR, 2) |= MCF5206E_TMR_ICLK_MSCLK;
77}
78
79/*
[a5cd2271]80 *  The following controls the behavior of benchmark_timer_read().
[00717a3]81 *
82 *  FIND_AVG_OVERHEAD *  instructs the routine to return the "raw" count.
83 *
84 *  AVG_OVEREHAD is the overhead for starting and stopping the timer.  It
85 *  is usually deducted from the number returned.
86 *
87 *  LEAST_VALID is the lowest number this routine should trust.  Numbers
88 *  below this are "noise" and zero is returned.
89 */
90
91#define AVG_OVERHEAD      0  /* It typically takes 2.0 microseconds */
92                             /* (Y countdowns) to start/stop the timer. */
93                             /* This value is in microseconds. */
94#define LEAST_VALID       1  /* Don't trust a clicks value lower than this */
95
[a5cd2271]96/* benchmark_timer_read --
[00717a3]97 *     Read timer value in microsecond units since timer start.
98 *
99 * PARAMETERS:
100 *     none
101 *
102 * RETURNS:
103 *     number of microseconds since timer has been started
104 */
105int
[a5cd2271]106benchmark_timer_read( void )
[00717a3]107{
[8bc4430b]108    uint16_t   clicks;
109    uint32_t   total;
[00717a3]110
111    /*
112     *  Read the timer and see how many clicks it has been since counter
113     *  rolled over.
114     */
115    clicks = *MCF5206E_TCN(MBAR, 2);
[1857b00]116
[00717a3]117    /* Stop Timer... */
118    *MCF5206E_TMR(MBAR, 2) = MCF5206E_TMR_ICLK_STOP |
119                             MCF5206E_TMR_RST;
120
121    /*
[1857b00]122     *  Total is calculated by taking into account the number of timer
[00717a3]123     *  overflow interrupts since the timer was initialized and clicks
124     *  since the last interrupts.
125     */
126
127    total = (Timer_interrupts * TRR2_VAL) + clicks;
128
[a5cd2271]129    if ( benchmark_timer_find_average_overhead == 1 )
[00717a3]130        return total;          /* in XXX microsecond units */
131
132    if ( total < LEAST_VALID )
133        return 0;            /* below timer resolution */
134
135    /*
136     *  Return the count in microseconds
137     */
138    return (total - AVG_OVERHEAD);
139}
140
[a5cd2271]141/* benchmark_timer_disable_subtracting_average_overhead --
[00717a3]142 *     This routine is invoked by the "Check Timer" (tmck) test in the
[a5cd2271]143 *     RTEMS Timing Test Suite. It makes the benchmark_timer_read routine not
[00717a3]144 *     subtract the overhead required to initialize and read the benchmark
145 *     timer.
146 *
147 * PARAMETERS:
[3551166d]148 *     find_flag - boolean flag, true if overhead must not be subtracted.
[00717a3]149 *
150 * RETURNS:
151 *     none
152 */
153void
[3551166d]154benchmark_timer_disable_subtracting_average_overhead(bool find_flag)
[00717a3]155{
[a5cd2271]156  benchmark_timer_find_average_overhead = find_flag;
[00717a3]157}
Note: See TracBrowser for help on using the repository browser.