# Hey Emacs, this is a -*- makefile -*-
#
#   $Id$
#   Copyright (C) 2003-2005 Pascal Brisset, Antoine Drouin
#
# This file is part of paparazzi.
#
# paparazzi 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, or (at your option)
# any later version.
#
# paparazzi 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 paparazzi; see the file COPYING.  If not, write to
# the Free Software Foundation, 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#


#
# This is the common Makefile for the avr-target.
# Edit the configuration part to suit your local install
#

ATMELBIN = /usr/bin
ATMEL_INCLUDES = -I /usr/avr/include
ATMEL_LIBPATH = -B /usr/avr/lib/avr4 -B /usr/avr/lib/avr5
PROG_PORT = /dev/parport0

VERIFY=--verify

SRC_ARCH = $(PAPARAZZI_SRC)/sw/airborne/arch/avr

CC		= $(ATMELBIN)/avr-gcc -mmcu=$($(TARGET).MCU)
LD		= $(CC) $(ATMEL_LIBPATH)
SIZE		= $(ATMELBIN)/avr-size
OBJCOPY         = $(ATMELBIN)/avr-objcopy


SERIAL_FLAGS	=							\
	-dprog=avr910							\
	-dpart=auto							\
	-dserial=/dev/ttyS0						\
	-dspeed=38400							\

ISP_FLAGS	=							\
	-dlpt=$(PROG_PORT) -dprog=stk200 -v=3				\

UISP		= uisp
UISP_FLAGS	= $(ISP_FLAGS)
#UISP_FLAGS	= $(SERIAL_FLAGS)

# Launch with "make Q=''" to get full command display
Q=@


#
# End of configuration part.
#
SRCAVR = $($(TARGET).srcs)

CFLAGS		=							\
	-W -Wall -Wundef \
	$(ATMEL_INCLUDES)					  	\
	$(INCLUDES)					  	\
	-Wstrict-prototypes						\
	$($(TARGET).CFLAGS)					\
	$(LOCAL_CFLAGS)					\
	-O3								\

LDFLAGS		=	-lm						\

#
# General rules
#

#$(TARGET).srcsnd = $(notdir $($(TARGET).srcs))
#$(TARGET).objso	= $($(TARGET).srcsnd:%.c=$(OBJDIR)/%.o)
#$(TARGET).objs	= $($(TARGET).objso:%.S=$(OBJDIR)/%.o)


all: build
#all compile: $($(TARGET).objs) $(OBJDIR)/$(TARGET).elf
#all:
#	@echo !!!!!!!
#	@echo $($(TARGET).objs)
#	@echo !!!!!!!

load upload: $(TARGET).install


#
# Fuses
#

rd_fuses: check_arch
	$(UISP) $(ISP_FLAGS) --rd_fuses

wr_fuses : check_arch
	$(UISP) $(ISP_FLAGS) --wr_fuse_h=$($(TARGET).HIGH_FUSE)
	$(UISP) $(ISP_FLAGS) --wr_fuse_l=$($(TARGET).LOW_FUSE)
	$(UISP) $(ISP_FLAGS) --wr_fuse_e=$($(TARGET).EXT_FUSE)
	$(UISP) $(ISP_FLAGS) --wr_lock=$($(TARGET).LOCK_FUSE)

TMPFILE	= '/tmp/check_fuses.tmp'

check_fuses: check_arch
	@echo "##### Check of fuses #####"
	@$(UISP) $(ISP_FLAGS) --rd_fuses >$(TMPFILE)
	@if (grep -i 'Fuse Low Byte' $(TMPFILE) | cut -c24- | grep -iq $($(TARGET).LOW_FUSE)) && (grep -i 'Fuse High Byte' $(TMPFILE) |cut -c24- | grep -iq $($(TARGET).HIGH_FUSE)) && (grep -i 'Fuse Extended Byte' $(TMPFILE) |cut -c24- | grep -iq $($(TARGET).EXT_FUSE)) && (grep -i 'Lock Bits' $(TMPFILE) |cut -c24- | grep -iq $($(TARGET).LOCK_FUSE)); then echo "-> Fuses are Ok"; rm $(TMPFILE); else echo "-> Wrong fuses. Type 'make wr_fuses'"; rm $(TMPFILE); exit 1; fi

# Define all object files.
COBJ      = $(SRC:%.c=$(OBJDIR)/%.o)
COBJAVR   = $(SRCAVR:%.c=$(OBJDIR)/%.o)


build: elf hex

elf: $(OBJDIR)/$(TARGET).elf
hex: $(OBJDIR)/$(TARGET).hex


%.hex: %.elf
	@echo OBJC $@
	$(OBJCOPY) -O ihex -R .eeprom $< $@

.SECONDARY : $(OBJDIR)/$(TARGET).elf
.PRECIOUS : $(COBJ) $(COBJAVR)

%.elf: $(COBJ) $(COBJAVR)
	@echo LD $@
	$(Q)$(LD) $(LOCAL_LDFLAGS) $^ -o $@ $(LDFLAGS)
	@echo SIZE
	$(Q)$(SIZE) $@


#%.s: %.c
#	$(CC) $(CFLAGS) -S -o $@ $<

#$(OBJDIR)/%.s: %.c
#	$(CC) $(CFLAGS) -S -o $@ $<

$(OBJDIR)/%.o: %.c $(OBJDIR)/../Makefile.ac
	@echo CC $@
	$(Q)test -d $(dir $@) || mkdir -p $(dir $@)
	$(Q)$(CC)  -c $(CFLAGS)  $< -o $@

#$(OBJDIR)/%.o: $(SRC_ARCH)/%.S
#	$(CC) $(CFLAGS) -c -o $@ $<



%.install: $(OBJDIR)/%.hex check_arch
# stk200 needs to be erased first
	$(UISP) $(UISP_FLAGS) --erase
	@echo SIZE
	$(Q)$(SIZE) $<
	$(UISP) $(UISP_FLAGS) --upload $(VERIFY) if=$<

erase: check_arch
	$(UISP) $(ISP_FLAGS) --erase

check_arch :
	@echo "CHECKING link with device $($(TARGET).MCU) on $(PROG_PORT)"
	$(Q)$(UISP) $(UISP_FLAGS)
	$(Q)if ($(UISP) $(UISP_FLAGS) 2>&1 | tr '[:upper:]' '[:lower:]' | grep $($(TARGET).MCU)); then : ; else echo "Wrong architecture (mcu0 vs mcu1 ?)"; exit 1; fi

avr_clean:
	$(Q)rm -rf $(OBJDIR)


#
# Dependencies
#

$(OBJDIR)/.depend:
	@echo DEPEND $@
	@test -d $(OBJDIR) || mkdir -p $(OBJDIR)
	$(Q)$(CC) -MM -MG $(CFLAGS) $($(TARGET).srcs) | sed 's|\([^\.]*\.o\)|$(OBJDIR)/\1|' > $@

ifneq ($(MAKECMDGOALS),clean)
ifneq ($(MAKECMDGOALS),erase)
-include $(OBJDIR)/.depend
endif
endif
