Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:43

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*  timer.c
0004  *
0005  *  This file manages the benchmark timer used by the RTEMS Timing
0006  *  Test Suite.  Each measured time period is demarcated by calls to
0007  *  benchmark_timer_initialize() and benchmark_timer_read().
0008  *  benchmark_timer_read() usually returns the number of microseconds
0009  *  since benchmark_timer_initialize() exitted.
0010  *
0011  *  NOTE: It is important that the timer start/stop overhead be
0012  *        determined when porting or modifying this code.
0013  *
0014  *  COPYRIGHT (c) 1989-1999.
0015  *  On-Line Applications Research Corporation (OAR).
0016  *
0017  * Redistribution and use in source and binary forms, with or without
0018  * modification, are permitted provided that the following conditions
0019  * are met:
0020  * 1. Redistributions of source code must retain the above copyright
0021  *    notice, this list of conditions and the following disclaimer.
0022  * 2. Redistributions in binary form must reproduce the above copyright
0023  *    notice, this list of conditions and the following disclaimer in the
0024  *    documentation and/or other materials provided with the distribution.
0025  *
0026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0027  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0028  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0029  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0030  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0031  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0032  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0033  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0034  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0035  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0036  * POSSIBILITY OF SUCH DAMAGE.
0037  *
0038  *  Jukka Pietarinen <jukka.pietarinen@mrf.fi>, 2008,
0039  *  Micro-Research Finland Oy
0040  */
0041 
0042 #include <rtems.h>
0043 #include <bsp.h>
0044 #include <rtems/btimer.h>
0045 #include <rtems/bspIo.h>
0046 #include "../include/system_conf.h"
0047 #include "../../shared/clock/clock.h"
0048 
0049 static inline int timerread(unsigned int reg)
0050 {
0051 #if ON_SIMULATOR && defined(TIMER0_BASE_ADDRESS)
0052   return *((int*)(TIMER0_BASE_ADDRESS + reg));
0053 #elif defined(TIMER1_BASE_ADDRESS)
0054   return *((int*)(TIMER1_BASE_ADDRESS + reg));
0055 #else
0056 #warning "Benchmarking timer not available!"
0057   return 0;
0058 #endif
0059 }
0060 
0061 static inline void timerwrite(unsigned int reg, int value)
0062 {
0063 #if ON_SIMULATOR && defined(TIMER0_BASE_ADDRESS)
0064   *((int*)(TIMER0_BASE_ADDRESS + reg)) = value;
0065 #elif defined(TIMER1_BASE_ADDRESS)
0066   *((int*)(TIMER1_BASE_ADDRESS + reg)) = value;
0067 #endif
0068 }
0069 
0070 bool benchmark_timer_find_average_overhead;
0071 
0072 void benchmark_timer_initialize( void )
0073 {
0074   /* Set timer period */
0075   timerwrite(LM32_CLOCK_PERIOD, 0xffffffff);
0076   /* Stop timer */
0077   timerwrite(LM32_CLOCK_CR, LM32_CLOCK_CR_STOP);
0078   /* Clear status register */
0079   timerwrite(LM32_CLOCK_SR, 0);
0080   /* Start timer */
0081   timerwrite(LM32_CLOCK_CR, LM32_CLOCK_CR_START);
0082 }
0083 
0084 /*
0085  *  The following controls the behavior of benchmark_timer_read().
0086  *
0087  *  AVG_OVEREHAD is the overhead for starting and stopping the timer.  It
0088  *  is usually deducted from the number returned.
0089  *
0090  *  LEAST_VALID is the lowest number this routine should trust.  Numbers
0091  *  below this are "noise" and zero is returned.
0092  */
0093 
0094 #define AVG_OVERHEAD      4  /* It typically takes X.X microseconds */
0095                              /* (Y countdowns) to start/stop the timer. */
0096                              /* This value is in microseconds. */
0097 #define LEAST_VALID       4  /* Don't trust a clicks value lower than this */
0098 
0099 benchmark_timer_t benchmark_timer_read( void )
0100 {
0101   uint32_t ticks;
0102   uint32_t total;
0103   uint32_t status;
0104 
0105   ticks = 0xffffffff - timerread(LM32_CLOCK_SNAPSHOT);
0106   status = timerread(LM32_CLOCK_SR);
0107   if (status & LM32_CLOCK_SR_TO)
0108     printk("Timer overflow!\n");
0109 
0110   total = ticks / (CPU_FREQUENCY / 1000000);
0111 
0112   if ( benchmark_timer_find_average_overhead == true )
0113     return total;          /* in XXX microsecond units */
0114   else
0115     {
0116       if ( total < LEAST_VALID )
0117     return 0;            /* below timer resolution */
0118       /*
0119        *  Somehow convert total into microseconds
0120        */
0121       return (total - AVG_OVERHEAD);
0122     }
0123 }
0124 
0125 void benchmark_timer_disable_subtracting_average_overhead(
0126   bool find_flag
0127 )
0128 {
0129   benchmark_timer_find_average_overhead = find_flag;
0130 }