###############################################################################
#SECU-3  - An open source, free engine control unit
#Copyright (C) 2007 Alexey A. Shabelnikov. Ukraine, Kiev
#
# Maklefile for build firmware of SECU-3 project using IAR
# Created by Alexey A. Shabelnikov, Kiev 17 July 2011. 
#
# Note: It requires IAR compiler and linker
# Also: Before using of this Makefile you must define environment variable IAR_HOME.
#       For instance "C:\Program Files\IAR Systems\Embedded Workbench 5.4" (without the double quotation marks)
#       Additionaly make sure CC, LD and LIBS variables in this Makefile are correct.

#include platform-specific options from another file
include platform_cfg

TARGET = secu-3_app
OBJDIR = ./output
CC = "$(IAR_HOME)/avr/bin/iccavr.exe"
LD = "$(IAR_HOME)/avr/bin/xlink.exe"
#LD = "$(IAR_HOME)/common/bin/xlink.exe"

# Compile options common for all C compilation units.
CFLAGS = --cpu $(MCU)
CFLAGS += -DREALTIME_TABLES -DTHERMISTOR_CS -DCOOLINGFAN_PWM -DDIAGNOSTICS -DHALL_OUTPUT -DFUEL_PUMP -DREV9_BOARD
CFLAGS += -DSM_CONTROL -DSTROBOSCOPE -DDWELL_CONTROL -DVREF_5V -DGD_CONTROL -DSECU3T
CFLAGS += -DBL_BAUD_RATE=115200 -DFW_BAUD_RATE=115200 -DSPEED_SENSOR -DINTK_HEATING -DBLUETOOTH_SUPP -DIMMOBILIZER -DUNI_OUTPUT -DAIRTEMP_SENS -DSEND_INST_VAL
CFLAGS += -DLITTLE_ENDIAN_DATA_FORMAT -DENABLE_BIT_DEFINITIONS -DEGOS_HEATING
#CFLAGS += -DIOCFG_FUNC_INIT
CFLAGS += -ms -e --initializers_in_flash
CFLAGS += -s9
#CFLAGS += --cross_call_passes=1 --do_cross_call
CFLAGS += --eeprom_size $(EE_SIZE)
ifeq ($(MCU), m1284)
CFLAGS += --dlib_config "$(IAR_HOME)/avr/lib/dlib/dlAVR-3s-ec_mul-n.h"
else
CFLAGS += --dlib_config "$(IAR_HOME)/avr/lib/dlib/dlAVR-3s-ec_mul-sf-n.h"
endif

# non-native end of line sequence detected (this diagnostic is only issued once)
CFLAGS += --diag_suppress Pa050
# this pragma must immediately precede a declaration
CFLAGS += --diag_suppress Pe606
# undefined behavior: the order of volatile accesses is undefined in this statement
CFLAGS += --diag_suppress Pa082

# Linker flags
LDFLAGS = -Fintel-extended -xs -s main
LDFLAGS += -f "$(IAR_HOME)/avr/config/$(LNKXCL)"
LDFLAGS += -l $(OBJDIR)/secu3.map
LDFLAGS += -Z.boot_loader=$(BL_START) -Z.firmware_data=$(FWD_START)
ifeq ($(MCU), m1284)
LIBS = "$(IAR_HOME)/avr/lib/dlib/dlAVR-3s-ec_mul-n.r90"
else
LIBS = "$(IAR_HOME)/avr/lib/dlib/dlAVR-3s-ec_mul-sf-n.r90"
endif
#LIBS = "$(IAR_HOME)/avr/lib/dlib/dl3s-ec-sf.r90"

# Include Directories
INCLUDES = -I "$(IAR_HOME)/avr/inc/dlib" -I "$(IAR_HOME)/avr/inc"

# Define all source files.
SRC = adc.c bootldr.c ce_errors.c ckps.c crc16.c \
	eeprom.c pwrvalve.c funconv.c fuelcut.c eculogic.c \
	jumper.c knklogic.c knock.c measure.c params.c \
	procuart.c secu3.c starter.c suspendop.c tables.c \
	uart.c ventilator.c vstimer.c camsens.c fuelpump.c \
	diagnost.c wdt.c ioconfig.c pwrrelay.c bc_input.c \
	smcontrol.c choke.c hall.c bluetooth.c onewire.c \
	immobiliz.c ckps2ch.c intkheat.c injector.c uni_out.c \
	lambda.c ecudata.c gasdose.c gdcontrol.c carb_afr.c \
	ckpsn+1.c mathemat.c obd.c dbgvar.c evap.c aircond.c \
	egosheat.c ckps-cs.c pwm2.c grheat.c grvalve.c \
	ckps-odd.c ltft.c

# Define all object files and dependencies
OBJECTS = $(SRC:%.c=$(OBJDIR)/%.r90)
DEPS = $(OBJECTS:$(OBJDIR)/%.r90=$(OBJDIR)/%.d)

# Define all listing files.
LST = $(SRC:%.c=$(OBJDIR)/%.lst)

#Thanks to guys from Microsoft we have to deal with spaces...
SPAT=$(shell echo "$(IAR_HOME)" | sed -e 's/\\\/\\\\\\\\\\\/g')
RPAT=$(shell echo "$(IAR_HOME)" | sed -e 's/\\\/\\\\\\\\\\\/g' -e 's/ /\\\\\\\\\\\ /g')

# Build
all: OBJ_DIRS $(TARGET).hex

#Include make files containing dependencies
-include $(DEPS)

#Create directory for object files
OBJ_DIRS :
	@mkdir -p $(OBJDIR)

# Compile
$(OBJDIR)/%.r90 : sources/%.c 
	$(CC) $(INCLUDES) $(CFLAGS) $< -o $@ -lC $(OBJDIR:%.r90=$(OBJDIR)/%.lst) --dependencies=m $(OBJDIR:%.r90=$(OBJDIR)/%.d)
	@sed -e 's/$(SPAT)/$(RPAT)/' $(patsubst %.r90, %.d, $@) > tmpfile; mv tmpfile $(patsubst %.r90, %.d, $@)

#Link
$(TARGET).hex: $(OBJECTS)
	$(LD) $(LDFLAGS) $(OBJECTS) $(LIBDIRS) $(LIBS) -o $(TARGET).hex

# Clean target
clean:
	@rm -f $(OBJECTS) $(LST) $(TARGET).hex $(OBJDIR)/*.map $(DEPS)
