.SECONDEXPANSION:

# MCU name
MCU = atmega32
F_CPU = 16000000
BOOTLOADER_ADR = 0x7000

# Output format. (can be srec, ihex, binary)
FORMAT = ihex

TARGET = tx_main

# Name of text file containing build number.
BUILD_NUMBER_FILE=.build_number

# List C source files here. (C dependencies are automatically generated.)
OBJDIR = obj
GLOBAL_SRC_DIR = ../../global_files
GLOBAL_SRC = uart.cpp btm222.cpp i2c.cpp avr_helper.cpp timer.cpp addr_list.cpp eeprom_helper.cpp
PROJECT_SRC = ${TARGET}.cpp output.cpp command.cpp interrupt.cpp adc.cpp config.cpp digital_input.cpp 
PROJECT_SRC += analog_input.cpp samsung_lcd.cpp lcd_common.cpp eeprom_config.cpp parseCommand.cpp
PROJECT_SRC += menu.cpp

# Optimization level, can be [0, 1, 2, 3, s].
#     0 = turn off optimization. s = optimize for size.
#     (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
OPT = s

# Compiler flag to set the C Standard level.
#     c89   = "ANSI" C
#     gnu89 = c89 plus GCC extensions
#     c99   = ISO C99 standard (not yet fully implemented)
#     gnu99 = c99 plus GCC extensions
CSTANDARD = -std=gnu99

#---------------- Assembler Options ----------------
#  -Wa,...:   tell GCC to pass this to the assembler.
#  -ahlms:    create listing
#  -gstabs:   have the assembler create line number information; note that
#             for use in COFF files, additional information about filenames
#             and function names needs to be present in the assembler source
#             files -- see avr-libc docs [FIXME: not yet described there]
ASFLAGS = -Wa,-adhlns=$(<:%.S=$(OBJDIR)/%.lst),-gstabs

#---------------- Linker Options ----------------
#  -Wl,...:     tell GCC to pass this to linker.
#    -Map:      create map file
#    --cref:    add cross reference to  map file
LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
//LDFLAGS += -Ttext=$(BOOTLOADER_ADR) 

# Compiler flags to generate dependency files.
GENDEPFLAGS = -MD -MP -MF .dep/$(@F).d

# Place -D or -U options here for C sources
CDEFS = -DF_CPU=$(F_CPU)UL -DBOOTLOADER_ADR=$(BOOTLOADER_ADR)
CDEFS += -D_IS_TX

# Combine all necessary flags and optional flags.
# Add target processor to flags.
ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS) $(CDEFS) $(CSTANDARD) $(BUILD_NUMBER_CCFLAGS)
ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)

#---------------- Programming Options (avrdude) ----------------
AVRDUDE_PROGRAMMER = avrispmkII
#AVRDUDE_PROGRAMMER_SER = A00128255
AVRDUDE_PROGRAMMER_SER = 200084757
AVRDUDE_PORT = usb:$(AVRDUDE_PROGRAMMER_SER)
AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eeprom
FUSES = -U hfuse:w:hfuse.hex:i -U lfuse:w:lfuse.hex:i -u 
# Uncomment the following if you want avrdude's erase cycle counter.
AVRDUDE_NO_VERIFY = -V

AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER) -B 30
AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)

# Define programs and commands.
SHELL = sh
CC = avr-gcc
CC_PP = avr-g++
OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump
SIZE = avr-size
NM = avr-nm
AVRDUDE = avrdude
REMOVE = rm -f
REMOVEDIR = rm -rf
COPY = cp
WINSHELL = cmd

#define all object files
OBJ_PROJECT = $(PROJECT_SRC:%.cpp=$(OBJDIR)/%.o)
OBJ_GLOBAL = $(GLOBAL_SRC:%.cpp=$(OBJDIR)/%.o)

# Define all listing files.
PROJECT_LST = $(PROJECT_SRC:%.cpp=$(OBJDIR)/%.lst)
GLOBAL_LST = $(GLOBAL_SRC:%.cpp=$(OBJDIR)/%.lst)

# Change the build target to build a HEX file
build: elf hex bin eep lss sym size

elf: $(TARGET).elf
hex: $(TARGET).hex
bin: $(TARGET).bin
eep: $(TARGET).eep
lss: $(TARGET).lss
sym: $(TARGET).sym

install: program

# Program the device.
program: $(TARGET).hex $(TARGET).eep
	$(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)

size:
	avr-size $(TARGET).elf
	#avr-size --mcu=$(MCU) -C $(TARGET).elf

all:
	echo "Project" $(OBJ_PROJECT)
	echo "Global" $(OBJ_GLOBAL)
	echo "Project lst" $(PROJECT_LST)
	echo "Global lst" $(GLOBAL_LST)

# Create final output files (.hex, .eep) from ELF output file.
%.hex: %.elf
	@echo
	@echo $(MSG_FLASH) $@
	$(OBJCOPY) -O $(FORMAT) -R .eeprom -R .fuse $< $@

%.bin: %.elf
	@echo
	@echo $(MSG_FLASH) $@
	$(OBJCOPY) -O binary -R .eeprom -R .fuse $< $@

%.eep: %.elf
	@echo
	@echo $(MSG_EEPROM) $@
	-$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
	--change-section-lma .eeprom=0 -O $(FORMAT) $< $@

# Create final output files (.hex, .eep) from ELF output file.
%.hex: %.elf
	@echo
	@echo $(MSG_FLASH) $@
	$(OBJCOPY) -O $(FORMAT) -R .eeprom -R .fuse $< $@

%.bin: %.elf
	@echo
	@echo $(MSG_FLASH) $@
	$(OBJCOPY) -O binary -R .eeprom -R .fuse $< $@

%.eep: %.elf
	@echo
	@echo $(MSG_EEPROM) $@
	-$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
	--change-section-lma .eeprom=0 -O $(FORMAT) $< $@

# Create extended listing file from ELF output file.
%.lss: %.elf
	@echo
	@echo $(MSG_EXTENDED_LISTING) $@
	$(OBJDUMP) -h -S $< > $@

# Create a symbol table from ELF output file.
%.sym: %.elf
	@echo
	@echo $(MSG_SYMBOL_TABLE) $@
	$(NM) -n $< > $@

# Link: create ELF output file from object files.
.SECONDARY : $(TARGET).elf
.PRECIOUS : $(OBJ_PROJECT) $(OBJ_GLOBAL)
%.elf: $(OBJ_PROJECT) $(OBJ_GLOBAL) $(BUILD_NUMBER_FILE) 
	$(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS)

# Compile: create object files from C source files.
$(OBJ_PROJECT) : $(OBJDIR)/%.o : %.cpp
	@echo
	@echo $(MSG_COMPILING) $<
	$(CC_PP) -c $(ALL_CFLAGS) $< -o $@


# Compile: create object files from C source files.
$(OBJ_GLOBAL) : $(OBJDIR)/%.o : $(GLOBAL_SRC_DIR)/%.cpp
	@echo
	@echo $(MSG_COMPILING) "global src" $<
	$(CC) -c $(ALL_CFLAGS) $< -o $@

# Compile: create assembler files from C source files.
%.s : %.c
	$(CC) -S $(ALL_CFLAGS) $< -o $@

# Compile: create assembler files from C source files.
%.s : %.cpp
	$(CC_PP) -S $(ALL_CFLAGS) $< -o $@

# Target: clean project.
clean: clean_list

clean_list :
	@echo
	@echo $(MSG_CLEANING)
	$(REMOVE) $(TARGET).hex
	$(REMOVE) $(TARGET).bin
	$(REMOVE) $(TARGET).eep
	$(REMOVE) $(TARGET).cof
	$(REMOVE) $(TARGET).elf
	$(REMOVE) $(TARGET).map
	$(REMOVE) $(TARGET).sym
	$(REMOVE) $(TARGET).lss
	$(REMOVEDIR) $(OBJDIR)
	$(REMOVE) $(SRC:.c=.s)
	$(REMOVE) $(SRC:.c=.d)
	$(REMOVE) $(COMMON_SRC:.c=.s)
	$(REMOVE) $(COMMON_SRC:.c=.d)
	$(REMOVEDIR) .dep

# Create object files directory
$(shell mkdir $(OBJDIR) 2>/dev/null)

# Include the dependency files.
-include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)

# Include build number rules.
include buildnumber.mak

