# Makefile for compiling AVR AtmegaXXX programs
# Author: Gerhard Gappmeier
# License: GPL V3

######################################
# Hardware configuration
#MCU      = atmega328p
#MCU      = atmega128
MCU      = atmega168
#F_CPU    = 16000000
F_CPU    = 8000000UL
#F_CPU    = 1000000

########################################################
# avrdude configuration for upload using the bootloader
#PROCESSOR = m328p
#PROTOCOL  = stk500v1
#TTY       = /dev/ttyUSB0
#BAUDRATE  = 57600 
PROCESSOR = m168
#PROCESSOR = m128
PROTOCOL  = USBasp
TTY       = 
BAUDRATE  =

#####################################
# Tool chain configuration
CC      = avr-gcc
CXX     = avr-g++
OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump
SIZE    = avr-size
AVRDUDE = avrdude
REMOVE  = rm -f
COPY    = cp

#####################################
# Programming support using avrdude.
# output format (can be srec, ihex, binary)
FORMAT  = ihex
HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
ELFSIZE = $(SIZE) -A $(TARGET).elf

#####################################
# Software configuration
TARGET = uart
SRC = main.c
OBJDIR = .obj
OBJ=$(addprefix $(OBJDIR)/, $(SRC:.c=.o))
DEFINES=-DF_CPU=$(F_CPU) #-DYY_INPUT\(buf,result,max_size\)
CFLAGS_DEBUG    = -O1 -g
CFLAGS_RELEASE  = -Os
CFLAGS_WARNINGS = -Wall #-ansi
#CFLAGS = $(CFLAGS_DEBUG)
CFLAGS = $(CFLAGS_RELEASE)
# concatenate all CFLAGS
CFLAGS_ALL = $(CFLAGS) $(CFLAGS_WARNINGS) -mmcu=$(MCU) -I. $(DEFINES)

# Optional assembler flags.
#  -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=.lst),-gstabs 

# Optional linker flags.
#  -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
# remove dead code
LDFLAGS += -ffunction-sections -Wl,--gc-sections -Wl,--relax

# Add optional libraries
#LDFLAGS += -lm

# End of configuration
####################################################

# Define Messages
# English
MSG_SIZE_BEFORE = Size before: 
MSG_SIZE_AFTER = Size after:
MSG_FLASH = Creating load file for Flash:
MSG_EEPROM = Creating load file for EEPROM:
MSG_EXTENDED_LISTING = Creating Extended Listing:
MSG_SYMBOL_TABLE = Creating Symbol Table:

all: gccversion sizebefore createtmpdirs $(TARGET).elf $(TARGET).hex $(TARGET).eep \
        $(TARGET).lss $(TARGET).sym sizeafter

# Display size of file.
sizebefore:
	@if [ -f $(TARGET).elf ]; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); echo; fi

sizeafter:
	@if [ -f $(TARGET).elf ]; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); echo; fi

# Display compiler version information.
gccversion: 
	@$(CC) --version

# create temp dirs
createtmpdirs: 
	mkdir -p $(OBJDIR)

# lexer
lexer.c: lexer.c.in
	re2c -o lexer.c lexer.c.in

# parser generator
lang.tab.c: lang.y
	bison -d lang.y

$(OBJDIR)/%.o : %.c Makefile
	$(CC) -c $(CFLAGS_ALL) $< -o $@ 

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

%.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) $@
	avr-nm -n $< > $@

$(TARGET).elf : $(OBJ)
	@echo "Linking elf file..."
	$(CC) $(LDFLAGS) -o $@ $(OBJ)

clean:
	rm -rf $(OBJDIR) $(TARGET).elf $(TARGET).hex $(TARGET).map $(TARGET).eep $(TARGET).lss $(TARGET).sym

upload: $(TARGET).hex
	# reset arduino before upload
	#stty -F $(TTY) hupcl
	# upload now
	#avrdude -v -v -p $(PROCESSOR) -c $(PROTOCOL) -P $(TTY) -b$(BAUDRATE) -D -U flash:w:$(TARGET).hex:i
	$(AVRDUDE) -v -v -p $(PROCESSOR) -c $(PROTOCOL) -U flash:w:$(TARGET).hex:i

# Listing of phony targets.
.PHONY : all sizebefore sizeafter gccversion clean 

