#+---------------------------------------------------------------------------
#
#  Copyright (c) 2008 Anton Gusev aka AHTOXA (http://AHTOXA.NET)
#
#  File:       makefile
#
#  Contents:   makefile to build software under avr-gcc with gnu make
#
#----------------------------------------------------------------------------

#############  program name
	program_name	= nokia3310


#############  mcu type
	MCU = attiny2313
#	MCU = atmega8
#	MCU = atmega16
#	MCU = atmega128



###########################################################
#  common part for all avr-gcc projects
###########################################################

	base		= .
	compiler	= avr-gcc
	linker		= avr-gcc
	assembler	= $(compiler) -x assembler-with-cpp
	objcopy		= avr-objcopy
	objdump		= avr-objdump
	size		= avr-size -d
	programmer	= avreal32.exe
	archiver	= Rar.exe
	rm			= rm -f
	cp			= cp



	src_dir		= $(base)/src
	sch_dir		= $(base)/sch
	obj_dir		= $(base)/obj
	exe_dir		= $(base)/exe
	lst_dir		= $(base)/lst
	bak_dir		= $(base)/bak

	hexfile		= $(exe_dir)/$(program_name).hex
	elffile		= $(exe_dir)/$(program_name).elf
	eepfile		= $(exe_dir)/$(program_name).eep
	okfile		= $(exe_dir)/$(program_name).ok
	lstfile		= $(lst_dir)/$(program_name).lst

############# files
source_dirs  := $(src_dir)
include_dirs := $(patsubst %, -I "%", $(source_dirs))
source_files := $(wildcard $(addsuffix /*.c, $(source_dirs))) $(wildcard $(addsuffix /*.asm, $(source_dirs)))
obj_files	 := $(notdir $(source_files) )
obj_files	 := $(obj_files:.c=.o)
obj_files	 := $(obj_files:.asm=.obj)
obj_files	 := $(obj_files:.ASM=.obj)
obj_files	 := $(patsubst %, $(obj_dir)/%, $(obj_files))

	rars	= $(src_dir)\*.asm $(src_dir)\*.c $(src_dir)\*.inc $(src_dir)\*.h $(sch_dir)\*.sch $(sch_dir)\*.pcb $(base)\makefile $(base)\*.mpj

############# flags
#	compiler_flags	= -g -Os -mcall-prologues -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wall -Wstrict-prototypes -mmcu=$(MCU)
	compiler_flags	= -g -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wall -Wstrict-prototypes -mmcu=$(MCU)
	assembler_flags = -Wa, -gstabs -mmcu=$(MCU)
#	linker_flags	= -Wl,-u,vfprintf -lprintf_flt -lm -map="$(lst_dir)\$(program_name).map" --cref -mmcu=$(MCU)
#	linker_flags	= -Wl,-u,vfprintf -lprintf_min -Wl,-lm,-Map="$(program_name).map",--cref -mmcu=$(MCU)
#	linker_flags	= -Wl,-lm,-Map="$(program_name).map",--cref -mmcu=$(MCU)
	linker_flags	= -Wl,-Map="$(program_name).map",--cref -mmcu=$(MCU)
	prog_flags		= +$(MCU) -as -p1 -o8.0MHZ -e -b -w -c

#	programmer options
#	for quartz >= 8MHz
#	prog_flags_f	= +$(MCU) -as -fCKSEL=F,CKDIV=1 -p1 -o8.0MHZ -e -b -w -c

#	for internal oscillator 8 MHz
	prog_flags_f	= +$(MCU) -as -fCKSEL=4,CKDIV=1,BLEV=5 -p1 -o8.0MHZ -w

#	for internal oscillator 4 MHz
#	prog_flags_f	= +$(MCU) -as -fCKSEL=2,CKDIV=1 -p1 -o8.0MHZ -w



.PHONY: all clean program

############# targets

all : $(elffile) $(hexfile) $(lstfile) $(okfile)

build: clean all


$(hexfile):	$(elffile)
	@echo --- making hex...
	@$(objcopy) -O ihex -R .eeprom $(elffile) $(hexfile)


$(eepfile): $(elffile)
	@echo --- making eep...
	@$(objcopy) -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 -O ihex $(elffile) $(eepfile)

$(lstfile): $(elffile)
	@echo --- making asm-lst...
	@$(objdump) -h -S $(elffile) > $(lstfile)

$(okfile): $(elffile)
	@$(size) $(elffile)
	@echo "Errors: none"


#program: $(hexfile) $(eepfile)
#	@echo *** programming...
#	@$(programmer) $(prog_flags) $(hexfile) $(eepfile)

program: $(hexfile)
	@echo --- programming...
	@$(programmer) $(prog_flags) "$(hexfile)"


fuses:
	@echo --- programming FUSES...
	@$(programmer) $(prog_flags_f) "$(hexfile)"


$(elffile):	$(obj_files)
	@echo --- linking...
	@$(linker) $(obj_files) $(linker_flags) -o "$(elffile)"


VPATH := $(source_dirs)

$(obj_dir)/%.o: %.c
	@echo --- compiling $(*F).c
	@$(compiler) -c $(compiler_flags) -Wa,-ahlms=$(lst_dir)\$(*F).lst -o $@ $<


$(obj_dir)/%.obj: %.asm
	@echo --- assembling $(*F).asm
	@$(assembler) $(assembler_flags) -o $@ $<


clean:
	-@$(rm) $(obj_dir)/*.o 2>nul
	-@$(rm) $(exe_dir)/*.hex 2>nul
	-@$(rm) $(exe_dir)/*.elf 2>nul
	-@$(rm) $(exe_dir)/*.eep 2>nul
	-@$(rm) $(lst_dir)/*.lst 2>nul
	-@$(rm) $(lst_dir)/*.map 2>nul


archive:
	@echo *** archiving...
	@$(archiver) a -inul -agyy-mm-dd,hh-nn-ss $(bak_dir)\$(program_name)_.rar $(rars)
	@echo *** done!
