source: rtems/bsps/lm32/shared/btimer/btimer.c

Last change on this file was 2b87d7c5, checked in by Joel Sherrill <joel@…>, on 07/08/22 at 13:50:12

bsps/lm32/shared: Change license to BSD-2

Updates #3053.

  • Property mode set to 100644
File size: 4.3 KB
Line 
1/* SPDX-License-Identifier: BSD-2-Clause */
2
3/*  timer.c
4 *
5 *  This file manages the benchmark timer used by the RTEMS Timing
6 *  Test Suite.  Each measured time period is demarcated by calls to
7 *  benchmark_timer_initialize() and benchmark_timer_read().
8 *  benchmark_timer_read() usually returns the number of microseconds
9 *  since benchmark_timer_initialize() exitted.
10 *
11 *  NOTE: It is important that the timer start/stop overhead be
12 *        determined when porting or modifying this code.
13 *
14 *  COPYRIGHT (c) 1989-1999.
15 *  On-Line Applications Research Corporation (OAR).
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 *    notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 *    notice, this list of conditions and the following disclaimer in the
24 *    documentation and/or other materials provided with the distribution.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 *
38 *  Jukka Pietarinen <jukka.pietarinen@mrf.fi>, 2008,
39 *  Micro-Research Finland Oy
40 */
41
42#include <rtems.h>
43#include <bsp.h>
44#include <rtems/btimer.h>
45#include <rtems/bspIo.h>
46#include "../include/system_conf.h"
47#include "../../shared/clock/clock.h"
48
49static inline int timerread(unsigned int reg)
50{
51#if ON_SIMULATOR && defined(TIMER0_BASE_ADDRESS)
52  return *((int*)(TIMER0_BASE_ADDRESS + reg));
53#elif defined(TIMER1_BASE_ADDRESS)
54  return *((int*)(TIMER1_BASE_ADDRESS + reg));
55#else
56#warning "Benchmarking timer not available!"
57  return 0;
58#endif
59}
60
61static inline void timerwrite(unsigned int reg, int value)
62{
63#if ON_SIMULATOR && defined(TIMER0_BASE_ADDRESS)
64  *((int*)(TIMER0_BASE_ADDRESS + reg)) = value;
65#elif defined(TIMER1_BASE_ADDRESS)
66  *((int*)(TIMER1_BASE_ADDRESS + reg)) = value;
67#endif
68}
69
70bool benchmark_timer_find_average_overhead;
71
72void benchmark_timer_initialize( void )
73{
74  /* Set timer period */
75  timerwrite(LM32_CLOCK_PERIOD, 0xffffffff);
76  /* Stop timer */
77  timerwrite(LM32_CLOCK_CR, LM32_CLOCK_CR_STOP);
78  /* Clear status register */
79  timerwrite(LM32_CLOCK_SR, 0);
80  /* Start timer */
81  timerwrite(LM32_CLOCK_CR, LM32_CLOCK_CR_START);
82}
83
84/*
85 *  The following controls the behavior of benchmark_timer_read().
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      4  /* It typically takes X.X microseconds */
95                             /* (Y countdowns) to start/stop the timer. */
96                             /* This value is in microseconds. */
97#define LEAST_VALID       4  /* Don't trust a clicks value lower than this */
98
99benchmark_timer_t benchmark_timer_read( void )
100{
101  uint32_t ticks;
102  uint32_t total;
103  uint32_t status;
104
105  ticks = 0xffffffff - timerread(LM32_CLOCK_SNAPSHOT);
106  status = timerread(LM32_CLOCK_SR);
107  if (status & LM32_CLOCK_SR_TO)
108    printk("Timer overflow!\n");
109
110  total = ticks / (CPU_FREQUENCY / 1000000);
111
112  if ( benchmark_timer_find_average_overhead == true )
113    return total;          /* in XXX microsecond units */
114  else
115    {
116      if ( total < LEAST_VALID )
117        return 0;            /* below timer resolution */
118      /*
119       *  Somehow convert total into microseconds
120       */
121      return (total - AVG_OVERHEAD);
122    }
123}
124
125void benchmark_timer_disable_subtracting_average_overhead(
126  bool find_flag
127)
128{
129  benchmark_timer_find_average_overhead = find_flag;
130}
Note: See TracBrowser for help on using the repository browser.