AdaCore Blog

Ada on the micro:bit

by Fabien Chouteau

The micro:bit is a very small ARM Cortex-M0 board designed by the BBC for computer education. It's fitted with a Nordic nRF51 Bluetooth enabled 32bit ARM microcontroller. At $15 it is one of the cheapest yet most fun piece of kit to start embedded programming.

In this blog post I will explain how to start programming your micro:bit in Ada.

How to set up the Ada development environment for the Micro:Bit

pyOCD programmer

The micro:bit comes with an embedded programming/debugging probe implementing the CMSIS-DAP protocol defined by ARM. In order to use it, you have to install a Python library called pyOCD. Here is the procedure:

On Windows:

Download the binary version of pyOCD from this link:

https://launchpad.net/gcc-arm-embedded-misc/pyocd-binary/pyocd-20150430/+download/pyocd_win.exe

Plug your micro:bit using an USB cable and run pyOCD in a terminal:

C:\Users\UserName\Downloads>pyocd_win.exe -p 1234 -t nrf51822
Welcome to the PyOCD GDB Server Beta Version
INFO:root:Unsupported board found: 9900
INFO:root:new board id detected: 9900000037024e450073201100000021000000009796990
1
INFO:root:board allows 5 concurrent packets
INFO:root:DAP SWD MODE initialised
INFO:root:IDCODE: 0xBB11477
INFO:root:4 hardware breakpoints, 0 literal comparators
INFO:root:CPU core is Cortex-M0
INFO:root:2 hardware watchpoints
INFO:root:GDB server started at port:1234

On Linux (Ubuntu):

Install pyOCD from pip:

$ sudo apt-get install python-pip
$ pip install --pre -U pyocd

pyOCD will need permissions to talk with the micro:bit. Instead of running the pyOCD as privileged user (root), it's better to add a UDEV rules saying that the device is accessible for non-privileged users:

$ sudo sh -c 'echo SUBSYSTEM==\"usb\", ATTR{idVendor}==\"0d28\", ATTR{idProduct}==\"0204\", MODE:=\"666\" > /etc/udev/rules.d/mbed.rules'
$ sudo udevadm control --reload

Now that there's a new UDEV rule and if you already plugged your micro:bit before, you have to unplug it and plug it back again.

To run pyOCD, use the following command:

$ pyocd-gdbserver -S -p 1234
INFO:root:DAP SWD MODE initialised
INFO:root:ROM table #0 @ 0xf0000000 cidr=b105100d pidr=2007c4001
INFO:root:[0]<e00ff000: cidr=b105100d, pidr=4000bb471, class=1>
INFO:root:ROM table #1 @ 0xe00ff000 cidr=b105100d pidr=4000bb471
INFO:root:[0]<e000e000:SCS-M0+ cidr=b105e00d, pidr=4000bb008, class=14>
INFO:root:[1]<e0001000:DWT-M0+ cidr=b105e00d, pidr=4000bb00a, class=14>
INFO:root:[2]<e0002000:BPU cidr=b105e00d, pidr=4000bb00b, class=14>
INFO:root:[1]<f0002000: cidr=b105900d, pidr=4000bb9a3, class=9, devtype=13, devid=0>
INFO:root:CPU core is Cortex-M0
INFO:root:4 hardware breakpoints, 0 literal comparators
INFO:root:2 hardware watchpoints
INFO:root:Telnet: server started on port 4444
INFO:root:GDB server started at port:1234
[...]

Download the Ada Drivers Library

Ada drivers library if a firmware library written in Ada. We currently have support for some ARM Cortex-M microcontrollers like the STM32F4/7 or the nRF51, but also the HiFive1 RISC-V board.

You can download or clone the repository from GitHub:

https://github.com/AdaCore/Ada_Drivers_Library

$ git clone https://github.com/AdaCore/Ada_Drivers_Library

Install the Ada ZFP run-time

In Ada_Drivers_Library, go to the microb:bit example directory and download or clone the run-time from this GitHub repository:https://github.com/Fabien-Chouteau/zfp-nrf51

$ cd Ada_Drivers_Library/examples/MicroBit/
$ git clone https://github.com/Fabien-Chouteau/zfp-nrf51

Install the GNAT ARM ELF toolchain

If you have a GNAT Pro ARM ELF subscription, you can download the  toolchain from your GNATtracker account. Otherwise you can use the Community release of GNAT from this address: https://www.adacore.com/community

Open the example project and build it

Start GNAT Programming studio (GPS) and open the Micro:Bit example project: "Ada_Drivers_Library/examples/MicroBit/microbit_example.gpr".

Press F4 and then press Enter to build the project.

Program and debug the board

Make sure your pyocd session is still running and then in GPS, start a debug session with the top menu "Debug -> Initialize -> main". GPS will start Gdb and connect it to pyOCD.

In the gdb console, use the "load" command to program the board:

(gdb) load
Loading section .text, size 0xbd04 lma 0x0
Loading section .ARM.exidx, size 0x8 lma 0xbd04
[...]

Reset the board with this command:

(gdb) monitor reset

And finally use the "continue" command to run the program:

(gdb) continue

You can interrupt the execution with the "CTRL+backslash" shortcut and then insert breakpoints, step through the application, inspect memory, etc.

Conclusion

That’s it, your first Ada program on the Micro:Bit! If you have an issue with this procedure, please tell us in the comments section below.

Note that the current support is limited but we working on adding tasking support (Ravenscar), improving the library as well as the integration into GNAT Programing Studio, so stay tuned.

In the meantime, here is an example of the kind of project that you can do with Ada on the Micro:Bit

Posted in #embedded    #Ada    #nRF51    #ARM   

About Fabien Chouteau

Fabien joined AdaCore in 2010 after his engineering degree at the EPITA (Paris). He is involved in real-time, embedded and hardware simulation technology. Maker/DIYer in his spare time, his projects include electronics, music and woodworking.