################################################################################
# File automatically generated by Atmel Start.
################################################################################
#
# Makefile for AVR8 IAR C/C++ compiler and assembler.
#
################################################################################
#
# WARNING: THIS IS AN EXPERIMENTAL FEATURE. Glitches may occur and
#          manual adjustments may be required.
#
# The IAR toolchain is not cross-platform. This is a GNU makefile for
# windows (cygwin or mingw32), rely on running it in wine on linux.
#
# Prerequisites: Must have access to valid IAR license, IAR_ROOT must be
# correctly specified (see below)
#
# Invocation: From project root folder:
#
# make [options] -f iar/Makefile
#
################################################################################
#### USER TWEAKABLE SETTINGS
################################################################################
# Version of IAR Embedded Workbench
IAR_VERSION ?= 8.0

# Root of IAR installation, override by user if needed
# Must use := assignment here courtesy of Cygwin
IAR_ROOT := "C:/Program Files (x86)/IAR Systems/Embedded Workbench $(IAR_VERSION)/avr"

# CONFIG is either debug or release. Intel Hex is produced for release.
CONFIG ?= debug

# IDE setting is either studio or c-spy (embedded workbench),
# affects the debug output format produced.
# For Studio, UBROF8 is produced. For c-spy, refer to -rt linker option.
IDE ?= studio

# Name of target image file (less extension).
TARGET ?= atmelstart

# Memory model, please see IAR compiler documentation.
MEM_MODEL ?= s

################################################################################
#### END OF USER TWEAKABLE SETTINGS
################################################################################

# Command-line tools used. Cannot use ?= here! These should not be changed.
CC = $(IAR_ROOT)/bin/iccavr.exe  
AS = $(IAR_ROOT)/bin/aavr.exe
LD = $(IAR_ROOT)/bin/xlink.exe



#Notice: No space between -I/-D and argument courtesy of IAR assembler
#
# IAR uses at least 3 different representations of device name
#
# CPU name is m*** for mega devices, xm*** for xmega devices, tiny***
# for tiny devices, all letters lower case. Example: m324pb, xm32a4u, tiny817
CPU ?= tiny817

DEVICE ?= __ATtiny817__

INCLUDE_PATHS += -I"config" -I"examples/include" -I"include" -I"utils" -I"utils/assembler" -I"." -I"include" 
EEPROM_SIZE = 128
OBJDIR = $(CONFIG)/obj
EXEDIR = $(CONFIG)/exe
SRC_DIRS +=  src examples/src

# We use .o suffix rather than .r90 for all object files.

OBJS +=  \
$(OBJDIR)/adc_window.o \
$(OBJDIR)/bod.o \
$(OBJDIR)/cpuint.o \
$(OBJDIR)/rtc.o \
$(OBJDIR)/clkctrl.o \
$(OBJDIR)/usart_basic_example.o \
$(OBJDIR)/driver_isr.o \
$(OBJDIR)/usart_basic.o \
$(OBJDIR)/main.o \
$(OBJDIR)/evsys.o \
$(OBJDIR)/atmel_start.o \
$(OBJDIR)/adc_window_example.o \
$(OBJDIR)/slpctrl.o \
$(OBJDIR)/driver_init.o \
$(OBJDIR)/protected_io.o

# Used for assembler & linker options
arch = arch1

dlib = $(IAR_ROOT)/lib/dlib/dlAVR-1s-arch4-n

cfgfile1 = $(IAR_ROOT)/src/template/cfg$(CPU).xcl
cfgfile2 = $(IAR_ROOT)/src/template/cfg_avr_arch4_1soim.xcl
AS_CPUFLAGS ?= -v1 -u_enhancedCore -D__HAS_ENHANCED_CORE__=1 -D__HAS_MUL__=1 -D__AVR_ARCH4__=1 -D__MEMORY_MODEL__=2 -D__ATtiny817__=1

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

CPPFLAGS = -D__$(CPU)__ -DF_CPU=5000000 $(INCLUDE_PATHS) \
  -DENABLE_BIT_DEFINITIONS
CFLAGS =  --cpu=$(CPU) -m$(MEM_MODEL) --initializers_in_flash -e --dlib \
  --dlib_config $(dlib).h --eeprom_size $(EEPROM_SIZE)
ASFLAGS += -s+ -w+ $(AS_CPUFLAGS)

LDFLAGS = -l $(CONFIG)/list/$(TARGET).map -f $(cfgfile1)  -f iar/avr8_iar.xcl -f $(cfgfile2) \
  -s __program_start $(dlib).r90 

vpath %.c $(SRC_DIRS)
vpath %.s $(SRC_DIRS)
vpath %.S $(SRC_DIRS)
vpath %.o $(OBJDIR)

ifeq (debug,$(CONFIG))
	OPTIMIZE ?= -Ol
	CFLAGS += --no_cse --no_inline --no_code_motion --no_cross_call --no_clustering --no_tbaa --debug $(OPTIMIZE)
	ASFLAGS += -r
	ifeq (studio,$(IDE))
		# UBROF8 is the latest version supported by Studio.
		LDFLAGS += -Fubrof8
	else
		# Format for IAR C-SPY, not understood by Studio
		LDFLAGS += -rt
	endif
	EXE = $(EXEDIR)/$(TARGET).d90
else
	CPPFLAGS += -DNDEBUG
	OPTIMIZE ?= -Ohz
	CFLAGS += $(OPTIMIZE)
	LDFLAGS += -FINTEL-STANDARD
	EXE = $(EXEDIR)/$(TARGET).hex
endif


$(OBJDIR)/%.o: %.c
	$(CC) $^ $(CPPFLAGS) $(CFLAGS) -o$@ 

$(OBJDIR)/%.o: %.s
	$(AS) $^ $(INCLUDE_PATHS) $(ASFLAGS) -o $@

# IAR assembler has built-in preprocessor, no distinction between .s and .S
$(OBJDIR)/%.o: %.S
	$(AS) $^ $(INCLUDE_PATHS) $(ASFLAGS) -o $@

all: $(TARGET)

$(TARGET): dirs $(EXE)

$(EXE): $(OBJS)
	$(LD) $^ -o $@ $(LDFLAGS)


dirs:
	mkdir -p $(OBJDIR) $(EXEDIR) $(CONFIG)/list

clean:
	$(RM) $(OBJS) $(EXE)

.PHONY: all clean dirs $(TARGET)
