#########################################################################
#
# MODULE:   JN-AN-1168-Smart-Lamp-Test-Application 
#
# DESCRIPTION: Bulb MakeFile
#
############################################################################
# 
# This software is owned by NXP B.V. and/or its supplier and is protected
# under applicable copyright laws. All rights are reserved. We grant You,
# and any third parties, a license to use this software solely and
# exclusively on NXP products [NXP Microcontrollers such as JN5148, JN5142, JN5139].
# You, and any third parties must reproduce the copyright and warranty notice
# and any other legend of ownership on each copy or partial copy of the
# software.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# Copyright NXP B.V. 2012. All rights reserved
#
#
#############################################################################

# Application target name

TARGET = Smart_Lamp

#  Note: Target name must be the same as the subfolder name
##############################################################################
#User definable make parameters that may be overwritten from the command line

# Default target device is the JN5168

JENNIC_CHIP ?= JN5168

##############################################################################
# Default DK4 development kit target hardware
JENNIC_PCB ?= DEVKIT4

##############################################################################
# Select the network stack (e.g. MAC, ZBPRO)

JENNIC_STACK ?= MAC


##############################################################################
# Select the SDK 

JENNIC_SDK ?= JN-SW-4168

##############################################################################
# Select the MAC 

JENNIC_MAC ?= MAC

##############################################################################
# Debug options define DEBUG for HW debug
#DEBUG ?=HW
#
#
# Define which UART to use for debug
#DEBUG_PORT ?= UART1

CFLAGS += -DEMBEDDED
CFLAGS += -DUSER_VSR_HANDLER

##############################################################################
# Define TRACE to use with DBG module
TRACE ?=1


# Default driver device is DR1175 (see eclipse build configs for others)
DR   ?= DR1175
TYPE ?= MONO


#The DR1221 optionally incorporates optional NE555TIMER 

ifeq ($(DR),DR1221)
ifeq ($(OPT1),SW555)
CFLAGS += -D$(OPT1)
VARIANT := _SW555
endif
endif

#The DR1221-69 driver incorporates optional NE555TIMER HW & CCT 

ifeq  ($(DR),DR1221_69)
CFLAGS += -DDEBUG_DRIVER
OPT1 ?=NULL
OPT2 ?=NULL
CFLAGS += -D$(OPT1)
CFLAGS += -D$(OPT2)
ifeq ($(OPT1),CCT)
ifeq ($(OPT2),VAUX)
VARIANT := _CCT_VAUX
else
VARIANT := _CCT
endif
else
ifeq ($(OPT2),VAUX)
VARIANT := _VAUX
endif
endif
endif

# DR1175 (DK4) can be built for both  colour and monochrome bulbs
ifeq ($(DR),DR1175)
CFLAGS += -D$(TYPE)
ifeq ($(TYPE),RGB)
VARIANT := _RGB
endif
endif

ifeq  ($(DR),DR1221_Dimic)
OPT1 ?=NULL
OPT2 ?=NULL
CFLAGS += -D$(OPT1)
CFLAGS += -D$(OPT2)
endif

LINEAR_MODE ?= FALSE
ifeq ($(LINEAR_MODE),TRUE)
CFLAGS += -DLINEAR_MODE
VARIANT += _LINEAR
endif


# Build Recipes for the RGB driver: 0v1/0v2 SSB PCB use positve PWM 0v3/0v4 use negative (default) 
ifeq ($(DR),DR1223)
VARIANT := _$(REV)
ifeq ($(REV),0v1)
CFLAGS += -DMK_USE_POSITIVE_PWM=TRUE
else
CFLAGS += -DMK_USE_POSITIVE_PWM=FALSE
endif
endif

CFLAGS += -DAN1166
CFLAGS += -D$(DR)
CFLAGS += -DMK_DRIVER_REV_STR='"$(DR)_$(REV)"'

##############################################################################
# Path definitions
# Select definitions for either single or multiple targets

# Use if application directory contains multiple targets
SDK_BASE_DIR                    = ../../../../sdk/$(JENNIC_SDK)
APP_BASE                        = ../..
APP_BLD_DIR                     = $(APP_BASE)/$(TARGET)/Build
APP_SRC_DIR                     = $(APP_BASE)/$(TARGET)/Source
APP_COMMON_SRC_DIR              = $(APP_BASE)/Common/Source
APP_DRIVER_SRC_DIR              = $(APP_BASE)/$(TARGET)/Source/DriverBulb

# Use if application directory contains single target
#SDK_BASE_DIR                   = $(abspath ../../..)
#APP_BASE                       = $(abspath ..)
#APP_BLD_DIR                    = $(APP_BASE)/Build
#APP_SRC_DIR                    = $(APP_BASE)/Source

##############################################################################
# Application Source files

# Note: Path to source file is found using vpath below, so only .c filename is required
APPSRC += Smart_Lamp.c
ifneq ($(DR),DR1223)
APPSRC += DriverBulb_$(DR).c
endif
APPSRC += DriverBulb_Shim.c
APPSRC += AppQueueApi.c


##############################################################################
# Additional Application Source directories
# Define any additional application directories outside the application directory
# e.g. for AppQueueApi
ADDITIONAL_SRC_DIR += $(COMPONENTS_BASE_DIR)/AppQueueApi/Source

##############################################################################
# Standard Application header search paths

INCFLAGS += -I$(APP_SRC_DIR)
INCFLAGS += -I$(APP_SRC_DIR)/..
INCFLAGS += -I$(APP_COMMON_SRC_DIR)
INCFLAGS += -I$(APP_DRIVER_SRC_DIR)

# Application specific include files
INCFLAGS += -I$(COMPONENTS_BASE_DIR)/JennicLogo/Include
INCFLAGS += -I$(COMPONENTS_BASE_DIR)/Utilities/Include
INCFLAGS += -I$(COMPONENTS_BASE_DIR)/AppQueueApi/Include
INCFLAGS += -I$(COMPONENTS_BASE_DIR)/Recal/Include 
INCFLAGS += -I$(COMPONENTS_BASE_DIR)/MicroSpecific/Include 
INCFLAGS += -I$(COMPONENTS_BASE_DIR)/DBG/Include 

##############################################################################
# Application libraries
# Specify additional Component libraries

#APPLIBS += 
APPLIBS += DBG
APPLIBS += Recal

##############################################################################

# You should not need to edit below this line

##############################################################################
##############################################################################
# Configure for the selected chip or chip family

include $(SDK_BASE_DIR)/Chip/Common/Build/config.mk
include $(SDK_BASE_DIR)/Platform/Common/Build/Config.mk
include $(SDK_BASE_DIR)/Stack/Common/Build/Config.mk

##############################################################################

APPOBJS = $(APPSRC:.c=.o)

##############################################################################
# Application dynamic dependencies

APPDEPS = $(APPOBJS:.o=.d)

#########################################################################
# Linker

# Add application libraries before chip specific libraries to linker so
# symbols are resolved correctly (i.e. ordering is significant for GCC)

LDLIBS := $(addsuffix _$(JENNIC_CHIP_FAMILY),$(APPLIBS)) $(LDLIBS)

#########################################################################
# Dependency rules

.PHONY: all clean
# Path to directories containing application source 
vpath % $(APP_SRC_DIR):$(APP_COMMON_SRC_DIR):$(ADDITIONAL_SRC_DIR):$(APP_DRIVER_SRC_DIR)


ifeq ($(DR),DR1223)
all: $(TARGET)_$(JENNIC_CHIP)_$(DR)$(VARIANT).bin lib$(DR).a
else
all: $(TARGET)_$(JENNIC_CHIP)_$(DR)$(VARIANT).bin
endif

-include $(APPDEPS)
%.d:
	rm -f $*.o


%.o: %.S
	$(info Assembling $< ...)
	$(CC) -c -o $(subst Source,Build,$@) $(CFLAGS) $(INCFLAGS) $< -MD -MF $*.d -MP
	@echo

%.o: %.c 
	$(info Compiling $< ...)
	$(CC) -c -o $(subst Source,Build,$@) $(CFLAGS) $(INCFLAGS) $< -MD -MF $*.d -MP
	@echo

ifeq ($(DR),DR1223)
$(TARGET)_$(JENNIC_CHIP)_$(DR)$(VARIANT).elf: $(APPOBJS) $(addsuffix _$(JENNIC_CHIP_FAMILY).a,$(addprefix $(COMPONENTS_BASE_DIR)/Library/lib,$(APPLIBS))) lib$(DR).a
	$(info Linking $@ ...)
	$(CC) -Wl,--gc-sections -Wl,-u_AppColdStart -Wl,-u_AppWarmStart $(LDFLAGS) -T$(LINKCMD) -o $@ $(APPOBJS) -Wl,--start-group  $(addprefix -l,$(LDLIBS)) lib$(DR).a -Wl,--end-group -Wl,-Map,$(TARGET)_$(JENNIC_CHIP)_$(DR)$(VARIANT).map 
	${SIZE} $@
	@echo
else
$(TARGET)_$(JENNIC_CHIP)_$(DR)$(VARIANT).elf: $(APPOBJS) $(addsuffix _$(JENNIC_CHIP_FAMILY).a,$(addprefix $(COMPONENTS_BASE_DIR)/Library/lib,$(APPLIBS))) 
	$(info Linking $@ ...)
	$(CC) -Wl,--gc-sections -Wl,-u_AppColdStart -Wl,-u_AppWarmStart $(LDFLAGS) -T$(LINKCMD) -o $@ $(APPOBJS) -Wl,--start-group  $(addprefix -l,$(LDLIBS)) -Wl,--end-group -Wl,-Map,$(TARGET)_$(JENNIC_CHIP)_$(DR)$(VARIANT).map 
	${SIZE} $@
	@echo
endif

lib$(DR).a: DriverBulb_DR1223.o
	$(AR) rcs $@ $<

$(TARGET)_$(JENNIC_CHIP)_$(DR)$(VARIANT).bin: $(TARGET)_$(JENNIC_CHIP)_$(DR)$(VARIANT).elf 
	$(info Generating binary ...)
	$(OBJCOPY) -S -O binary $< $@
	
#########################################################################

clean:
	rm -f $(APPOBJS) $(APPDEPS) $(TARGET)_$(JENNIC_CHIP)_$(DR)$(VARIANT).bin $(TARGET)_$(JENNIC_CHIP)_$(DR)$(VARIANT).elf $(TARGET)_$(JENNIC_CHIP)_$(DR)$(VARIANT).map

#########################################################################
