source: rtems/c/src/lib/libcpu/arm/at91rm9200/timer/timer.c @ c499856

4.11
Last change on this file since c499856 was c499856, checked in by Chris Johns <chrisj@…>, on Mar 20, 2014 at 9:10:47 PM

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

  • Property mode set to 100644
File size: 2.7 KB
Line 
1/*
2 * Cogent CSB337 Timer driver
3 *
4 * This uses timer 0 for timing measurments.
5 *
6 * Copyright (c) 2004 by Jay Monkman <jtm@lopingdog.com>
7 *
8 *  The license and distribution terms for this file may be
9 *  found in the file LICENSE in this distribution or at
10 *  http://www.rtems.org/license/LICENSE.
11 *
12 * Notes:
13 *  This file manages the benchmark timer used by the RTEMS Timing Test
14 *  Suite.  Each measured time period is demarcated by calls to
15 *  benchmark_timer_initialize() and benchmark_timer_read().  benchmark_timer_read() usually returns
16 *  the number of microseconds since benchmark_timer_initialize() exitted.
17 *
18 *  It is important that the timer start/stop overhead be determined
19 *  when porting or modifying this code.
20 */
21
22#include <rtems.h>
23#include <bsp.h>
24#include <at91rm9200.h>
25#include <at91rm9200_pmc.h>
26
27uint16_t tstart;
28bool benchmark_timer_find_average_overhead;
29uint32_t tick_time;
30/*
31 * Set up TC0 -
32 *   timer_clock2 (MCK/8)
33 *   capture mode - this shouldn't matter
34 */
35void benchmark_timer_initialize( void )
36{
37    uint32_t tmr_freq;
38
39    /* since we are using timer_clock2, divide mck by 8 */
40    tmr_freq = at91rm9200_get_mck() / 8;
41
42    TC_TC0_REG(TC_CMR) = TC_CMR_TCCLKS(1);   /* timer_clock2 */
43    TC_TC0_REG(TC_CCR) = (TC_CCR_CLKEN       /* enable the counter */
44                          | TC_CCR_SWTRG);   /* start it up */
45
46    /* tick time in nanoseconds */
47    tick_time = 1000000000/tmr_freq;
48
49}
50
51/*
52 *  The following controls the behavior of benchmark_timer_read().
53 *
54 *  AVG_OVEREHAD is the overhead for starting and stopping the timer.  It
55 *  is usually deducted from the number returned.
56 *
57 *  LEAST_VALID is the lowest number this routine should trust.  Numbers
58 *  below this are "noise" and zero is returned.
59 */
60
61#define AVG_OVERHEAD      0  /* It typically takes X.X microseconds */
62                             /* (Y countdowns) to start/stop the timer. */
63                             /* This value is in microseconds. */
64#define LEAST_VALID       1  /* Don't trust a clicks value lower than this */
65
66int benchmark_timer_read( void )
67{
68  uint16_t t;
69  uint32_t total;
70  t = TC_TC0_REG(TC_CV);
71
72  /*
73   *  Total is calculated by taking into account the number of timer overflow
74   *  interrupts since the timer was initialized and clicks since the last
75   *  interrupts.
76   */
77
78  total = t * tick_time;
79
80  if ( benchmark_timer_find_average_overhead == 1 )
81    return total;          /* in nanosecond units */
82  else {
83    if ( total < LEAST_VALID )
84      return 0;            /* below timer resolution */
85  /*
86   *  Somehow convert total into microseconds
87   */
88      return (total - AVG_OVERHEAD);
89    }
90}
91
92void benchmark_timer_disable_subtracting_average_overhead(bool find_flag)
93{
94  benchmark_timer_find_average_overhead = find_flag;
95}
96
Note: See TracBrowser for help on using the repository browser.