Using Linux and the TI MSP430 processor to create blinking LEDs is a learning exercise, not just a way to make cheap sci-fi movie panels.
B Y B R I A N C . L A N E
#
-9
#--9
#9
#-9
+
HOLYGRAIL LINUX JOURNALPDF
4 2nF E B R U A R Y 2 0 0 6 W W W . L I N U X J O U R N A L . C O M
n F E A T U R E E M B E D D E D
my MSP430 code.
Thanks to Stephan Linz and his Cross Development Kit for MSP430, I don’t need to spend more than five minutes installing development tools. He has done all the hard work of getting mspgcc compiled and packaged as RPMs. Stephan also has created Cross Development Kits for the AVR processor and the Altera soft core NIOS, if you are interested in those target processors. If I ever need to write code for the AVR, I know where to go.
The cdk4msp project is available from SourceForge, and here is a minimal list of the packages that need to be installed from the cdk4msp SourceForge download page:
n cdk-msp-base-0.2-20031111.i386.rpm n cdk-msp-binutils-2.14-20031106.i386.rpm
n cdk-msp-examples-libc-20031101cvs-20031102.noarch.rpm n cdk-msp-examples-mspgcc-20031101cvs-20031102.noarch.rpm n cdk-msp-gcc-3.3.2-20031106.i386.rpm
n cdk-msp-gdb-5.1.1-20031106.i386.rpm n cdk-msp-gdb-proxy-5.1.1-20031106.i386.rpm n cdk-msp-jtag-lib-20031101cvs-20031102.i386.rpm n cdk-msp-libc-20031101cvs-20031102.noarch.rpm
Additional document packages can be downloaded, depend-ing on your preference for man pages, info files, PDF or HTML pages. I have successfully used these packages on Fedora Core releases 1 through 4, and although I haven’t tried any other RPM-based distributions, I expect them to work just fine. These RPM packages function as a self-contained unit, and don’t depend on any outside packages.
Install the packages in this order with the following commands:
rpm -Uhv cdk-msp-base-0.2-20031111.i386.rpm rpm -Uhv cdk-msp-binutils-2.14-20031106.i386.rpm rpm -Uhv cdk-msp-libc-20031101cvs-20031102.noarch.rpm rpm -Uhv cdk-msp-gcc-3.3.2-20031106.i386.rpm
rpm -Uhv cdk-msp-gdb-5.1.1-20031106.i386.rpm
rpm -Uhv cdk-msp-jtag-lib-20031101cvs-20031102.i386.rpm rpm -Uhv cdk-msp-gdb-proxy-5.1.1-20031106.i386.rpm
rpm -Uhv cdk-msp-examples-libc-20031101cvs-20031102.noarch.rpm rpm -Uhv cdk-msp-examples-mspgcc-20031101cvs-20031102.noarch.rpm
The install places everything in the directory tree below /opt/cdk4msp. Take a look at the examples in /opt/cdk4msp/examples/mspgcc and the documents in the /opt/cdk4msp/doc, info and man directories, depending on which style of documentation you installed.
A Simple Blinking LED
Blinking an LED is the embedded equivalent of “Hello World”. We will modify one of the examples to make it a little easier to understand. Copy the leds example from
/opt/cdk4msp/examples/mspgcc/leds/ to a working directory,
and replace main.c with the simplified version below. Edit the Makefile and set the CPU variable to msp430x149 so that it compiles for the correct target. If you are using a different ver-sion of the MSP430, you can get a list of the supported types by running msp430-gcc --target-help | lessand then set CPU to the appropriate type.
Figure 1. Picture of My LED Blinker Development Board and JTAG Adapter
Figure 2. Simple Schematic of the LED Blinker Setup
Replacement for main.c:
/* Simple LED Blinker program for MSP430 */
#include <msp430x14x.h>
/* Brute Force delay loop */
void delay(unsigned int d) {
for (; d>0; d--) { nop();
nop();
} }
int main( void ) {
/* Init watchdog timer to off */
WDTCTL = WDTPW|WDTHOLD;
W W W . L I N U X J O U R N A L . C O M F E B R U A R Y 2 0 0 6n4 3 /* Init Output ports to GND */
P1OUT = 0x00;
P2OUT = 0x00;
/* I/O not module control */
P1SEL = 0x00;
P2SEL = 0x00;
/* Set up the data direction registers P1.0 output, input on the rest
*/
P1DIR = 0x01;
P2DIR = 0x00;
/* No Interrupts on Port Pins */
P1IES = 0x00;
P2IES = 0x00;
P1IE = 0x00;
P2IE = 0x00;
/* Loop until the universe breaks down */
while (1) {
/* Toggle P1.0 ouput pin */
P1OUT ^= 0x01;
/* Delay for a while before blinking */
delay(0x4fff);
} /* while */
}
Run maketo compile it. You should see no warnings or errors:
msp430-gcc -mmcu=msp430x149 -O2 -Wall -g -c -o main.o main.c msp430-gcc -mmcu=msp430x149 -o leds.elf main.o
msp430-objcopy -O ihex leds.elf leds.a43 msp430-objdump -dSt leds.elf >leds.lst
At this point, we have the software development set up, but no hardware to run it on or LEDS to blink.
MSP430 Simple Hardware Setup
Very little hardware or money is required to get started with the MSP430. You need a PC board with the processor on it and a JTAG adapter to connect the board to the parallel port.
Olimex makes a number of inexpensive evaluation boards and JTAG adapters for the MSP430, ARM, AVR and PIC. In the US, their products are carried by a neat place called Spark Fun Electronics, which carries the Olimex boards as well as its own unique collection of adapter boards and projects. Table 1 shows what’s needed to build the circuit on the schematic in Figure 1.
One problem with low-powered devices like the MSP430 is that when you try to turn them off, they don’t discharge the supply capacitors all the way to ground. This can result in a brownout con-dition when the processor won’t reboot until reset properly. For the sake of simplicity, we are going to run the processor directly off of a pair of AA batteries. It will also run off the power from the JTAG adapter itself if you don’t have any batteries handy.
In a production design, I would add a power supply with reset manager to prevent any brownout problems, but for our