# 20190211AHz - MinGW makefile for amForth on an Arduino Uno board.
#               Derived from the default appl/arduino Makefile (slightly streamligned)
               
# Simple makefile for building the 
# Arduino amforth vor various targets

# Examples of usage for Arduino uno:
#
# 1) Assemble the whole flash and eemprom files
#     make uno.hex
#
# 2) Backup the current flash & eeprom values 
#     make uno.bak
#
# 3) Erase the whole MCU Flash
#    make uno.era
#
# 4) Upload the new firmware using the hex file generated
#    make uno
#
# 5) Set the appropiate MCU fuses
#    make uno.fuse
#
# 6) Clear files (except backup)
#    make uno.clr

.phony: ALL

ALL: clean uno.hex uno.era uno uno.fuse


##############################
# TARGET DEPENDANT VARIABLES #
##############################

# Set the amForth main dir. Missed in original Makefile
AMFORTH=../..
CORE=$(AMFORTH)/avr8

# Atmel tools
ATMELDIR="/c/Program Files (x86)/Atmel/Studio/7.0"
AVRPROGRAM=$(ATMELDIR)/atbackend/atprogram.exe
ASM=$(ATMELDIR)/toolchain/avr8/avrassembler/avrasm2.exe
AVRASMINC=$(ATMELDIR)/packs/atmel/ATmega_DFP/1.2.150/avrasm/inc

# ------------------------
# PROGRAMMER CONFIGURATION
# ------------------------

# Settings for Atmel programmer
#   Some hints of use are from https://www.avrfreaks.net/forum/atmel-studio6-avrisp-mkii-problem-programming
AVRPROGRAM_FLAGS=-t avrispmk2 -i isp -d atmega328p

# ----------------
# ASSEMBLER TO USE
# ----------------

AS_INCLUDE=-I $(AVRASMINC) -I$(CORE) -I$(CORE)/devices/ -I$(AMFORTH)/common

# flags Specific to avrasm2.exe
AS_FLAGS=$(AS_INCLUDE) -fI -v0

#--------------------------
# Generic assemble patterns
#--------------------------

# NOTE: 
#   To assemble the project one must comment out the definition of SPMEM
#   in ../../avr8/devices/atmega328p/device.asm

# Assemble the target
%.hex : %.asm
	@echo "Producing Hexfiles for Arduino $*" 
	@$(ASM) $(AS_FLAGS) -I $(CORE)/devices/atmega328p -e $*.eep.hex -m $*.map -l $*.lst $<

# Flash the target
% : %.hex
	@echo "Uploading Hexfiles to Arduino $* flash" 
	@$(AVRPROGRAM) $(AVRPROGRAM_FLAGS) program --verify -fl -f $*.hex
	@echo "Uploading Hexfiles to Arduino $* eeprom" 
	@$(AVRPROGRAM) $(AVRPROGRAM_FLAGS) program --verify -ee -f $*.eep.hex

# Set the fuse bits
%.fuse :
	@echo "Setting fuses to Arduino $*" 
	@$(AVRPROGRAM) $(AVRPROGRAM_FLAGS) write -fs --values FFD905    # low high extended
	@# Note: Fuses are read as :03000000FFD9FD28. Ignoring the 0x28, it gives the ext. Fuse as 0xFD instead 0x05.
	@#       This is ok because bits 7..3 are set to '1' by hw.
	
# Erase the whole MCU
%.era :
	@echo "Erasing entire Arduino $*" 
	@$(AVRPROGRAM) $(AVRPROGRAM_FLAGS) chiperase

# Clear assembled & auxilars files
%.clr:
	@echo "Cleaning all aux files" 
	@rm -f $*.hex ; rm -f $*.eep.hex ; rm -f $*.lst ; rm -f $*.map ; rm -f $*.cof ; rm -f $*.obj

# Backup arduino Flash & EEPROM files
%.bak:
	@echo "Backup Flash & EEPRON from Arduino $*" 
	@echo "    !!! NOT implemented in this (test-)version !!!" 
	@# $(AVRDUDE) $(AVRDUDE_FLAGS) -p $(PART) -U flash:r:$*.hex.bak:i -U eeprom:r:$*.eep.hex.bak:i

# Cleans everything
clean:
	rm -f *.hex ; rm -f *.eep.hex ; rm -f *.lst ; rm -f *.map ; rm -f *.cof ; rm -f *.obj
