# Project configuration:
#------------------------------------------------
OBJS  = main.o
ASMS  = main.s
BIN   = main


# Hardware Konfiguration
#------------------------------------------------

MCU     = atmega8

# Programmer Konfiguration
#------------------------------------------------

# CPU Type für avrdude
MCU_DUDE=m8
# Schnittstelle für avrdude
AVRDUDE=/dev/ttyUSB0
# Protokoll für avrdude
PROTO=stk500v2

# Festlegung der Programmstartadresse
#------------------------------------------------

# Startadresse des Programms
# 0x0000	-> bei normalem Programmstart

# ( Achtung ! Laut Datenblatt Wortadressen !!)

# ATMega8 
# 0x1C00 	-> Bootloader von 1024 Byte
# 0x1800 	-> Bootloader von 2048 Byte

# ATMega32
# 0x7C00 	-> Bootloader von 1024 Byte

ROM_START = 0x0000

# Compiler flags
#------------------------------------------------

CCFLAGS = -g -Os -Wall -Wstrict-prototypes -mmcu=$(MCU) -mcall-prologues -fomit-frame-pointer -fno-inline-small-functions -fno-tree-scev-cprop -fno-split-wide-types -lm
#CCFLAGS = -g -Os -Wall -Wstrict-prototypes -mmcu=$(MCU) -mcall-prologues -fomit-frame-pointer -fno-inline-small-functions -fno-tree-scev-cprop -fno-split-wide-types -lm
#CCFLAGS = -g -Os -Wall -Wstrict-prototypes -mmcu=$(MCU) -mcall-prologues -fomit-frame-pointer


# Linker flags
#------------------------------------------------

LDFLAGS = -Wl,-Map=$(BIN).map,--cref,--section-start=.text=$(ROM_START),--relax -mmcu=$(MCU) -gstabs,-ahlms=$(<:.c=.lst)
#LDFLAGS = -Wl,-Map=$(BIN).map,--cref,--section-start=.text=$(ROM_START),--relax -mmcu=$(MCU) -gstabs,-ahlms=$(<:.c=.lst)
#LDFLAGS = -Wl,-Map=$(BIN).map,--cref,--section-start=.text=$(ROM_START) -mmcu=$(MCU) -gstabs,-ahlms=$(<:.c=.lst)

# Assembler config
#ASTFLAG = 

# Compiler configuration:
CC      = avr-gcc
LD      = avr-gcc
OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump
UPLOAD  = avrdude

# Convert ELF to COFF for use in debugging / simulating in
# AVR Studio or VMLAB.
COFFCONVERT=$(OBJCOPY) --debugging \
	--change-section-address .data-0x800000 \
	--change-section-address .bss-0x800000 \
	--change-section-address .noinit-0x800000 \
	--change-section-address .eeprom-0x810000 

#Do all
all: image

#create ROM-Image
image: 		$(BIN)
		avr-size $(BIN).elf
	
# Programm in der AVR laden
#------------------------------------------------

upload_uisp:	$(BIN).hex
		uisp  -dprog=dasa2 -dserial=/dev/ttyS0 -dpart=$(MCU) --erase
		uisp  -dprog=dasa2 -dserial=/dev/ttyS0 -dpart=$(MCU) --upload if=$(BIN).hex


upload_boot:	$(BIN).hex
		avrdudspi.s st7565.s systimer.s i2c.s pvf8583.s rs232.s menu.s crontab.s comm.s error.s 24lc64.s timer.se -p $(MCU) -c AVR109 -P /dev/ttyUSB0 -u  -U flash:w:$(BIN).hex
		# Hinweis :Standart Baudrate ist anscheinend 19200

upload_avrdude:	$(BIN).hex
		avrdude -c $(PROTO) -p $(MCU_DUDE) -P $(AVRDUDE) -u -U flash:w:$(BIN).hex

info:		$(BIN).hex
		avr-size $(BIN).elf

# die Fuse Bits setzen
#------------------------------------------------

read_lfuse:	$(BIN).hex
		avrdude -c $(PROTO) -p $(MCU_DUDE) -P $(AVRDUDE) -U lfuse:r:-:h

read_hfuse:	$(BIN).hex
		avrdude -c $(PROTO) -p $(MCU_DUDE) -P $(AVRDUDE) -U hfuse:r:-:h
		
read_efuse:	$(BIN).hex
		avrdude -c $(PROTO) -p $(MCU_DUDE) -P $(AVRDUDE) -U efuse:r:-:h		

read_lock:	$(BIN).hex
		avrdude -c $(PROTO) -p $(MCU_DUDE) -P $(AVRDUDE) -U lock:r:-:h

read_signature:	$(BIN).hex
		avrdude -c $(PROTO) -p $(MCU_DUDE) -P $(AVRDUDE) -U signature:r:-:h

read_calib:	$(BIN).hex
		avrdude -c $(PROTO) -p $(MCU_DUDE) -P $(AVRDUDE) -U calibration:r:-:h

# Mega 32 , externer Quarz, Bodenlevel 3,3 V
set1_lfuse:	$(BIN).hex
		avrdude -c $(PROTO) -p $(MCU_DUDE) -P $(AVRDUDE) -U lfuse:w:0xAF:m
		
# Mega 32 , interner RC, Bodenlevel 3,3 V
set2_lfuse:	$(BIN).hex
		avrdude -c $(PROTO) -p $(MCU_DUDE) -P $(AVRDUDE) -U lfuse:w:0xA1:m

# Mega 32 , Bootloader mit 1 kB und darauf eingestellten Resetvektor
set1_hfuse:	$(BIN).hex
		avrdude -c $(PROTO) -p $(MCU_DUDE) -P $(AVRDUDE) -U hfuse:w:0xD2:m

######################################  Einstellungen für MEGA324P ##################

# Bodenlevel auf 2,5V einstellen
set1_efuse_mega324p:	$(BIN).hex
			avrdude -c $(PROTO) -p $(MCU_DUDE) -P $(AVRDUDE) -U efuse:w:0xFD:m

# externer Quartz > 16MHZ, kein Teiler, kein CLK Ausgang, Startupzeit 65ms
set1_lfuse_mega324p:	$(BIN).hex
			avrdude -c $(PROTO) -p $(MCU_DUDE) -P $(AVRDUDE) -U lfuse:w:0xF7:m

# OCD und JTAG aus,SPI ein,Watchdog aus,EEPROM Schutz ein,kein Bootcode
set1_hfuse_mega324p:	$(BIN).hex
			avrdude -c $(PROTO) -p $(MCU_DUDE) -P $(AVRDUDE) -U hfuse:w:0xD1:m


#####################################################################################

######################################  Einstellungen für MEGA8 ##################

# externer Quartz > 16MHZ, kein Teiler, kein CLK Ausgang, Startupzeit 65ms
set1_lfuse_mega8:	$(BIN).hex
			avrdude -c $(PROTO) -p $(MCU_DUDE) -P $(AVRDUDE) -U lfuse:w:0x1f:m

# OCD und JTAG aus,SPI ein,Watchdog aus,EEPROM Schutz ein,Bootcode 2kB, booten
set1_hfuse_mega8:	$(BIN).hex
			avrdude -c $(PROTO) -p $(MCU_DUDE) -P $(AVRDUDE) -U hfuse:w:0xC0:m


#asm
asm: 		$(ASMS)


#clean source directory
.phony:	clean
clean:
		rm -f *.o *.s *.bak *.hex *.cof *.elf *.map *.lst

readfuse:	$(BIN).hex
		uisp -dprog=dasa2 -dpart=$(MCU) -dserial=/dev/ttyS0 --rd_fuses


#create docs
doc:
	#doxygen doxy.dxy
	doxygen doxygen.config

# Build rules:
%.o: %.c
	$(CC) $(CCFLAGS) $(ASTFLAG) -c $<
	
%.s: %.c
	$(CC) $(CCFLAGS) -S $(ASTFLAG) -c $<
		
$(BIN): $(OBJS)
	$(LD) $(LDFLAGS) -o$(BIN).elf $^
	$(OBJCOPY) -O ihex -R .eeprom $(BIN).elf $(BIN).hex
#	$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 -O ihex $(BIN).elf $(BIN)_eeprom.hex
	$(OBJDUMP) -t -h -S $(BIN).elf >$(BIN).lst
	
