"... and no one shall work for money, and no one shall work for fame; But each for the joy of the working, and each, in his separate star, shall draw the thing as he sees it, for the god of things as they are"

-Kipling

 

YakIO: An Open Source C++ Library for the BBC Micro:Bit

What is YakIO?

YakIO is an open source library which is intended to enable the creation of C++ based executables for the BBC micro:bit microcontroller. The YakIO Library is entirely C++ code. It has been designed to encapsulate the functionality and abstract away as much of the problematic parts of writing C++ programs for the microbit as is possible.

Also of interest is the provision of a simple and well documented compilation tool chain. Part of the previously mentioned encapsulation is the removal of many of the components that might "break" a compile and link operation. For example, there is no makefile. Make is tricky and problematic to install on Windows (particularly for the inexperienced) and it is just not necessary to get started. Instead, easily readable batch files have been provided to enable single command compilation and and link.

Please be aware that when you use YakIO you are programming bare metal. There is no operating system, no Mbed, no DAL/CODAL or anything like that. This means you don't have the things like processes, malloc() or free() etc that you would typically get from the system libraries. Actually, you can have that sort of functionality but you have to approach it differently. Bare metal code has the nice advantage that the output binary is much smaller in size.

TL;DR

"I'm not going to read all that stuff below. Just tell me what I need to do to get going...." Well, OK, just clone or download the GitHub repo: https://github.com/OfItselfSo/YakIO. After that, open up the QuickStart.txt file and do what it says. In about 15 minutes you should be programming the microbit in bare metal C++. Be aware that, at the moment, YakIO only supports the microbit V1.

Example YakIO Code: The Center LED Follows the State of Pin 2

Example YakIO Code: The Center LED Follows the State of Pin 2

The Backstory

The BBC Micro:Bit is an ingenious little microcontroller introduced into UK schools in 2015 with the goal of teaching 1 million pupils to program. However, it is much more than just a childs teaching device - it is so robustly built and contains so many features that it really is a powerful Single Board Computer in its own right. Since so many have been produced (and continue to be made) the cost per unit is very low relative to the functionality it contains. Used ones can be purchased for next to nothing on Internet auction sites.

The primary programming environment is graphical - the students (typically year 7, 13-14 year olds) drag little programming blocks into position. This graphically constructs loops and sets processes running and these can turn on LEDs and activate pins on the card edge. There are also a multitude of other features that can be accessed (bluetooth, compass, accelerometer etc). For the more adventurous, Python code backing the graphical view can be observed and adjusted. All-in-all it is a very clever teaching system.

However, leaving that aside, the BBC Micro:Bit really is a seriously capable and sophisticated little micro-controller. It could reasonably be used for any number of other tasks - not the least of which is learning to program embedded systems.

Indeed, it used to be possible to program the microbit in the C language and tools were developed to facilitate this. However, times change, funding fades and the various parties involved appear to have given up even the pretense of offering such functionality. Much of the programming toolchain that used to exist is still available online - however, some parts are hard to find and the whole thing appears to been left as a complicated "abandonware" mess for at least the last 5 years. From personal experience, it is nearly impossible for the average Joe to get anything at all "C" or "C++" to compile using the existing documentation (such as it is).

Besides not being able to get the improbably complicated toolchain (the software steps you need to run to create a viable executable) to work, the programming environment uses Mbed (which is a small operating system released by ARM) as a substrate. They too, have abandoned this software - they still maintain and update it - but not for the Cortex-M0+ chip the microbit runs. On top of that you have DAL or CODAL which is an API produced by Lancaster University. This adds one more layer of now disinterested complexity.

So, the YakIO project was conceived. YakIO is a C++ library for the BBC Micro:Bit. It is easy to compile, it encapsulates the useful functionality on the microbit and it provides an object oriented API for easy access. YakIO is "bare metal". In other words, it does not use an operating system (however small) and it is still possible for the programmer to directly work with the silicon outside of the library enviroment.

The Origin of the Name: YakIO

All my projects have silly code names and, in fact, I am uncomfortable working on one until I have given it a name. The name "Yak" is derived from the old programmer term "Yak Shaving" which basically means diverting off onto a series of other tasks, most of which are probably not really necessary, in the hope of completing a current objective.

I was working on a component which was a very minor part of another project and this needed a couple of variable 1 to 100Hz pulse trains. Honestly, you can get those anywhere - but somehow I found myself in the middle of a six week long, full throttle, effort to write a C++ library for the microbit. And here we are, sigh ...

A Summary of the Current YakIO Capabilities

What Does YakIO Not Do?

What Tools Do I Need?

First, please note that the the working assumption below is that the YakIO Library is being compiled on a Windows 10 or 11 system. However, seeing as how it is cross compiling (generating code for one type of CPU on another), the YakIO code will work fine if compiled on Linux or Apple platforms.

The arm-none-eabi-gcc compiler package is absolutely necessary. It is free! The one used for development was the Windows installer:

        gcc-arm-none-eabi-4_9-2015q2-20150609-win32.exe 

available from the GNU Arm Embedded Toolchain website

        https://launchpad.net/gcc-arm-embedded/+download

There are later versions, but this was not noticed until fairly late in the development process so the decision was made to stay with the one known to work.

The arm-none-eabi-gcc.exe compiler and arm-none-eabi-objcopy.exe converter included with the above package should be on the path. Either that or a full path will have to be specified when compiling. If you get it right, the following command should always work from the Windows Command prompt or Powershell:

     > arm-none-eabi-gcc.exe --version

        arm-none-eabi-gcc.exe (GNU Tools for ARM Embedded Processors) 4.9.3 20150529 (release) [ARM/embedded-4_9-branch revision 224288]
        Copyright (C) 2014 Free Software Foundation, Inc.

That's all! Download the arm-none-eabi-gcc compiler, put it on the path, and you are good to go. Specifically, you do NOT have to set up complicated LD_LIBRARY_PATH variables or anything like that.

How Do I Learn How to Use YakIO

Let's be upfront and state that the target demographic of the YakIO Library is not the year 7 students the microbit was originally intended for. Probably it would be best to have a working knowledge of C and C++. Embedded systems, such as the microbit, are really not the best way to "get started" with either of those languages.

There is no documentation yet. Instead, the YakIO Library source code has been extensively commented. Please refer to those files for an API reference. They are quite readable - honest!

The YakIO Examples form a sequential tutorial. These examples demonstrate how to implement your own C++ program and provide a template you can adapt. Each project demonstrates some new features - you really should review each example project in order because they are cumulative. Techniques that are discussed in a prior example might be used but not explained in subsequent examples. As with the YakIO Library source, the example code contains quite detailed comments explaining what is going on.

How Do I Get Started

  1. Download the arm-none-eabi-gcc compiler package and put the executables on the path.
  2. Download the YakIO source from the GitHub repo.
  3. Open up a Command or Powershell prompt to the top of the 01_Blinky example.
  4. Review the aaReadMe.txt, main.cpp and main.h files you find there so that you understand what you are doing.
  5. Execute the CompileProgram.bat script.
  6. The resulting .hex file can be dragged and dropped onto your microbit so you can see it work.
  7. Make a cup of tea and enjoy programming the BBC micro:bit in bare metal C++.

Download

The latest version of YakIO is v00.91 Beta and it can be found on the GitHub Repo.

Can I Contribute Code and Bug Reports?

Yes, absolutely, the YakIO Library is under active development and bug reports and pull requests are welcome. Before you submit your code please read the Developer Notes page.

Acknowledgements

Mike Spivey's Bare metal micro:bit book provided many insights into the inner workings of the microbit. This book is currently a work in progress but the chapters that are present are informative and authoritative. Although the content is online, I fully intend to purchase a physical copy once it becomes available.

License

The contents of the YakIO web pages, the help pages, the downloadable executables and the source code are are provided "as is" without any warranty of any kind and without any claim to being error free or complete. Please be aware that the documentation, executables or source code provided may be out-of-date, incomplete, erroneous or simply unsuitable for your purposes. Any use you make of the YakIO documentation, source code and executables is entirely at your discretion and any consequences of that use are entirely your responsibility.

All source code and executable objects are provided under the terms of the MIT License.