#
# Makefile for Mignonette programs
#
#
# revision history:
#
# - may 17, 2010 - mitch
#               add burn-fuse8 target, for 8MHz internal osciallator.
#
# - mar 3, 2010 - rolf
#		update "burn-fuse" target for ceramic resonator (that's us).
#
# - mar 2, 2010 - rolf
#		set default program to be munch+test.
#		(some cleanup & comments too)
#
# - feb 3, 2010 - rolf
#		(comment)
#
# - jan 29, 2010 - rolf
#		added "make sprogram" to program via FTDI serial cable.
#
# - dec 30, 2009 - rolf
#		and fix "burn-fuse" target to properly configure the atmega168 for 16mhz operation
#
# - dec 27, 2009 - rolf
#		change MCU_TARGET to atmega168 (for Mig V.2)
#
# - apr 11, 2009 - rolf
#		created.  (borrowed from munch+test program, oct 11, 2008)
#

PRG            = munch+test
OBJ            = munch+test.o miggl.o
##PRG          = mig-sample1
##OBJ          = mig-sample1.o miggl.o

PRGWORKING     = munch+test.hex
##PRGWORKING     = munch+test.hex-v0.61
##PRGWORKING     = munch.hex-v0.61

MCU_TARGET     = atmega168
OPTIMIZE       = -O2

DEFS           =
LIBS           =

# set to one of the following:
# 	"usbtiny" for the ladyada usbtiny programmer OR
#	"avrispmkII" for the atmel AVR ISP MKII programmer
AVRDUDE_PROGRAMMER = usbtiny
##AVRDUDE_PROGRAMMER = avrispmkII


# You should not have to change anything below here. (except dependencies!)

CC             = avr-gcc

# Override is only needed by avr-lib build system.

override CFLAGS        = -g -Wall $(OPTIMIZE) -mmcu=$(MCU_TARGET) $(DEFS)
override LDFLAGS       = -Wl,-Map,$(PRG).map

OBJCOPY        = avr-objcopy
OBJDUMP        = avr-objdump

##all: $(PRG).elf lst text eeprom
all: $(PRG).elf lst text

$(PRG).elf: $(OBJ)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)

# dependencies (optional)
miggl.o: miggl.h miggl-private.h

clean:
	rm -rf *.o $(PRG).elf *.eps *.png *.pdf *.bak
	rm -rf *.lst *.map $(EXTRA_CLEAN_FILES)

lst:  $(PRG).lst

%.lst: %.elf
	$(OBJDUMP) -h -S $< > $@

# Rules for building the .text rom images

text: hex bin srec

hex:  $(PRG).hex
bin:  $(PRG).bin
srec: $(PRG).srec

%.hex: %.elf
	$(OBJCOPY) -j .text -j .data -O ihex $< $@

%.srec: %.elf
	$(OBJCOPY) -j .text -j .data -O srec $< $@

%.bin: %.elf
	$(OBJCOPY) -j .text -j .data -O binary $< $@

# Rules for building the .eeprom rom images

eeprom: ehex ebin esrec

ehex:  $(PRG)_eeprom.hex
ebin:  $(PRG)_eeprom.bin
esrec: $(PRG)_eeprom.srec

%_eeprom.hex: %.elf
	$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@

%_eeprom.srec: %.elf
	$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O srec $< $@

%_eeprom.bin: %.elf
	$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O binary $< $@


#
# Rules for programming via avrdude
#

AVRDUDE_PORT = usb
AVRDUDE_FLAGS = -p $(MCU_TARGET) -c $(AVRDUDE_PROGRAMMER) -P $(AVRDUDE_PORT)

program: $(PRG).hex
	avrdude $(AVRDUDE_FLAGS) -e -U flash:w:$(PRG).hex

programonly:
	avrdude $(AVRDUDE_FLAGS) -e -U flash:w:$(PRGWORKING)

#
# setup for "serial" programming via FTDI cable
#	note: to use this, there must be a bootloader in the AVR first!
#
# special notes:
#	-c stk500v1 sets protocol (apparently this is what the bootloader expects)
#	-b 19200 sets baud rate (to 19200)
#	-F disables reading of device signature
#	-V disabled automatic verify check after uploading data
#
##SER_PORT = /dev/tty.usbserial-FTDXRBEB
SER_PORT = \\.\COM7
AVRDUDE_SER_FLAGS = -p $(MCU_TARGET) -c stk500v1 -P $(SER_PORT) -b 19200

sprogram: $(PRG).hex
	avrdude $(AVRDUDE_SER_FLAGS) -e -U flash:w:$(PRG).hex


# to burn fuses with avrispmkII, don't do "-B 250" as it messes up the programmer.

# this is what's really needed for bootloaded atmega168 @ 16mhz
burn-fuse:
	avrdude $(AVRDUDE_FLAGS) -V -U hfuse:w:0xdf:m -U lfuse:w:0xcf:m

# bootloaded atmega168 @ 8mhz with internal oscillator
burn-fuse8osc:
	avrdude $(AVRDUDE_FLAGS) -V -U hfuse:w:0xdf:m -U lfuse:w:0xe2:m

# bootloaded atmega168 @ 8mhz with external ceramic resonator
burn-fuse8cr:
	avrdude $(AVRDUDE_FLAGS) -V -U hfuse:w:0xdf:m -U lfuse:w:0xd6:m

# XXX this seems to cause an avrdude error msg (safemode: efuse changed!)
##	avrdude $(AVRDUDE_FLAGS) -V -U efuse:w:0xf8:m -U hfuse:w:0xdf:m -U lfuse:w:0xcf:m

burn-fuse-old:
	avrdude $(AVRDUDE_FLAGS) -V -U hfuse:w:0xdd:m -U lfuse:w:0xff:m


# this leaves the result in 3 small text files
read-fuse:
	avrdude $(AVRDUDE_FLAGS) -u -U lfuse:r:l.txt:r
	avrdude $(AVRDUDE_FLAGS) -u -U hfuse:r:h.txt:r
	avrdude $(AVRDUDE_FLAGS) -u -U efuse:r:e.txt:r


# Every thing below here is used by avr-libc's build system and can be ignored
# by the casual user.

FIG2DEV                 = fig2dev
EXTRA_CLEAN_FILES       = *.hex *.bin *.srec

dox: eps png pdf

eps: $(PRG).eps
png: $(PRG).png
pdf: $(PRG).pdf

%.eps: %.fig
	$(FIG2DEV) -L eps $< $@

%.pdf: %.fig
	$(FIG2DEV) -L pdf $< $@

%.png: %.fig
	$(FIG2DEV) -L png $< $@
