Automated Deployment of MSP430 Firmware (Part 1)

I now have completed Part 2 of the article where I discuss the ansible playbook to deploy the firmware.

I have recently started looking into all the DevOps tools that have constant articles on Hacker News and reddit lately. I use linux around my apartment for all my machines (mostly debian based) and wanted a better way to control and configure them. As someone that works with python often, Ansible caught my eye. After reading about this cool demo, I wanted to try out Ansible on a Raspberry Pi cluster of my own but also add in some MSP430s getting programmed.

For those that don't know MSP430 is a ultra low-power microcontroller made by Texas Instruments. A great starting point for information is the TI LaunchPad website.

As a side project I sought out an automated way to deploy MSP430 firmware images to MSP430F5529 LaunchPads connected to Raspberry Pis. The master ansible server is responsible for compiling of the MSP430 firmware images and then using ansible pushes the image to the Raspberry Pis which then program the firmware.

Here's a simple organization chart showing how the firmware images flow.
Organization

Since the Raspberry Pi will be doing the programming of the MSP430F5529 it will need a tool for downloading firmware images. MSPDebug is a command line tool for downloading to MSP430s and the rest of the blog post will cover compiling it for use on the Raspberry Pi.

All the following compiling steps were run on a Raspberry Pi 2 running Raspbian.

Compiling HIDAPI

The first step will be compiling HIDAPI. From their GitHub page, HIDAPI is described as "A Simple library for communicating with USB and Bluetooth HID devices on Linux, Mac, and Windows." The library is used by libmsp430.so that we will be building later on.

There are a few dependencies I found I needed to build the library, so go ahead and install those first.

sudo apt-get update  
sudo apt-get install libusb-1.0-0-dev  
sudo apt-get install libudev-dev  

Now we can download the source code for version 0.7.0 from their GitHub Releases Page. We'll download and build in a "build" directory.

mkdir ~/build  
cd ~/build  
wget https://github.com/signal11/hidapi/archive/hidapi-0.7.0.zip  
unzip hidapi-0.7.0.zip  

After extracting we can now build the library.

cd hidapi-hidapi-0.7.0/linux  
make -j4 CXXFLAGS="-Wall -g -lpthread -lrt"  

If the make successfully completes you should have a hid-libusb.o file located in your current directory.

Compiling MSPDebugStack

Disclaimer: Texas Instruments does not officially support the MSPDebugStack on Raspbian or the Raspberry Pi. This is meant as a learning exercise rather than a production solution.

Next step will be compiling the MSPDebugStack from Texas Instruments. The source is avaiable from the TI website.

There are a few dependencies we are going to need to install for this as well. Go ahead and run the following:

sudo apt-get install libasio-dev  
sudo apt-get install libboost-all-dev  

Now we are ready to build

cd ~/build  
wget http://www.ti.com/lit/sw/slac460k/slac460k.zip  
unzip slac460k.zip  

Now we need to copy the hidapi library we built previously and the hidapi header file.

cd MSPDebugStack_OS_Package/ThirdParty  
mkdir include lib  
cp -p ~/build/hidapi-hidapi-0.7.0/linux/hid-libusb.o lib  
cp -p ~/build/hidapi-hidapi-0.7.0/hidapi/hidapi.h include  

Now that we have the copied dependencies we can build libmsp430 library from the root of the package.

cd ..  
make -j4 STATIC=1  

Go ahead and grab a beer because this will probably take awhile on your Raspberry Pi. Once it's done though you will have a libmsp430.so in your current directory, run the following to copy your library to your library path.

sudo make install  

Compiling MSPDebug

The final piece will be compiling MSPDebug itself. This is the tool that will actually program the MSP430F5529 in our automated deployment.

First let's install the dependencies:

sudo apt-get install libusb-dev  
sudo apt-get install libreadline-dev  

Now we can download the source and extract.

cd ~/build  
wget http://downloads.sourceforge.net/project/mspdebug/mspdebug-0.23.tar.gz  
tar -zxvf mspdebug-0.23.tar.gz  

Now we can compile the source:

cd mspdebug-0.23  
make -j4  

Once the process is complete you will have an executable file mspdebug, go ahead and install the file to your PATH by running

sudo make install  

Testing MSPDebug

Now that everything has been compiled to run on a Raspberry Pi we can finally connect to our device using mspdebug!

Go ahead and plug in your MSP430F5529 LaunchPad and then launch mspdebug with the following commands. The first puts the libmsp430.so that we compiled previously in your linker library path and the second launches mspdebug. The arguments for mspdebug tell it to use the TI library (the one that we built) and to allow a firmware update if the debugger firmware is out of date.

export LD_LIBRARY_PATH=/usr/local/lib:${LD_LIBRARY_PATH}  
mspdebug tilib --allow-fw-update  

If all works fine you should see something like this:
MSPDebug Launched

Indicating you are now connected to the MSP430!

Next Steps

Next we will be using Ansible to distribute MSP430F5529 firmware images to a group of Raspberry Pis which will then use the mspdebug tool to download to their connected LaunchPads.

If you had trouble following any of the steps or have suggestions/improvements for the guide please leave a comment below! If you're having trouble getting it to work and just want the binaries they are checked into my GitHub repository.

I now have completed Part 2 of the article where I discuss the ansible playbook to deploy the firmware. I have recently started looking into all the DevOps tools that have constant articles on Hacker News and reddit lately. I use linux around my apartment for all my machines (mostly…

Read More

Web App over the file:// Protocol using AngularJS

After reading a lot about AngularJS I decided to run through the Code School tutorials about it and learn more. After a few code snippets I got a glimpse of how powerful a framework it is.

At work I am mainly an embedded software enginner. Not a javascript/web type of software guy. We mostly work in C with a little assembly thrown in. However, I've developed a recent urge to learn more about the web.

One project I needed to work on was a simple user interface to view some content. The requirements though was that all the pages needed to work over the file:/// protocol because the user would not be running any sort of webserver. This task proved to be a challenge when getting certain things in AngularJS to work. The issues were not directly related to AngularJS but more to browser security policies and accessing the file system.

1. HTTP templates loading in ng-view

The first hurdle I hit was getting my *.html to load within the ng-view. After struggling with this for awhile I came to the conclusion that it was best to convert my html to javascript and store it in the $templateCache variable. This variable keeps a cache of all your templates so that a request doesn't need to be made if your browser already has the template. By converting all our html to js and loading it into the $templateCache it gives the browser access to all the templates without a request.

Because I was actively working on the html and didn't want to keep converting it to js I setup a grunt job to watch for html changes and run a grunt task to convert it to javascript.

    module.exports = function(grunt) {
      grunt.initConfig({
          pkg: grunt.file.readJSON('package.json'),
          ngtemplates: {
              myapp: {
                  options: {
                      base: "web",
                      module: "app",
                  },
                  src: "app/html/*.html",
                  dest: "app/js/templates.js"
              }
          },
          watch: {
            templates: {
              files: ['app/html/*.html'],
              tasks: ['ngtemplates'],
            }
          }
      });

      grunt.loadNpmTasks('grunt-angular-templates');
      grunt.loadNpmTasks('grunt-contrib-watch');

      grunt.registerTask('default', ["ngtemplates"]);
      };

This Gruntfile sets up a task to watch my app/html folder and when it sees an update runs the ngtemplates task. The ngtemplates task works by using the grunt-angular-templates module. When the task runs it generates a templates.js file that resembles the snippet below:

angular.module('app').run(['$templateCache', function($templateCache) {  
  'use strict';

  $templateCache.put('app/html/details.html',
   "<div class=\"row-fluid\">\n" +
   "  <div class=\"span9\">\n" +
   ...

Now that we have the html converted to js and loaded into our $templateCache all we need to do it include the templates.js file in our main index.html and AngularJS takes care of the rest. The grunt-angular-templates module takes care of all the javascript for you, all you need to do is write the html and point it to your source.

2. Requesting data over the file:/// protocol

Another part of my application was serveral JSON documents. These documents get shipped with the application and don't get updated by the user but I wanted an easy way to perform updates. This way I can just update the JSON file in the next release and the user sees the new content.

My solution to this wasn't as "clean" as to my first problem, but I have it working none the less. I started out using the $http AngularJS functions to try and load the JSON files from the hard drive. $http.get() was not working due to security issues once again so I moved on to $http.jsonp(). This also didn't work because without a backend server running you cannot name the callback appropriatly for AngularJS.

So I resorted to using a third party AngularJS plugin called AngularLoad. This plug adds javascript files to the DOM dynamically. So within your controller or serivice you can determine the data you need and add it accordingly. It did require some changes to my JSON documents though. It essentialy required my JSON documents to be converted into javascript files with a variable assignment.

So this:

 {
   "name": "Zack",
   "class": "Math"
 }

became

var student = {  
  "name": "Zack",
  "class": "Math"
};

This way once the "script" is loaded you have access to a variable that contains the data. You could do something similar with a callback function as well. To request the data my controller performed a simple call to

// Download student
angularLoad.loadScript('app/data/zack.js').then(function() {  
    // Script loaded succesfully
    $scope.student = student;
});

Once loading the script completes the above snippet takes the student variable from the script loaded and applies it to the $scope so it can be used within the template.

In order for the above code to work you also must inject the angularLoad serivce into your controller and add it do your app dependencies. This results in a controller that looks like:

app.controller('StudentController', function($scope, angularLoad) {  
  this.getStudent = function($scope, angularLoad) {
    // Download student
    angularLoad.loadScript('app/data/zack.js').then(function() {
      // Script loaded succesfully
      $scope.student = student;
    });
  };
});

Conclusion

In conclusion AngularJS is a very powerful framework you can use to create some very powerful apps with even if you are forced over the file:///. I'll update this post if I find anymore gotcha's along the way or come up with better ways for working around the issues I saw.

After reading a lot about AngularJS I decided to run through the Code School tutorials about it and learn more. After a few code snippets I got a glimpse of how powerful a framework it is. At work I am mainly an embedded software enginner. Not a javascript/web type…

Read More

NodeJS Serial GUI

I haven't messed around with NodeJS yet and after seeing discusisons on reddit and Hacker News I thought it was time to try it out.

For those who have never heard of Processing it is a development environment used to create GUIs. It has gathered a lot of popularity in the maker communicty especially among people wanting to do GUIs to interact with Arduinos. While, Processing is a great tool, there are so many cool UIs built with javascript/html/css on the web today. My "Hello World" project for NodeJS was to read data from an MSP430 LaunchPad and plot the data in real time.

node-webkit

I wanted to create a GUI similar to those made in Processing but using web languages. After looking around node-webkit seemed like the perfect fit to create a distributable GUI out of a webpage. The great part is that it creates shippable binaries so end users don't need to have nodejs or any other dependencies installed. Also it works cross-platform which is a plus.

node-serialport

For the serial communication node-serialport was the clear winner. There was already a ton of information about using it. The only problem I had is that there is no binary shipped for the library and building it on Windows requires Visual Studio so getting it working on Windows was a bit of a pain.

The Result

After combining the two, a GUI was born.

The code uses jquery to fire when the user chooses a serial-port from the form at the top and then starts plotting using the flot javascript library.

In conclusion I found NodeJS to be pretty neat and node-webkit is a powerful tool to create GUIs using HTML/CSS/JS that most developers are familiar with.

All my code can be found on github. Along with how to build and run the code.

I haven't messed around with NodeJS yet and after seeing discusisons on reddit and Hacker News I thought it was time to try it out. For those who have never heard of Processing it is a development environment used to create GUIs. It has gathered a lot of popularity in…

Read More