Avr makefile

From Attie's Wiki
Jump to: navigation, search
# the output binary name (found in ./bin)
ELFOUT:=main.elf
HEXOUT:=main.hex
 
# the source files you wish to use (without the '.c') - main.c is added later
SRCS:=
 
# which device are you using?
DEVICE:=atmega328p
 
# what protocol do you use to communicate with the IC?
AVRDUDE_PROTO:=stk500v1
#AVRDUDE_PROTO:=buspirate
 
# which serial port is it connected to?
AVRDUDE_PORT:=/dev/ttyUSB0
AVRDUDE_BAUD:=57600
 
###################################################################################
# There should be no need to change below here for most projects
 
DIRS:=bin obj hex
SRCS+=main
OPTIMISATION:=-falign-functions \
              -falign-jumps \
              -falign-labels \
              -falign-loops \
              -fif-conversion2 \
              -finline-functions \
              -fregmove \
              -frename-registers \
              -fshort-enums \
              -freorder-blocks \
              -freorder-functions \
              -fsplit-wide-types \
              -ftoplevel-reorder \
              -funroll-all-loops \
              -fvariable-expansion-in-unroller
CROSS_COMPILE:=avr-
CC:=$(CROSS_COMPILE)gcc
OBJCOPY:=$(CROSS_COMPILE)objcopy
CFLAGS:=$(OPTIMISATION) -mmcu=$(DEVICE) -c -Wall -O9
CLINKS:=$(OPTIMISATION) -fwhole-program -mmcu=$(DEVICE)
 
.PHONY: all new clean spotless .dirs upload download
 
all: .dirs ./hex/$(HEXOUT)
 
new: clean ./hex/$(HEXOUT)
 
clean:
	rm -f *~
	rm -f ./hex/$(HEXOUT)
	rm -f ./bin/$(ELFOUT)
	rm -f ./obj/*.o
 
spotless: clean
	rm -rdf $(addprefix ./,$(DIRS)) .dirs
.dirs: makefile
	mkdir -p $(addprefix ./,$(DIRS))
	@touch .dirs
 
 
upload: ./hex/$(HEXOUT)
	avrdude -c$(AVRDUDE_PROTO) -P$(AVRDUDE_PORT) -b$(AVRDUDE_BAUD) -D -Uflash:w:./hex/$(HEXOUT):a
 
download:
	rm ./hex/$(HEXOUT).downloaded
	avrdude -c$(AVRDUDE_PROTO) -P$(AVRDUDE_PORT) -b$(AVRDUDE_BAUD) -D -Uflash:r:./hex/$(HEXOUT).downloaded:a
 
 
./hex/$(HEXOUT): ./bin/$(ELFOUT)
	$(OBJCOPY) -j .text -O ihex $^ $@
./bin/$(ELFOUT): $(addprefix ./obj/,$(addsuffix .o,$(SRCS))) makefile
	$(CC) $(CLINKS) $(filter %.o,$^) -o $@
 
./obj/%.o: %.c %.h makefile
	$(CC) $(CFLAGS) $*.c -o $@
./obj/%.o: %.c makefile
	$(CC) $(CFLAGS) $*.c -o $@

Memory Usage

It is very handy to know the current memory usage of your application. Especially when working with the smaller devices. Add this to your makefile and then type make meminfo to get detailed information on the memory usage

.PHONY: meminfo
 
meminfo: .meminfo
  @cat .meminfo
 
.meminfo: ./bin/$(ELFOUT)
  @echo "Regenerating memory usage information..."
  @avr-objdump ./bin/$(ELFOUT) -t | grep -e "^[0-9a-f]* ......O \.bss" > .meminfo.t1
  @cat .meminfo.t1 | sed -re "s/.*\.bss[ \t]0*//" | awk --non-decimal-data '{print ("0x"$$1)+0 "\t" $$2}' > .meminfo.t2
  @cat .meminfo.t2 | sort -nr > .meminfo
  @cat .meminfo | awk '{val += $$1}END{print "TOTAL: " val " bytes"}' >> .meminfo
  @rm .meminfo.t*
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox