Skip to main content

First attempt at using Ada to program the STM32F4 Discovery board

Here's my first attempt at pushing the user button to blink the red LED on a STM32F4 Discovery board, using AdaCore's GNAT Community Edition.


What you need:
STM32F4 Discovery board
GNAT Community Edition (download and install both the compiler toolchain and the arm-elf)
Ada Drivers Library with examples

It's been a while since I had my STM32 boards installed on my computer, so you may (or may not) need to install a driver for the ST-LINK/V2 programmer...

1. Connect the board, you should have a STM32 STLink device.

2. Start GPS (GNAP Programming Studio).

3. Open one of the examples from the Drivers library / examples / STM32F4_DISCO.

4. Edit main.adb and replace all text with the following source code.
----------------------------------------------------------------
--                                                            --
-- Testing user button and red LED on STM32F4 Discovery board --
--                                                            --
----------------------------------------------------------------

with Ada.Real_Time;      use Ada.Real_Time;
with STM32.Board;        use STM32.Board;
with STM32.User_Button;

procedure Main is

   procedure My_Delay (Milli : Natural);

   procedure My_Delay (Milli : Natural) is
   begin
      delay until Clock + Milliseconds (Milli);
   end My_Delay;

begin
   Initialize_LEDs;

   STM32.User_Button.Initialize;

   loop
      if STM32.User_Button.Has_Been_Pressed then
         STM32.Board.Red_LED.Set;
         My_Delay (100);
         STM32.Board.Red_LED.Clear;
      end if;
   end loop;

end Main;

5. Click the Flash to Board icon.

6. Enjoy :)


Comments

Popular posts from this blog

Raspberry Pi, Ada, and Linux Simple I/O Library

Programming in Ada on a Raspberry Pi is simple enough, Raspbian has a fairly recent version of  GNAT, but if you don't use Raspbian, you can always download GNAT directly from  AdaCore . sudo apt-get install gnat A simple hello test shows that everything works as expected. Tasking in Ada will require a couple more tests, but it should work since GNAT seems to support the Ravenscar profile ... Accessing the GPIO pins requires a library, and thanks to Philip Munts we have one :) Read the readme , find the user manual and install the Linux Simple I/O Library. The library also contains a couple of programs, I just modified one and got a simple blinky. I used the makefile from the library, unmodified, to compile this example. Assume a LED (and a small resistor) on GPIO17 (that's Wiring Pi pin 0, or pin 11 on the board). Next step is to use tasks to blink a couple of LEDs in 'real time', and to check Philip Munt's  MuntsOS Embedded Linux Framework .

Ada Web Server - LED on/off control on Raspberry Pi

Inspired by all those Arduino / ESP8266 / etc. tutorials on controlling a LED from a web page... Requirements: - GNU Ada compiler (sudo apt-get install gnat) - Ada Web Server (sudo apt-get install libaws-bin libaws-doc libaws3.3.2-dev) - Munts Linux Simple I/O Library (see my post here about this, User Manual available here ) - LED (with a resistor) on GPIO17 I used as a starting point the "web-block" sample from AWS, changing a couple of things. For dealing with Raspberry Pi's GPIOs I made a simple package, using libsimpleio, of course ;) I also changed the directory structure, and added to the .gpr project all the libsimpleio related stuff. The entire project is on github here .

Tasking in Ada on a Raspberry Pi

Ada provides an extensive set of capabilities for creating programs with concurrent code modules (tasks), for real-time programming, similar to Java threads. Those tasks are executed concurrently with the rest of the program, so I thought the best way to test them on a Raspberry Pi is by blinking two LEDs at different time intervals ;) Accessing the GPIO pins on a Raspberry Pi from an Ada program requires a library, I'm using  Linux Simple I/O Library by    Philip Munts. Assuming we have two LEDs (and corresponding resistors) on GPIO17 and 18, here's the source code for blinking them at different intervals, using tasks. -- -- GPIO LED Test using libsimpleio -- task version -- WITH Ada.Text_IO; USE Ada.Text_IO; WITH GPIO.libsimpleio; PROCEDURE blinky_tasks IS LED_red : GPIO.Pin := GPIO.libsimpleio.Create(0, 17, GPIO.Output); LED_green : GPIO.Pin := GPIO.libsimpleio.Create(0, 18, GPIO.Output); Ch : Character; Available : Boolean; task First_task; task