GCC is a free, open-source compiler, and it is one of the main ways to write C or C++ for TI’s MSP430 microcontrollers. You can use it from the command line, or through Energia, an open-source IDE that builds on the same kind of GCC toolchain. This article shows how both routes work, using a simple LED blink program on a LaunchPad board.
What GCC means for MSP430 development
The MSP430 is a low-power 16-bit microcontroller family. Most beginners start with the MSP-EXP430G2 LaunchPad, which carries a chip such as the MSP430G2553 and two on-board LEDs.
To program a chip like this in C, you need a compiler that produces MSP430 machine code. GCC has a build for this target. It compiles your C source into a small binary, and you then flash that binary to the chip through the LaunchPad’s USB programmer.
The benefits of using a GCC-based toolchain are practical:
- It is free and open source, with no code size limit.
- It runs on Windows, Linux and macOS.
- It has optimisation levels that can reduce the size of your program, which matters on chips with only a few kilobytes of flash.
- You can use it with any editor and with build tools such as Make.
Option 1: Use Energia
Energia is an open-source IDE for TI microcontrollers. Its interface and programming model follow Arduino and Wiring. It uses a GCC-based compiler for MSP430 boards, so you get the benefits of GCC without setting up the toolchain by hand.
To get started:
- Install Energia and connect your LaunchPad with the USB cable.
- Select your board under the Tools menu, for example the MSP430G2553 variant.
- Select the serial port that appeared when you plugged the board in.
- Paste in your sketch and click Upload.
Blink example
The sketch below blinks the two LEDs on the LaunchPad. It writes straight to the port registers instead of using the Energia helper functions, which keeps the generated code small.
unsigned long int i;
void setup()
{
P1DIR = 0x41;
P1OUT=0;
}
void loop()
{
P1OUT=0x41;
for(i=0;i<950000;i++);
P1OUT=0x0;
for(i=0;i<950000;i++);
}
How the code works
- P1DIR = 0x41 sets bits 0 and 6 of Port 1 as outputs. In binary, 0x41 is 01000001. On the G2 LaunchPad, P1.0 and P1.6 are connected to the two LEDs.
- P1OUT = 0 starts with both pins low, so both LEDs are off.
- P1OUT = 0x41 drives both pins high and turns the LEDs on.
- The for loops are crude software delays. Each one counts to 950000 using a 32-bit unsigned long, which keeps the LEDs on or off for a visible time.
If you want to know more about the registers used here, see our articles on MSP430 digital output and MSP430 digital input.
A note on delay loops
Empty counting loops are easy to write, but they have two drawbacks. The delay depends on the clock speed and the optimisation level, so the blink rate can change when you change compiler settings. An optimising compiler may also shorten or remove a loop that does nothing visible.
Declaring the counter volatile tells the compiler to keep the loop. Energia’s delay() function is a more reliable choice for sketches, and a hardware timer is better still because the CPU can sleep while it waits.
Option 2: Use msp430-gcc from the command line
If you prefer a plain editor and a terminal, install the MSP430 GCC toolchain. TI distributes a build, and many Linux distributions package one. Depending on the version, the compiler is called msp430-gcc or msp430-elf-gcc.
Here is the same blink program as a standalone C file, without Energia’s setup() and loop() structure:
#include <msp430.h>
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; /* stop the watchdog timer */
P1DIR |= 0x41; /* P1.0 and P1.6 as outputs */
P1OUT = 0;
for (;;) {
P1OUT ^= 0x41; /* toggle both LEDs */
volatile unsigned long i;
for (i = 0; i < 50000; i++);
}
}
Two things differ from the Energia sketch. The watchdog timer is stopped at the start, because otherwise it resets the chip after a short time. The delay counter is volatile, so the loop is not optimised away.
Compile and check the size
msp430-elf-gcc -mmcu=msp430g2553 -Os -o blink.elf blink.c
msp430-elf-size blink.elf
The -mmcu option picks your exact chip, so the compiler uses the right memory map. The -Os option optimises for size. The size tool prints how much flash (text and data) and RAM (data and bss) the program uses.
Flash the board
A common tool for flashing is mspdebug, which talks to the LaunchPad’s programmer. The driver name depends on your LaunchPad version, so check the mspdebug documentation for your board. Then run its prog command with your .elf file. TI’s Code Composer Studio and Uniflash can also load the same file.
Compare memory use yourself
Memory is tight on small MSP430 chips, and the G2 series has only a few kilobytes of flash. The easiest way to see what GCC does for you is to measure it:
- Compile the blink program with -O0 (no optimisation) and note the size output.
- Compile it again with -Os and compare the numbers.
- Replace the register writes with pinMode() and digitalWrite() in Energia and compare the sketch size reported after compiling.
Direct register access is usually smaller than helper functions, but the exact difference depends on the compiler version and settings, so measure it on your own setup.
Tips for smaller, safer code
- Use the smallest integer type that fits. A 32-bit counter costs more flash and time than a 16-bit one on this 16-bit CPU.
- Avoid printf and floating-point maths where you can, since they add a lot of library code.
- Mark variables shared with interrupts as volatile.
- Put constant tables in flash by declaring them const.
- If you need exact control of every instruction, learn MSP430 assembly language, and use C for the rest.
FAQ
Is Energia the same as GCC?
No. Energia is an IDE and a set of libraries. For MSP430 boards it calls a GCC-based compiler to build your sketch, so GCC does the compiling behind the scenes.
Which GCC should I install for MSP430?
Use a current MSP430 GCC package, either from TI or from your Linux distribution. The tool names start with msp430- or msp430-elf-, depending on the build. Check the documentation for your version.
Why does my LED blink at a different speed after changing optimisation?
Software delay loops depend on how the compiler turns them into instructions. Use delay() or a hardware timer for predictable timing.
Can I use GCC with other MSP430 LaunchPad boards?
Yes. Change the -mmcu option to match your chip, and check that the pins you use match your board’s schematic, because the LED pins can differ between boards.