Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 36 ยท Systems and Tools

Embedded and Low-Level Ada

Ada on microcontrollers: memory-mapped registers with address clauses, Volatile, interrupt handlers in protected objects, the Ravenscar profile, restrictions and run-times.

Ada's home territory is embedded and real-time software: flight computers, railway signaling, engine controllers, medical pumps, and increasingly hobby boards. The language gives you direct hardware access without leaving type safety behind, and a restricted tasking model that can be analyzed for timing.

No Ada compiler or microcontroller is available on this site, so the Ada here is read-only and tool output is not shown. The runnable stand-ins are C programs that simulate a memory-mapped device and an interrupt-driven design.

Memory-mapped registers

On a microcontroller, peripherals are controlled by reading and writing special addresses. In Ada you map a typed object onto an address:

ADA
with System;
with System.Storage_Elements; use System.Storage_Elements;
with Interfaces; use Interfaces;

GPIO_Out : Unsigned_32
  with Volatile,
       Address => To_Address (16#4002_0014#);

GPIO_Out := 16#0000_0020#;       --  set pin 5
  • Address places the object at the hardware address.
  • Volatile tells the compiler that the value can change outside its knowledge (or that every access matters), so it must not cache it in a register or remove "redundant" reads and writes. Related aspects: Atomic (indivisible access) and Volatile_Full_Access (always access the whole object).
  • Combine this with a record representation from the previous lesson to describe each bit field of the register, and you get typed, named access instead of magic numbers.

Because the compiler checks the types and ranges of the fields, Reg.Speed := 20; for a 4-bit field is rejected or raises Constraint_Error, and a wrong bit position is a compile-time error, not a flaky board.

Hardware access

Address map object to location
Volatile never optimize away
Atomic indivisible access
Rep clauses named bit fields

Typed registers, not raw pointers.

1 / 2

Interrupt handlers

An interrupt handler in Ada is a protected procedure attached to an interrupt:

ADA
protected Button
  with Interrupt_Priority => System.Interrupt_Priority'Last
is
   procedure Handler
     with Attach_Handler => Button_Interrupt_Id;

   entry Wait_For_Press;
private
   Pressed : Boolean := False;
end Button;

protected body Button is

   procedure Handler is
   begin
      Pressed := True;           --  keep this tiny
   end Handler;

   entry Wait_For_Press when Pressed is
   begin
      Pressed := False;
   end Wait_For_Press;

end Button;

The hardware (or the run-time on top of it) calls Handler when the interrupt fires; the handler does the minimum, here setting a flag, and a normal task blocked at Wait_For_Press is released through the entry's barrier. Because the handler is part of a protected object, the mutual exclusion with the task is guaranteed by the language: on a single core, by running at a ceiling priority.

Periodic tasks and Ada.Real_Time

Control loops must run at fixed intervals without drifting. Use absolute times:

ADA
with Ada.Real_Time; use Ada.Real_Time;

task body Controller is
   Period : constant Time_Span := Milliseconds (10);
   Next   : Time := Clock;
begin
   loop
      Read_Sensors;
      Update_Outputs;
      Next := Next + Period;
      delay until Next;           --  absolute, no accumulated drift
   end loop;
end Controller;

delay 0.01; would add the loop's own run time to every cycle and drift; delay until keeps the schedule.

Ravenscar and restrictions

The Ravenscar profile (and its extension Jorvik) limits tasking so that timing behavior can be analyzed: tasks are created only at start-up, tasks are never terminated, there is no abort or select, entries are few and simple, and scheduling is by fixed priorities with ceiling locking. You enable it with pragma Profile (Ravenscar);, and the run-time can then be tiny. Restrictions are a general mechanism (pragma Restrictions (No_Exceptions, No_Allocators, No_Recursion);) letting you ban features that a certification standard or a small run-time cannot support. The compiler enforces them at build time.

Many boards use a zero-footprint or light run-time that omits features such as exception propagation tables, so check what your run-time supports. Alire has crates for common boards (STM32, nRF52, RP2040, Arduino-compatible AVR) with ready-made drivers.

Stand-in: a simulated register block (C, runnable)

The program below simulates a device whose registers are stored in a struct, with the same Control_Register layout as before (Enable bit 0, Mode bits 1-3). A "device step" function acts on the register the way hardware would, so the main program interacts only through register reads and writes.

C C stand-in: driving a simulated peripheral

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The expected output is counter=12 busy=1, then counter=12 busy=0.

Stand-in: interrupt flag and main loop (C, runnable)

An interrupt handler sets a flag, and the main loop reacts. We "fire" interrupts from a simulation, as a timer would. Only a flag changes inside the handler, which mirrors the advice to keep handlers tiny.

C C stand-in: interrupt handler plus event loop

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the Volatile aspect tell the compiler?

    Volatile prevents optimizations that assume the value changes only by the program.
  2. 2. How is an interrupt handler written in Ada?

    Protected procedures run at interrupt priority and are mutually exclusive with the rest of the object.
  3. 3. Why use delay until instead of delay in a periodic loop?

    delay adds to the time already spent; delay until keeps a fixed schedule.
  4. 4. What does the Ravenscar profile do?

    Ravenscar permits static tasks, protected objects and fixed priorities, enabling timing analysis.

Summary

  • Address, Volatile, Atomic and representation clauses give typed access to hardware registers.
  • Interrupt handlers are protected procedures; keep them minimal and release tasks via entries.
  • Periodic tasks use Ada.Real_Time and delay until to avoid drift.
  • The Ravenscar profile and pragma Restrictions shrink the run-time and enable timing analysis.
  • Check what your board's run-time supports; Alire has crates for many boards.

Next: Project: Bounded Ring Buffer