###############################################################################
# Makefile for the project TransistorTestNew
###############################################################################

## General Flags
PROJECT = TransistorTestNew
TARGET = TransistorTestNew.elf
CC = avr-gcc

CPP = avr-g++

CFLAGS = -Wall

# ********************** Änderbare Flags zur Configuration des Transistortesters
# ********************** config options for your Semiconductor tester
# NOTE: Software will deselect impossible Options for atmega48 automatically!!
#every changing of this Makefile will result in new compiling the whole
#programs, if you call make or make upload

#select your Part-No. for avrdude :
# atmega8  : m8
# atmega48 : m48 or m48p
# atmega88 : m88 or m88p
# atmega168: m168 or m168p
# atmega328: m328 or m328p
PARTNO = m8

#select your language
# available languages are: LANG_ENGLISH, LANG_GERMAN, LANG_POLISH, LANG_CZECH, LANG_SLOVAK
UI_LANGUAGE = LANG_GERMAN

# the LCD_CYRILLIC option is nessasary, if you have a display with cyrillic characterset
# this lcd-display don't have a character for Ohm and for µ (micro)
#CFLAGS += -DLCD_CYRILLIC

# the WITH_SELFTEST option enables selftest function 
CFLAGS += -DWITH_SELFTEST

# the WITH_AUTO_REF option enables reading of internal REF-voltage to get factors for the Capacity measuring
CFLAGS += -DWITH_AUTO_REF
#CFLAGS += -DREF_KORR=14

# the WITH_UART option enables the software UART
CFLAGS += -DWITH_UART

# the R_MESS option enables Resistor measuring 
CFLAGS += -DR_MESS

# the C_MESS option enables Capacity measuring 
CFLAGS += -DC_MESS

# the AUTOSCALE_ADC option enables the autoscale ADC (VCC and Bandgap Ref)
#CFLAGS += -DAUTOSCALE_ADC
# NO_AREF_CAP tells your Software, that you have no Capacitor installed at pin AREF (21)
# this enables a shorter wait-time for AUTOSCALE_ADC function
CFLAGS += -DNO_AREF_CAP

# the OP_MHZ option tells the software the Operating Frequency
OP_MHZ = 8
#OP_MHZ = 1

# the USE_EEPROM option specify where you wish to locate fix text and tables
CFLAGS += -DUSE_EEPROM

# the PULLUP_DISABLE option disable the pull-up Resistors of IO-Ports
# to use this option a external pull-up Resistor (10k to 30k)
# from Pin 13 to VCC must be installed!
CFLAGS += -DPULLUP_DISABLE

# the ANZ_MESS option specifies, how often an ADC value is read and accumulated
# possible values for 5V Operation are 44, 22 and 11
CFLAGS += -DANZ_MESS=44

# The POWER_OFF option enables the power off function, otherwise loop measurements infinitely
# until power is disconnected with a ON/OFF switch (CFLAGS += -DPOWER_OFF).
# If you have the tester without the power off transistors, you can deselect POWER_OFF
# If you have NOT selected the POWER_OFF option with the transistors installed,
# you can stop measuring by holding the key several seconds after a result is
# displayed. After releasing the key, the tester will be shut off by timeout.
# Otherwise you can also specify, after how many measurements without found part
# the tester will shut down (CFLAGS += -DPOWER_OFF=10).
#  You can specify up to 255 empty measurements (CFLAGS += -DPOWER_OFF=255).
CFLAGS += -DPOWER_OFF
# option BAT_CHECK enables the Battery Voltage Check, otherwise output SW Version
# is only needed for battery powered tester version
CFLAGS += -DBAT_CHECK
# the BAT_OUT option enables Battery Voltage Output on LCD (if BAT_CHECK is selected)
CFLAGS += -DBAT_OUT


# select your programmer type and port if you will use avrdude
PROGRAMMER=avrisp2
PORT=usb
# ********************** end of selectable options

ifeq ($(PARTNO),m8)
MCU = atmega8
ifeq ($(OP_MHZ),8)
# BODLEV 4V, BODEN enabeld, SUT 65ms, CLK 8MHz
FUSES_INT = -U lfuse:w:0x24:m -U hfuse:w:0xd9:m
FUSES_CRY = -U lfuse:w:0x3f:m -U hfuse:w:0xd9:m
else
# BODLEV 4V, BODEN enabeld, SUT 65ms, CLK 1MHz
FUSES_INT = -U lfuse:w:0x21:m -U hfuse:w:0xd9:m
# Betrieb mit 1MHz Quarz!!!
FUSES_CRY = -U lfuse:w:0x3d:m -U hfuse:w:0xd9:m
endif
endif

ifeq ($(PARTNO),m48)
MCU = atmega48
ifeq ($(OP_MHZ),8)
FUSES_INT = -U lfuse:w:0xe2:m -U hfuse:w:0xdc:m
# -U efuse:w:0xff:m
FUSES_CRY = -U lfuse:w:0xf7:m -U hfuse:w:0xdc:m
# -U efuse:w:0xff:m
else
FUSES_INT = -U lfuse:w:0x62:m -U hfuse:w:0xdc:m
# -U efuse:w:0xff:m
# Betrieb mit 8MHz Quarz und :8 Teiler
FUSES_CRY = -U lfuse:w:0x77:m -U hfuse:w:0xdc:m
# -U efuse:w:0xff:m
endif
endif

ifeq ($(PARTNO),m48p)
MCU = atmega48
ifeq ($(OP_MHZ),8)
FUSES_INT = -U lfuse:w:0xe2:m -U hfuse:w:0xdc:m
# -U efuse:w:0xff:m
FUSES_CRY = -U lfuse:w:0xf7:m -U hfuse:w:0xdc:m
# -U efuse:w:0xff:m
else
FUSES_INT = -U lfuse:w:0x62:m -U hfuse:w:0xdc:m
# -U efuse:w:0xff:m
# Betrieb mit 8MHz Quarz und :8 Teiler
FUSES_CRY = -U lfuse:w:0x77:m -U hfuse:w:0xdc:m
# -U efuse:w:0xff:m
endif
endif

ifeq ($(PARTNO),m88)
MCU = atmega88
ifeq ($(OP_MHZ),8)
FUSES_INT = -U lfuse:w:0xe2:m -U hfuse:w:0xdc:m
# -U efuse:w:0xf9:m
FUSES_CRY = -U lfuse:w:0xf7:m -U hfuse:w:0xdc:m
# -U efuse:w:0xf9:m
else
FUSES_INT = -U lfuse:w:0x62:m -U hfuse:w:0xdc:m
# -U efuse:w:0xf9:m
# Betrieb mit 8MHz Quarz und :8 Teiler
FUSES_CRY = -U lfuse:w:0x77:m -U hfuse:w:0xdc:m
# -U efuse:w:0xf9:m
endif
endif

ifeq ($(PARTNO),m88p)
MCU = atmega88
ifeq ($(OP_MHZ),8)
FUSES_INT = -U lfuse:w:0xe2:m -U hfuse:w:0xdc:m
# -U efuse:w:0xf9:m
FUSES_CRY = -U lfuse:w:0xf7:m -U hfuse:w:0xdc:m
# -U efuse:w:0xf9:m
else
FUSES_INT = -U lfuse:w:0x62:m -U hfuse:w:0xdc:m
# -U efuse:w:0xf9:m
# Betrieb mit 8MHz Quarz und :8 Teiler
FUSES_CRY = -U lfuse:w:0x77:m -U hfuse:w:0xdc:m
# -U efuse:w:0xf9:m
endif
endif

ifeq ($(PARTNO),m168)
MCU = atmega168
ifeq ($(OP_MHZ),8)
FUSES_INT = -U lfuse:w:0xe2:m -U hfuse:w:0xdc:m
# -U efuse:w:0xf9:m
FUSES_CRY = -U lfuse:w:0xf7:m -U hfuse:w:0xdc:m
# -U efuse:w:0xf9:m
else
FUSES_INT = -U lfuse:w:0x62:m -U hfuse:w:0xdc:m
# -U efuse:w:0xf9:m
# Betrieb mit 8MHz Quarz und :8 Teiler
FUSES_CRY = -U lfuse:w:0x77:m -U hfuse:w:0xdc:m
# -U efuse:w:0xf9:m
endif
endif

ifeq ($(PARTNO),m168p)
MCU = atmega168
ifeq ($(OP_MHZ),8)
FUSES_INT = -U lfuse:w:0xe2:m -U hfuse:w:0xdc:m
# -U efuse:w:0xf9:m
FUSES_CRY = -U lfuse:w:0xf7:m -U hfuse:w:0xdc:m
# -U efuse:w:0xf9:m
else
FUSES_INT = -U lfuse:w:0x62:m -U hfuse:w:0xdc:m
# -U efuse:w:0xf9:m
# Betrieb mit 8MHz Quarz und :8 Teiler
FUSES_CRY = -U lfuse:w:0x77:m -U hfuse:w:0xdc:m
# -U efuse:w:0xf9:m
endif
endif

ifeq ($(PARTNO),m328)
MCU = atmega328
ifeq ($(OP_MHZ),8)
FUSES_INT = -U lfuse:w:0xe2:m -U hfuse:w:0xd9:m
# -U efuse:w:0xfc:m
FUSES_CRY = -U lfuse:w:0xf7:m -U hfuse:w:0xd9:m
# -U efuse:w:0xfc:m
else
FUSES_INT = -U lfuse:w:0x62:m -U hfuse:w:0xd9:m
# -U efuse:w:0xfc:m
# Betrieb mit 8MHz Quarz und :8 Teiler
FUSES_CRY = -U lfuse:w:0x77:m -U hfuse:w:0xd9:m
# -U efuse:w:0xfc:m
endif
endif

ifeq ($(PARTNO),m328p)
MCU = atmega328
ifeq ($(OP_MHZ),8)
FUSES_INT = -U lfuse:w:0xe2:m -U hfuse:w:0xd9:m
# -U efuse:w:0xfc:m
FUSES_CRY = -U lfuse:w:0xf7:m -U hfuse:w:0xd9:m
# -U efuse:w:0xfc:m
else
FUSES_INT = -U lfuse:w:0x62:m -U hfuse:w:0xd9:m
# -U efuse:w:0xfc:m
# Betrieb mit 8MHz Quarz und :8 Teiler
FUSES_CRY = -U lfuse:w:0x77:m -U hfuse:w:0xd9:m
# -U efuse:w:0xfc:m
endif
endif

ifeq ($(OP_MHZ),8)
CFLAGS += -DF_CPU=8000000UL
else
CFLAGS += -DF_CPU=1000000UL
endif
## Options common to compile, link and assembly rules
COMMON = -mmcu=$(MCU)

## Compile options common for all C compilation units.
CFLAGS += $(COMMON)
CFLAGS += -gdwarf-2 -std=gnu99 -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
CFLAGS += -MD -MP -MT $(*F).o -MF dep/$(@F).d 

## Assembly specific flags
ASMFLAGS = $(COMMON)
ASMFLAGS += $(CFLAGS)
ASMFLAGS += -x assembler-with-cpp -Wa,-gdwarf2

## Linker flags
LDFLAGS = $(COMMON)
LDFLAGS +=  -Wl,-Map=TransistorTestNew.map


## Intel Hex file production flags
HEX_FLASH_FLAGS = -R .eeprom -R .fuse -R .lock -R .signature

HEX_EEPROM_FLAGS = -j .eeprom
HEX_EEPROM_FLAGS += --set-section-flags=.eeprom="alloc,load"
HEX_EEPROM_FLAGS += --change-section-lma .eeprom=0 --no-change-warnings



## Objects that must be built in order to link
OBJECTS = lcd-routines.o main.o swuart.o wait1000ms.o CombineToLong.o ReadADC.o

## Objects explicitly added by the user
LINKONLYOBJECTS = 


## Build
all: $(TARGET) TransistorTestNew.hex TransistorTestNew.eep TransistorTestNew.lss size

## Compile
lcd-routines.o: ../lcd-routines.c Makefile
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

ReadADC.o: ../ReadADC.S Makefile
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

main.o: ../main.c Makefile
	$(CC) $(INCLUDES) $(CFLAGS) -D$(UI_LANGUAGE) -c  $<

swuart.o: ../swuart.S Makefile
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

wait1000ms.o: ../wait1000ms.S Makefile
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

CombineToLong.o: ../CombineToLong.S Makefile
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

##Link
$(TARGET): $(OBJECTS)
	 $(CC) $(LDFLAGS) $(OBJECTS) $(LINKONLYOBJECTS) $(LIBDIRS) $(LIBS) -o $(TARGET)

%.hex: $(TARGET)
	avr-objcopy -O ihex $(HEX_FLASH_FLAGS)  $< $@

%.eep: $(TARGET)
	-avr-objcopy $(HEX_EEPROM_FLAGS) -O ihex $< $@ || exit 0

%.lss: $(TARGET)
	avr-objdump -h -S $< > $@

size: ${TARGET}
	@echo
	#@avr-size ${TARGET}
	@avr-size -C --mcu=${MCU} ${TARGET}

## Clean target
.PHONY: clean
clean:
	-rm -rf $(OBJECTS) TransistorTestNew.elf dep/* TransistorTestNew.hex TransistorTestNew.eep TransistorTestNew.lss TransistorTestNew.map


## Other dependencies
-include $(shell mkdir dep 2>/dev/null) $(wildcard dep/*)


# device programming
# make fuses  call if you don't have installed a crystal
fuses:
	avrdude -c $(PROGRAMMER) -B 10.0 -p $(PARTNO) -P $(PORT)  $(FUSES_INT)

# fuses-crytal can only used if a 8MHz Crytal is installed (between Pin 9 and 10)
fuses-crystal:
	avrdude -c $(PROGRAMMER) -B 10.0 -p $(PARTNO) -P $(PORT)  $(FUSES_CRY)

# make upload  additionally calls make to compile the software for selected target
upload:
	make
	avrdude -c $(PROGRAMMER) -B 5.0 -p $(PARTNO) -P $(PORT) -U flash:w:./TransistorTestNew.hex:a \
	-U eeprom:w:./TransistorTestNew.eep:a
upload_orig:
	avrdude -c $(PROGRAMMER) -B 5.0 -p $(PARTNO) -P $(PORT) -U flash:w:./TransistorTestorig.hex:a \
	-U eeprom:w:./TransistorTestorig.eep:a
