Compiling the first program

So now that we have mspgcc compiler and mspdebug tool installed we finally can write our own programs.

To start we create the blinking led example.

Here is the source code for main.c

/* Blink LED example */


/** Delay function. **/
delay(unsigned int d) {
  int i;
  for (i = 0; i<d; i++) {

int main(void) {
  P1DIR = 0xFF;
  P1OUT = 0x01;

  for (;;) {
    P1OUT = ~P1OUT;

Now you can compile the program via:

msp430-gcc -Os -mmcu=msp430x2013 -o led.elf main.c

msp430-objdump -DS led.elf > led.lst

msp430-objcopy -o ihex led.elf > led.hex

The first command while compile the c source code into an executable in elf format. The second command will generate an assembler listing of your program. The third command will give a hexdump of the opcodes in Intel Hex Format. You can actually look at the led.lst and led.hex files they are human readable.

Now it would be cumbersome to actually do this every time manually. The better way is to create a make file and let make take of this.

Here is a example for the makefile:

# makefile configuration

NAME = led
OBJECTS = main.o
CPU = msp430x2013
CFLAGS = -mmcu=${CPU} -Os -Wall -g

#switch the compiler (for the internal make rules)
CC = msp430-gcc
.PHONY: all FORCE clean download dist

#all should be the first target. it's built when make is run without args
all: ${NAME}.elf ${NAME}.hex ${NAME}.lst

#additional rules for files
${NAME}.elf: ${OBJECTS}
  ${CC} -mmcu=${CPU} -o $@ ${OBJECTS}

${NAME}.hex: ${NAME}.elf
  msp430-objcopy -O ihex $^ $@

${NAME}.lst: ${NAME}.elf
  msp430-objdump -dSt $^ >$@

  rm -f ${NAME}.elf ${NAME}.hex ${NAME}.lst ${OBJECTS}

#backup archive
  tar czf dist.tgz *.c *.h *.txt makefile

#dummy target as dependecy if something has to be build everytime

#project dependencies
main.o: main.c

If you are coping the file make replace the indention spaces with a <tab> otherwise the file will not be correct ( sorry I could not figure out how blogilo would not convert them). As you can see this actually does a few more things than 3 commands before. First how to use this makefile:

You can build all via ‘make‘ or ‘make all‘ since ‘all’ is the first rule defined it will be the default.

The first lines NAME defines the common part of all the files to create (here led.*). The OBJECT variable lists all object files. The CPU sets the target cpu type and CFLAGS sets all the compiler options. CC tells make what compile it should use. All the following lines starting at position 0 define a build rule (e.g. all/clean). The names listed after the ‘:’ define the dependencies for the rule which also need to be called. If the build rule has some idented lines (need to start with a <tab>) it defines the command this rule executes, e.g. the clean rule executes the rm command.

Next step is to load the program on the target and debug the code.

Comments are closed.