source: rtems/c/src/lib/libbsp/i386/i386ex/timer/timer.c @ 5453f8a

4.104.114.95
Last change on this file since 5453f8a was 5453f8a, checked in by Ralf Corsepius <ralf.corsepius@…>, on 09/05/08 at 06:16:34

Convert to "bool".

  • Property mode set to 100644
File size: 3.7 KB
Line 
1/*  Timer_init()
2 *
3 *  This routine initializes the timer on the FORCE CPU-386 board.
4 *
5 *  Input parameters:  NONE
6 *
7 *  Output parameters:  NONE
8 *
9 *  NOTE: This routine will not work if the optimizer is enabled
10 *        for some compilers.  The multiple writes to the Z8036
11 *        may be optimized away.
12 *
13 *        It is important that the timer start/stop overhead be
14 *        determined when porting or modifying this code.
15 *
16 *  COPYRIGHT (c) 1989-1999.
17 *  On-Line Applications Research Corporation (OAR).
18 *
19 *  The license and distribution terms for this file may be
20 *  found in the file LICENSE in this distribution or at
21 *  http://www.rtems.com/license/LICENSE.
22 *
23 *  $Id$
24 */
25
26#include <rtems.h>
27#include <bsp.h>
28#include <stdlib.h>
29
30int Ttimer_val;
31bool benchmark_timer_find_average_overhead;
32
33extern void timerisr(void);
34extern int ClockIsOn(const rtems_raw_irq_connect_data*);
35
36#define TMR0      0xF040
37#define TMR1      0xF041
38#define TMR2      0xF042
39#define TMRCON    0xF043
40#define TMRCFG    0xF834
41
42void TimerOn(const rtems_raw_irq_connect_data* used)
43{
44
45  Ttimer_val = 0;                           /* clear timer ISR count */
46
47  outport_byte  ( TMRCON , 0xb0 ); /* select tmr2, stay in mode 0 */
48  outport_byte  ( TMR1   , 0xfa ); /* set to 250 usec interval */
49  outport_byte  ( TMR1   , 0x00 );
50  outport_byte  ( TMRCON , 0x64 ); /* change to mode 2 ( starts timer ) */
51                                   /* interrupts ARE enabled */
52  /*  outport_byte( IERA, 0x41 );             enable interrupt */
53  /*
54   * enable interrrupt at i8259 level
55   */
56  BSP_irq_enable_at_i8259s(used->idtIndex - BSP_IRQ_VECTOR_BASE);
57}
58
59void TimerOff(const rtems_raw_irq_connect_data* used)
60{
61    /*
62     * disable interrrupt at i8259 level
63     */
64     BSP_irq_disable_at_i8259s(used->idtIndex - BSP_IRQ_VECTOR_BASE);
65     /* reset timer mode to standard (DOS) value */
66}
67
68static rtems_raw_irq_connect_data timer_raw_irq_data = {
69  BSP_RT_TIMER3 + BSP_IRQ_VECTOR_BASE,
70  timerisr,
71  TimerOn,
72  TimerOff,
73  ClockIsOn
74};
75
76void Timer_exit(void)
77{
78 if (!i386_delete_idt_entry(&timer_raw_irq_data)) {
79      printk("Timer_exit:Timer raw handler removal failed\n");
80      rtems_fatal_error_occurred(1);
81 }
82}
83
84void benchmark_timer_initialize(void)
85{
86
87  static bool First = true;
88
89  if (First)
90  {
91    First = false;
92
93    atexit(Timer_exit); /* Try not to hose the system at exit. */
94    if (!i386_set_idt_entry (&timer_raw_irq_data)) {
95      printk("benchmark_timer_initialize: raw handler installation failed\n");
96      rtems_fatal_error_occurred(1);
97    }
98  }
99  /* wait for ISR to be called at least once */
100  Ttimer_val = 0;
101  while (Ttimer_val == 0)
102    continue;
103  Ttimer_val = 0;
104}
105
106#define AVG_OVERHEAD      3  /* It typically takes 3.0 microseconds */
107                             /* (3 ticks) to start/stop the timer. */
108#define LEAST_VALID       4  /* Don't trust a value lower than this */
109
110int benchmark_timer_read(void)
111{
112  register uint32_t         clicks;
113  register uint32_t         total;
114
115/*  outport_byte( TBCR, 0x00 );  stop the timer -- not needed on intel */
116
117  outport_byte ( TMRCON, 0x40 );   /* latch the count */
118  inport_byte  ( TMR1,   clicks ); /* read the count */
119
120  total = Ttimer_val + 250 - clicks;
121
122/*  outport_byte( TBCR, 0x00 );   initial value */
123/*  outport_byte( IERA, 0x40 );   disable interrupt */
124
125  /* ??? Is "do not restore old vector" causing problems? */
126
127  if ( benchmark_timer_find_average_overhead == true )
128    return total;          /* in one microsecond units */
129
130  else {
131    if ( total < LEAST_VALID )
132      return 0;            /* below timer resolution */
133    return (total - AVG_OVERHEAD);
134  }
135}
136
137void benchmark_timer_disable_subtracting_average_overhead(
138  bool find_flag
139)
140{
141  benchmark_timer_find_average_overhead = find_flag;
142}
Note: See TracBrowser for help on using the repository browser.