TARGET = digiweb
CC = avr-gcc

# On command line:
# make all = Make software.
# make clean = Clean out built project files.
# make coff = Convert ELF to COFF using objtool.
#
# To rebuild project do "make clean" then "make all".
#



# Output format. (can be srec, ihex)
FORMAT = ihex

# Target file name (without extension).


vpath %.c                                       \
          /quelle/digiweb                       \
          /quelle/tools/mega128                 \
          /quelle/tools/protokolle              \
          /quelle/tools

vpath %.cpp                                     \
          /quelle/digiweb                       \
          /quelle/tools/mega128                 \
          /quelle/tools/protokolle              \
          /quelle/tools

vpath %.h                                       \
          /quelle/digiweb                       \
          /quelle/tools/mega128                 \
          /quelle/tools/protokolle              \
          /quelle/tools

# List H files here.
HDR = bootp.h \
      cambus.h \
      code64.h \
      com_dev.h \
      comdirekt.h \
      compiler.h \
      config.h \
      dataflash.h \
      digiweb.h \
      digiwebio.h \
      dns.h \
      dw_fronius2.h \
      dw_rk512.h \
      dw_mpi.h \
      dw_nnetz.h \
      dw_protokoll.h \
      eepromio.h \
      emu_language.h \
      emudwh.h \
      emufile.h \
      emugif.h \
      emuhtml.h \
      error_dw.h \
      ethernet.h \
      expression.h \
      ftpd.h \
      file.h \
      httpd.h \
      httpparameter.h \
      httpvar.h \
      httpvarip.h \
      icmp.h \
      ip.h \
      linkstr.h \
      muldiv.h \
      multiuser.h \
      nnetz.h \
      ntp.h \
      qsm.h \
      rs232.h \
      rs232connect.h \
      s5l1.h \
      security.h \
      smtp.h \
      smtpparser.h \
      sqlcsv.h \
      string_e.h \
      task.h \
      tcp.h \
      tcpdef.h \
      udp.h \
      users.h \
      utility.h \
      vmalloc.h \
      watchdog.h \

#      workingDir.h

# List C source files here. (C dependencies are automatically generated.)
SRC = digiweb.c      	\
      cambus.c          \
      multiuser.c       \
      s5l1.c            \
      as511.c           \
      3964r.c           \
      nnetz.c           \
      config.c	      	\
      httpd.c			\
      ftpd.c			\
      comdirekt.c       \
      smtp.c			\
      smtpparser.c		\
      users.c			\
      httpparameter.c	\
      emufile.c			\
      emugif.c			\
      emudwh.c			\
      emuhtml.c			\
      emu_language.c	\
      expression.c		\
      main.c     		\
      dataflash.c 		\
      utility.c	      	\
      qsm.c 			\
      configdata.c 		\
      eepromio.c		\
      string_mega128.c  \
      file.c 			\
      ethernet.c		\
      ip.c				\
      bootp.c			\
      ntp.c             \
      dns.c             \
      icmp.c			\
      udp.c				\
      tcp.c				\
      rs232.c		    \
      com_dev.c			\
      rs232connect.c	\
      httpvar.c			\
      httpvarip.c		\
      dw_mpi.c			\
      dw_rk512.c		\
      dw_nnetz.c		\
      dw_fronius2.c     \
      dw_protokoll.c    \
      security.c        \
      sqlcsv.c          \
      linkstr.c         \
      string_e.c        \
      vmalloc.c			\
      code64.c			\
      muldiv.c			\
      task.c            \

# List CPP source files here. (C dependencies are automatically generated.)
SRCPP = \
#    new_delete.cpp   \



# List Assembler files
ASRC = 

# Optimization level (can be O0, O1, O2, O3, Os) 
#  -gdwarf-2 \ # für neuen Compiler und debugger!
# Optional compiler flags.
# -fno-inline \ #damit man besser debuggen kann
CFLAGS = -mmcu=atmega128 -I. -g -Os -mcall-prologues \
         -funsigned-char \
         -funsigned-bitfields \
         -fpack-struct \
         -fshort-enums \
         -D NEWPROTOKOLL \
         -D MAXOPENFILES=8 \
         -D NOTASK \
         -D BETZ \
         -D DIGIWEB \
         -D USDEBUG \
         -D FRONIUS2 \
         -Wall -Wstrict-prototypes  -Wa,-ahlms=$(<:.c=.lst)

CPPFLAGS = -mmcu=atmega128 -I. -g -Os -mcall-prologues \
         -funsigned-char \
         -funsigned-bitfields \
         -fpack-struct \
         -fshort-enums \
         -D NEWPROTOKOLL \
         -D MAXOPENFILES=8 \
         -D NOTASK \
         -D BETZ \
         -D DIGIWEB \
         -D USDEBUG \
         -D FRONIUS2 \
         -Wall -Wa,-ahlms=$(<:.cpp=.lst)

# Optional assembler flags.
ASFLAGS = -mmcu=atmega128 -I. -x assembler-with-cpp -Wa,-ahlms=$(<:.S=.lst),-gstabs 

# Optional linker flags.
LDFLAGS = -Wl,-Tdata,0x801100,--defsym=__heap_end=0x80dffff,-Map=$(TARGET).map,--cref
#LDFLAGS = -Wl,-Tdata,-Map=$(TARGET).map,--cref

# Additional libraries
#
# Minimalistic printf version
#LDFLAGS += -Wl,-u,vfprintf -lprintf_min
#
# Floating point printf version (requires -lm below)
#LDFLAGS +=  -Wl,-u,vfprintf -lprintf_flt
#
# -lm = math library
LDFLAGS += -lm


OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump

# Programming support using avrdude.
#AVRDUDE = avrdude

REMOVE = rm -f
COPY = cp

ELFCOFF = objtool

HEXSIZE = avr-size --target=$(FORMAT) $(TARGET).hex
ELFSIZE = avr-size -A $(TARGET).elf

FINISH = echo Errors: none
BEGIN = echo -------- begin --------
END = echo --------  end  --------



# Define all object files.
OBJ = $(SRCPP:.cpp=.o) $(SRC:.c=.o) $(ASRC:.S=.o)

# Define all listing files.
LST = $(ASRC:.S=.lst) $(SRC:.c=.lst) $(SRCPP:.cpp=.lst)

# Default target.
all: begin gccversion sizebefore $(TARGET).elf $(TARGET).hex $(TARGET).eep \
$(TARGET).lss sizeafter finished end


# Eye candy.
begin:
	@$(BEGIN)

finished:
	@$(FINISH)

end:
	@$(END)


# Display size of file.
sizebefore:
	@if [ -f $(TARGET).elf ]; then echo Size before:; $(ELFSIZE);fi

sizeafter:
	@if [ -f $(TARGET).elf ]; then echo Size after:; $(ELFSIZE);fi



# Display compiler version information.
version: 
	$(CC) --version




# 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 


coff: $(TARGET).elf
	$(COFFCONVERT) -O coff-avr $< $(TARGET).cof


extcoff: $(TARGET).elf
	$(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof




# Create final output files (.hex, .eep) from ELF output file.
%.hex: %.elf
	$(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@

%.eep: %.elf
	-$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
    --change-section-lma .eeprom=0 -O $(FORMAT) $< $@

# Create extended listing file from ELF output file.
%.lss: %.elf
	$(OBJDUMP) -h -S $< > $@


# Link: create ELF output file from object files.
.SECONDARY : $(TARGET).elf
.PRECIOUS : $(OBJ)
%.elf: $(OBJ)
	$(CC) $(LDFLAGS) $(CFLAGS) $(OBJ) --output $@

# Compile: create object files from C source files.
%.o : %.cpp makefile $(HDR)
	$(CC) -c $(CPPFLAGS) $< -o $@


# Compile: create object files from C source files.
# $(HDR)
%.o : %.c makefile $(HDR)
	$(CC) -c $(CFLAGS) $< -o $@


# Compile: create assembler files from C source files.
%.s : %.c
	$(CC) -S $(CFLAGS) $< -o $@


# Target: clean project.
clean: begin clean_list finished end

clean_list :
	$(REMOVE) $(TARGET).hex
	$(REMOVE) $(TARGET).eep
	$(REMOVE) $(TARGET).obj
	$(REMOVE) $(TARGET).cof
	$(REMOVE) $(TARGET).elf
	$(REMOVE) $(TARGET).map
	$(REMOVE) $(TARGET).obj
	$(REMOVE) $(TARGET).a90
	$(REMOVE) $(TARGET).sym
	$(REMOVE) $(TARGET).lnk
	$(REMOVE) $(TARGET).lss
	$(REMOVE) $(OBJ)
	$(REMOVE) $(LST)
	$(REMOVE) $(SRC:.c=.s)
	$(REMOVE) $(SRC:.c=.d)


# Remove the '-' if you want to see the dependency files generated.
-include $(SRC:.c=.d)



# Listing of phony targets.
.PHONY : all begin finish end sizebefore sizeafter gccversion coff clean clean_list


