Follow @jack_ganssle


The logo for The Embedded Muse For novel ideas about building embedded systems (both hardware and firmware), join the 27,000+ engineers who subscribe to The Embedded Muse, a free biweekly newsletter. The Muse has no hype, no vendor PR. It takes just a few seconds (just enter your email, which is shared with absolutely no one) to subscribe.

The Embedded Muse Video Blog

(Go to the complete list of videos)

Episode 9: Ada - Even on a Microcontroller!

August 4, 2014

The bug rate for Ada code is about one tenth that of typical C, with about the same efficiency. In this video Jack shows that Ada, using free tools, is perfectly suited even for microcontrollers. He's using an ARM Cortex M4 and the FOSS IDE from AdaCore.

Video Transcription

Hi, I'm Jack Ganssle, and welcome to The Embedded Muse video blog which is the companion to my free online Embedded Muse e-newsletter. Today we're going to disprove a commonly held thesis about one of my favorite programming languages.

Are you familiar with the Ada programming language? Ada's been around for a long time. It's come out in many different versions. It's popularity has never really taken off like it should.

Ada is a type-safe programming language that tends to yield much higher quality programs than you get from C and other similar sorts of languages. It's not a silver bullet, but it's a much better framework for building complex embedded systems than C is.

When I first saw that you could define types with constructs like this, it blew my mind. Is this cool or what? Even way back in the days of Fortran, we had some ability to check things at run time, for example subscript out of range. We gave all that up with C for very good reasons, because we had to minimize CPU cycles and memory.

Those days are largely over, just like Fortran's days are largely over. Ada can check not only subscripts. It will check the values of every variable to make sure that they are within whatever range you've defined. I think that is a huge improvement in programming.

One excuse I hear for not using Ada is that it's not available for microcontrollers. Well, the truth is I've seen it used even on 8-bit controllers but that's pretty unusual.

We're in a new world today. ARM's Cortex-M series parts are pushing out the 8 and 16 bitters. Some of these Cortex-M parts are available for just tens of cents in quantity. With 32 bits and many of these controllers have gobs of memory on board, Ada makes an awful lot of sense even for relatively small control applications.

AdaCore is, in a sense, the company that keeps the sacred flame of the language glowing. They've now introduced a free and open source version of their tools for the Cortex-M4 processors. Here's a Cortex-M4 board from STMicrosystems which is running a little Ada program. This board connects to the PC or a Linux machine, the tools support both Windows and Linux, over a USB cable which is used to both power the board and to provide a debugging connection to the board.

AdaCore calls their development environment GPS which is short for the GNAT Programming Studio, GNAT being the name of the GNU version essentially of the Ada compiler. I'm not going to review GPS in this video. Instead, I just want to show that Ada is now a viable alternative to C and C++ even for small microcontroller projects.

This is the STMicrosystems Discovery demo board. The little Ada program I'll be discussing simply turns the LEDs on in a sequence, and when you press the blue button, it reverses the sequence.

This is the main part of the program. This demo is a procedure. It's similar to a function in C except that a procedure doesn't have a return value. You'd use a function if you wanted a return value. Note that it's just a null loop.

Another bit of code, the driver, does the real work. Ada has built in multi-tasking which is used in this demonstration here because it does the work. Notice it's a task, not a function or a procedure.

There's a little bit of code that handles the button to decide if the sequence should be clockwise or anti-clockwise. It has some code that turns on the LEDs in the selected pattern. There's a delay which is handled by the multi-tasking built into the language. Here's the code that sets the GPIO bits to turn the LEDs on and off. It's not much different from C, maybe a tiny bit wordier but sometimes I think there's a rational goal in C to minimize typing no matter what the cost might be.

I always like to see how fast things are. If we modify the task this way we'll get rid of the code that handles the button and the multi-tasking so it's just a very tight loop cycling LEDs on and off. Here you see on the scope that the time required to do this is only about 1.6 microseconds per loop. Now, of course, the LEDs are blinking faster, but I can't see a 1.6 microsecond LED flash.

There you have it. Ada is an option today for us using microcontrollers. I urge you just to go ahead and try it. The tools are free.

Thanks for watching. Don't forget to go to ganssle.com where there are over a thousand articles about building embedded systems and plenty of other free videos available.