summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/m68k/mvme162/timer/timer.c
blob: 4662d3792048e2f3cefbab0d26cd3fdb3508f893 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
 *  This routine initializes the Tick Timer 1 on the MVME162 board.
 *
 *  COPYRIGHT (c) 1989-1999.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.com/license/LICENSE.
 *
 *  Modifications of respective RTEMS file: COPYRIGHT (c) 1994.
 *  EISCAT Scientific Association. M.Savitski
 *
 *  This material is a part of the MVME162 Board Support Package
 *  for the RTEMS executive. Its licensing policies are those of the
 *  RTEMS above.
 */

#include <rtems.h>
#include <rtems/btimer.h>
#include <bsp.h>

/* Periodic tick interval */
#define TICK_INTERVAL         0x10000U
#define TIMER_INT_LEVEL       6

uint32_t            Ttimer_val;
bool                benchmark_timer_find_average_overhead;

rtems_isr timerisr(void);

void benchmark_timer_initialize(void)
{
  (void) set_vector( timerisr, VBR0 * 0x10 + 0x8, 0 );

  Ttimer_val = 0;                     /* clear timer ISR count */
  lcsr->vector_base |= MASK_INT;      /* unmask VMEchip2 interrupts */
  lcsr->intr_clear |= 0x01000000;     /* clear pending interrupt */
  lcsr->to_ctl = 0xE7;                /* prescaler to 1 MHz (see Appendix A1) */
  lcsr->timer_cmp_1 = TICK_INTERVAL;
  lcsr->timer_cnt_1 = 0;              /* clear counter */
  lcsr->board_ctl |= 7;               /* increment, reset-on-compare, */
                                      /*   and clear-overflow-cnt */

  lcsr->intr_level[0] |= TIMER_INT_LEVEL; /* set int level */
  lcsr->intr_ena |= 0x01000000;           /* enable tick timer 1 interrupt */
}

#define AVG_OVERHEAD      3U    /* It typically takes 3.0 microseconds */
                                /* (3 countdowns) to start/stop the timer. */
#define LEAST_VALID       10U   /* Don't trust a value lower than this */

uint32_t benchmark_timer_read(void)
{
  uint32_t            total;

  total = (Ttimer_val * TICK_INTERVAL) + lcsr->timer_cnt_1;

  if ( benchmark_timer_find_average_overhead == true )
    return total;          /* in one-half microsecond units */

  if ( total < LEAST_VALID )
    return 0;            /* below timer resolution */

  return (total-AVG_OVERHEAD) >> 1;
}

void benchmark_timer_disable_subtracting_average_overhead(
  bool find_flag
)
{
  benchmark_timer_find_average_overhead = find_flag;
}