##############################################################################
#                                                                            #
# Teacup - Lean and efficient firmware for RepRap printers                   #
#                                                                            #
# by Triffid Hunter, Traumflug, jakepoz, Markus Hitter, many others          #
#                                                                            #
# This firmware is Copyright (c) ...                                         #
#   2009 - 2010 Michael Moon aka Triffid_Hunter                              #
#   2010 - 2013 Markus "Traumflug" Hitter <mah@jump-ing.de>                  #
#                                                                            #
# This program is free software; you can redistribute it and/or modify       #
# it under the terms of the GNU General Public License as published by       #
# the Free Software Foundation; either version 2 of the License, or          #
# (at your option) any later version.                                        #
#                                                                            #
# This program is distributed in the hope that it will be useful,            #
# but WITHOUT ANY WARRANTY; without even the implied warranty of             #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              #
# GNU General Public License for more details.                               #
#                                                                            #
# You should have received a copy of the GNU General Public License          #
# along with this program; if not, write to the Free Software                #
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA #
#                                                                            #
##############################################################################

##############################################################################
#                                                                            #
# Makefile for AVR (ATmega-based) targets. Use it with                       #
#                                                                            #
#   make -f Makefile-AVR                                                     #
#                                                                            #
# or copy/link it to Makefile for convenience.                               #
#                                                                            #
##############################################################################

##############################################################################
#                                                                            #
# Change these to suit your hardware                                         #
#                                                                            #
##############################################################################

# MCU_TARGET = atmega168
# MCU_TARGET = atmega328p
MCU_TARGET = atmega644p
# MCU_TARGET = atmega1280
# MCU_TARGET = atmega2560
# MCU_TARGET = at90usb1287
# MCU_TARGET = atmega32u4

# CPU clock rate
F_CPU = 16000000L
# F_CPU = 8000000L

##############################################################################
#                                                                            #
# Where to find your compiler and linker. Later, this is completed like      #
#   CC = $(TOOLCHAIN)gcc                                                     #
#                                                                            #
##############################################################################

TOOLCHAIN = avr-
# TOOLCHAIN = /usr/bin/avr-
# TOOLCHAIN = <path-to-arduino-folder>/hardware/tools/avr/bin/avr-

##############################################################################
#                                                                            #
# Programmer settings for "make program"                                     #
#                                                                            #
##############################################################################

# avrdude, typical for AVR-based architectures.
#
# Flags:
#   -c <programmer-type>      Typically stk500 or stk500v2.
#   -b <baudrate>             Upload baud rate. Depends on the bootloader in
#                             use. Not used for USB programmers.
#   -p <mcu type>             See MCU_TARGET above.
#   -P <port>                 Serial port the electronics is connected to.
#   -C <config file>          Optional, default is /etc/avrdude.conf.

UPLOADER = avrdude
# UPLOADER = <path-to-arduino-folder>/hardware/tools/avrdude

UPLOADER_FLAGS  = -c stk500v2
# UPLOADER_FLAGS += -b 19200
# UPLOADER_FLAGS += -b 57600
UPLOADER_FLAGS += -b 115200
UPLOADER_FLAGS += -p $(MCU_TARGET)
# UPLOADER_FLAGS += -P COM1
# UPLOADER_FLAGS += -P /dev/ttyACM0
UPLOADER_FLAGS += -P /dev/ttyUSB0
# UPLOADER_FLAGS += -C <path-to-arduino-folder>/hardware/tools/avrdude.conf


##############################################################################
#                                                                            #
# Below here, defaults should be ok.                                         #
#                                                                            #
##############################################################################

PROGRAM = teacup

# The thing we build by default, and also the thing we clean.
TARGET = $(PROGRAM).hex

# Arduino IDE takes the "compile everything available"-approach, so we have
# to keep this working and can take a shortcut:
SOURCES = $(wildcard *.c)

CFLAGS  = -DF_CPU=$(F_CPU)
CFLAGS += -mmcu=$(MCU_TARGET)
CFLAGS += -g
CFLAGS += -Wall
CFLAGS += -Wstrict-prototypes
CFLAGS += -std=gnu99
CFLAGS += -funsigned-char
CFLAGS += -funsigned-bitfields
CFLAGS += -fpack-struct
CFLAGS += -fshort-enums
CFLAGS += -Winline
CFLAGS += -fno-move-loop-invariants
CFLAGS += -fno-tree-scev-cprop
CFLAGS += -Os
CFLAGS += -ffunction-sections
CFLAGS += -finline-functions-called-once
CFLAGS += -mcall-prologues

LDFLAGS  = -Wl,--as-needed

ifneq ($(realpath ../simulavr/src/simulavr_info.h),)
  # Neccessary for simulavr support, doesn't hurt others.
  CFLAGS += -DSIMINFO
  LDFLAGS += -Wl,--section-start=.siminfo=0x900000
else
  # Doesn't work for simulavr, can't allow dead code removal.
  LDFLAGS += -Wl,--gc-sections
endif

LIBS  = -lm

OBJ = $(patsubst %.c,$(BUILDDIR)/%.o,$(SOURCES))

-include Makefile-common

# Architecture specific targets

.PHONY: program size

all: $(PROGRAM).hex $(BUILDDIR)/$(PROGRAM).lst $(BUILDDIR)/$(PROGRAM).sym size

program: $(PROGRAM).hex config.h
	$(UPLOADER) $(UPLOADER_FLAGS) -U flash:w:$(PROGRAM).hex

size: $(BUILDDIR)/$(PROGRAM).elf
	@echo "    SIZES             ATmega...  '168    '328(P)    '644(P)    '1280"
	@$(OBJDUMP) -h $^ | perl -MPOSIX -ne '/.(text)\s+([0-9a-f]+)/ && do { $$a += eval "0x$$2" }; END { printf "    FLASH : %5d bytes          %3d%%      %3d%%       %3d%%      %3d%%\n", $$a, ceil($$a * 100 / (14 * 1024)), ceil($$a * 100 / (30 * 1024)),ceil($$a * 100 / (62 * 1024)), ceil($$a * 100 / (126 * 1024)) }' 
	@$(OBJDUMP) -h $^ | perl -MPOSIX -ne '/.(data|bss)\s+([0-9a-f]+)/ && do { $$a += eval "0x$$2" }; END { printf "    RAM   : %5d bytes          %3d%%      %3d%%       %3d%%      %3d%%\n", $$a, ceil($$a * 100 / (1 * 1024)), ceil($$a * 100 / (2 * 1024)),ceil($$a * 100 / (4 * 1024)), ceil($$a * 100 / (8 * 1024)) }'
	@$(OBJDUMP) -h $^ | perl -MPOSIX -ne '/.(eeprom)\s+([0-9a-f]+)/ && do { $$a += eval "0x$$2" }; END { printf "    EEPROM: %5d bytes          %3d%%      %3d%%       %3d%%      %3d%%\n", $$a, ceil($$a * 100 / (1 * 1024)), ceil($$a * 100 / (2 * 1024)), ceil($$a * 100 / (2 * 1024)), ceil($$a * 100 / (4 * 1024)) }'
