#_________________________________________________________________________________________
#
#          
#     -     
#
#     :  GLB_DEBUG = 1
#     : GLB_DEBUG = 0
#
NAME  		= 408
HMOD  		= a$(NAME)
AMOD  		= a$(NAME)
CMOD            = c$(NAME)
SHELL		= cmd.exe
GLB_DEBUG	= 1

TMP             = $(PF_TMP)

#_________________________________________________________________________________________
#
#  
#
MSG_AS		= as:
MSG_CC		= cc:
MSG_LD		= ld:
MSG_LB		= lb:
MSG_CD		= cd:
MSG_TX		= tx:
MSG_HT		= ht:

#_________________________________________________________________________________________
#
#     (   )
#
UTIL_PATH	= D:\ECC\UTIL
TOOL_PATH	= D:\ECC\GNU\ARM472
TOOL_LIB	= $(TOOL_PATH)\lib\gcc\arm-none-eabi\4.7.3\armv7-m\libgcc.a
TOOL_PATH_INC	= $(TOOL_PATH)\arm-none-eabi\include

AS		= $(TOOL_PATH)\bin\arm-none-eabi-as.exe
CP		= $(TOOL_PATH)\bin\arm-none-eabi-cpp.exe
CC		= $(TOOL_PATH)\bin\arm-none-eabi-gcc
LD		= $(TOOL_PATH)\bin\arm-none-eabi-ld.exe
LB		= $(TOOL_PATH)\bin\arm-none-eabi-ar.exe
OBJC		= $(TOOL_PATH)\bin\arm-none-eabi-objcopy.exe
RM		= del

TX     		= $(UTIL_PATH)\atxt32.exe
SREC		= $(UTIL_PATH)\srec_cat.exe

#_________________________________________________________________________________________
#
#   
# 
INC	= -I$(TOOL_PATH_INC)

AFLAGS 	= -aghlms						\
	  -mcpu=cortex-m3					\
	  -mthumb						\
	  -mlittle-endian					\
 	  $(INC)

CFLAGS 	= $(INC)						\
	  -c							\
	  -std=c99						\
	  -Os							\
	  -Wa,-a,-ad						\
	  -ffunction-sections					\
	  -mcpu=cortex-m3					\
	  -mthumb						\
          -mlittle-endian					\
	  -mno-unaligned-access					\
	  -nostartfiles						\
	  -Wchar-subscripts					\
	  -Wimplicit-int					\
	  -Wimplicit-function-declaration			\
	  -Wcomment						\
	  -Wformat						\
	  -Wmain						\
	  -Wmissing-braces					\
	  -Wnonnull						\
	  -Wparentheses						\
	  -Wsequence-point					\
	  -Wsign-compare					\
	  -Wswitch						\
	  -Wreturn-type						\
	  -Wtrigraphs						\
	  -Wunknown-pragmas					\
	  -Wuninitialized					\
	  -Wunused-label					\
	  -Wunused-value					\
	  -Wunused-variable					\
	  -Wvolatile-register-var				\
	  -DDBG_ENABLE=$(GLB_DEBUG)

PFLAGS	=
LFLAGS	= --gc-sections						\
	  --entry=bsp_image_base

#_________________________________________________________________________________________
#
#  
#
define	do_as
	@echo $(MSG_AS) $<
	$(TX) $< $(TMP)\$(basename $(notdir $<)).s -a -e -t -l@@
	$(CP) $(TMP)\$(basename	$(notdir $<)).s	-o $(TMP)\$(basename $(notdir $<)).spp $(INC) $(PFLAGS)
	$(AS) $(AFLAGS)	$(TMP)\$(basename $(notdir $<)).spp -o $@ -ahclms=$(TMP)\$(basename $(notdir $<)).lst
endef

define	do_cc
	@echo $(MSG_CC) $<
	$(CC) $(CFLAGS)	$< -o $@ -Wa,-alhs=$(TMP)\$(basename $(notdir $<)).lst
endef

#_________________________________________________________________________________________
#
all:		$(HMOD).hex
OBJS		= $(TMP)\$(AMOD).o                  		\
		  $(TMP)\$(CMOD).o 	     	      		\
		  $(TMP)\main.o 	     	      		\
		  $(TMP)\debug.o

HDRS		= $(CMOD).h					\
		  debug.h					\
		  ioport.h

LIBS		= $(TOOL_LIB)

#_________________________________________________________________________________________
#
# :  RAM  AHB     0x2001C000
# (       )
#
$(HMOD).hex:	$(OBJS) $(LIBS) $(HDRS)
	@echo	MEMORY							>  $(TMP)\l$(NAME).ld
	@echo	{							>> $(TMP)\l$(NAME).ld
	@echo	  ROM (rx) : ORIGIN = 0x08004000, LENGTH = 16K		>> $(TMP)\l$(NAME).ld
	@echo	  CCM (rw) : ORIGIN = 0x10000000, LENGTH = 64K		>> $(TMP)\l$(NAME).ld
	@echo	  RAM (rw) : ORIGIN = 0x20000000, LENGTH = 48K		>> $(TMP)\l$(NAME).ld
	@echo	  AHB (rw) : ORIGIN = 0x2001C000, LENGTH = 16K		>> $(TMP)\l$(NAME).ld
	@echo	  RTC (rw) : ORIGIN = 0x40024000, LENGTH = 4K		>> $(TMP)\l$(NAME).ld
	@echo	}							>> $(TMP)\l$(NAME).ld
	@echo	SECTIONS						>> $(TMP)\l$(NAME).ld
	@echo	{							>> $(TMP)\l$(NAME).ld
	@echo	  .text :						>> $(TMP)\l$(NAME).ld
	@echo	  {							>> $(TMP)\l$(NAME).ld
	@echo	    *(IHAL)						>> $(TMP)\l$(NAME).ld
	@echo	    *(.text .text.* .gnu.linkonce.t.*)			>> $(TMP)\l$(NAME).ld
	@echo	    *(.gnu.warning)					>> $(TMP)\l$(NAME).ld
	@echo	    *(.rodata .rodata.* .gnu.linkonce.r.*)		>> $(TMP)\l$(NAME).ld
	@echo	    *(XHAL)						>> $(TMP)\l$(NAME).ld
	@echo	  } ^> ROM						>> $(TMP)\l$(NAME).ld
	@echo	  . = ALIGN(4);						>> $(TMP)\l$(NAME).ld
	@echo	  __data_init__ = . ;					>> $(TMP)\l$(NAME).ld
	@echo	  .data : AT (__data_init__)				>> $(TMP)\l$(NAME).ld
	@echo	  {							>> $(TMP)\l$(NAME).ld
	@echo	    __data_start__ = .;					>> $(TMP)\l$(NAME).ld
	@echo	    *(.data)						>> $(TMP)\l$(NAME).ld
	@echo	    *(.data.*)						>> $(TMP)\l$(NAME).ld
	@echo	    __data_end__ = . ;					>> $(TMP)\l$(NAME).ld
	@echo	  } ^> RAM						>> $(TMP)\l$(NAME).ld
	@echo	  .bss (NOLOAD) :					>> $(TMP)\l$(NAME).ld
	@echo	  {							>> $(TMP)\l$(NAME).ld
	@echo	     __bss_start__ = . ;				>> $(TMP)\l$(NAME).ld
	@echo	     *(.bss)						>> $(TMP)\l$(NAME).ld
	@echo	     *(.bss.*)						>> $(TMP)\l$(NAME).ld
	@echo	     . = ALIGN(4);					>> $(TMP)\l$(NAME).ld
	@echo	     *(COMMON)						>> $(TMP)\l$(NAME).ld
	@echo	     __bss_end__ = . ;					>> $(TMP)\l$(NAME).ld
	@echo	  } ^> RAM						>> $(TMP)\l$(NAME).ld
	@echo	  .no_init (NOLOAD) :					>> $(TMP)\l$(NAME).ld
	@echo	  {							>> $(TMP)\l$(NAME).ld
	@echo	     *(NO_INIT)						>> $(TMP)\l$(NAME).ld
	@echo	     . = ALIGN(3);					>> $(TMP)\l$(NAME).ld
	@echo	     *(HANDLER_STACK)					>> $(TMP)\l$(NAME).ld
	@echo	     . = ALIGN(3);					>> $(TMP)\l$(NAME).ld
	@echo	     *(THREAD_STACK)					>> $(TMP)\l$(NAME).ld
	@echo	     __stack_end__ = . ;				>> $(TMP)\l$(NAME).ld
	@echo	  } ^> RAM						>> $(TMP)\l$(NAME).ld
	@echo	}							>> $(TMP)\l$(NAME).ld
	@echo	$(MSG_LD) $(HMOD).hex
	$(LD) 	$(LFLAGS) $(OBJS) $(LIBS) -o $(TMP)\$(HMOD).elf -Map $(TMP)\m$(NAME).map -T $(TMP)\l$(NAME).ld
	$(OBJC)	$(TMP)\$(HMOD).elf $(TMP)\$(HMOD)_gcc.hex --gap-fill 255 -O ihex
	$(SREC) $(TMP)\$(HMOD)_gcc.hex -Intel -offset -0x08004000 -o $(HMOD).hex -Intel

#_________________________________________________________________________________________
#
$(TMP)\$(AMOD).o:	$(AMOD).a79	$(HDRS)		; $(do_as)
$(TMP)\$(CMOD).o:	$(CMOD).c	$(HDRS)		; $(do_cc)
$(TMP)\main.o:		main.c		$(HDRS)		; $(do_cc)
$(TMP)\debug.o:		debug.c		$(HDRS)		; $(do_cc)
