Getting started with mbed – ARMs new IoT platform and the Nordic Semiconductor nRF51-DK

Aaron ArdiriBlogs, Tutorials

Tweet about this on TwitterShare on FacebookShare on LinkedInShare on Google+

mbed-enabled-logo ARM has been quite active recently with various announcements in the IoT space so we decided was time to pull out the various mbed enabled devices we had access to in our office and take the mbed development environment for a spin. This tutorial will walk you through compiling your first mbed application and deploying it to mbed enabled micro-controller. For this tutorial; we will be using the Nordic Semiconductor nRF51 Developer Kit.

So; what exactly is mbed OS?

mbed OS is a modern full-stack operating system that is designed specifically for ARM Cortex┬«-M-based MCUs; … optimised for energy efficiency, connectivity, security and reusable software components, it will become the foundation that enables widespread innovation in the IoT space.
developer.mbed.org

ARM hasn’t just launched this initiative; they have been working hard to establish partnerships with various micro-controller hardware providers that utilise the Cortex-M based processors to launch with multiple development boards available – with over fifteen different vendors already providing hardware to developers at the time of writing this post.

Obtaining mbed enabled hardware

A number of reference hardware is already available for use with mbed – it is easy to see when a board is mbed enabled as it will typically have the “mbed enabled” logo printed prominently on its PCB.

mbed-nordic-nrf51-dk

Creating a mbed developer account

ARM has created an excellent developer community website that hosts not only information on the platform itself; but they also wrap the various tools required to develop for mbed directly from the browser with a web based IDE – meaning there is no need to download compilers to get started.

http://developer.mbed.org

Registration is free and it does not take long to create an account to get started. This is a welcomed relief for those who find other development environments frustrating when it takes hours to download, install and get all the tools and environment set up correctly.

Add target platforms to your compilation environment

In order to get started with mbed development; it is important to specify the hardware targets that you wish to target – each micro-controller may be slightly configured differently so in order for the online tools to generate the right binary it is important to select the right hardware to target.

In our case we are using the Nordic Semiconductor nRF51-DK – when viewing this while logged in it is possible to include this in your environment by clicking on the “Add to your mbed Compiler” button displayed on the page. If you have other mbed enabled devices; you could search for them and add them to your environment as well – you will be able to switch between target platforms just before compiling.

mbed-select_platform

Accessing this option in the web based IDE is done by clicking on the target platform (if any) shown in the top right hand corner of the development environment.

Create your first mbed program

Within the web based IDE; you can create a new program by clicking on the New button in the menu bar. This will offer you a number of applications that have been uploaded by the hardware manufacturer showing off the various features of the device to compile and deploy to the device. However; let’s take a step back and look at creating a much simpler mbed application – click on the Import button instead.

mbed-import_program

For this tutorial we will import the mbed_blinky program; a “Hello World” application utilizing the LED on the development board and goes into an infinite loop by setting the digital out PIN associated to the LED to HIGH (on) and LOW (off) every 200 ms repeatedly showing you the application is running by blinking the LED.

Within the IDE; it should present a new project with the following main.cpp file:

#include "mbed.h"

DigitalOut myled(LED1);

int main() {
  while(1) {
    myled = 1;
    wait(0.2);
    myled = 0;
    wait(0.2);
  }
}

It has a very familiar “Arduino” feel to it; but slightly refined – you specify a global variable referring to a DigitalOut object, that is associated to the PIN defined by the constant LED1 and then in the main application loop turn toggle it’s state with a small delay in between.

Unfortunately; “LED1” may not be correctly defined for every environment so it is important to check the reference pin out to know exactly which PIN to use for the LED’s on the board. For the nRF51-DK, there are four LED’s which are named P0.21, P0.22, P0.23 and P0.24 respectively.

A simple change is required to the source code:

- DigitalOut myled(LED1);
+ DigitalOut myled(P0.21); // LED1 (refer to pin-out on back of board)

Once this is done; you simply select the target platform – in this case it will be the platform named “Nordic nRF51-DK” and then press the “Compile” button within the web based IDE. If everything goes ok; it should offer you the following file for download:

mbed_blinky_NRF51_DK_.hex

You have now successfully compiled your first mbed application and are ready to deploy!

Deploying your mbed application

When you connect an mbed enabled device to a computer via the USB board it will mount the device as an external storage media device with the name “MBED” or “JLINK” depending on the hardware you are using. In the case of the Nordic Semiconductor nRF51-DK; it is also important to have the Segger J-Link drivers installed as well.

Deploying the application simply involves copying the hex file to the external storage medium. Once the copying is complete; the device will restart and launch your application – and then the drive will re-mount waiting for the next application to be written to it. Deploying subsequent builds is a matter of repeating the process.

If you are familiar with other IoT development environments; this may seem a little different initially as typically the local IDE uploads the application to the device automatically. The device will disconnect and automatically reconnect after it tries to load the mbed application onto the device. When unsuccessful, a “fail.txt” file on the external media will be created with additional information as to why it failed to deploy.

Conclusion

It is nice to see ARM making a serious effort to make mbed development available to a lot of developers without the need to download and configure compilers – the use of an online IDE environment also means that they can ensure the tools are up-to-date and as devices are added they can be easily accessed by developers.

There is a lot more to investigate in the developer.mbed.org website and how to specifically take advantage of specific features in various supported micro-controllers. We will continue to look into the platform over the next few days and we may report on our experiences further.

Currently; the mbed offering is limited to a few basic functions and whatever functionality has been provided by the various micro-controller manufacturers in regards to sample code. ARM has clearly stated it has large plans with the release of mbed v3.0 in the later part of 2015; specifically introducing a number of abstraction layers for connectivity across the various platforms and a more detailed operating system stack including a TLS/DTLS library.

In any event it is a step in the right direction and it looks to be a great platform for IoT.

We definitely look forward to mbed v3.0 where the BLE stack across the various mbed devices will be standardised – it will allow us to use a generic BLE protocol from Evothings Studio and build applications for iOS and Android across various hardware providers – something which is unfortunately fragmented a little right now.