source: rtems/c/src/lib/libcpu/m68k/mcf5206/timer/timer.c @ 05c1886

4.104.115
Last change on this file since 05c1886 was 3551166d, checked in by Ralf Corsepius <ralf.corsepius@…>, on 09/05/08 at 11:55:58

Convert to "bool".

  • 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 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 *
20 *  http://www.rtems.com/license/LICENSE.
21 *
22 *  $Id$
23 */
24
25#include <rtems.h>
26#include <bsp.h>
27#include "mcf5206/mcf5206e.h"
28
29#define TRR2_VAL 65530 
30
31uint32_t   Timer_interrupts;
32
33bool benchmark_timer_find_average_overhead;
34
35/* External assembler interrupt handler routine */
36extern rtems_isr timerisr(rtems_vector_number vector);
37
38
39/* benchmark_timer_initialize --
40 *     Initialize timer 2 for accurate time measurement.
41 *
42 * PARAMETERS:
43 *     none
44 *
45 * RETURNS:
46 *     none
47 */
48void
49benchmark_timer_initialize(void)
50{
51    /* Catch timer2 interrupts */
52    set_vector(timerisr, BSP_INTVEC_TIMER2, 0);
53   
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);
59       
60    /* Enable interrupts from timer2 */
61    *MCF5206E_IMR(MBAR) &= ~MCF5206E_INTR_BIT(MCF5206E_INTR_TIMER_2);
62   
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/*
80 *  The following controls the behavior of benchmark_timer_read().
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
96/* benchmark_timer_read --
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
106benchmark_timer_read( void )
107{
108    uint16_t   clicks;
109    uint32_t   total;
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);
116 
117    /* Stop Timer... */
118    *MCF5206E_TMR(MBAR, 2) = MCF5206E_TMR_ICLK_STOP |
119                             MCF5206E_TMR_RST;
120
121    /*
122     *  Total is calculated by taking into account the number of timer
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
129    if ( benchmark_timer_find_average_overhead == 1 )
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
141/* benchmark_timer_disable_subtracting_average_overhead --
142 *     This routine is invoked by the "Check Timer" (tmck) test in the
143 *     RTEMS Timing Test Suite. It makes the benchmark_timer_read routine not
144 *     subtract the overhead required to initialize and read the benchmark
145 *     timer.
146 *
147 * PARAMETERS:
148 *     find_flag - boolean flag, true if overhead must not be subtracted.
149 *
150 * RETURNS:
151 *     none
152 */
153void
154benchmark_timer_disable_subtracting_average_overhead(bool find_flag)
155{
156  benchmark_timer_find_average_overhead = find_flag;
157}
Note: See TracBrowser for help on using the repository browser.