CategoryCode

Useful Code Snippet: AVR and C++11

U

Despite what a lot of people say about the Arduino crowd and using C++ on a microcontroller, I like it.  Sometimes it incurs a performance or code-size overhead, but I’m not often approaching the RAM or Flash limits of an Atmega 328, or especially an Atmega 2560.  When appropriate features are used, C++ results in more readable code.  Lately, I’ve branched into using templates with some AVR Code.  Here’s a super useful snippet for creating a quick ringbuffer:

No malloc or new to be seen here.  As long as equality operators and copy constructors are sorted, it should work with arbitrary types (although I’ve only tested it with built-ins and simple structs).

C++11 also allows you to typedef templates, like this: typedef AVRRingBuffer<char, 25> SerialBuffer;

ARM Cortex-Mx Quickstart

A

By now, almost everyone’s managed to acquire a bajillion cheap ARM dev boards, and there are always more coming.  As these chips get cheaper, and available in more hacker-friendly packages, they’re going to overtake chips and boards like the AVR and the Arduino, it’s just a matter of time.  Unfortunately, the ARM ecosystem isn’t as simple to work with as the AVR or PIC ecosystem.  With both of these 8-bitters, you’re working in the manufacturer’s walled garden.  ARM manufacturers, on the other hand, are free to glue whatever crazy stuff they want onto the ARM core.  As a result, peripheral sets and their use vary widely, even within the same manufacturer.  Efforts like the CMSIS ameliorate this effect to some degree, but this isn’t a cake-walk.  Add on to that the difficulties in simply getting a toolchain up and running, and anyone with mere Arduino experience is lost (even more experienced 8-bit devs are going to have some tough going).

So, here’s a short guide to getting a toolchain for the STM32 series up and running quickly.  These instructions will be specific to Linux, but should translate to OSX or Windows fairly easily.

First, I downloaded the Linaro Bare-Metal ARM toolchain (http://www.linaro.org/downloads/).  It’s important to get the Bare-Metal toolchain.  Other toolchains generate binaries that aren’t capable of running sans operating system. The bare-metal toolchain is at the bottom of the page.  This package contains the compiler, linker, debugger, and other tools used to turn source code into machine code.  I used the precompiled package from Linaro because it’s a simple off-the-shelf method to get up and running quickly.  Unlike Codesourcery/Sourcery Tools, the Linaro toolchain supports the Cortex M4 with FPU right out of the box.

In the past, I have attempted to compile their own toolchain (https://github.com/esden/summon-arm-toolchain/), but this is not a simple process.

In Linux, I just extracted the tar.gz file to the folder I wanted it to live, and added that folder to my PATH.

Next comes OpenOCD.  OpenOCD is an open source debugger/flash utility for lots and lots of different chips.  I had to get the latest dev version from the git repo, (http://openocd.sourceforge.net/repos/), as the latest stable release does not include STLink code.  If a version beyond 0.5.0 is released by the time you’re reading this, download that instead.

Installation was pretty easy.  I installed the dependencies using “sudo apt-get build-dep openocd”, then ran “./bootstrap”, followed by “./configure –enable-maintainer-mode –enable-stlink”, followed by “make” and “make install”.

Once I had the toolchain and debugger up and running, I downloaded some software to compile.  This github repository (https://github.com/nabilt/STM32F4-Discovery-Firmware) includes the test firmware provided by ST, with a Makefile capable of building it using GCC. Chibios (http://chibios.org/dokuwiki/doku.php) also includes demo files for all the discovery boards.

The first time I attempted to build the STM32F4 firmware, it didn’t quite work out.  Due to the varied nature of the ecosystem, a separate linker script is needed for each chip.  Furthermore, different compilers need different sections to their linker scripts, so it might not be possible to simply yank one from somewhere else.  The linker script tells the linker where different memory sections map into real, physical memory on the chip.

Here’s the linker script I eventually got working for my STM32F4-Discovery:

Makefiles themselves are beyond the scope of this post (and beyond the scope of my brain, in a lot of ways), but taking a peek at the Makefile used in the STM32F4 Demonstration firmware from above, there are  a boatload of dependencies from outside the project.  These are all CMSIS drivers, or required startup code.  This is a big part of why ARM chips are more difficult to use than ARM or PIC chips.  Fortunately, most manufacturers release some boilerplate startup code for their chips.  As long as it’s included in the makefile, everything will work as it should.

And those are the basics of going from nothing, to compiling demonstration code.  Right now, I’m looking heavily at the ChibiOS platform to develop some projects.  This abstracts a lot of the hardware stuff, although it isn’t nearly as simple as Arduino for getting up and running.  Hopefully with the upcoming release of the Arduino Due, there will be some development in making a really easy-to-use ARM platform.

Introducing: Arduino Simple Task Scheduler

I

Get the code here: https://github.com/Zuph/AVRQueue

Introducing the Arduino Simple Task Scheduler.  This is part of the balloon flight computer code I wrote for White Star, with some more polish. This library allows you to create a schedule queue of functions to be executed at specified times, on specified intervals.  For example, say you’re trying to log some sensor data and update a display in the same program. With the task scheduler, you can simply write a function to gather sensor data, write a function to update the display, add them to your queue, and let the library handle the rest.

This isn’t really useful for blinking LEDs, but it’s great for complex systems. For example, the balloon computer was gathering sensor data, sending short reports, sending long reports, monitoring vertical speed, monitoring GPS Status, monitoring flight state, managing ballast, and managing a backup high-frequency radio at the same time.  Halfway through development, it was obvious that we would need to integrate a watchdog timer to keep other systems from freezing the flight computer.  If all of these tasks had been occurring simultaneously, spread throughout spaghetti code, it would have been very difficult to add watchdog resets in all the right places.  With the task queue, I simply defined another function that reset the watchdog, and put it in the queue.  Two minutes, tops!

You can find extensive documentation and examples in the Github project.  To install, just copy the “Arduino” directory contents to the “Libraries” folder of your Arduino IDE install.  Restart your IDE, and it should pop right up.  Here’s a really simple example program:

 

 This will print “Hello: X” where X is the number of milliseconds since startup, starting 5 seconds after startup, and repeating every 1 second.
The Arduino library has some limitations, so I’ve also included an AVR “library” (just a couple of source files to include in your project).  This one’s a little easier to tweak to your specific application, and doesn’t suffer some of the same drawbacks as the Arduino library.  That said, the Arduino library will be find for almost every project out there!  The limitations are listed in more detail at the Github site.
If you find any bugs, let me know! Submit a Github issue, fork, fix and submit a pull request, or contact me directly!  If you find this useful, let me know!  It isn’t a lot, but I hope it’s well documented, and easy to use/read/understand.