source: rtems/c/src/lib/libbsp/lm32/shared/timer/timer.c @ 5d64375f

4.104.11
Last change on this file since 5d64375f was 5d64375f, checked in by Joel Sherrill <joel.sherrill@…>, on Feb 26, 2009 at 8:45:48 PM

2009-02-26 Joel Sherrill <joel.sherrill@…>

  • shared/clock/ckinit.c, shared/start/start.S, shared/timer/timer.c: Run better on simulator. Timer is probably not right yet but
  • Property mode set to 100644
File size: 3.1 KB
Line 
1/*  timer.c
2 *
3 *  This file manages the benchmark timer used by the RTEMS Timing
4 *  Test Suite.  Each measured time period is demarcated by calls to
5 *  benchmark_timer_initialize() and benchmark_timer_read().
6 *  benchmark_timer_read() usually returns the number of microseconds
7 *  since benchmark_timer_initialize() exitted.
8 *
9 *  NOTE: It is important that the timer start/stop overhead be
10 *        determined when porting or modifying this code.
11 *
12 *  COPYRIGHT (c) 1989-1999.
13 *  On-Line Applications Research Corporation (OAR).
14 *
15 *  The license and distribution terms for this file may be
16 *  found in the file LICENSE in this distribution or at
17 *  http://www.rtems.com/license/LICENSE.
18 *
19 *  $Id$
20 *
21 *  Jukka Pietarinen <jukka.pietarinen@mrf.fi>, 2008,
22 *  Micro-Research Finland Oy
23 */
24
25#include <rtems.h>
26#include <bsp.h>
27#include "../include/system_conf.h"
28#include "../../shared/clock/clock.h"
29
30static inline int timerread(unsigned int reg)
31{
32#if ON_SIMULATOR && defined(TIMER0_BASE_ADDRESS)
33  return *((int*)(TIMER0_BASE_ADDRESS + reg));
34#elif defined(TIMER1_BASE_ADDRESS)
35  return *((int*)(TIMER1_BASE_ADDRESS + reg));
36#else
37#warning "Benchmarking timer not available!"
38  return 0;
39#endif
40}
41
42static inline void timerwrite(unsigned int reg, int value)
43{
44#if ON_SIMULATOR && defined(TIMER0_BASE_ADDRESS)
45  *((int*)(TIMER0_BASE_ADDRESS + reg)) = value;
46#elif defined(TIMER1_BASE_ADDRESS)
47  *((int*)(TIMER1_BASE_ADDRESS + reg)) = value;
48#endif
49}
50
51bool benchmark_timer_find_average_overhead;
52
53void benchmark_timer_initialize( void )
54{
55  /* Set timer period */
56  timerwrite(LM32_CLOCK_PERIOD, 0xffffffff);
57  /* Stop timer */
58  timerwrite(LM32_CLOCK_CR, LM32_CLOCK_CR_STOP);
59  /* Clear status register */
60  timerwrite(LM32_CLOCK_SR, 0);
61  /* Start timer */
62  timerwrite(LM32_CLOCK_CR, LM32_CLOCK_CR_START);
63}
64
65/*
66 *  The following controls the behavior of benchmark_timer_read().
67 *
68 *  AVG_OVEREHAD is the overhead for starting and stopping the timer.  It
69 *  is usually deducted from the number returned.
70 *
71 *  LEAST_VALID is the lowest number this routine should trust.  Numbers
72 *  below this are "noise" and zero is returned.
73 */
74
75#define AVG_OVERHEAD      4  /* It typically takes X.X microseconds */
76                             /* (Y countdowns) to start/stop the timer. */
77                             /* This value is in microseconds. */
78#define LEAST_VALID       4  /* Don't trust a clicks value lower than this */
79
80int benchmark_timer_read( void )
81{
82  uint32_t ticks;
83  uint32_t total;
84  uint32_t status;
85
86  ticks = 0xffffffff - timerread(LM32_CLOCK_SNAPSHOT);
87  status = timerread(LM32_CLOCK_SR);
88  if (status & LM32_CLOCK_SR_TO)
89    printk("Timer overflow!\n");
90
91  total = ticks / (CPU_FREQUENCY / 1000000);
92
93  if ( benchmark_timer_find_average_overhead == true )
94    return total;          /* in XXX microsecond units */
95  else
96    {
97      if ( total < LEAST_VALID )
98        return 0;            /* below timer resolution */
99      /*
100       *  Somehow convert total into microseconds
101       */
102      return (total - AVG_OVERHEAD);
103    }
104}
105
106void benchmark_timer_disable_subtracting_average_overhead(
107  bool find_flag
108)
109{
110  benchmark_timer_find_average_overhead = find_flag;
111}
Note: See TracBrowser for help on using the repository browser.