#
# BSD-License:
#
# Copyright (c) 2007 by Nils Springob, nicai-systems, Germany
# Copyright (c) 2010 by Matthias Bunte, Germany
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#   * Redistributions of source code must retain the above copyright notice,
#     this list of conditions and the following disclaimer.
#   * Redistributions in binary form must reproduce the above copyright notice,
#     this list of conditions and the following disclaimer in the documentation
#     and/or other materials provided with the distribution.
#   * Neither the names of the authors nor the name nicai-systems nor
#     the names of its contributors may be used to endorse or promote products
#     derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
# OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#


# Configuration
TARGET = beerun
DEVICE = atmega16
#DEVICE = atmega644
F_CPU = 15000000

OBJS = \
   parcours.o \
   main.o \


# AVR compiler setup
PREFIX =
CC = $(PREFIX)avr-gcc
OBJCOPY = $(PREFIX)avr-objcopy
OBJDUMP = $(PREFIX)avr-objdump


# AVR compiler and linker flags
CFLAGS =  -Os -ffunction-sections -DAVR
CFLAGS += -std=c99
CFLAGS += -Wall -g -Wa,-ahls=$(@:.o=.lst),-L
CFLAGS += -I../include/beelib
CFLAGS += -I../include/beelib_hal_$(DEVICE)
CFLAGS += -mmcu=$(DEVICE) -DF_CPU=$(F_CPU)
CFLAGS += -D_NIBOBEE_
CFLAGS += -DVERSION="\"$(VERSION)\""

CLDFLAGS += -v -mmcu=$(DEVICE) -L../lib

# MATH lib and Stdlib
#MATH = -lm -Wl,-u,vfprintf -lprintf_flt
MATH = -lm

# Nibobee libraries
LIBS = -lbee-$(DEVICE)

#            1  means not!
# 7 BODLEVEL 1  Brown-out Detector trigger level
# 6 BODEN 6  1  Brown-out Detector enable
# 5 SUT1 5   1  Select start-up time
# 4 SUT0 4   0  Select start-up time
# 3 CKSEL3   1  Select Clock source
# 2 CKSEL2   1  Select Clock source
# 1 CKSEL1   1  Select Clock source
# 0 CKSEL0   1  Select Clock source
LFUSE = 0xef

#            1  means not!
# 7 OCDEN    1  Enable OCD
# 6 JTAGEN   1  Enable JTAG
# 5 SPIEN    0  Enable SPI Serial Program and Data Downloading
# 4 CKOPT    1  Oscillator options
# 3 EESAVE   0  EEPROM memory is preserved through the Chip Erase
# 2 BOOTSZ1  0  Select Boot Size
# 1 BOOTSZ0  0  Select Boot Size
# 0 BOOTRST  1  Select reset vector
HFUSE = 0xd1




# ATmega644
#LFUSE = 0xef
#HFUSE = 0xd1
EFUSE = 0xff






PROGRAMMER = usbasp
INTERFACE = usb

# build intel hex files
all: $(TARGET).hex

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

%.d:%.c
	set -e; $(CC) -MM $(CFLAGS) $< \
	| sed 's#\($*\)\.o[ :]*#\1.o $@ : #g' > $@ ; \
	[ -s $@ ] || rm -f $@

# avr specific entries
%.elf: $(OBJS) $(NIBO_OBJS)
	$(CC) $(CLDFLAGS) -o $@ $(OBJS) $(LIBS) $(MATH)
	avr-size -A $(TARGET).elf

%.hex: %.elf
	$(OBJCOPY) -j .text -j .data -O ihex $< $@
#	cp $@ ../../../hex/

$(TARGET).lss: $(TARGET).elf
	avr-objdump -h -S $(TARGET).elf >$(TARGET).lss

lss: $(TARGET).lss


avrdude: $(TARGET).hex
	avrdude -c $(PROGRAMMER) -P $(INTERFACE) -p $(DEVICE) -y -B 2 -U flash:w:$(TARGET).hex:a

avrdude-fuses:
	avrdude -c $(PROGRAMMER) -P $(INTERFACE) -p $(DEVICE) -y -B 10 \
    -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m -U efuse:w:$(EFUSE):m

avrdude-stk: $(TARGET).hex
	avrdude -c stk500v2 -p m16 -B 100 -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m
	avrdude -c stk500v2 -p m16 -B 2 -U flash:w:$(TARGET).hex

clean:
	rm -f *.d *.o *.lst *~ *.elf $(TARGET).hex $(TARGET).lss $(NIBO_OBJS)

include $(OBJS:.o=.d)

.PHONY: clean
.SECONDARY: $(TARGET).hex $(OBJS) $(LOBJS)

