| Getting Started with M68K Ada: Ada 95 Compilation System for the Motorola M68000 Family | ||
|---|---|---|
| Prev | Chapter 3. Real-Time Programs | Next |
Like periodic tasks, aperiodic tasks have an outer loop and a single statement to invoke the task body.
In the following example, we declare a task that runs in response to an interrupt. You can use this code with a main subprogram to build a complete application that will run on the M68K simulator.
Here is the code for the package and its body:
Example 3-4. An Interrupt-Driven Task
package EG4_Pack is
task Task2 is
pragma Priority (1);
end Task2;
end EG4_Pack;
with Ada.Interrupts.Names;
with Interfaces;
with Text_IO;
package body EG4_Pack is
use Ada.Interrupts.Names;
use Interfaces;
use Text_IO;
protected IO is
procedure Handler;
pragma Attach_Handler (Handler, Level1_Autovector);
entry Get (C : out Character);
private
Rx_Ready : Boolean := False;
end IO;
protected body IO is
procedure Handler is
Status_Word : Unsigned_8;
for Status_Word'Address use 16#00050069#;
begin
Rx_Ready := (Status_Word and 16#01#) /= 0;
end Handler;
entry Get (C : out Character) when Rx_Ready is
Data_Word : Unsigned_8;
for Data_Word'Address use 16#00050063#;
begin
C := Character'Val (Data_Word and 16#7f#);
Rx_Ready := False;
end Get;
end IO;
task body Task2 is
C : Character;
begin
loop
IO.Get (C);
-- Do something with the character
Put ("C = '"); Put (C); Put (''');
New_Line;
end loop;
end Task2;
end EG4_Pack;
Points to note are as follows:
The package Ada.Interrupts.Names declares the names of the M68K external interrupts.
We use address clauses to declare memory-mapped IO locations.
The type Unsigned_32 permits bitwise operators such as 'and' and 'or'.
The interrupt handler runs in supervisor mode with the Interrupt Mask set to the level of the interrupt.