Friday, October 19, 2012

Making thinks easier with make and geany


This post will be about speeding things, from previous post we know how we made program for uC step by step. It is good to now how things work under the hood but for everyday development it's to time consuming so we will use very common and easy tools to help us out.

1. Make program.

Make know how to compile, link and download program to microcontroller. Whole procedure is described in file called "Makefile". We can define partial goals and variables and many other useful things to automate our process.

In this example i will use very simple makefile so it is easy to understand and control:
Important, when you write your makefile make sure command(e.g. line 10) after goal starts with <TAB>

PROJ=blink
CC=msp430-gcc
MCU=msp430g2553
CFLAGS=-Os -g -Wall -mmcu=$(MCU)
LDFLAGS=-g -mmcu=$(MCU)

OBJS=main.o

all:$(OBJS)
       @echo linking: $(PROJ).elf
       $(CC) $(LDFLAGS) -o $(PROJ).elf $(OBJS)
       @echo  "--== Size of firmware ==--"
       msp430-size $(PROJ).elf
%.o:%.c %.h
       @echo compiling file: $<
       $(CC) $(CFLAGS) -c $<

clean:
       @echo cleaning $<
       rm -fr $(PROJ).elf $(OBJS)


flash: all
       mspdebug rf2500 'erase' 'load $(PROJ).elf' 'exit'


First four lines are just defining variables for our compiler exec, mcu type, compiler,linker flags and objects to compile. When we want to add more file to our project we just
Next we define our goals:
all - creates our executable file and prints size statistics.
%.o - we tell that we want to build object files so make finds %.c files and compile them using command that is given below. After colon we add files that when changed force compilation.
clean - remove all object and elf files ( it's handy when you want to be sure for recompiling program )
flash - download our elf file to uC, word after colon tells that before flashing program needs to achieve goal "all".
Now we put this file in directory where our main.c file is and execute command:
make
Output:

msp430-gcc -Os -g -Wall -mmcu=msp430g2553   -c -o main.o main.c
linking: blink.elf
msp430-gcc -g -mmcu=msp430g2553 -o blink.elf main.o 
--== Size of firmware ==--
msp430-size blink.elf
   text   data    bss    dec    hex filename
    144      0      2    146     92 blink.elf


Make looks for our makefile and compile and link our program.
If we want to make our other goals we must invoke make with name of this goal:
make flash
make clean

To have permission for our lauchpad as normal user( no need to use sudo before make flash ) we have to make file with rules which will tell that launchpad can be used users that are in group for example dialout.

sudo vim /etc/udev/rules.d/46-TI_launchpad.rules

content:
ATTRS{idVendor}=="0451", ATTRS{idProduct}=="f432", MODE="0660", GROUP="dialout"

Now we have to add our user to dialout group and restart udev rules:
sudo usermod -a -G dialout our_user_name

sudo restart udev


2.Geany


Geany is nice little IDE for programing, we combine our makefile with geany so everything will be available after mouse click . To install geany:

sudo apt-get install geany
cd ~/our_project_dir
geany main.c




From menu we choose "Build"->"Set Build Commands" and we can fill like this:
"Compile" -> "make" 
"Build" -> "make clean; make" ( so we are sure we rebuild all project )
"Execute" -> "make flash"

Now everything we need is to press "F5" or nice gear icon to compile and download our project to launchpad:)


3 comments:

  1. Great post! That's what I was looking for

    ReplyDelete
  2. Hi
    Correct me if I'm wrong but to execute "make flash" command from Makefile by Geany F5 keypad you have to add sudo to last line of Makefile
    so:
    flash: all
    sudo mspdebug rf2500 'erase' 'load $(PROJ).elf' 'exit'
    instead off:
    flash: all
    mspdebug rf2500 'erase' 'load $(PROJ).elf' 'exit'
    otherwise you get following error:
    mspdebug rf2500 'erase' 'load pierwszy.elf' 'exit'
    MSPDebug version 0.20 - debugging tool for MSP430 MCUs
    Copyright (C) 2009-2012 Daniel Beer
    This is free software; see the source for copying conditions. There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

    usbutil: unable to find a device matching 0451:f432
    make: *** [flash] Error 255

    Gerwazy

    ReplyDelete
    Replies
    1. Yes, that's correct. You don't have permissions for device as a user. I added instruction to avoid using sudo when using lp.

      Delete