PRG            = spi_reader
#MCU_TARGET     = atmega88
MCU_TARGET     = at90s2313   # EDIT THIS !!
AVRDUDE_PROG = stk500v2     # EDIT THIS !!
AVRDUDE_PORT = /dev/ttyUSB1
#AVRDUDE_DEVICE  = t45
AVRDUDE_DEVICE  = 2313

LFUSE=0xe2
HFUSE=0xdd

# -------------------------------

# Found on http://www.tty1.net/blog/2008-04-29-avr-gcc-optimisations_en.html
OPTIMIZE       = -O2 -mcall-prologues -mtiny-stack  -fomit-frame-pointer \
    -funsigned-char -funsigned-bitfields \
    -ffunction-sections -fpack-struct -fshort-enums \
    -ffreestanding

INCLUDE        = -I. -I../lib_src

LIBS           =  -lm
# You should not have to change anything below here.
CC             = avr-gcc
OBJCOPY        = avr-objcopy
OBJDUMP        = avr-objdump

## Options common to compile, link and assembly rules
COMMON = -mmcu=$(MCU_TARGET)

ODIR = build


# Override is only needed by avr-lib build system.
override CFLAGS         = $(COMMON) -gdwarf-2 -Wall $(OPTIMIZE) $(INCLUDE)  -std=c99  -fms-extensions
override LDFLAGS        = $(COMMON) -g -Wl,--relax,--gc-sections,-Map,$(PRG).map -L$(ODIR)


#------- dependencies ---------
# this comes from http://make.paulandlesley.org/autodep.html
DEPDIR = .deps
df = $(DEPDIR)/$(*F)

MAKEDEPEND = $(CC) $(COMMON) $(INCLUDE) -M $< > $(df).d

VPATH = ../lib_src


SRC = $(PRG).c uart.c buffer.c spi_sw.c spi_flash_en25f16.c
OBJ= $(patsubst %.c,$(ODIR)/%.o,$(SRC))

.PHONY = all prog push

all: $(ODIR) $(DEPDIR) $(PRG).elf lst text eeprom

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

lst:  $(PRG).lst

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

$(ODIR)/%.o: %.c
	@echo "~~~~~~ Building" $<
	@$(MAKEDEPEND)
	@echo -n $(ODIR)/ > $(df).P; \
	 cat < $(df).d  >>  $(df).P; \
	 sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
         -e '/^$$/ d' -e 's/$$/ :/' < $(df).d >> $(df).P; \
	 rm -f $(df).d;
	$(CC) $(CFLAGS) -o $@ -c $<

$(ODIR):
	mkdir $(ODIR)

$(DEPDIR):
	mkdir $(DEPDIR)

# Rules for building the .text rom images
text: hex
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 $< $@

prog : $(PRG).hex
	avrdude -y -c $(AVRDUDE_PROG) -P $(AVRDUDE_PORT) -p $(AVRDUDE_DEVICE) -U flash:w:$<

push : $(PRG).elf
	cp $< ../wc_fresh_avrstudio/default/wc_fresh_avrstudio.elf


prog_fuse :
#http://palmavr.sourceforge.net/cgi-bin/fc.cgi
#default value tiny45 : low=0x62 high=0xDF  (1MHz internal ocsilator)
	avrdude -c $(AVRDUDE_PROG) -P $(AVRDUDE_PORT) -p $(AVRDUDE_DEVICE) -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m  # 8Mhz brown out 2.7V

prog_fuse_default :
	avrdude -c $(AVRDUDE_PROG) -P $(AVRDUDE_PORT) -p $(AVRDUDE_DEVICE) -U lfuse:w:0x62:m -U hfuse:w:0xdf:m  # 1Mhz internal

read_fuse :
	avrdude -c $(AVRDUDE_PROG) -P $(AVRDUDE_PORT) -p $(AVRDUDE_DEVICE) -U hfuse:r:high.txt:h -U lfuse:r:low.txt:h
	@head low.txt high.txt

read_eeprom :
	avrdude -c $(AVRDUDE_PROG) -P $(AVRDUDE_PORT) -p $(AVRDUDE_DEVICE) -U eeprom:r:eeprom_read.txt:h
	@head eeprom_read.txt

prog_eeprom :
	avrdude -c $(AVRDUDE_PROG) -P $(AVRDUDE_PORT) -p $(AVRDUDE_DEVICE) -U eeprom:w:main_eeprom.hex

# Rules for building the .eeprom rom images
eeprom: ehex
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 $< $@

clean:
	rm -rf $(PRG).elf *.eps *.png *.pdf *.bak *~
	rm -rf *.lst *.map
	rm -rf $(ODIR)
	rm -rf $(DEPDIR)

-include $(SRC:%.c=$(DEPDIR)/%.P)

