PRG		= licht
OBJ		= licht.o
MCU_TARGET	= at90s2323
MCU_DUDE	= 2323
OPTIMIZE	= -O2

DEFS		=
LIBS		=

# You should not have to change anything below here.

CC		= avr-gcc

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

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

OBJCOPY		= avr-objcopy
OBJDUMP		= avr-objdump
NM		= avr-nm

AVRDUDE		= avrdude

.PHONY: all clean upload up upee lst text eeprom hex bin srec ehex ebin esrec

all: $(PRG).elf lst text eeprom

clean:
	rm -fv *.o $(PRG).elf *.bak *~ *.hex *.bin *.srec *.lst *.map

upload: up upee

up:
	uisp -dprog=stk200 --erase --upload --verify if=$(PRG).hex	
#	avrdude -c stk200 -p $(MCU_DUDE) -U flash:w:$(PRG).hex

upee:
	uisp -dprog=stk200 --segment=eeprom --upload --verify if=$(PRG)_eeprom.hex	
#	avrdude -c stk200 -p $(MCU_DUDE) -U eeprom:w:$(PRG)_eeprom.hex
	
lst:  $(PRG).lst

text: hex bin srec

eeprom: ehex ebin esrec

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

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

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

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

%.lst: %.elf
	$(OBJDUMP) -h -S $< > $@
	echo -e '\nVariables:' >> $@
	$(NM) -n $< | grep ' B ' >> $@
	avr-objdump -d -j .eeprom $< >> $@

%.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 $< $@

%_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 $< $@

