## define some variables based on the AVR base path in $(AVR)
CC	= avr-gcc
OBJCOPY	= avr-objcopy
OBJDUMP = avr-objdump

ifeq ($(patsubst com%,com,$(port)),com)
	PORT=com
	PORTNO=$(subst com,,$(port))
endif
ifeq ($(patsubst lpt%,lpt,$(port)),lpt)
	PORT=lpt
	PORTNO=$(subst lpt,,$(port))
endif
ifeq ($(patsubst usb%,usb,$(port)),usb)
	PORT=usb
endif


ISPPROG=default

# NANO-24
ifeq ($(PLATFORM),nano-24)
ifeq ($(PORT),lpt)
	ISPPROG=ponyprog
endif
ifeq ($(PORT),usb)
	ISPPROG=avrdude_usb
endif
endif

ifeq ($(PLATFORM),etri-ssn)
ifeq ($(PORT),lpt)
	ISPPROG=ponyprog
endif
ifeq ($(PORT),usb)
	ISPPROG=avrdude_usb
endif

endif

ifeq ($(PLATFORM),zigbex)
ifeq ($(PORT),lpt)
	ISPPROG=ponyprog
endif
ifeq ($(PORT),usb)
	ISPPROG=avrdude_usb
endif
ifeq ($(PORT),com)
	ISPPROG=uisp_com
endif
	COMNO = $(shell expr $(PORTNO) - 1)
endif

# MICAZ
ifeq ($(PLATFORM),micaz)
ifeq ($(PORT),com)
	ISPPROG=uisp_com
endif
	COMNO = $(shell expr $(PORTNO) - 1)
endif

# TEST_PLATFORM
ifeq ($(PLATFORM),test-platform)
ifeq ($(PORT),lpt)
	ISPPROG=ponyprog
endif
ifeq ($(PORT),usb)
	ISPPROG=avrdude_usb
endif
endif

# OCTACOMM_SHIB
ifeq ($(PLATFORM),octacomm-shib)
ifeq ($(PORT),lpt)
	ISPPROG=ponyprog
endif
endif


#---------------------------------------------------------------------------------------------------------------
burn : $(ISPPROG)

default : 
	@echo "Error : Wrong ISP port given for the selected target platform."
	@echo "Usage : make burn port=[lpt1|lpt2|...|usb|com1|com2|...]"

#ISP through parallel port 
ponyprog : 
	@echo "$(TRG).rom ==> $(MCU) by PonyProg2000" 
	@echo -e "SELECTDEVICE $(MCU)\nLOAD-PROG $(TRG).rom\nWRITE-PROG" > isp.e2s 
	@PonyProg2000.exe isp.e2s 
	@$(RM) isp.e2s
	@echo "================== Program Download Finished =================="

#usbasp : ISP for Nano24_interface_2.0 through usb.
avrdude_usb :
	@echo "$(TRG).rom ==> $(MCU) by AVRDUDE through USB"
	@echo ""
	$(NOS_HOME)/bin/avrdude/avrdude.exe -p m128 -c usbasp -e -U flash:w:$(TRG).rom
	@echo "================== Program Download Finished =================="	

#mib510 : ISP for Crossbow MIB510 through serial port
uisp_com :
	@echo "$(TRG).rom ==> $(MCU) by UISP through COM$(PORTNO)"
	@echo ""
	$(NOS_HOME)/bin/uisp/uisp.exe -dprog=mib510 -dserial=/dev/ttyS$(COMNO) -dpart=$(MCU) --wr_fuse_e=ff --erase --upload if=$(TRG).rom 
	@echo "================== Program Download Finished =================="




#--option----------------------------------------------------------------------------------------------------------
#To let avrdude or uisp use parallel port , you must run $(NOS_HOME)/bin/avrdude/install_giveio.bat
#						    or $(AVR_HOME)/bin/install_giveio.bat

avrdude_lpt :
	@echo "$(TRG).rom ==> $(MCU) by AVRDUDE through LPT$(PORTNO)"
	@echo ""
	$(NOS_HOME)/bin/avrdude/avrdude.exe -p m128 -c stk200 -P lpt$(PORTNO) -e -U flash:w:$(TRG).rom
	@echo "================== Program Download Finished =================="

#parallel port address : 0x378(default), or 0x278, 0x3BC
uisp_lpt :
	@echo "$(TRG).rom ==> $(MCU) by UISP"
	@echo ""
	$(NOS_HOME)/bin/uisp/uisp.exe -dprog=stk200 -dlpt=0x378 -dpart=$(MCU) --wr_fuse_e=ff --erase --upload if=$(TRG).rom 
	@echo "================== Program Download Finished =================="


