We have already discovered how to use the online editor to create a C/C++ program. Now we are going to move to the desktop with an offline approach. This has the advantage that we can use any tools we care to select and no Internet connection is needed.

Now On Sale!

You can now buy a print edition of micro:bit IoT in C.

You can buy it from:

USA and World  Amazon.com
Canada              Amazon.ca
UK                      Amazon.co.uk
France                Amazon.fr
Germany            Amazon.de
Spain                  Amazon.es
Brazil                  Amazon.br
Italy                    Amazon.it
Japan                 Amazon.co.jp
Mexico               Amazon.com.mx 

 

 The full contents can be seen below. 

Chapter List

  1. Getting Started With C/C++
    Anyone who wants to use the BBC micro:bit to its full potential as an IoT device needs to look outside the coding environments provided by its own website. As an mbed device, however, the micro:bit  is capable of being programmed in C/C++. Here we look at how to use the mbed online compiler for a simple demo program.

  2. Offline C/C++ Development  
    We have already discovered how to use the online editor to create a C/C++ program. Now we are going to move to the desktop with an offline approach. This has the advantage that we can use any tools we care to select and no Internet connection is needed.
  3. First Steps With The GPIO 
    The most basic task when working with the micro:bit is controlling the I/O lines. This isn't difficult if you use the framework provided but there some subtle points to watch out for. This chapter looks a the basics of using the GPIO.

  4. Working Directly With The Hardware - Memory Mapping. 
    The framework makes working with the GPIO and other devices as easy as it can be but there are many layers of software to go through before you get to the hardware. Writing directly to the hardware can make things up to ten times faster and give you access to things that their framework doesn't. It is also an educational experience to deal with the raw hardware directly.

  5. Pulse Width Modulation, Servos And More
    In this chapter we take a close look at pulse width modulation PWM including, sound, driving LEDs and servos.

  6. I2C
    The I2C bus is one of the most useful ways of connecting moderately sophisticated sensors and peripherals to the any processor. The only problem is that it can seem like a nightmare confusion of hardware, low level interaction and high level software. There are few general introductions to the subject because at first sight every I2C device is different, but here we present one.

  7. I2C Temperature Measurement
    Using I2C devices is fairly easy once you have successfully used one - and hence know what information you need and what to look for in a working system. In this chapter we use the HTU21D temperature and humidity sensor as a case study of I2C in action. It also happens to be a useful sensor.

  8. A Custom Protocol - The DHT11/22

  9. The DS18B20 - One Wire Bus

  10. The SPI Bus
    The SPI bus can be something of a problem because it doesn't have a well defined standard that every device conforms to. Even so if you only want to work with one specific device it is usually easy to find a configuration that works - as long as you understand what the possibilities are. 

  11. SPI MCP3008/4 AtoD   
    The SPI bus can be difficult to make work at first but once you know what to look for about how the slave claims to work it gets easier. To demonstrate how its done let's add eight channels of 12 bit AtoD using the MCP3008.

  12. Serial Connections
    The serial port is one of the oldest of ways of connecting devices together but it is still very, very useful. The micro:bit has a single serial interface but it can be directed to use any of the GPIO piins as Rx and Tx. 

  13. WiFi 
    The micro:bit has a radio that works in Bluetooth LE and point-to-point ad-hoc mode, but at the moment it lacks WiFi connectivity. The solution is to use the low cost ESP8266 to make the connection via the micro:bit's serial port. 

  14. LED Display 
    The micro:bit's LED display may only be 5x5 but it is very versatile. If you want to make use of it directly then you are going to have to master some lower level functions.

 


Getting micro:bit development setup on the desktop isn't exactly the same as getting a general mbed development system setup up but it is very nearly the same. The only real difference is the library used to support the hardware.

To work locally we need to install the mbed build system Yocto, a suitable compiler tool set, and something to use as an editor.

Fortunately this is all surprisingly easy once you know how.

Build systems 

The biggest problem facing the embedded programmer is the apparent complexity of the build system. Back in simpler times an embedded C program might grow to a few hundred lines, perhaps with a simple single support library. Today libraries such as mbed or the microbit library are complex and need other software components to complete them. You could manage these manually, which is what we did in the past.  Each time you included a header you would have to find and resolve references to other headers and then find the object files corresponding to these all of the libraries and link them into the final program. When the total amount of code was small this was entirely possible but, as already mentioned, this is no longer the case. 

The solution to the problem is a build system which can do the job for you automatically. You may already know build tools such as make that run scripts that invoke the compiler and linker on all of the necessary files. A build system goes beyond this in that it will also deduce dependencies, perhaps even get the files needed, and incorporate them into the build. An example of such as system is cmake which generates make files for you.

For the mbed project a new build system called yotta was invented and it is currently under development. Offline development without using yotta is possible, but it would require you to manually organize all of the files needed to build the project and create the commands for the compiler and linker to perform the build; yotta can do this for you. 

In fact yotta generates cmake files which are then used to build the system using make or ninja which is the default build engine. 

There are some problems with this approach. The first is that yotta is a complicated system and if things go wrong you can be left wondering what to do. Complex systems like yotta are great when they work, but quickly become a nightmare and a time waste when the don't. The second problem is that this is something new to learn. There are enough problems in embedded programming without adding another layer of complexity and, of course, the need to learn something new feeds back into the difficulties of working out what is going wrong. 

However in practice yotta turns out to be easy to use and mostly reliable. 

How yotta works

You don't need to know how yotta works under the covers to use it, but a little knowledge is helpful. 

Yotta works with modules which roughly speaking is a group of code and header files that does something. A yotta module includes a json format file which provides metadata such as its name, licence, author and any modules that it makes use of i.e. its dependencies. 

You don't have to create the json file manually yotta has commands that do it for you. 

Yotta also supports the same system for an executable and when you tell yotta to build the executable it reads the json file, gets the modules which are needed and then creates cmake files to build those modules for a specified platform - the target. Finally cmake generates files that are actually used to build the entier project using ninja or one of other make systems that it supports. 

This is complicated because you have yotta feeding cmake which feeds ninja which does the final job of compiling and linking the code. 

If you are feeling confused it will make a lot more sense after you have seen yotta in action. 

Notice that as well as automating the build of your program you can also use yotta to create and publish modules that can then be used by other people. 

Installing yotta

If you are installing on Windows don't worry too much about the web pages that give you long and detailed instructions on how to manually install yotta - unless the automatic installer doesn't work for some reason. 

For most users installing yotta on Windows is trivial.

Download the yotta installer.

Run the installer and follow the instructions. The installer will offer to install:

python - which is what yotta is written in

gcc - the gcc ARM cross compiler which you need to compile programs for the micro:bit

cMake - which yotta needs to generate make/ninja files

ninja - which actually runs the compiler to generate the program

yotta - which of course is what you actually want to use

 

A typical selection of options can be seen below: 

 

 

If you want to use either of the git-scm or mercurial version control programs then install these as well.

The mbed serial driver will let you establish a serial connection to the micro:bit for debugging and other uses. If you don't already have it installed it is worth adding to the list. 

The installer just gets on with the job and when it finishes you should be able to use yotta.

For OSX you can simply download and drag the yotta app  OS X yotta.app into the applications folder. When you run it a terminal will open.

Under Linux there is no installer, but it is easy to set things up using the package manager. You need to install the latest Python 2 - which is usually installed by default. You also need the latest python-setuptools, cmake, ninja and pip. You can then use pip to install yotta. You calso need to install the ARM cross-compiler gcc-arm-none-eabi. On Ubuntu you need gcc-arm-embedded instead.

You can find detailed instructions on all installations on the mbed website http://yottadocs.mbed.com/.

Before we move on there is one more dependency that is only needed for the micro:bit. The compiled binary is converted to a hex format file suitable for flashing the micro:bit's memory. This needs a utility called srecord. This you can get from Sourceforge as a source file that you can build yourself. Alternatively, and more simply, you can download a binary for Windows from:

https://sourceforge.net/projects/srecord/files/srecord-win32/

Simply unzip srec_cat.exe into the yotta directory.

If you don't do this then yotta will complain that it can't find srec_cat.exe in the final stage of the build and you will not have a .hex file in the output directory. 

Notice that while the micro:bit needs srecord other mbed project targets don't.

Creating a micro:bit project

You don't need to know all of the yotta commands to create and work with a micro:bit project. 

Open a command prompt and move to the yotta installation directory - usually c:/yotta on Windows - and run the batch file run_yotta. This sets the paths correctly to find all of the programs involved in using yotta. Alternatively you can run yotta via the icon that should have been installed on your desktop.

To create a new project you first create a new project directory and while in the directory use the command:

yotta init

For example, under Windows: 

mkdir pulse
cd pulse 
yotta init

At this point you will be asked a set of questions:

  1. Name of module - defaults to name of directory
  2. Initial version - defaults to 0.0.0
  3. Is this an executable? - Yes in this case, defaults to No
  4. short description - whatever you want to put
  5. author - whatever you want  to put
  6. licence - your preferred licence you want, defaults to Apache 2.0

Yotta will create a set of folders complete with a json file describing the project:

At this point the source and test folders are empty. These are where you are supposed to put your code files for the project and any tests you want to implement. 


Adding a dependency on the microbit library

You could start populating the source folder with whatever program you wanted to write, but an micro:bit program has a dependency on the microbit library which needs to be added. How this is done is slightly different from installing a standard mbed dependency.

At the moment the microbit library isn't hosted by mbed and so you have to download it from its github repository. However, before we can add any dependencies we have to set the target for the project. In principle you only have to do this once for any yotta session, but in practice you might find that it gets forgotten or overwritten. For the micro:bit you set the target using:

yotta target bbc-microbit-classic-gcc

This adds a yotta_targets directory with some files that are needed for the target.

device

 

After this you can add the dependency to the microbit library:

yotta install lancaster-university/microbit

This might take a few minutes because the library is downloaded from GitHub.  After it is complete you will see a module folder has been added to the project:

 

 

 

 

 

 

This contains the code for the libraries you might want to use in your programs:

At this point you can build the project. If you plan to stay with yotta and use a simple editor to create your C/C++ files then this is how you will build your application each time. If you plan to move to an IDE such as NetBeans then the IDE will make use of the ninja files created to build your program in the future. 

So we just need to use yotta to build the project. The problem is that there are no files in the source or test folders and yotta will simply ignore any empty folders in the build. To give the system something to build we need to add a .cpp file in the source folder. 

Use any editor to add something trivial like:

#include "MicroBit.h"

MicroBit uBit;

int main() {
 uBit.init();
 uBit.display.scroll("NEW PROGRAM");
 release_fiber();
}

and store the result as main.cpp or any name you care to use. 

Now we can build and test the program using:

yotta build

If you watch what happens you will see the program build and it will tell you that the build files have been written to the  build/bbc-microbit-classic-gcc directory int he project. The important part is that you will find the hex file in the directory: 

pulse\build\bbc-microbit-classic-gcc\Source

The file that you need to download to the micro:bit is pulse-combined.hex and not the pulse.hex file. If you can't find a .hex file then check the messages produced by yotta and you will probably find that srec_cat.exe is mentioned as missing.

See the instructions given earlier about obtaining and installing srecord

You should be able to see the "NEW PROGRAM" message scroll past. 

This is all you need to do. You can now edit and add .cpp files to the source directory. If you need to add other dependencies then use the install command. Each time you use the build command new cmake and ninja files are generated so you can keep things up-to-date but also note that there is no point in editing any of the cmake or ninja files. 

To summarise:

To create a project first create a new folder and make it the current directory. To create the project folders and basic files use: 

yotta init

To target a project for the micro:bit use:

yotta target bbc-microbit-classic-gcc

To install the essential dependencies to run your new project use:

yotta install lancaster-university/microbit

To install any other dependencies use the same command but change the name of the module or GitHub project you want to include.

Add at least one .cpp file to the source folder and to the test folder if you want to construct tests.

To build the project use:

yotta build

The hex file you need to download will be found in 

project folder\build\bbc-microbit-classic-gcc\Source

and it will be called project-folder-combined.hex

Using NetBeans

While you can use the yotta route to building your programs, it is fairly easy to use an IDE such as NetBeans or Eclipse to make your job easier. The description here is for NetBeans, but getting Eclipse to work follows the same route. 

It is assumed that you have yotta installed and working and have built a micro:bit program using yotta. You do not have to run it, just build it, unless you want to check that everything is working. 

It is also assumed that you have NetBeans for C/C++ projects installed and working. 

You need to add the yotta directory to the path so that it can be found automatically by the build process.  In the case of Windows add c:\yotta to the existing path. 

Install Make

If you are working with Windows you also need to download and install a version of make that works under Windows. You can get this by installing cygwin or mwin but the simplest way is to download GNU make for Windows from:

 http://gnuwin32.sourceforge.net/packages/make.htm

Simply install it using the setup program and the default settings.

Set up Tool Collection

Your first task in configuring NetBeans is to set up the tool collection. The ARM cross compilers that you need should be already set up as part of the yotta installation. To specify where they are you need to run NetBeans and use the command Tools,Servers. If this is a new installation of NetBeans and you haven't created any C++ projects yet then the Server window might take some time to list C/C++ Build Hosts - wait for a few minutes. 

Navigate down to C/C++ Build Hosts and expand the localhost node. Right click on ToolCollections under localhost and select add New Tool Collection. 

In the dialog box that appears set the base directory to:

C:\yotta\gcc\bin

This is where the compilers are stored, but NetBeans probably won't recognize them. We could create a new tool set definition but it is easier just to ignore the warning and press on. To unlock the dialog you need to select one of the other compiler set types, say cygwin, and then set it back to unknown and you will find you can enter a name for the tool set. 

 

You now have to fill in the details of where to find the compilers etc manually as NetBeans didn'ta automatically locate them:

  • C Compiler
    C:\yotta\gcc\bin\arm-none-eabi-cpp.exe
  • C++ Compiler
    C:\yotta\gcc\bin\arm-none-eabi-g++.exe
  • Assembler
    C:\yotta\gcc\bin\arm-none-eabi-as.exe
  • make 
    C:\Program Files (x86)\GnuWin32\bin\make.exe
  • Debugger 
    C:\yotta\gcc\bin\arm-none-eabi-gdb.exe

Not all of these are required and you might have to change the directories depending on where things were installed. 

toolset2

 

The final step is to make sure that c:\yotta is in the path. To do this right click on This PC, select properties and select Advanced system settings. In the dialog box that appears click the  Environment Variables button and edit Path in the System Variables list. Add c:\yotta; to the start. Note: there are other yotta directories already in the path but you will still need to add c:\yotta. 

 

Import the Project

With the tool collection specified we can now import the project we created using yotta. Simply use the command File,New Project and select Project With Existing Sources:

 

 

Finally navigate to the project directory
 \build\bbc-microbit-classic-gcc

and import the project:

 

 

It takes time for the project to import, but when it has you can try to build it. In many cases you will see the message:

ninja: no work to do.

because all of the files are up-to-date following the clean rebuild NetBeans has just performed.

To give it a good test you need to edit the C++ source file and then rebuild. Alternatively you could try another full clean build but this will recompile the library so it is slow. 

 

 

If you get an error message to the effect that ninja.exe cannot be found then you are missing c:\yotta in the path and you need to add it - see earlier. If you change the path variable you will need to restart NetBeans for it to have any effect. 

You can now edit the source file and add files to the project and in most cases the build will compile the changes.

Notice that you cannot use the Run command as NetBeans has no idea how to run the file that it compiles. You need to use the Build or Clean Build commands - the "hammer" icons next to the green run icon.

 

You will find the hex file that has to be downloaded to the micro:bit in the usual directory

project folder\build\bbc-microbit-classic-gcc\Source

and it will be called project-folder-combined.hex 

So in this case the directory is

pulse\build\bbc-microbit-classic-gcc\Source

and the file will be called pulse-combined.hex 

If you need to add another dependency or make structural changes to the project then you need to return to using yotta to modify things. You usually have to build the project using yotta once before NetBeans notices the difference. The reason for this is that the changes are only put into the ninja files as part of a yotta build and NetBeans uses these to build your project. 

The fact of the matter is that yotta is in charge of the project and NetBeans follows what it says. 

From here you can explore other configurations and even automatic running of the project and debugging.

Now On Sale!

You can now buy a print edition of micro:bit IoT in C.

You can buy it from:

USA and World  Amazon.com
Canada              Amazon.ca
UK                      Amazon.co.uk
France                Amazon.fr
Germany            Amazon.de
Spain                  Amazon.es
Brazil                  Amazon.br
Italy                    Amazon.it
Japan                 Amazon.co.jp
Mexico               Amazon.com.mx 

 

 The full contents can be seen below. 

Chapter List

  1. Getting Started With C/C++
    Anyone who wants to use the BBC micro:bit to its full potential as an IoT device needs to look outside the coding environments provided by its own website. As an mbed device, however, the micro:bit  is capable of being programmed in C/C++. Here we look at how to use the mbed online compiler for a simple demo program.

  2. Offline C/C++ Development  
    We have already discovered how to use the online editor to create a C/C++ program. Now we are going to move to the desktop with an offline approach. This has the advantage that we can use any tools we care to select and no Internet connection is needed.
  3. First Steps With The GPIO 
    The most basic task when working with the micro:bit is controlling the I/O lines. This isn't difficult if you use the framework provided but there some subtle points to watch out for. This chapter looks a the basics of using the GPIO.

  4. Working Directly With The Hardware - Memory Mapping. 
    The framework makes working with the GPIO and other devices as easy as it can be but there are many layers of software to go through before you get to the hardware. Writing directly to the hardware can make things up to ten times faster and give you access to things that their framework doesn't. It is also an educational experience to deal with the raw hardware directly.

  5. Pulse Width Modulation, Servos And More
    In this chapter we take a close look at pulse width modulation PWM including, sound, driving LEDs and servos.

  6. I2C
    The I2C bus is one of the most useful ways of connecting moderately sophisticated sensors and peripherals to the any processor. The only problem is that it can seem like a nightmare confusion of hardware, low level interaction and high level software. There are few general introductions to the subject because at first sight every I2C device is different, but here we present one.

  7. I2C Temperature Measurement
    Using I2C devices is fairly easy once you have successfully used one - and hence know what information you need and what to look for in a working system. In this chapter we use the HTU21D temperature and humidity sensor as a case study of I2C in action. It also happens to be a useful sensor.

  8. A Custom Protocol - The DHT11/22

  9. The DS18B20 - One Wire Bus

  10. The SPI Bus
    The SPI bus can be something of a problem because it doesn't have a well defined standard that every device conforms to. Even so if you only want to work with one specific device it is usually easy to find a configuration that works - as long as you understand what the possibilities are. 

  11. SPI MCP3008/4 AtoD   
    The SPI bus can be difficult to make work at first but once you know what to look for about how the slave claims to work it gets easier. To demonstrate how its done let's add eight channels of 12 bit AtoD using the MCP3008.

  12. Serial Connections
    The serial port is one of the oldest of ways of connecting devices together but it is still very, very useful. The micro:bit has a single serial interface but it can be directed to use any of the GPIO piins as Rx and Tx. 

  13. WiFi 
    The micro:bit has a radio that works in Bluetooth LE and point-to-point ad-hoc mode, but at the moment it lacks WiFi connectivity. The solution is to use the low cost ESP8266 to make the connection via the micro:bit's serial port. 

  14. LED Display 
    The micro:bit's LED display may only be 5x5 but it is very versatile. If you want to make use of it directly then you are going to have to master some lower level functions.

 

More Information

developer.mbed.org

http://lancaster-university.github.io/microbit-docs/

Related Articles

The BBC Micro:bit Is An Mbed Device In C/C++ 

Commando Jump Game For The Micro:bit In Python 

BBC Micro To micro:bit 

 

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on, Twitter, FacebookGoogle+ or Linkedin.

 

 

comments powered by Disqus