On the Ada language and embedded development

2017-08-02

I’ve been recently drawn to Ada (once again). Now, I’m a Lisper through and through and in a way Ada is the exact opposite of Lisp, especially in terms of typing, but perhaps because of that I find it quite interesting. Showing the end result right now, here is the canonical blinking LED done in Ada:

images/ada-blink

Arduino blinking through Ada code

Searching around I found the Make With Ada competition, and I went looking for ways to use Ada with what I had immediately available. I found the AVR-Ada project which looked great. The build process depends on specific versions of the different components (which is quite common), and I spent some time following the manual build process… but then I remembered that I use Docker as a way to encapsulate certain environments.

Since I didn’t found an existing AVR-Ada image I created one, built on the shoulders of Tero Koskinen‘s Fedora 25 RPMs,and uploaded them to the Docker hub. The goal is to reduce the “cost of entry” for using AVR-Ada.

Building it

As an example, building a typical “make the internal LED of the Arduino blink” solution will be as simple as:

docker pull fsmunoz/avr-ada
alias avr-ada="docker run -it --rm \
 -v $HOME/src:/src:Z \
 --device /dev/ttyACM0 \
 --name avr-ada \
 --group-add dialout \
 fsmunoz/avr-ada"
-- Blink the internal LED of an Arduino (and others) using AVR-Ada
--
-- Code by Tero Koskinen
-- http://arduino.ada-language.com/blinking-led.html

with AVR;
with AVR.MCU;
with AVR.Real_Time.Delays;

use AVR;

procedure Blink is
   Led_On : Boolean := False;
   LED_PIN : constant := 5;

begin
   MCU.DDRB_Bits (LED_PIN) := DD_Output;
   loop
      Led_On := not Led_On;
      MCU.PORTB_Bits (LED_PIN) := Led_On;
      delay 8.0;
   end loop;
end Blink;
$ make blink.prog
avr-gnatmake -XMCU=atmega328p -p -Pbuild.gpr -XAVRADA_MAIN=blink
avr-gnatmake: "/src/ada/arduino/blink/blink.elf" up to date.
avrdude -p atmega328p -P /dev/ttyACM0 -c arduino -U flash:w:blink.hex

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
 To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "blink.hex"
avrdude: input file blink.hex auto detected as Intel Hex
avrdude: writing flash (1096 bytes):

Writing | ################################################## | 100% 0.19s

avrdude: 1096 bytes of flash written
avrdude: verifying flash memory against blink.hex:
avrdude: load data flash data from input file blink.hex:
avrdude: input file blink.hex auto detected as Intel Hex
avrdude: input file blink.hex contains 1096 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.15s

avrdude: verifying ...
avrdude: 1096 bytes of flash verified

avrdude: safemode: Fuses OK (E:00, H:00, L:00)

avrdude done. Thank you.

On a more general note I confess that I find the typical objections to Ada tiresome, especially the “bondage and discipline language designed by committee”. I also read the Jargon File and I was raised in that very same culture, but it comes a time when one needs to go a bit deeper, and the “designed by committee” objection is more of an ideological statement than a practical one. Additionally, I have read some comments on how AdaCore (the main company behind Ada technoloy, but no the only one) is somehow "wrong" in supplying all the tools (GNAT) but not providing an exception clause to the GPL. I must say that I find this usage perfectly natural and to the spirit of the GPL: the exception clause on GCC was always a tactical measure, not a strategic one, and AdaCore is as far as I know working steadily on the GPL edition, and this is actually the whole idea behind the GPL.

This is a completely different topic but the “business friendly” mantra that BSDers in general like to use is mostly the liberty for companies to use code and then close it up and profit, and this is seen as good – but when a company opens up the code by using the GPL and thus benefits users it is seen as "unfriendly". As with most things an analysis of good and bad is dependent on who has to gain — cui bono? This is perhaps worthy of a different post.


This document was generated on July 25, 2024 using texi2any.