AdaCore: Build Software that Matters
AdaCore Hero Image
Oct 17, 2011

Gem #112: Lego Mindstorms Ada Environment — Part 1

Let’s get started...

The Lego Mindstorms processor has limited storage available, certainly not enough for a nontrivial application and a complete run-time library. Therefore, the GNAT tool-chain does not implement the full Ada language for this platform. A considerable number of language capabilities are still included, especially the Ravenscar tasking profile, but the entire language is not available.

For example, full exception semantics are not implemented. Programmers may raise user-defined and language-defined exceptions, and may handle them locally, but unhandled (and reraised) exceptions do not propagate dynamically up the call chain as they do in full Ada. Instead, an unhandled exception results in a call to a single global routine referred to as the “last chance handler,” so called because it does not return to the application. Developers may define application-specific last chance handler replacements for the default implementation.

The default implementation shipped with this compiler simply shuts down the Mindstorms processor. However, the Ada interfaces define an alternative that displays the file name and line number corresponding to the location of the exception, using the LCD on the Mindstorms “brick.” The routine also briefly buzzes the speaker to get the user’s attention. It then goes into an infinite loop so that the user can note the location of the exception. This implementation is declared in the NXT.Last_Chance package, shown below.

with System;
package NXT.Last_Chance is
   procedure Last_Chance_Handler
     (Source_Location : System.Address;
      Line            : Integer);
   pragma Export (C, Last_Chance_Handler, "__gnat_last_chance_handler");
end NXT.Last_Chance;

Note the pragma exporting the name as "__gnat_last_chance_handler", the routine name invoked by the run-time library when an unhandled exception is raised. Applications do not call this routine themselves, since it does not return to the caller and shuts down the system. Instead, they override the symbol and have the linker include their version of the routine in the executable so that the run-time library can call it if necessary. In the case of the Mindstorms interfaces, the user specifies the package NXT.Last_Chance in a with-clause, typically in the main program.

with NXT.Last_Chance;
...
procedure ...

You can see the source for the body of the procedure, indeed the sources for all the interfaces, in the “drivers” directory under your installation root. By default this would be:

C:\GNAT\2011\lib\mindstorms-nxt\drivers

Although not a very large or complicated procedure, in such a memory-constrained environment every byte may be precious, so you should take the size of the code into account. The procedure uses the audio interface to buzz the speaker, for example, and the LCD display package is also pulled in. If you were not otherwise using those packages (and their dependents), this could make a difference. However, there is no requirement for you to use this version of the last chance handler, so you can simply comment out or remove the with-clause if you don’t want the fancier handler and associated code included.

Blog_

Latest Blog Posts