Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:22:48

0001 /**
0002  *  @file
0003  *  @brief Cogent CSB336 Timer driver
0004  *
0005  * This uses timer 2 for timing measurments.
0006  */
0007 
0008 /*
0009  * Copyright (c) 2004 Cogent Computer Systems
0010  *        Written by Jay Monkman <jtm@lopingdog.com>
0011  *
0012  *  The license and distribution terms for this file may be
0013  *  found in the file LICENSE in this distribution or at
0014  *  http://www.rtems.org/license/LICENSE.
0015  */
0016 
0017 #include <rtems.h>
0018 #include <bsp.h>
0019 #include <rtems/btimer.h>
0020 #include <mc9328mxl.h>
0021 
0022 uint32_t g_start;
0023 uint32_t g_freq;
0024 
0025 bool benchmark_timer_find_average_overhead;
0026 
0027 
0028 /*
0029  * Set up Timer 1
0030  */
0031 void benchmark_timer_initialize( void )
0032 {
0033     MC9328MXL_TMR2_TCTL = (MC9328MXL_TMR_TCTL_CLKSRC_PCLK1 |
0034                             MC9328MXL_TMR_TCTL_FRR |
0035                             MC9328MXL_TMR_TCTL_TEN);
0036     /* set prescaler to 1 (register value + 1) */ \
0037     MC9328MXL_TMR2_TPRER = 0;
0038 
0039     /* get freq of counter in KHz */
0040     g_freq = get_perclk1_freq() / 1000;
0041 
0042     g_start =  MC9328MXL_TMR2_TCN;
0043 }
0044 
0045 /*
0046  *  The following controls the behavior of benchmark_timer_read().
0047  *
0048  *  AVG_OVEREHAD is the overhead for starting and stopping the timer.  It
0049  *  is usually deducted from the number returned.
0050  *
0051  *  LEAST_VALID is the lowest number this routine should trust.  Numbers
0052  *  below this are "noise" and zero is returned.
0053  */
0054 
0055 #define AVG_OVERHEAD      0  /* It typically takes X.X microseconds */
0056                              /* (Y countdowns) to start/stop the timer. */
0057                              /* This value is in microseconds. */
0058 #define LEAST_VALID       1  /* Don't trust a clicks value lower than this */
0059 
0060 benchmark_timer_t benchmark_timer_read( void )
0061 {
0062   uint32_t t;
0063   unsigned long long total;
0064 
0065   t =  MC9328MXL_TMR2_TCN;
0066   /*
0067    *  Total is calculated by taking into account the number of timer overflow
0068    *  interrupts since the timer was initialized and clicks since the last
0069    *  interrupts.
0070    */
0071 
0072   total = (t - g_start);
0073 
0074   /* convert to nanoseconds */
0075   total = (total * 1000)/ g_freq;
0076 
0077   if ( benchmark_timer_find_average_overhead == 1 ) {
0078     return (int) total;
0079   } else if ( total < LEAST_VALID ) {
0080       return 0;
0081   }
0082   /*
0083    *  Somehow convert total into microseconds
0084    */
0085 
0086   return (total - AVG_OVERHEAD);
0087 }
0088 
0089 void benchmark_timer_disable_subtracting_average_overhead(bool find_flag)
0090 {
0091   benchmark_timer_find_average_overhead = find_flag;
0092 }
0093