#
# File: Common_ARMCC.make
#
# Copyright (c) 2017-2018, Code-Time Technologies Inc.
# All rights reserved.		
#

# NOTE:
# This Makefile is included by all ARMCC projects based on a makefile
# It's to simplify the work needed when something is changed for a target or for a demo #

# ----------------------------------------------------------------------------------------------------
# Platform specific files & build options
# Only this section needs to be updated when adding a new platform
# If performance monitoring is supported, then update the next section too (only for source code)
#
# BOARD_ID   - Name used to construct the ELF file name
# LIB_ID     - When mAbassi is used in library form, identifier of the library
#              Ignored with source code
# LNK_SCRIPT - Name of the linker scatter file located at this level
# ----------------------------------------------------------------------------------------------------

ifeq ($(PLATFORM), 0x0000AAC5)
  BOARD_ID   := CY5
  LIB_ID     := AR5_CY5
  LNK_SCRIPT := AR5_CY5.scat
else ifeq ($(PLATFORM), 0x0100AAC5)
  BOARD_ID   := DE0
  LIB_ID     := AR5_CY5
  LNK_SCRIPT := AR5_CY5.scat
else ifeq ($(PLATFORM), 0x0200AAC5)
  BOARD_ID   := SOCRATES
  LIB_ID     := AR5_CY5
  LNK_SCRIPT := AR5_CY5.scat
else ifeq ($(PLATFORM), 0x0300AAC5)
  BOARD_ID   := SOCKIT
  LIB_ID     := AR5_CY5
  LNK_SCRIPT := AR5_CY5.scat
else ifeq ($(PLATFORM), 0x0400AAC5)
  BOARD_ID   := CUSTOM1
  LIB_ID     := AR5_CY5
  LNK_SCRIPT := AR5_CY5.scat
else ifeq ($(PLATFORM), 0x4000AAC5)
  BOARD_ID   := AR5
  LIB_ID     := AR5_CY5
  LNK_SCRIPT := AR5_CY5.scat
else ifeq ($(PLATFORM), 0x0000AA10)
  BOARD_ID   := AR10
  LIB_ID     := AR10
  LNK_SCRIPT := AR10.scat
else
  $(error Error : Unsupported value for the defintion of PLATFORM)
endif

# ----------------------------------------------------------------------------------------------------
#
# YOU SHOULDN'T HAVE TO CHANGE / ADD ANYTHING PAST THIS POINT
#
# ----------------------------------------------------------------------------------------------------

# ----------------------------------------------------------------------------------------------------
# Common "C" and "ASM files

ifeq ($(BUILD_TYPE), TYPE_SRC)
  C_SRC   += uAbassi.c
  S_SRC   += ARMv7_SMP_L1_L2_ARMCC.s
  S_SRC   += uAbassi_SMP_CORTEXA9_ARMCC.s
endif

C_SRC   += uAbassiCfgA9.c

ifeq ($(DBG_SHELL), YES)
  C_SRC   += Shell.c
  C_SRC   += SubShell.c
endif
ifeq ($(PERF_MON), YES)
   $(error Error : performance monitoring is not available in uAbassi)
endif

CFLAGS += -DOS_DEMO=-$(DEMO_NMB)

ifeq ($(UART_TYPE), POLL)
  CFLAGS  += -DUART_Q_SIZE_RX=0
  CFLAGS  += -DUART_Q_SIZE_TX=0
else ifeq ($(UART_TYPE), MBX)
  CFLAGS  += -DDUART_Q_SIZE_RX=OX_MAX_PEND_RQST/4
  CFLAGS  += -DDUART_Q_SIZE_TX=OX_MAX_PEND_RQST/4
else
  CFLAGS  += -DUART_Q_SIZE_RX=1
  CFLAGS  += -DUART_Q_SIZE_TX=1
  CFLAGS  += -DUART_CIRC_BUF_RX=$(UART_TYPE)
  CFLAGS  += -DUART_CIRC_BUF_TX=$(UART_TYPE)
endif

# ----------------------------------------------------------------------------------------------------
# Common "C" and "ASM flags (target processor options)

CFLAGS  += --cpu=Cortex-A9
CFLAGS  += --c99 --gnu 
CFLAGS  += --fpu=VFPv3
CFLAGS  += --md
CFLAGS  += --depend_format=unix_escaped 
CFLAGS  += -Dinline=__inline

CFLAGS  += -D_UABASSI_=1
CFLAGS  += -DOS_PLATFORM=$(PLATFORM)
CFLAGS  += -DOS_N_CORE=$(N_CORE)
ifeq ($(BUILD_TYPE), TYPE_BM)
  CFLAGS  += -DOS_KEIL_REENT=1
else ifeq ($(BUILD_TYPE), TYPE_FREE)
  CFLAGS  += -DOS_KEIL_REENT=1
else
  CFLAGS  += -DOS_KEIL_REENT=$(LIB_REENT)
endif

ifeq ($(DBG_SHELL), YES)
  C_INC   += ../../../Abassi/Shell.h
  CFLAGS  += -DUSE_SHELL=1
  CFLAGS  += -DSHELL_LOGIN=0
  CFLAGS  += -DSHELL_HISTORY=64
endif

AFLAGS  += --cpu=Cortex-A9
AFLAGS  += --fpu=VFPv3
AFLAGS  += --apcs=/interwork
AFLAGS  += --md
AFLAGS  += --depend_format=unix_escaped
AFLAGS  += --predefine "OS_PLATFORM SETA $(PLATFORM)"

ifeq ($(BUILD_TYPE), TYPE_SRC)
  AFLAGS  += --predefine "OS_N_CORE SETA $(N_CORE)"
  AFLAGS  += --predefine "OS_KEIL_REENT SETA $(LIB_REENT)"
  AFLAGS  += --predefine "OS_RUN_PRIVILEGE SETA 1"
  AFLAGS  += --predefine "OS_HANDLE_PSR_Q SETA 0"
  AFLAGS  += --predefine "OS_SPINLOCK SETA 1"
  AFLAGS  += --predefine "OS_USE_CACHE SETA 1"
  AFLAGS  += --predefine "OS_L2_BASE_ADDR SETA -1"
  AFLAGS  += --predefine "OS_SAME_L1_PAGE_TBL SETA 0"
  AFLAGS  += --predefine "OS_ARM_ERRATA_ALL SETA 1"
  AFLAGS  += --predefine "OS_MMU_ALL_INVALID SETA 0"
  AFLAGS  += --predefine "OS_L1_CACHE_BP SETA 1"
  AFLAGS  += --predefine "OS_L1_CACHE_PF SETA 1"
  AFLAGS  += --predefine "OS_L2_CACHE_PF SETA 1"
  AFLAGS  += --predefine "OS_CACHE_WRITE_ZERO SETA 1"
  AFLAGS  += --predefine "OS_USE_NON_SHARED SETA 1"
  AFLAGS  += --predefine "OS_ABORT_STACK_SIZE SETA 1024"
  AFLAGS  += --predefine "OS_FIQ_STACK_SIZE SETA 1024"
  AFLAGS  += --predefine "OS_IRQ_STACK_SIZE SETA 8192"
  AFLAGS  += --predefine "OS_SUPER_STACK_SIZE SETA 1024"
  AFLAGS  += --predefine "OS_UNDEF_STACK_SIZE SETA 1024"
  AFLAGS  += --predefine "OS_MMU_EXTERN_DEF SETA 0"
endif

# ----------------------------------------------------------------------------------------------------
# Package source selection
# ----------------------------------------------------------------------------------------------------

ifneq ($(BUILD_TYPE), TYPE_SRC)
 ifeq ($(BUILD_TYPE), TYPE_BM)
  LIBRR := 
 else ifeq ($(BUILD_TYPE), TYPE_FREE)
  LIBRR := R_
 else ifneq ($(LIB_REENT), 0)
  LIBRR := R_
 else
  LIBRR :=
 endif
 LIBREENT := $(LIBRR)

 ifeq ($(BUILD_TYPE), TYPE_EVAL)
  LIB_TYPE := EVAL_
 else ifeq ($(BUILD_TYPE), TYPE_FREE)
  LIB_TYPE := FREE_
 else ifeq ($(BUILD_TYPE), TYPE_BM)
  LIB_TYPE := BM_
 else
  LIB_TYPE :=
 endif

 LIBS  += ../../../Platform/lib/lib_uAbassi_$(LIB_TYPE)$(LIBREENT)ARMCC_$(LIB_ID).a
 LDEP  += ../../../Platform/lib/lib_uAbassi_$(LIB_TYPE)$(LIBREENT)ARMCC_$(LIB_ID).a
endif

# ----------------------------------------------------------------------------------------------------
# Compilation, assembly, linking
# ----------------------------------------------------------------------------------------------------

AS := "armasm"
CC := "armcc"
LD := "armlink"

RM := rm -f
CP := cp -f

ifeq ($(DEMO_NMB), 0xBABA)
  ELF    := BareMetal_Free_$(BOARD_ID)_A9_ARMCC.axf
  MAP    := BareMetal_Free_$(BOARD_ID)_A9_ARMCC.map
else ifeq ($(DEMO_NMB), 0xDEDE)
  ELF    := uDemo_Free_$(BOARD_ID)_A9_ARMCC.axf
  MAP    := uDemo_Free_$(BOARD_ID)_A9_ARMCC.map
else
 ifeq ($(DEMO_NMB), 10)
  ELF_NAME := 10-19
 else ifeq ($(DEMO_NMB), -10)
  ELF_NAME := 10-19
 else ifeq ($(DEMO_NMB), 11)
  ELF_NAME := 10-19
 else ifeq ($(DEMO_NMB), -11)
  ELF_NAME := 10-19
 else ifeq ($(DEMO_NMB), 12)
  ELF_NAME := 10-19
 else ifeq ($(DEMO_NMB), -12)
  ELF_NAME := 10-19
 else ifeq ($(DEMO_NMB), 13)
  ELF_NAME := 10-19
 else ifeq ($(DEMO_NMB), -13)
  ELF_NAME := 10-19
 else ifeq ($(DEMO_NMB), 14)
  ELF_NAME := 10-19
 else ifeq ($(DEMO_NMB), -14)
  ELF_NAME := 10-19
 else ifeq ($(DEMO_NMB), 15)
  ELF_NAME := 10-19
 else ifeq ($(DEMO_NMB), -15)
  ELF_NAME := 10-19
 else ifeq ($(DEMO_NMB), 16)
  ELF_NAME := 10-19
 else ifeq ($(DEMO_NMB), -16)
  ELF_NAME := 10-19
 else ifeq ($(DEMO_NMB), 17)
  ELF_NAME := 10-19
 else ifeq ($(DEMO_NMB), -17)
  ELF_NAME := 10-19
 else ifeq ($(DEMO_NMB), 18)
  ELF_NAME := 10-19
 else ifeq ($(DEMO_NMB), -18)
  ELF_NAME := 10-19
 else ifeq ($(DEMO_NMB), 19)
  ELF_NAME := 10-19
 else ifeq ($(DEMO_NMB), -19)
  ELF_NAME := 20-29
 else ifeq ($(DEMO_NMB), 20)
  ELF_NAME := 20-29
 else ifeq ($(DEMO_NMB), -20)
  ELF_NAME := 20-29
 else ifeq ($(DEMO_NMB), 21)
  ELF_NAME := 20-29
 else ifeq ($(DEMO_NMB), -21)
  ELF_NAME := 20-29
 else ifeq ($(DEMO_NMB), 22)
  ELF_NAME := 20-29
 else ifeq ($(DEMO_NMB), -22)
  ELF_NAME := 20-29
 else ifeq ($(DEMO_NMB), 23)
  ELF_NAME := 20-29
 else ifeq ($(DEMO_NMB), -23)
  ELF_NAME := 20-29
 else ifeq ($(DEMO_NMB), 24)
  ELF_NAME := 20-29
 else ifeq ($(DEMO_NMB), -24)
  ELF_NAME := 20-29
 else ifeq ($(DEMO_NMB), 25)
  ELF_NAME := 20-29
 else ifeq ($(DEMO_NMB), -25)
  ELF_NAME := 20-29
 else ifeq ($(DEMO_NMB), 26)
  ELF_NAME := 20-29
 else ifeq ($(DEMO_NMB), -26)
  ELF_NAME := 20-29
 else ifeq ($(DEMO_NMB), 27)
  ELF_NAME := 20-29
 else ifeq ($(DEMO_NMB), -27)
  ELF_NAME := 20-29
 else ifeq ($(DEMO_NMB), 28)
  ELF_NAME := 20-29
 else ifeq ($(DEMO_NMB), -28)
  ELF_NAME := 20-29
 else ifeq ($(DEMO_NMB), 29)
  ELF_NAME := 20-29
 else ifeq ($(DEMO_NMB), -29)
  ELF_NAME := 20-29
 else
  ELF_NAME := $(DEMO_NMB)
 endif
  ELF  := Demo_$(ELF_NAME)_SMP_$(BOARD_ID)_A9_ARMCC.axf
  MAP  := Demo_$(ELF_NAME)_SMP_$(BOARD_ID)_A9_ARMCC.map
endif

OBJ    := $(patsubst %.c,%.o,$(C_SRC))
SOBJ   := $(patsubst %.s,%.o,$(S_SRC))

.PHONY: all
all: $(ELF)
	@echo "Done"

.PHONY: clean
clean:
	-$(RM) *.d *.o *.axf *.map *.lst

$(OBJ): %.o: %.c Makefile ../Common_ARMCC.make ../$(MAKE_NAME) $(C_INC)
	$(CC) $(CFLAGS) -c -o $(notdir $(basename $<)).o $<

$(SOBJ): %.o: %.s Makefile ../Common_ARMCC.make ../$(MAKE_NAME)
	$(AS) $(AFLAGS) -o $(notdir $(basename $<)).o $<

$(ELF): $(SOBJ) $(OBJ) $(O_SRC) Makefile ../Common_ARMCC.make ../$(MAKE_NAME) $(LDEP) ../$(LNK_SCRIPT)
	$(LD) --cpu=Cortex-A9 --fpu=VFPv3 --entry=Reset_Handler --scatter=../$(LNK_SCRIPT)				\
          --map --info=sizes --info=totals --info=unused --symbols  --datacompressor off			\
          --diag_suppress 6314 -o $(notdir $@) $(notdir $(SOBJ)) $(notdir $(OBJ)) $(O_SRC) $(LIBS)	\
          > $(MAP)

#EOF
