Al Williams

Dr. Dobb's Bloggers

Mbedded Systems

April 30, 2014

The Mbed libraries use objects to make accessing the embedded hardware on supported ARM platforms very simple.

I've been working with two different ARM-based systems lately. One is an inexpensive Freescale FRDM-KL25Z and the other is a simple breadboard with an LPC1114 CPU in a DIP package. These are pretty powerful 32-bit processors with a built-in clock that will run at 48MHz. The Freescale board has 128K of flash and 16K of RAM while the LPC1114 CPU has 32K of flash and 4K of RAM. The Freescale board is a bargain at about $13, and the LPC1114 chip is just a few dollars (and doesn't require much more than a 3.3V power supply).

I wanted to focus on the software side of things this time. If you've tried using ARM processors before, you may have found it difficult to get a toolchain (unless you bought one or used a commercial offering). Things have gotten better, but even with a toolchain you may have found you needed vendor-specific startup files and library code. ARM's done a lot to make that better, too, with some standardized libraries like CMSIS.

The Mbed library (I almost hate to call it a library, since that's only part of what it is) solves just about all of those problems. Much like the Arduino, the Mbed libraries use objects to make accessing the embedded hardware on supported ARM platforms very simple. On supported hardware, it handles all the startup, interrupt handling, and pin multiplexing. You just write code using abstractions for the platform hardware. The library is open source (it has not always been open source, but now it resides on github).

The toolchain isn't a problem either. The Mbed website has a complete IDE that lets you write and compile code. You can import a large number of examples and specialty libraries right from the IDE (see the figure below).

The IDE compiles your code (and does a nice job, creating smaller files than my copy of the GNU compiler) and downloads a .BIN file. For the LPC1114, you can program the chip using the onboard bootloader or via a debug probe like an inexpensive LPC-Link board. I'll talk about the details of that setup soon.

For this time, though, I wanted to focus on the FRDM-KL25Z board. It plugs into your computer and appears like a USB drive. Simply copying the .BIN file to this USB drive is sufficient to program the board. You do need to update the loader on the board to make this work with Mbed, but that's a simple process.

The FRDM-KL25Z has some fun peripherals including a three-color LED, an accelerometer, and a capacitive touch sensor. It also has USB ports. Without some help, it would be quite a few lines of code to handle the processor reset, the switching of all the I/O lines to the right devices, and then actually driving or reading the device. The Mbed library takes care of all of that for you.

Consider this example that I put online (this is a modification of one of the default examples provided). The three-color LED appears to software to be three distinct LEDs and setting them up to drive using pulse width modulation (PWM) is trivial:

Note the libraries use floating-point numbers, which is no issue for the compiler. The PWM (and the touch sensor) use a number from 0.0 to 1.0 instead of the more common integer representation.

This is pretty easy. Yet, you still have access to a full C++ compiler and all the features of the CPU. The development effort is on par with the ubiquitous Arduino, but with a lot more horsepower than a typical Arduino.

One of my usual complaints about the Arduino is that without doing gymnastics, you can't really do hardware debugging. The same is true here. As nice as the Mbed online IDE is, it doesn't support debugging (although you are free to pepper your code with printf statements, of course). Debugging is possible on the Arduino with some extra work and the same is true here.

To do debugging, you need an external debug probe and a local IDE. The Mbed IDE can, sort of, export your project to an external IDE for just this purpose. I'll talk about the options along with some more about the custom hardware next time.

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task.
However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Video

This month's Dr. Dobb's Journal

This month,
Dr. Dobb's Journal is devoted to mobile programming. We introduce you to Apple's new Swift programming language, discuss the perils of being the third-most-popular mobile platform, revisit SQLite on Android
, and much more!