
# make requires to be run from sh (not cmd!) 
# Some targets need cp and mv commands:
# in case the user forgets to set up his own my_make and etc/ files.
# (cp and mv provided in winavr for winferior systems)
# perl is also needed for some targets (see activestate perl)

# WARNTEXT = $(SHLVL)
WARNTEXT ?= "runme from sh shell, win32 cmd is broken, dies with CreateProcess error"

#Target processor:
	MCU = atmega128

#Main target file
	TRG = vems

#C sources
	SRC  = vems.c helpers.c adc.c comm.c fuelcalc.c tables.c fuelcontrol.c storage.c actuators.c ve.c iac.c keyboard.c lcd.c heap.c eventqueue.c button.c benchmark.c lcd_display.c injconf.c ignconf.c timing.c kb.c menu.c ringbuf.c ego.c wbo2.c pid.c log.c multitooth.c spi.c knock.c boostcontrol.c ign_logging.c sipr.c sipr_protocol.c slcan.c ipid.c

#firmware.tgz goes here:
	DISTDIR = ..
	
# Debugging format.
#     Native formats for AVR-GCC's -g are dwarf-2 [default] or stabs.
#     AVR Studio 4.10 requires dwarf-2.
#     AVR [Extended] COFF format requires stabs, plus an avr-objcopy run.
DEBUG = stabs

#Assembler flags
	ASFLAGS = -Wa, -g$(DEBUG)
	
#C Compiler flags -O2 fails sometimes (lcd.c and button.c), use -O0
	CPBASICFLAGS = -g$(DEBUG) -Wall -Wstrict-prototypes -Wa,-ahlms=$(<:.c=.lst)

#Linker flags
	LDFLAGS = -Wl,--script,avr5-vems.x,-Map=$(TRG).map,--cref
	
# fixed start adressses for the temperature lookup tables, so they can be written from bootloader separately
# note, these are byte adresses, while the adresses in the atmel atmega128 datasheet are word adresses
# Placed to the first 64kbyte of flash, as avr/pgmspace.h recommends 
# (btw, this is the only sane place to be compatible with uC that has <64kbyte flash, like atmega168)

	# text is a composite, eg. it starts with vectors. 
	# .progmem.airdenfactor=0x... does NOT work, unfortunately !!!
	# So we must use our own linker script to split the text segment
	# so interrupt vectors are placed to the right location
	# prog.pl must be fixed (currently bails out if sees a gap!)
	LDFLAGS += -Wl,--section-start=.interrupt_vectors=0x0
	LDFLAGS += -Wl,--section-start=.airdenfactor=0x100
	LDFLAGS += -Wl,--section-start=.matfactor=0x200
	LDFLAGS += -Wl,--section-start=.thermfactor=0x300
	LDFLAGS += -Wl,--section-start=.text=0x400

	# avr-ld --script,avr5-vems.x ...
	# or
	# avr-gcc -Wl,--script,avr5-vems.x ...

# TODO: should also be fixed location:
#	1/x table 
#	any CRC related table
#	injopen table
#	entry point for main control tables (VE, lambda, ignadv, config, etc...). Latest when ported to ARM.

# include personal configuration file
include my_make

	CPBASICFLAGS += $(MY_CONF)
	
	CPFLAGS = -O3 $(CPBASICFLAGS)		# Optimize for speed
	CPSAFEFLAGS = -O3 $(CPBASICFLAGS)
	
#	CPFLAGS = -O0 $(CPBASICFLAGS)		# Debug mode. Don't optimize
#	CPSAFEFLAGS = -O0 $(CPBASICFLAGS)

include avr_make

# if the user forgets to provide his own files, this "fixes" it (with incorrect values, most likely)
$(TRG).pre: SH_NEEDED my_make etc/thermfactor.c etc/airdenfactor.c etc/matfactor.c etc/injopentable.c

#$(SHLVL:-hihi)
# could it be tested with $SHELL or $SHLVL variable?
.PHONY: SH_NEEDED

SH_NEEDED:
	echo $(WARNTEXT)

my_make:
	cp -i doc/my_make .

etc/thermfactor.c:
	cp -i thermfactor.c etc

etc/matfactor.c:
	cp -i matfactor.c etc

etc/airdenfactor.c: airdenfactor.c
	cp -i airdenfactor.c etc

airdenfactor.c: inc/airdenfactor.inc bin/inc2tbl
	perl bin/inc2tbl <inc/airdenfactor.inc >airdenfactor.c
	
matfactor.c: inc/matfactor.inc bin/inc2tbl
	perl bin/inc2tbl <inc/matfactor.inc >matfactor.c
	
thermfactor.c: inc/thermfactor.inc bin/inc2tbl
	perl bin/inc2tbl <inc/thermfactor.inc >thermfactor.c


###### dependencies, add any dependencies you need here ###################
$(TRG).o	: $(TRG).c vems.h global.h helpers.h adc.h storage.h comm.h actuators.h ve.h iac.h button.h compile_time.h
helpers.o	: helpers.c helpers.h global.h
adc.o		: adc.c adc.h global.h
comm.o		: comm.c comm.h global.h storage.h actuators.h ve.h varstr.h
fuelcalc.o	: fuelcalc.c fuelcalc.h global.h 
fuelcontrol.o	: fuelcontrol.c fuelcontrol.h fuelcalc.h global.h adc.h actuators.h iac.h
storage.o	: storage.c storage.h global.h
tables.o	: tables.c tables.h etc/thermfactor.c etc/matfactor.c etc/airdenfactor.c 
actuators.o	: actuators.c actuators.h global.h
ve.o		: ve.c global.h
iac.o		: iac.c global.h
heap.o		: heap.c
lcd.o		: lcd.c
ego.o		: ego.c
lcd_display.o: lcd_display.c lcd_display.h magicstr.h
	$(CC) -c $(CPSAFEFLAGS) -I$(INCDIR) $< -o $@

kb.o		: kb.c kbpindefs.h scancodes.h
comm.o		: comm.c
keyboard.o	: keyboard.c
button.o	: button.c
	$(CC) -c $(CPSAFEFLAGS) -I$(INCDIR) $< -o $@

wbo2.o		: wbo2.c
pid.o		: pid.c
log.o		: log.c



############################

# make coff = Convert ELF to AVR COFF (for use with AVR Studio 3.x or VMLAB).
#
# make extcoff = Convert ELF to AVR Extended COFF (for use with AVR Studio
#                4.07 or greater).

OBJCOPY = avr-objcopy
COFFCONVERT=$(OBJCOPY) --debugging \
--change-section-address .data-0x800000 \
--change-section-address .bss-0x800000 \
--change-section-address .noinit-0x800000 \
--change-section-address .eeprom-0x810000


vems.cof: vems.elf
	$(COFFCONVERT) -O coff-ext-avr vems.elf > vems.cof


# oops, this requires perl interpreter... (should we add it to cvs?? or sometimes upload to a webpage?)
# just in case, check 
# could be autofetched with wget (which you probably have if sh is installed from unxutils)
magicstr.h: global.h bin/stru.pl
	perl bin/get_name.pl global.h || (wget http://www.x-dsl.hu/genboard/magicstr.h && echo "magicstr might be OLD")

varstr.h: global.h bin/get_name.pl
	perl bin/get_name.pl global.h || (wget http://www.x-dsl.hu/genboard/varstr.h && echo "varstr might be OLD")

compile_time.h: bin/ctime.pl $(SRC)
	perl bin/ctime.pl || echo "const char c_time[] PROGMEM = \"Unknown\"; " > compile_time.h

mtt: etc/config.mtt etc/tables.mtt

etc/config.mtt: etc/config.txt global.h
	perl bin/make_conf.pl global.h etc/config.txt etc/config.mtt

etc/tables.mtt: etc/tables.txt
	perl bin/make_table.pl etc/tables.txt etc/tables.mtt

# |gzip could be added, but avoid tar czf
# boot/main.hex not in firmware dist any longer
#	tar cf - . | gzip >$(DISTDIR)/firmware.tgz
dist: vems.hex
	zip -r /tmp/firmware_dist.zip . -x \*.o -x \*.elf -x \*.asm -x \*.lst -x \*.map \*.svn\*

boot/main.hex:
	cd boot; make

etc/injopentable.c:
	bin/injopentable.pl > etc/injopentable.c

