#########################################################################
#									#
#									#
#  Makefile for AVR atmega328p version of the Xinu operating system	#
#									#
#  includes: .deps and .defs						#
#									#
#									#
#########################################################################

# PLATFORM	=	-Dlgt8f328p
# HZ		=	4000000

PLATFORM	=	-DATMEGA
HZ		=	16000000

COMPILER_ROOT	=	avr-
PNAME		=	ATMEGA328P
PLAT		=	Platform_$(PNAME)
TOPDIR		=	..

CC		=	${COMPILER_ROOT}gcc
LD		=	${COMPILER_ROOT}gcc
OBJCOPY		=	${COMPILER_ROOT}objcopy
OBJDUMP		=	${COMPILER_ROOT}objdump
QEMU		=	/home/lechnerm/rafa/xinu/qemu_stm32/arm-softmmu/qemu-system-arm
XINU		=	$(TOPDIR)/compile/xinu.elf
XINUBIN		=	$(TOPDIR)/compile/xinu.bin
XINUHEX		=	$(TOPDIR)/compile/xinu.hex
XINUBOOT	=	$(TOPDIR)/compile/xinu
BUILDMAKE	=	$(TOPDIR)/compile/bin/build-make
MKVERS		=	$(TOPDIR)/compile/bin/mkvers

MAKEDEP		=	$(CC) -M -MG

DEPSFILE	=	.deps
DEFSFILE	=	.defs
VERSIONFILE	=	version
LDSCRIPT	=	ld.script
MKVERS		=	$(TOPDIR)/compile/bin/mkvers


			# RAFA -s $(TOPDIR)/device/ram			\
REBUILDFLAGS	=	-s $(TOPDIR)/system  debug.c 	\
			#-s $(TOPDIR)/device/gpio			\
			#-s $(TOPDIR)/device/spi			\
			#-s $(TOPDIR)/device/avr_eeprom			\
			-s $(TOPDIR)/device/tty			\
			-s $(TOPDIR)/shell  'xsh_rdstest*'
			#
REBUILDFLAGS	=	-s $(TOPDIR)/system   	\
			-s $(TOPDIR)/lib			\
			-s $(TOPDIR)/device/nam			\
			-s $(TOPDIR)/main  

INCLUDE		=	-I$(TOPDIR)/include
# RAFA DEFS		= 	-DBSDURG -DVERSION=\""`cat $(VERSIONFILE)`"\"
DEFS		= 	-DVERSION=\""Xinu AVR 0.1 RAFA\0"\"

# Compiler flags
# CFLAGS  = -DF_CPU=16000000UL -mmcu=atmega328p  -fno-builtin -c -Wall -Os ${DEFS} ${INCLUDE}
CFLAGS  = -DF_CPU=$(HZ)UL $(PLATFORM) -mmcu=atmega328p  -fno-builtin -c -Wall -Os ${DEFS} ${INCLUDE}
# RAFA AGREGA LAS 3 de ABAJO para reducir size
CFLAGS += -Wl,-static
CFLAGS += -ffunction-sections
CFLAGS += -fdata-sections
CFLAGS += -Wl,--gc-sections
CFLAGS += -mcall-prologues 
CFLAGS += -mrelax
CFLAGS += -Wno-int-conversion
CFLAGS += -Wno-implicit-function-declaration
CFLAGS += -Wno-incompatible-pointer-types

SFLAGS  = ${INCLUDE}

# Loader flags

# RAFA LDFLAGS = -Wl,--wrap,malloc,--wrap,free,--wrap,realloc,--wrap,fdevopen
# LDFLAGS = -mmcu=atmega328p -nostartfiles -T ld.script  -Wl,-Map,xinu.map
LDFLAGS = -mmcu=atmega328p -Wl,-Map,xinu.map -lm -Wl,--gc-sections -mrelax


all:	xinu

#--------------------------------------------------------------------------------
# Handle generation of a new version string when initialize is recompiled
#--------------------------------------------------------------------------------

newversion:
	@echo creating new version
	@$(MKVERS) $(PNAME) > version

#--------------------------------------------------------------------------------
# Include generic make targets and rules from the file generated by build-make
#--------------------------------------------------------------------------------
-include $(DEFSFILE)

#--------------------------------------------------------------------------------
# Add files that require special rules and place start.o at the front
#--------------------------------------------------------------------------------
# RAFA quitamos el start.S y start.o porque intentaremos el startup de avr
LD_LIST = binaries/start.o $(filter-out binaries/start.o,$(OBJ_FILES))
# LD_LIST = $(filter-out binaries/start.o,$(OBJ_FILES))

#------------------------------------------------------------
# Rules for files that need special handling
#------------------------------------------------------------

# Define variables for the Configuration file and generated files

CONFFILE	= $(TOPDIR)/config/Configuration
CONFH		= $(TOPDIR)/include/conf.h
CONFC		= $(TOPDIR)/system/conf.c

# Define variables for the config program and its sources

CONFPGM		= $(TOPDIR)/config/config
CONFL		= $(TOPDIR)/config/config.l
CONFY		= $(TOPDIR)/config/config.y

# Set up the required build directory structure

BLDDIRS	= binaries
export

#--------------------------------------------------------------------------------
# Specific additional rules and exceptions
#--------------------------------------------------------------------------------

xinu:	Makefile rebuild $(BLDDIRS) $(DEFSFILE) $(DEPSFILE) $(CONFH) $(CONFC) $(LD_LIST)
	@echo;echo 'Loading object files to produce xinu'
	# RAFA original : @$(LD) $(LDFLAGS) $(LD_LIST) -o $(XINU) -L/usr/lib/gcc/arm-none-eabi/6.3.1/ --print-memory-usage
	@$(LD) $(LDFLAGS) $(LD_LIST) -o $(XINU) 
	@echo "Creating Binary..."
	@$(OBJCOPY) -O binary $(XINU) $(XINUBIN)

examine-all:
	$(OBJDUMP) -D $(XINU) | less

examine-header:
	$(OBJDUMP) -x $(XINU) | less

flash:
	avr-objcopy -O ihex -R .eeprom $(XINU) $(XINUHEX)
	avrdude -p atmega328p -c arduino -P `ls -1 /dev/ttyUSB* /dev/ttyACM* 2>/dev/null | head -1` -b 115200 -D -U flash:w:$(XINUHEX):i 


qemu:
	$(QEMU) -serial stdio -M stm32-f103c8 -kernel $(XINUBIN) 
monitor:
	$(QEMU) -monitor stdio -serial stdio -M stm32-f103c8 -kernel $(XINUBIN) 

$(BLDDIRS): 
	@mkdir -p $(BLDDIRS)

objects: $(LD_LIST)

$(CONFH): $(CONFFILE) $(CONFPGM)
	@echo making $(CONFH)
	@make configure

$(CONFC): $(CONFFILE) $(CONFPGM)
	@echo making $(CONFC)
	@make configure

$(CONFPGM): $(CONFL) $(CONFY)
	@echo making the config program
	@make -C $(TOPDIR)/config clean all install

configure:
	@echo forcing a rebuild of conf.h and conf.c
	@make newversion
	@(cd $(TOPDIR)/config; make install)

clean:
	@echo removing .o files
	@rm -f ${LD_LIST}
	@echo   removing configuration files ...
	@rm -f $(CONFH) $(CONFC)
	@(cd $(TOPDIR)/config; make clean)
	@echo removing xinu ...
	@rm -f $(XINU)
	@rm -f $(XINUBIN)
	@rm -f $(XINUBOOT)

#--------------------------------------------------------------------------------
# Locations of source directories and exceptions (.c and .[sS] files to exclude)
#--------------------------------------------------------------------------------
	
$(DEFSFILE):
	@rm -f $(DEFSFILE)
	@echo "" > $(DEFSFILE)
	@make rebuild

$(DEPSFILE):
	@rm -f $(DEPSFILE)
	@echo "" > $(DEPSFILE)
	@make depend

rebuild: $(CONFC)
	@echo Rebuilding the $(DEFSFILE) file
	@$(BUILDMAKE) $(REBUILDFLAGS) > $(DEFSFILE)

defclean:
	rm -f $(DEFSFILE)
	echo "" > $(DEFSFILE)

depend: $(DEFSFILE)
	@echo;echo Making all dependencies in $(DEPSFILE)
	@$(MAKEDEP) ${INCLUDE} ${SRC_FULL} > $(DEPSFILE)
	@echo;echo Finished making dependencies

depclean:
	rm -f $(DEPSFILE)
	echo "" > $(DEPSFILE)

-include $(DEPSFILE)
