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

4.115
Last change on this file since df40cc9 was c499856, checked in by Chris Johns <chrisj@…>, on 03/20/14 at 21:10:47

Change all references of rtems.com to rtems.org.

  • 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.org/license/LICENSE.
18 *
19 *  Jukka Pietarinen <jukka.pietarinen@mrf.fi>, 2008,
20 *  Micro-Research Finland Oy
21 */
22
23#include <rtems.h>
24#include <bsp.h>
25#include <rtems/btimer.h>
26#include "../include/system_conf.h"
27#include "../../shared/clock/clock.h"
28
29static inline int timerread(unsigned int reg)
30{
31#if ON_SIMULATOR && defined(TIMER0_BASE_ADDRESS)
32  return *((int*)(TIMER0_BASE_ADDRESS + reg));
33#elif defined(TIMER1_BASE_ADDRESS)
34  return *((int*)(TIMER1_BASE_ADDRESS + reg));
35#else
36#warning "Benchmarking timer not available!"
37  return 0;
38#endif
39}
40
41static inline void timerwrite(unsigned int reg, int value)
42{
43#if ON_SIMULATOR && defined(TIMER0_BASE_ADDRESS)
44  *((int*)(TIMER0_BASE_ADDRESS + reg)) = value;
45#elif defined(TIMER1_BASE_ADDRESS)
46  *((int*)(TIMER1_BASE_ADDRESS + reg)) = value;
47#endif
48}
49
50bool benchmark_timer_find_average_overhead;
51
52void benchmark_timer_initialize( void )
53{
54  /* Set timer period */
55  timerwrite(LM32_CLOCK_PERIOD, 0xffffffff);
56  /* Stop timer */
57  timerwrite(LM32_CLOCK_CR, LM32_CLOCK_CR_STOP);
58  /* Clear status register */
59  timerwrite(LM32_CLOCK_SR, 0);
60  /* Start timer */
61  timerwrite(LM32_CLOCK_CR, LM32_CLOCK_CR_START);
62}
63
64/*
65 *  The following controls the behavior of benchmark_timer_read().
66 *
67 *  AVG_OVEREHAD is the overhead for starting and stopping the timer.  It
68 *  is usually deducted from the number returned.
69 *
70 *  LEAST_VALID is the lowest number this routine should trust.  Numbers
71 *  below this are "noise" and zero is returned.
72 */
73
74#define AVG_OVERHEAD      4  /* It typically takes X.X microseconds */
75                             /* (Y countdowns) to start/stop the timer. */
76                             /* This value is in microseconds. */
77#define LEAST_VALID       4  /* Don't trust a clicks value lower than this */
78
79uint32_t benchmark_timer_read( void )
80{
81  uint32_t ticks;
82  uint32_t total;
83  uint32_t status;
84
85  ticks = 0xffffffff - timerread(LM32_CLOCK_SNAPSHOT);
86  status = timerread(LM32_CLOCK_SR);
87  if (status & LM32_CLOCK_SR_TO)
88    printk("Timer overflow!\n");
89
90  total = ticks / (CPU_FREQUENCY / 1000000);
91
92  if ( benchmark_timer_find_average_overhead == true )
93    return total;          /* in XXX microsecond units */
94  else
95    {
96      if ( total < LEAST_VALID )
97        return 0;            /* below timer resolution */
98      /*
99       *  Somehow convert total into microseconds
100       */
101      return (total - AVG_OVERHEAD);
102    }
103}
104
105void benchmark_timer_disable_subtracting_average_overhead(
106  bool find_flag
107)
108{
109  benchmark_timer_find_average_overhead = find_flag;
110}
Note: See TracBrowser for help on using the repository browser.