TI MSP430 software developers need a little help

I’ve been working for a while on using MSP430 microcontrollers. We selected them for a bunch of reasons, including price, availability, physical size, USB support, and preloaded bootloader. As it turns out, a few of those weren’t quite in the shape we expected.

Programming difficulties

First, the bootloader is really a bare minimum. It does not cover such features as programming the flash memory on its own, so tools like python-msp430-tools download a secondary bootloader into RAM in order to function. That bootloader was presented as a binary blob, although with much searching it is possible to find the Custom Bootstrap Loader sources via a link within application note SLAA450 “Creating a Custom Flash-Based Bootstrap Loader (BSL)”. It’s also explicitly mentioned at the Software Tools section, with a link titled Open Source, but that goes to a wiki which does not provide this link. In the end, however, I gave up on that because not only is it obviously not free software or even open source, it completely failed to communicate once loaded. I ended up writing a workaround based on the user guide and BSL protocol guide (buried in an mbox file here, if you need it).

USB framework

The MSP430 USB Developers Package provides example code for USB. In fact, it contains no less than 52 copies of the same USB library – in turn divided into 4 USB device class sections and one common, all with lots of code duplication. It makes it all too clear that no forethought has gone into what’s a common section, as there’s no common code for talking to other endpoints than number 0; the rest is not only duplicated among classes, but present multiple times for each.

Once I got my code at least partially working with the USB library, I found some odd bugs – for instance, the MCU could hang when sent a bunch of data in quick succession. I tracked this down to an issue that’s not limited to the USB code, but in fact present already in SLAA294 “MSP430 Software Coding Techniques” – the starting point for pretty much all MSP430 code.

Interrupt flow chart
The above flowchart is based on one in SLAA294, and illustrates the combination of interrupts to react to events with a main thread that uses power saving sleep modes. The USB code didn’t even manage to follow this much, by the way; it was more sensitive to interrupt timing because the “Clear flag_1” portion was moved to after the “Execute flag_1 handler” section, meaning it could miss if this flag was applied again. However, this is only part of the problem.

There are two fundamental errors in the flowchart. First, there is no exit shown from the “Enter sleep” step, although it does continue to the first flag test once woken up. Secondly, the interrupts do not cause control to flow into that same test; they will return to wherever the main thread was. This could be anywhere within the main loop, including just before the “enter sleep” step – in which case the MCU will dutifully sleep until the next interrupt. For a simple example of this, consider what happens if first event 2 occurs, then event 1 while the main thread is at the flag_2 test step the second time around.

I propose a fairly simple solution. We add one more variable, let’s call it SR_sleep, containing the SR value used to enter sleep mode. When the ISRs decide to wake the main thread, they set not only the SR but also SR_sleep to active mode. Then the Enter sleep step is modified to contain precisely two instructions: One copies SR_sleep into SR, which will cause the main thread to sleep if SR_sleep has not been modified by an interrupt. The second sets SR_sleep to the sleep value. This acts similarly to the flags used to identify specific events, except there is no test; SR_sleep is set back to the sleep state immediately on wakeup, as we already know an interrupt must have occurred. This completely removes the window of time in which an interrupt may set a flag but fail to stop the main thread going to sleep. The trick revolves around the MSP430 not interrupting within an instruction, and being capable of loading SR from a memory variable in one instruction.

It gets somewhat more complicated if the main thread uses multiple sleep levels. In that case, the instruction resetting SR_sleep should read from a variable where the desired sleep mode is stored.

Expanding the computer’s interface

I’ve recently ordered yet another gadget – an oversized android tablet. The intent is to fill a role somewhere between my laptop, e-reader and phone, but also to provide more work area (the main reason I always want higher resolution displays). There just isn’t enough on just two monitors, and I’ll never quite understand why Dell saw fit to put seven video outputs but only two display controllers in the laptop.

My initial simple idea for how to do this involves using a larger framebuffer and VNC to display an off-screen section over the network. Or perhaps distributed multi-head X. I might have to tweak my window manager’s idea of what screens are about a bit, but it should fit neatly into the existing Xinerama support. That should cover getting a picture up as a third screen.

It doesn’t quite cover another issue I’ve been feeling a bit; the controls of my windowing system aren’t aging well. With the Alphagrip I’m already feeling that the super-shift-digit binding for moving windows is impossible, and the tablet won’t have any keys at all when I’m using it away from the work terminal itself (unless it’s in a dock). So it’s time to look at other schemes, like tagging windows and using gestures.

A few programs have their own gesture support, such as Xmonad, Blender, gschem, epiphany and firefox (some of those only through extensions). But we can do better, and I believe I shall try with easystroke – a gesture recognition tool that can send custom commands to other programs. It’s not proper TUIO control (which would support multitouch), but it’s a start.

Piano Booster

I rather like performing games like SingStar or Performous, but what they can teach you is in general rather limited. Most of your technique will be based on practice, and with things like Rock Band, that’s not going to be much good because the instruments aren’t really playable (though Band Hero’s drum set comes closer, being a MIDI set). In addition, they tend to use a simplification of the standard notation.

Recently I came across an exception. Piano Booster plays common MIDI files and expects you to play along with a MIDI instrument; I’d encountered keyboard specific ones before, but this time is uses standard notation. In addition, the difficulty is much more tunable than I’ve seen elsewhere – you can choose what track to play, adjust tempo and transpose, restrict hands (for piano) and even get it to pause the music until you get the chords right. In short, it’s more of a trainer than a game, and a competent one at that. For more details, check their website.

(Found, like many of the things I hear about, via Debian.)

TestDisk & PhotoRec

Allow me to present you with a few scenarios, all of which recently happened.

    A friend intended to boot his Windows partition, in order to update a laptop BIOS. By mistake he picked the “recovery” partition, easily done when GRUB’s OS prober can’t tell them apart. Without warning, it erases his GNU/Linux partition, leaving him stranded without a functioning bootloader (it couldn’t be bothered to install a functioning MBR while overwriting that sector). Luckily, he has a bootable USB memory, but all the data he cares about is in the lost partition.

    Another friend is presented with a freshly erased memory card off a camera, from which photos need to be recovered.

    I wanted to extract the music from a Playstation Portable game I own.

This is exactly what the two tools TestDisk and PhotoRec help with. The first finds lost file systems, and the second finds lost files. Both are incredibly easy to use and should be in your disaster recovery arsenal. They work, in many situations (don’t be fooled by “Photo” in the name), and are quite free. This is why I cared not one iota when my Lexar memory card didn’t come with the promised Image Rescue software.

FrankenCamera

My question on camera selection, for the moment, has one answer. Unfortunately I can’t afford it, and it still needs a lot of work. The Frankencamera project now supports one consumer available hardware platform, the Nokia N900. There’s lots going against it, too, such as really slow switching between live preview and high-resolution photo capture, but they are things I could work on – if I had anything it worked with. I might look at using it with UVC, but it won’t be nearly as useful as the raw sensor access it has in the N900.

Literate programming

That’s right, I’m finally beginning to take my first small steps towards literacy. I’ve known of the concept for quite some time, joining documentation and program code together into a unified document, but haven’t really been using it. Sure, I’ve used plenty of automatically extracted API documentation, but rarely (if ever) written any. And today, I needed something slightly different – I needed a report on a programming project.

As with earlier reports, I fired up LyX, because I’m a sucker for easy interfaces. I’m not really at home in LaTeX, and had prior experience that LyX could make entry of formulae, tables and such easier. This time, though, I needed some circuits, state diagrams, and above all, source code. So, looking at the options, I found LyX now supports both Noweb and Listings. So I sat about writing bits, documenting the circuit using CIRC, and inserting code with Noweb “scraps” as LyX calls them. Pretty soon, this got me tired.

LyX provided me with two options for the source code: scraps, where I had to use Ctrl+Enter to get halfway reasonable spacing, and had no indentation or syntax assistance, or Listings, where code was reformatted for printing but not in the editing view. Besides, my CIRC drawing was just literal code anyhow, so LyX didn’t help very much in the WYSIWYG department. Even looking at the file, it was clear that LyX was just adding overhead – my document would be cleaner in Noweb directly.

Having written just a little code inside LyX, I now knew I wanted back to a proper programmer’s editor. That meant Emacs or Vim. Emacs did open Noweb documents happily, but the syntax highlighting turned out to be a bit bipolar. It was switching, depending on the cursor (point?), between TeX and C sub-modes, and reinterpreting the whole document each time – which destroyed the source context. I did find a simple workaround by using /* and */ in TeX comments, letting the C mode know the text wasn’t code. Not really a big deal, but I’m not used to Emacs, and this swapping (reminescent of per window palette switching in X) was annoying either way. Vim is usually my editor of choice, but it didn’t recognize Noweb at all. For Vim, I found a few scripts, and the highest rated one actually worked. It’s not perfect – it has a few hardcoded languages it can recognize within Noweb – but it’s easy enough to modify if needed, and it does the job.

Noweb style programming is a considerable change for me. My code is now migrating from lots of different files into one larger document, within which I’m writing the structure of the code in an easier, modular fashion. It’s not perfect, but I’m learning. The current question is why double dashes (as in decrements in C) are converted to single ones in print. The same thing even happens here in wordpress. Still, a few steps forward.

54321, a forgotten game pack

The other day there was some discussion of a 4-dimensional game in an IRC channel I frequent. This immediately led me to think of 54321, a collection of 5 games in 4, 3 or 2 dimensions for 1 player – it’s the first four-dimensional spatial game I played. So I looked it up again, and found no hint of its existence on the author’s site (apparently now Mac-dedicated). I’d expect at least a mention of why it was taken down, but the page is simply removed. The source is still available in various places (my own copy), though, and still works today. It requires SDL and SDL_Image.

Fossil: project management on the quick

Sooner or later, development projects need some revision tracking. Usually right about when you either need an experimental branch for a new feature or sharing the project, which would include releases. You’ll also need to document the work, and if you’re maintaining it at all, probably track issues. Even better if this can all be done publically.
Traditionally, all these tasks are done in central repositories with specialized tools – perhaps RCS (with descendants like CVS and Subversion), Bugzilla, and so on. They’ve been more or less difficult to set up and serve, which lead to services like Sourceforge, Github, and Google Code. There are tools to handle the combination, like Trac. Most of these work, and sometimes they’re just the thing – because you know you’ll want to share the project and spend the time to set up that infrastructure.
Other times, you’re just doing a quick hack. And then you give it to someone. And, two months later, you run into an indirect friend who’s using that same hack, with their own changes, and experiencing an issue you solved later on.. but the code has grown so much you can’t easily track down the changes needed, let alone figure out which release their version is based on.

We’ve seen a move lately towards distributed revision control, with the likes of Git, Mercurial, Darcs, Bazaar and so on. They can, and do, solve the issue of independent development – but only if people use them. Mostly that tends to get stuck on either learning how to use them, or having the tool available. The first is an issue mostly because each tool is different, and the second because they have varying requirements. This is not at all unique to revision control; people hesitate all the time to install software because of complex requirements and so on.

Fossil is a project management tool intended to solve some of these issues. It’s not necessarily best at anything it does, but it does it with a minimum of setup. It has a discoverable web interface, works as one program file, stores data in self-contained files, and offers revision control, a wiki, account management for access, and issue tracking. All set up at a moment’s notice, anywhere. Of course there’s a command line interface too.

I intend to use it for a few minor projects so I get a good sense of how it’s used. At this moment, the most nagging question is if it does anything like Git’s bisection (also available in Mercurial), which is very convenient when tracking down regressions.

Text mode web browsers

Recently I encountered a quiz question about what a “command line web browser” for GNU/Linux was called. It was apparent that text mode was intended rather than command line – a command line program won’t actively use the whole screen, only the (current) command line itself. What caused more confusion, though, was that several of the answers were existing text mode browsers.

Both Lynx and Links were mentioned. The latter is about in at least two major flavors, ELinks and Links 2. The latter actually supports graphics more and even Javascript, but it’s lightweight enough that it long was my main browser (currently I tend to use Iceweasel, for convenience). The final text mode web browser that came to mind was w3m, which also bears the somewhat unusual distinction of supporting images inside a graphical terminal.

Metakit

Some years ago, I encountered a neat puzzle game called Deadly Rooms of Death. While an interesting game in itself, this post is not about that. Having caught my eye, though, I had a thought to port it to some portable gaming platform. I made some effort, unfinished, to port it to Nintendo DS, and then found out more about its structure.

During Software Freedom Day 2009, this came to mind again, as someone mentioned column-oriented databases. DROD happens to use one of these, which also eschews the tradition that database engines are squeezed into SQL interfaces. Instead it’s an embeddable library, with support for file based, in-memory, or indeed overlaid databases, simultaneous reads and writes, and automatic format conversions. It’s called Metakit. This quite remarkable bit of software struck me as convenient yet remarkably unknown. It would do an excellent job in many places SQLite is now used, and I’ve long had a slight distaste for that engine because it not only relies on reparsing SQL statements, it forces conversion to text strings for any data content – particularly numeric entries. I vaguely hope this post will bring Metakit some deserved attention.