#
# Microcontroller VL, SS 2005
#
# Date:		10.03.2005, Christian Troedhandl
#
# TU Vienna, Embedded Computing Systems Group
#
# Targets:
#	<none>		generate flash file
#	install		generate and download flash
#	install_flash	generate and download flash
#	clean		remove obj- and temporary files
#
# change PROJNAME for new projects
# add your object files to OBJS
#

#-------------------------------------------------------------------------
# project specific things
# change these definitions for new projects
#-------------------------------------------------------------------------

PROJNAME = main

OBJS = main.o

#-------------------------------------------------------------------------
# constants which you won't have to modify for each exercise
#-------------------------------------------------------------------------

# target architecture
MCU	= atmega16

#-------------------------------------------------------------------------
# macros for the tools
#-------------------------------------------------------------------------

# output format of binary; at16prog requires ihex
#BINFORMAT = srec
BINFORMAT = ihex

# for serial I/O; for parallel IFC, use stk200
PROGFORMAT = dasa2
#PROGFORMAT = dapa
#PROGFORMAT = stk200

# for serial I/O; for parallel IFC, do not specify anything
#IFCTYPE	= -dlpt=/dev/ttyS0
#IFCTYPE	= -dlpt=/dev/ttyS1
IFCTYPE	= -dlpt=/dev/ttyACM1
#IFCTYPE	= -dlpt=/dev/ttyACM0

# Tools
AS	= avr-as
ASLD	= avr-gcc -x assembler
LD	= avr-ld 
CC	= avr-gcc 
OBJCOPY = avr-objcopy
#PROGR	= uisp
PROGR	= at16prog


# Flags
CFLAGS  = -mmcu=$(MCU) -Wall -Os
LDFLAGS	= -Wl,-Map=$*.map,-section-start=.noinit=0x800100 -mmcu=$(MCU)
OCFLAGS	= -O $(BINFORMAT)
EEPFLAGS= --set-section-flags=.eeprom="alloc,load" --change-section-address .eeprom-0x810000
PRFLAGS	= $(IFCTYPE) -dprog=$(PROGFORMAT) -dpart=$(MCU)

#-------------------------------------------------------------------------
# the targets
#-------------------------------------------------------------------------

all: $(PROJNAME).hex $(PROJNAME).elf $(OBJS)

%.hex:	%.elf
	$(OBJCOPY) $(OCFLAGS) $< $@
	@chmod ugo-x $@

%.elf:	$(OBJS)
	$(CC) $(OBJS) $(LIBS) $(LDFLAGS) -o $@

%.o:	%.c
	$(CC) $(CFLAGS) -Wa,-a=$*.list -c -o $@ $<

install: install_flash

install_flash: $(PROJNAME).hex
	$(PROGR) $(PRFLAGS) --segment=flash --erase
	$(PROGR) $(PRFLAGS) --segment=flash --upload if=$(PROJNAME).hex
	#sleep 1
	#$(PROGR) $(PRFLAGS) --segment=flash --upload if=Table._hex

clean:
	rm -f *.hex *.elf *.map *.list *.o

