############################################################
#
#                         Makefile
#
############################################################

PROJECT    = oled_2313demo


INC_DIR    = -I./ -I../include

# hier alle zusaetzlichen Softwaremodule angegeben


PRINT_FL   = 0
SCAN_FL    = 0
MATH       = 0

# fuer Compiler / Linker
FREQ       = 8000000ul
MCU        = attiny2313

# fuer AVRDUDE
#Unterstuetzte Programmer des Makefiles:
#    usbasp, avrisp, wiring, stk500v2, ponyser, arduino, jtag2updi

PROGRAMMER = usbasp
PROGPORT   = /dev/ttyUSB0
BRATE      = 115200
DUDEOPTS   = -B 1

############################################################
#
# funktionaler Teil des Makfiles, ab hier bedarf es keiner
# Aenderung durch den Benutzer und wenn doch, sollten hier
# Aenderungen mit Bedacht vorgenommen werden
#
############################################################


CC        = avr-gcc
LD        = avr-gcc
OBJCOPY   = avr-objcopy
SIZE      = avr-size

ifeq ($(INC_DIR),)
	INC_DIR   := -I./ -I../include
endif

ifeq ($(PROGPORT),)
PROGPORT := /dev/ttyUSB0
endif


OBJECTS    = $(PROJECT).o $(SRCS)

CPU        = -mmcu=$(MCU)

CC_FLAGS   = -Os $(CPU) -std=c99
#CC_FLAGS   = -O3 $(CPU)

CC_SYMBOLS = -DF_CPU=$(FREQ)
LD_FLAGS   = $(CPU)

ifeq ($(PRINTF_FL), 1)
  LD_FLAGS += -Wl,-u,vfprintf
endif

ifeq ($(SCANF_FL), 1)
  LD_FLAGS += -Wl,-u,vfscanf -lscanf_flt
endif

LD_SYS_LIB =

ifeq ($(PRINTF_FL), 1)
  LD_SYS_LIB += -lprintf_flt
endif

ifeq ($(SCANF_FL), 1)
  LD_SYS_LIB += -lscanf_flt
endif

ifeq ($(MATH), 1)
  LD_SYS_LIB +=-lm
endif


.PHONY: all clean size compile flash

all: clean $(PROJECT).hex size

compile:
	$(CC) $(CC_FLAGS) $(CC_SYMBOLS) $(INC_DIR) -o $(PROJECT).o $(PROJECT).c

clean:
	rm -f $(PROJECT).o $(PROJECT).elf $(PROJECT).hex $(PROJECT).map
	rm -f $(SRCS)

.c.o:
	$(CC) $< -c -o $@ $(CC_FLAGS) $(CC_SYMBOLS) $(INC_DIR)

.S.o:
	$(CC) $< -c -o $@ $(CC_FLAGS) $(CC_SYMBOLS) $(INC_DIR)

$(PROJECT).elf: $(OBJECTS)
	$(LD) $(LD_FLAGS) $^$(LD_SYS_LIB) -o $@

$(PROJECT).hex: $(PROJECT).elf
	@$(OBJCOPY) -j .text -j .data -O ihex $< $@

$(PROJECT).lst: $(PROJECT).elf
	@$(OBJDUMP) -Sdh $< > $@

size: $(PROJECT).elf
	$(SIZE) -C $(PROJECT).elf --mcu=$(MCU) 1>&2

flash:

ifeq ($(PROGRAMMER), usbtiny)
	avrdude -c $(PROGRAMMER) -p $(MCU) $(DUDEOPTS) -V -U flash:w:$(PROJECT).hex
endif
ifeq ($(PROGRAMMER), usbasp)
	avrdude -c $(PROGRAMMER) -p $(MCU) $(DUDEOPTS) -V -U flash:w:$(PROJECT).hex
endif

ifeq ($(PROGRAMMER), avrisp)
	avrdude -c $(PROGRAMMER) -p $(MCU) -P $(PROGPORT) -b $(BRATE) $(DUDEOPTS) -V -U flash:w:$(PROJECT).hex
endif

ifeq ($(PROGRAMMER), wiring)
	avrdude -c $(PROGRAMMER) -p $(MCU) -P $(PROGPORT) -b $(BRATE) $(DUDEOPTS) -V -D -U flash:w:$(PROJECT).hex
endif

ifeq ($(PROGRAMMER), stk500v2)
	avrdude -c $(PROGRAMMER) -p $(MCU) -P $(PROGPORT) -b $(BRATE) $(DUDEOPTS) -V -U flash:w:$(PROJECT).hex
endif

ifeq ($(PROGRAMMER), ponyser)
	avrdude -c $(PROGRAMMER) -p $(MCU) -P $(PROGPORT) $(DUDEOPTS) -V -U flash:w:$(PROJECT).hex
endif
ifeq ($(PROGRAMMER), arduino)
	avrdude -c $(PROGRAMMER) -p $(MCU) -P $(PROGPORT) -b $(BRATE) $(DUDEOPTS) -V -U flash:w:$(PROJECT).hex
endif

ifeq ($(PROGRAMMER), jtag2updi)
	avrdude -c $(PROGRAMMER) -p $(MCU) -P $(PROGPORT) $(DUDEOPTS) -V -U flash:w:$(PROJECT).hex
endif

