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

4.104.114.95
Last change on this file since 5c9be63 was df2837ff, checked in by Joel Sherrill <joel.sherrill@…>, on Mar 12, 2007 at 11:16:53 AM

2007-03-12 Joel Sherrill <joel@…>

  • mcf5272/clock/ckinit.c, mcf5272/include/mcf5272.h, mcf5272/timer/timer.c, mcf5272/timer/timerisr.S: Correct license URL and/or fix mistake in copyright notice. Both of these mistakes appear to be from code submitted after these changes were made previously.
  • Property mode set to 100644
File size: 4.7 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 *
20 *  http://www.rtems.com/license/LICENSE.
21 *
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
29#define TRR2_VAL 65530 
30
31uint32_t Timer_interrupts;
32
33rtems_boolean Timer_driver_Find_average_overhead;
34
35/* External assembler interrupt handler routine */
36extern rtems_isr timerisr(rtems_vector_number vector);
37
38
39/* Timer_initialize --
40 *     Initialize timer 2 for accurate time measurement.
41 *
42 * PARAMETERS:
43 *     none
44 *
45 * RETURNS:
46 *     none
47 */
48void
49Timer_initialize(void)
50{
51    uint32_t icr;
52    /* Catch timer2 interrupts */
53    set_vector(timerisr, BSP_INTVEC_TMR2, 0);
54   
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/*
83 *  The following controls the behavior of Read_timer().
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
99/* Read_timer --
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
109Read_timer( void )
110{
111    uint16_t clicks;
112    uint32_t total;
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;
119 
120    /* Stop Timer... */
121    g_timer_regs->tmr2 = MCF5272_TMR_CLK_STOP | MCF5272_TMR_RST;
122
123    /*
124     *  Total is calculated by taking into account the number of timer
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
131    if ( Timer_driver_Find_average_overhead == 1 )
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
143
144/* Empty_function --
145 *     Empty function call used in loops to measure basic cost of looping
146 *     in Timing Test Suite.
147 *
148 * PARAMETERS:
149 *     none
150 *
151 * RETURNS:
152 *     RTEMS_SUCCESSFUL
153 */
154rtems_status_code
155Empty_function(void)
156{
157    return RTEMS_SUCCESSFUL;
158}
159
160/* Set_find_average_overhead --
161 *     This routine is invoked by the "Check Timer" (tmck) test in the
162 *     RTEMS Timing Test Suite. It makes the Read_timer routine not
163 *     subtract the overhead required to initialize and read the benchmark
164 *     timer.
165 *
166 * PARAMETERS:
167 *     find_flag - boolean flag, TRUE if overhead must not be subtracted.
168 *
169 * RETURNS:
170 *     none
171 */
172void
173Set_find_average_overhead(rtems_boolean find_flag)
174{
175  Timer_driver_Find_average_overhead = find_flag;
176}
Note: See TracBrowser for help on using the repository browser.