Automate programming the MSP430 Launchpad on Linux using Expect

Automate programming the MSP430 Launchpad on Linux using Expect

Programming and debugging the MSP430 on Windows is a bit of a straight forward affair. You simply install the Code Composer Studio (CCS) integrated development environment (IDE) and in a few minutes you are ready to go. On Linux systems however, there is a little bit of work involved[1] since CCS is not supported. Not being a fan of repetition[1:1], I decided to try my hand at automating things using Expect.

Expect is a tool designed to automate interactive shell prompts. I find that it's commonly used to automate logging into ssh sessions[1:2], but it's capable of so much more.

Installing the tools

If you don't already have it, you're going to need the MSP430 toolchain. This includes the binaries, GCC compiler, and the debugger. You can obtain these using your favorite package manager[1:3]. Here is the command to get it from apt.

sudo apt-get install binutils-msp430 gcc-msp430 msp430-libc mspdebug

Next, you'll need Expect- also available via apt.

sudo apt-get install expect

Debugging the normal way

Connect your Launch pad to your system and wait a few moments for it to be detected. It should normally flash it's LEDs indicating that it's ready. Ensure that it's ready by running the command lsusb in the terminal window.

lsusb

Here we can see that the launchpad has been detected thanks to the following output.

Bus 001 Device 003: ID 0451:f432 Texas Instruments, Inc. eZ430 Development Tool

Note the device ID for the launchpad 0451:f432. If you don't see this, you need to check your connection and try again.

Start the MSP debugger using the command

mspdebug rf2500

This will automatically detect your MSP430 Launchpad and start the environment.

erase

Clears any programs currently on the board

prog [path to your *.elf file]

programs the board with your compiled *.elf file. The next command closes the session.

exit

Automating using expect

Now that we know how to do this manually, you can automate the process. First start by creating a file which will contain the script[1:4]. Create a file to execute the script. I'll call mine program_msp.exp. Copy in the following script (the comments --lines beginning with #, explain how the code works).

#!/usr/bin/expect -f
# The first line sets the 
# path to the expect binaries

# This sets a timeout of 10 secs
# The script will terminate if there is no
# interaction before it's reached
set timeout 10 

# This allows you to provide a path to a file
# as an input argument
set file [lindex $argv 0]

# Check to see if an argument was provided
if {[llength $argv] == 0} {
  # If not, display usage instructions
  send_user "Usage: scriptname \'file\'\n"
  # and exit
  exit 1
}

# This starts the process for the command `mspdebug`
spawn mspdebug rf2500

# Expect the prompt, mspdebug
expect "(mspdebug)"

# Send the erase command
send "erase\r"

# Wait for the prompt
expect "(mspdebug)"

# Send the program command and the path to the file
send "prog $file\r"

# Wait for the prompt 
expect "(mspdebug)"

# Exit from mspdebug
send "exit\r"

# Wait 1 second. This gives mspdebug a chance to start
# the program that you've uploaded onto the board.
sleep 1

# We should now be done :) Exit expect.
exit 0

Next, it executable using the chmod command,

chmod +x program.exp

Finally, you can execute it like you would any any other shell script.

./program.exp
Footnotes and References

  1. Update - Jan. 15 2017 - Exiting the mspdebug shell implicitly runs the program which has been uploaded to the board, exiting the expect process before it does that means that the board has to be disconnected and reconnected in order to run it. I've updated the script with a short sleep to give it a chance to do that. ↩︎ ↩︎ ↩︎ ↩︎ ↩︎

Subscribe to Another Dev's Two Cents

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe