summaryrefslogtreecommitdiffstats
path: root/c/src/librtems++/src/rtemsInterrupt.cc
blob: f6026ee26629f6a9565a5503552e4b95dd153606 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
  ------------------------------------------------------------------------
  $Id$
  ------------------------------------------------------------------------

  COPYRIGHT (c) 1997
  Objective Design Systems Ltd Pty (ODS)
  All rights reserved (R) Objective Design Systems Ltd Pty
  
  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.

  ------------------------------------------------------------------------

  See header file.

  ------------------------------------------------------------------------
*/

#include <rtems++/rtemsInterrupt.h>

#if (CPU_SIMPLE_VECTORED_INTERRUPTS == TRUE)

/* ----
   Interrupt Table

   This table is used to re-direct the call from RTEMS to a user
   object
*/

static rtemsInterrupt **interrupt_table;

// has the table been initialised
static bool initialised = false;

/* ----
   rtemsInterrupt
*/

#include <cstdlib>

rtemsInterrupt::rtemsInterrupt()
  : vector(0),
    caught(false),
    old_handler(0),
    old_interrupt(0)
{
  if (!initialised)
  {
    interrupt_table = (rtemsInterrupt **)
        malloc(sizeof(rtemsInterrupt *) * CPU_INTERRUPT_NUMBER_OF_VECTORS);
    for (rtems_vector_number vec = 0;
         vec < CPU_INTERRUPT_NUMBER_OF_VECTORS;
         vec++)
    {
      interrupt_table[vec] = 0;
    }
    initialised = true;
  }
}

rtemsInterrupt::~rtemsInterrupt()
{
  release();
}

const rtems_status_code rtemsInterrupt::isr_catch(const rtems_vector_number vec)
{
  if (vec >= CPU_INTERRUPT_NUMBER_OF_VECTORS)
    return set_status_code(RTEMS_INVALID_NUMBER);
  
  if (caught)
    return set_status_code(RTEMS_RESOURCE_IN_USE);

  old_interrupt = interrupt_table[vec];
  interrupt_table[vec] = this;
  vector = vec;
  
#if (CPU_SIMPLE_VECTORED_INTERRUPTS == TRUE)
  set_status_code(rtems_interrupt_catch(redirector,
                                        vector,
                                        &old_handler));
#else
  set_status_code(RTEMS_NOT_DEFINED);
#endif
  if (successful())
    caught = true;
  else
  {
    interrupt_table[vector] = old_interrupt;
    old_interrupt = 0;
    old_handler = 0;
    vector = 0;
  }
  
  return last_status_code();
}

const rtems_status_code rtemsInterrupt::release(void)
{
  if (caught)
  {
#if (CPU_SIMPLE_VECTORED_INTERRUPTS == TRUE)
    set_status_code(rtems_interrupt_catch(old_handler,
                                          vector,
                                          &old_handler));
#else
  set_status_code(RTEMS_NOT_DEFINED);
#endif
    interrupt_table[vector] = old_interrupt;
    old_interrupt = 0;
    old_handler = 0;
    vector = 0;
    caught = false;
  }
  else
    set_status_code(RTEMS_SUCCESSFUL);
  
  return last_status_code();
}

void rtemsInterrupt::redirector(rtems_vector_number vector)
{
  if (interrupt_table[vector])
    interrupt_table[vector]->handler();
}
#endif