MSP430 Development on OS X Yosemite with CMake (The lazy way)

April 9, 2015


The TI MSP430 is a cheap and fun microcontroller for embedded development. It comes with a development suite called Code Composer Studio. However, it does not run on Linux nor OSX. Luckily, GCC has been ported to the MSP430, dubbed mspgcc.

This is quite easy to install on Linux, whether compiling from source or installing straight from the repositories. Sadly, it is not so straightforward on OSX. You could try using homebrew or MacPorts, but neither worked for me. The next step was to try compiling from source. However, compiling from source failed in the same place as the homebrew build. What now??

Finally a Solution

On my last ditch researching I stumbled across this project. Eureka. I'm a little mad I didnt think of this before. Basically, you can use the bundled toolchain binaries that come with Energia without using Energia. While the project above works fine, I wanted to use it with CMake because I hate maintaining makefiles manually.

So I first made this MSP430 toolchain file for CMake. Warning: All of what comes next assumes you are familiar with mspgcc and cmake.

INCLUDE(CMakeForceCompiler)

# the name of the target operating system
SET(CMAKE_SYSTEM_NAME Generic)

# specify the cross compiler
CMAKE_FORCE_C_COMPILER(/Applications/Energia.app/Contents/Resources/Java/hardware/tools/msp430/bin/msp430-gcc GNU)
CMAKE_FORCE_CXX_COMPILER(/Applications/Energia.app/Contents/Resources/Java/hardware/tools/msp430/bin/msp430-g++ GNU)

# here is the target environment located
SET(CMAKE_FIND_ROOT_PATH  /Applications/Energia.app/Contents/Resources/Java/hardware/tools/msp430) 

# adjust the default behavior of the FIND_XXX() commands:
# search headers and libraries in the target environment, search 
# programs in the host environment
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

There are a few important things here that are different from the CMake cross compiling example. The compiler name can be set to "Generic". The compiler paths are simply the paths to the compilers that shipped with Energia. They must be forced because they both require additional compiler flags that we will specify later, which are not in this toolchain file. You can, of course, add the msp430 folder to your system path to make this more cross platform. I am simply demonstrating where the binaries are by default.

We are going to set the flags in the main CMakeLists.txt file. To specify the target board, it must be passed as a compiler flag using the -mmcuflag. The board we are targeting is msp430g2553. So the resulting cmake command placed in CMakeLists.txt is below:

# Set the compiler flags
set(CPU_FLAG "-mmcu=msp430g2553")
set(CMAKE_C_FLAGS ${CPU_FLAG})
set(CMAKE_EXE_LINKER_FLAGS ${CPU_FLAG})

All Set. Now we just have to set the target to output an elf binary

# Create the executable
add_executable(hello_msp.elf ${SOURCES})

Now it should compile..

cd build/
cmake .. -DCMAKE_TOOLCHAIN_FILE=../cmake/msp430.cmake
make 

But I also want to be able to flash the board using the same Cmake script instead of having to type out the path to the Energia mspdebug. So I made a custom command in CMakeLists.txt to flash the binary.

# Set the mspdebug path
# OSX using Energia.. For some reason the if statement wont work cross compiling
set(MSPDEBUG_PATH /Applications/Energia.app/Contents/Resources/Java/hardware/tools/msp430/mspdebug/mspdebug)

add_custom_target(flash
    COMMAND ${MSPDEBUG_PATH} rf2500 'prog hello_msp.elf'
    DEPENDS hello_msp.elf
)

So now, the command make flash will flash the compiled binary to the board. Voila, we can develop for the MSP430 on OSX 10.10! You can see the full code for this project on GitHub. Happy hacking!



comments powered by Disqus
  • Copyright 2013, Matthew Iannucci
  • Site created with web.py, version 0.3
  • Updated January 2015