

# assembler make file
# with c-preprocessor
# thus #define can be used ... :-)
#
# by clemens helfmeier, clemenshelfmeier (at) gmx (dot) de
#
#
# the target will be preprocessed, the output assembled ...
#

# main target to build (whithout extension)
TARGET = main
LFUSE = lfuse
HFUSE = hfuse

# list include directories here ...
INCDIRS = /usr/share/avr/include


# programs are listed here:

# assembler ... this will be used for assembling the result 
ASM = tavrasm 
# tavrasm
# the preprocessor
PP = cpp
# the programmer
AVRDUDE = avrdude

# port for avrdude
AVRPORT = /dev/ttyS0
# tpye of programm
AVRPROG = avr910
# specify the avr-device
DEVICE = m32


# list preprocessorflags here
#	-x		specifie file type
#	-nostdinc	don't search in the systems search path for includes
PPFLAGS = -x assembler-with-cpp -nostdinc

# list assembler flags here... possible are:       
#        -v verbose
#        -w no warnings
#        -c case sensitive labels/defines
#        -l limit log width to 80 characters
#        -x allow local labels
#        -m output Motorola S-record format
#        -i output Intel HEX format (default)
#        -h omit address extension record from Intel HEX files
#        -j output .obj format
#        -g output generic hex
#        -b output binary format
#        -a wrap relative jumps
#        -f allow forward org's
# for tavrasm
ASMFLAGS = -v -i
# for avra
# ASMFLAGS = 



# specifie AVRDUDEFLAGS here
AVRDUDEFLAGS = -p $(DEVICE) -c $(AVRPROG) -P $(AVRPORT)

# do the preprocessing and output into $(TARGET).pp

build:	pp asm program

pp:
	@echo "preprocessor ..."
	-rm *.pp
	$(PP) $(PPFLAGS) -I $(INCDIRS) $(TARGET).asm | grep -v -E "^#" > $(TARGET).pp
	-$(PP) $(PPFLAGS) -I $(INCDIRS) $(LFUSE).asm | grep -v -E "^#" > $(LFUSE).pp
	-$(PP) $(PPFLAGS) -I $(INCDIRS) $(HFUSE).asm | grep -v -E "^#" > $(HFUSE).pp

asm:	pp
	@echo "assembling ..."
	-rm *.hex
	$(ASM) $(ASMFLAGS) -I $(INCDIRS) -o $(TARGET).hex $(TARGET).pp
	-$(ASM) $(ASMFLAGS) -I $(INCDIRS) -r $(LFUSE).hex $(LFUSE).pp
	-$(ASM) $(ASMFLAGS) -I $(INCDIRS) -r $(HFUSE).hex $(HFUSE).pp

disasm: asm
	avr-objdump -m avr -D $(TARGET).hex > main.lss
	
program: pp asm
	@echo "programming ..."
	$(AVRDUDE) $(AVRDUDEFLAGS) -e -U flash:w:$(TARGET).hex

lfuse:	pp asm
	@echo "programming the low fuse bits ..."
	@echo "note: put fuse bits in file $(LFUSE).asm with .eseg directive!"
	# @echo "this is currently disabled, remove the leading '#' to enable this function."
	$(AVRDUDE) $(AVRDUDEFLAGS) -U lfuse:w:$(LFUSE).hex

hfuse:	pp asm
	@echo "programming the high fuse bits ..."
	@echo "note: put fuse bits in file $(HFUSE).asm with .eseg directive!"
	# @echo "this is currently disabled, remove the leading '#' to enable this function."
	$(AVRDUDE) $(AVRDUDEFLAGS) -U hfuse:w:$(HFUSE).hex
	
all:	pp asm program lfuse

reset:
	@echo "resetting the target device ..."
	$(AVRDUDE) $(AVRDUDEFLAGS) -n
erase:
	@echo "erasing the chip ..."
	$(AVRDUDE) $(AVRDUDEFLAGS) -e
	
clean:
	-rm *.hex *.pp
# only list supported devices by asm and asm version
asm-version:
	$(ASM)
	$(ASM) -d
	$(AVRDUDE) -v
	$(AVRDUDE) -c $(AVRPROG)

help:
	@echo "ASM-Makefile"
	@echo "uses cpp, tavrasm, avrdude"
	@echo "targets:"
	@echo "   pp       only do preprocessing (output in *.pp)"
	@echo "   asm      do preprocessing and assemble"
	@echo "   build    build the code and program it"
	@echo "   program  same as build"
	@echo "   asm-version   display the version of asm and the supported devices"
	@choe "   lfuse hfuse   program the fuse bits. USE WITH CARE!"
