1 | |
---|
2 | -- This is an example of how to attach and handle interrupts in Ada 95. |
---|
3 | -- Interrupt handling is done as follows: |
---|
4 | -- |
---|
5 | -- 1. A protected procedure is attached to the interrupt |
---|
6 | -- 2. When activated, the procedure enables a conditional entry |
---|
7 | -- 3. A task waiting on the entry will carry out the work. |
---|
8 | -- |
---|
9 | -- In this way, we spend minimum amount of time in the protected |
---|
10 | -- procedure. Many other schemes are of course possible... |
---|
11 | -- |
---|
12 | -- Written by Tullio Vardanega and Jiri Gaisler |
---|
13 | -- European Space Agency, 1999. |
---|
14 | -- |
---|
15 | -- The license and distribution terms for this file may be |
---|
16 | -- found in the file LICENSE in this distribution or at |
---|
17 | -- http://www.rtems.com/license/LICENSE. |
---|
18 | -- |
---|
19 | -- $Id$ |
---|
20 | -- |
---|
21 | |
---|
22 | |
---|
23 | |
---|
24 | with Ada.Interrupts; |
---|
25 | with System; |
---|
26 | with Ada.Text_IO; |
---|
27 | with Ada.Real_Time; use Ada.Real_Time; |
---|
28 | |
---|
29 | package body Interrupt_pkg is |
---|
30 | |
---|
31 | type T_SEM is (HIGH, LOW); |
---|
32 | |
---|
33 | Protected_Priority : constant System.Interrupt_Priority := |
---|
34 | System.Interrupt_Priority'First; |
---|
35 | |
---|
36 | Start_Time : Ada.Real_Time.Time; |
---|
37 | Stop_Time : Ada.Real_Time.Time; |
---|
38 | |
---|
39 | -- Protected object, including interrupt handler (Signal) and conditional entry. |
---|
40 | |
---|
41 | protected Handler is |
---|
42 | procedure Signal; |
---|
43 | entry Wait; |
---|
44 | pragma Attach_Handler (Signal, 17); -- Signal 17 equals irq 1 on ERC32 |
---|
45 | pragma Priority (Protected_Priority); |
---|
46 | private |
---|
47 | BARRIER : T_SEM := HIGH; |
---|
48 | end Handler; |
---|
49 | |
---|
50 | protected body Handler is |
---|
51 | procedure Signal is |
---|
52 | begin |
---|
53 | BARRIER := LOW; |
---|
54 | end Signal; |
---|
55 | |
---|
56 | entry Wait when (BARRIER = LOW) is |
---|
57 | Took : Time_Span; |
---|
58 | begin |
---|
59 | Stop_Time := Ada.Real_Time.Clock; |
---|
60 | BARRIER := HIGH; |
---|
61 | |
---|
62 | Took := Stop_Time - Start_Time; |
---|
63 | |
---|
64 | Ada.Text_IO.Put_line ( "Interrupt took: " & |
---|
65 | Duration'Image(To_Duration(Took))); |
---|
66 | end Wait; |
---|
67 | end Handler; |
---|
68 | |
---|
69 | -- Sporadic task, waiting on entry (Wait) for the interrupt. |
---|
70 | |
---|
71 | task sporadic is |
---|
72 | pragma Priority (100); |
---|
73 | end sporadic; |
---|
74 | |
---|
75 | task body sporadic is |
---|
76 | Message : constant STRING := "sporadic activated"; |
---|
77 | begin |
---|
78 | loop |
---|
79 | Handler.Wait; |
---|
80 | |
---|
81 | end loop; |
---|
82 | end sporadic; |
---|
83 | |
---|
84 | -- Test program, generating interrupt 1 on ERC32 |
---|
85 | |
---|
86 | procedure itest is |
---|
87 | procedure irqforce(irq : integer); |
---|
88 | pragma Import (C, irqforce, "irqforce"); |
---|
89 | begin |
---|
90 | |
---|
91 | Start_Time := Ada.Real_Time.Clock; |
---|
92 | Stop_Time := Ada.Real_Time.Clock; |
---|
93 | |
---|
94 | Ada.Text_IO.Put_line ( "Timer Overhead: " & |
---|
95 | Duration'Image(To_Duration(Stop_Time - Start_Time))); |
---|
96 | |
---|
97 | for i in 1..10 loop |
---|
98 | Start_Time := Ada.Real_Time.Clock; |
---|
99 | irqforce(1); |
---|
100 | delay(0.05); |
---|
101 | end loop; |
---|
102 | |
---|
103 | -- Have to kill sporadic to exit since it is has an infinite loop |
---|
104 | abort sporadic; |
---|
105 | end; |
---|
106 | |
---|
107 | begin |
---|
108 | |
---|
109 | -- itest; |
---|
110 | NULL; |
---|
111 | |
---|
112 | end Interrupt_pkg; |
---|
113 | |
---|
114 | |
---|