# 
#   $Id: Makefile.std,v 1.3 2011-01-21 11:52:33 moellmer Exp $
#   Copyright (C) 2003 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
#

# Configuration
PAPARAZZI_DIR = \"$(BASE)/\"
ATMELBIN = /usr/bin
ATMEL_INCLUDES = -I $(BASE)/avr/include/
UISPBIN=/usr/bin
PROG_PORT = /dev/parport0
USB_PROG_PORT = /dev/ttyUSB1


# Architecture dependent
include $(BASE)/conf/Makefile.local
-include $(BASE)/conf/$(CONF).conf

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

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

USB_FLAGS       =
	-dserial=$(USB_PROG_PORT) \
	-dprog=avr910             \
	-dpart=auto               \
	-v=3


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


#
# End of configuration part.
#


CFLAGS = \
	-Wall \
	$(ATMEL_INCLUDES) \
	$(INCLUDES) \
	$(LOCAL_CFLAGS)	\
	-O2	\
	$(ARCH_CFLAGS) \
	$(USER_CFLAGS)
ifdef PAPABENCH_SINGLE
CFLAGS += -DPAPABENCH_SINGLE
endif
	

LDFLAGS = \
	$(ARCH_STDFLAGS) \
	$(ARCH_LDFLAGS) \
	$(USER_LDLFLAGS)

#
# General rules
#

all:									\
	$(TARGET).elf $(TARGET).dis

compile: $(TARGET).elf							\

load upload: check_fuses					 	\
	$(TARGET).install

$(TARGET).objs	=							\
	$($(TARGET).srcs:.c=.o)

#$(TARGET).elf: $($(TARGET).objs)
$(TARGET).elf: 
	$(CC) $(CFLAGS) $($(TARGET).srcs) -o $@


#
# Fuses
#

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

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

TMPFILE	= 'check_fuses.tmp'

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

ifdef PAPABENCH_NOLINK
%.elf:
else
%.elf:
	$(LD)								\
	$(LOCAL_LDFLAGS)						\
		$($(@:.elf=).objs)					\
		-o $@							\
		$(LDFLAGS)						\
		-static
	$(SIZE) $@
endif

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

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

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

%.dis: %.elf
	$(OBJDUMP) $< > $@


%.install: %.hex check_arch
	# stk200 needs to be erased first
	$(UISP) $(UISP_FLAGS) --erase
	$(UISP) $(UISP_FLAGS) --upload if=$<

verify: $(TARGET).hex
	$(UISP) $(UISP_FLAGS) --verify if=$<

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

check_arch :
	@echo "##### Check architecture #####"
	@if ($(UISP) $(UISP_FLAGS) 2>&1 | tr '[:upper:]' '[:lower:]' | grep $(ARCH)); then : ; else echo "-> Wrong architecture (mcu0 vs mcu1 ?)"; exit 1; fi

avr_clean:
	rm -f .depend $($(TARGET).srcs:.c=.s)
	rm -f $(TARGET).elf $(TARGET).elf.s $($(TARGET).srcs:.c=.o)


#
# Dependencies
#

.depend:
	$(CC) $(CFLAGS) $($(TARGET).srcs) > .depend

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


