Back to home page

LXR

 
 

    


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

0001 /**
0002  * @file
0003  * @brief Cogent CSB337 Timer driver
0004  *
0005  * This uses timer 0 for timing measurments.
0006  */
0007 
0008 /*
0009  *  Copyright (c) 2004 by Jay Monkman <jtm@lopingdog.com>
0010  *
0011  *  The license and distribution terms for this file may be
0012  *  found in the file LICENSE in this distribution or at
0013  *  http://www.rtems.org/license/LICENSE.
0014  */
0015 
0016 #include <bsp.h>
0017 #include <rtems.h>
0018 #include <rtems/btimer.h>
0019 #include <at91rm9200.h>
0020 #include <at91rm9200_pmc.h>
0021 
0022 uint16_t tstart;
0023 bool benchmark_timer_find_average_overhead;
0024 uint32_t tick_time;
0025 /*
0026  * Set up TC0 -
0027  *   timer_clock2 (MCK/8)
0028  *   capture mode - this shouldn't matter
0029  */
0030 void benchmark_timer_initialize( void )
0031 {
0032     uint32_t tmr_freq;
0033 
0034     /* since we are using timer_clock2, divide mck by 8 */
0035     tmr_freq = at91rm9200_get_mck() / 8;
0036 
0037     TC_TC0_REG(TC_CMR) = TC_CMR_TCCLKS(1);   /* timer_clock2 */
0038     TC_TC0_REG(TC_CCR) = (TC_CCR_CLKEN       /* enable the counter */
0039                           | TC_CCR_SWTRG);   /* start it up */
0040 
0041     /* tick time in nanoseconds */
0042     tick_time = 1000000000/tmr_freq;
0043 
0044 }
0045 
0046 /*
0047  *  The following controls the behavior of benchmark_timer_read().
0048  *
0049  *  AVG_OVEREHAD is the overhead for starting and stopping the timer.  It
0050  *  is usually deducted from the number returned.
0051  *
0052  *  LEAST_VALID is the lowest number this routine should trust.  Numbers
0053  *  below this are "noise" and zero is returned.
0054  */
0055 
0056 #define AVG_OVERHEAD      0  /* It typically takes X.X microseconds */
0057                              /* (Y countdowns) to start/stop the timer. */
0058                              /* This value is in microseconds. */
0059 #define LEAST_VALID       1  /* Don't trust a clicks value lower than this */
0060 
0061 benchmark_timer_t benchmark_timer_read( void )
0062 {
0063   uint16_t t;
0064   uint32_t total;
0065   t = TC_TC0_REG(TC_CV);
0066 
0067   /*
0068    *  Total is calculated by taking into account the number of timer overflow
0069    *  interrupts since the timer was initialized and clicks since the last
0070    *  interrupts.
0071    */
0072 
0073   total = t * tick_time;
0074 
0075   if ( benchmark_timer_find_average_overhead == 1 )
0076     return total;          /* in nanosecond units */
0077   else {
0078     if ( total < LEAST_VALID )
0079       return 0;            /* below timer resolution */
0080   /*
0081    *  Somehow convert total into microseconds
0082    */
0083       return (total - AVG_OVERHEAD);
0084     }
0085 }
0086 
0087 void benchmark_timer_disable_subtracting_average_overhead(bool find_flag)
0088 {
0089   benchmark_timer_find_average_overhead = find_flag;
0090 }
0091