##******************************* Makefile *********************************
##           #######
##           ##  ##
##           #  ##    ####   #####    #####  ##  ##   #####
##             ##    ##  ##  ##  ##  ##      ##  ##  ##
##            ##  #  ######  ##  ##   ####   ##  ##   ####
##           ##  ##  ##      ##  ##      ##   #####      ##
##          #######   ####   ##  ##  #####       ##  #####
##                                           #####
##          Z-Wave, the wireless language.
##
##              Copyright (c) 2001
##              Zensys A/S
##              Denmark
##
##              All Rights Reserved
##
##    This source file is subject to the terms and conditions of the
##    Zensys Software License Agreement which restricts the manner
##    in which it may be used.
##
##---------------------------------------------------------------------------
##
## Description:
##    Makefile for the ZW0x0x development kit SerialAPI
##    This makefile defines the targets built
##
## Author:   Erik Friis Harck
##
## Last Changed By:  $Author: efh $
## Revision:         $Revision: 22708 $
## Last Changed:     $Date: 2012-05-03 16:41:51 +0200 (Thu, 03 May 2012) $
##
##***************************************************************************/

#---------------------------------------------------------------------------------------------------------
# HOW-TO: Specifying which subset of "everything", you wan't to build:
#
# You can specify a single frequency to build for in 3 different ways:
# 1. By specifying the frequency in your command line, like:
#    > make FREQUENCY=EU ....
# 2. By uncommenting and correcting the following line:
#FREQUENCY:=US
# 3. Alternativly you can do the same by setting your environment with:
#    > SET FREQUENCY=EU
#    Remember to UNSET this when you jump to work on other things.
#
# You can specify a single code memory mode to build for in 3 different ways:
# 1. By specifying the code memory mode in your command line, like:
#    > make CODE_MEMORY_MODE=devmode_patch ....
# 2. By uncommenting and correcting the following line:
#CODE_MEMORY_MODE:=normal
# 3. Alternativly you can do the same by setting your environment with:
#    > SET CODE_MEMORY_MODE=normal
#    Remember to UNSET this when you jump to work on other things.
#
# You can specify a single library to build for in 3 different ways:
# 1. By specifying the library in your command line, like:
#    > make LIBRARY=controller_portable ....
# 2. By uncommenting and correcting the following line:
#LIBRARY:=controller_static
# 3. Alternativly you can do the same by setting your environment with:
#    > SET LIBRARY=controller_portable
#    Remember to UNSET this when you jump to work on other things.
#
# You can specify a single host interface to build for in 3 different ways:
# 1. By specifying the host interface in your command line, like:
#    > make HOST_INTERFACE=UART ....
# 2. By uncommenting and correcting the following line:
#HOST_INTERFACE:=UART
# 3. Alternativly you can do the same by setting your environment with:
#    > SET HOST_INTERFACE=UART
#    Remember to UNSET this when you jump to work on other things.
#
# You can specify a single sensor type to build for in 3 different ways:
# 1. By specifying the sensor type in your command line, like:
#    > make SENSOR_TYPE=NON_BATT ....
# 2. By uncommenting and correcting the following line:
SENSOR_TYPE:=NON_BATT
# 3. Alternativly you can do the same by setting your environment with:
#    > SET SENSOR_TYPE=NON_BATT
#    Remember to UNSET this when you jump to work on other things.
#
# You can specify a single board type to build for in 3 different ways:
# 1. By specifying the sensor type in your command line, like:
#    > make BOARD=ZDP03A .... UZB
# 2. By uncommenting and correcting the following line:
#BOARD=ZDP03A
# 3. Alternativly you can do the same by setting your environment with:
#    > SET BOARD=ZDP03A
#    Remember to UNSET this when you jump to work on other things.
#
# And yes... You can combine these methods in any way.
#
# If you do nothing of the above, you will be building target code for all combinations of frequencies and
# code memory modes in the lists below.
#
# And yes... You can of course manipulate the lists below, if you have special needs for ranges of targets.
#---------------------------------------------------------------------------------------------------------

# Full list of all supported frequencies can be found in $(ZWLIBROOT)\Common\Makefile.common
# Downgraded list of supported frequencies for SerialAPI.
LIST_OF_FREQUENCIES:=US EU ANZ HK MY IN JP

# Full list of all supported libraries can be found in $(ZWLIBROOT)\Common\Makefile.common
# Downgraded list of supported libraries for SerialAPI.
LIST_OF_LIBRARIES:=\
 controller_bridge\
 controller_installer\
 controller_portable\
 controller_static\
 controller_static_norep\
 controller_static_single\
 slave_enhanced\
 slave_enhanced_232\
 slave_routing

# Full list of all supported host interfaces can be found in $(ZWLIBROOT)\Common\Makefile.common
# Downgraded list of supported host interfaces for SerialAPI.
LIST_OF_HOST_INTERFACES:=UART USBVCP

#---------------------------------------------------------------------------------------------------------
# Skip the building for the following libraries for old-fashioned devmode
# during bunch-build, because of lack of code space.
ifeq ($(LIBRARY),controller_bridge)
LIST_OF_CODE_MEMORY_MODES:=normal starter_devmode starter_devmode_patch
endif

ifeq ($(LIBRARY),controller_static)
LIST_OF_CODE_MEMORY_MODES:=normal starter_devmode starter_devmode_patch
endif

ifeq ($(LIBRARY),controller_static_norep)
LIST_OF_CODE_MEMORY_MODES:=normal starter_devmode starter_devmode_patch
endif

ifeq ($(LIBRARY),controller_installer)
LIST_OF_CODE_MEMORY_MODES:=normal starter_devmode starter_devmode_patch
endif

# If building for the ZM4102 module, only make a very limited subset.
ifeq ($(BOARD),ZM4102)
LIST_OF_HOST_INTERFACES:=UART
LIST_OF_LIBRARIES:=controller_static
LIST_OF_CODE_MEMORY_MODES:=normal starter_devmode starter_devmode_patch
endif

# If building for the UZB USB dongle, only make a very limited subset.
ifeq ($(BOARD),UZB)
LIST_OF_HOST_INTERFACES:=USBVCP
LIST_OF_LIBRARIES:=controller_static
LIST_OF_CODE_MEMORY_MODES:=normal
endif

#---------------------------------------------------------------------------------------------------------

# If not supplied by application here, the name of the application will be set equal to it's directory name.
APP_NAME:=serialapi

# If building for the ZM4102 module, make special selections for LED1, SPI memory and UART
ifeq ($(BOARD),ZM4102)
## Set the driver for external serial non-volatile memory for use of P0.4 as chip select.
NVM_TYPE:=at25128a_p0_4
CHIPPACK:=ZM4102
# If building for the UZB USB dongle, only make a very limited subset.
else
 ifeq ($(BOARD),UZB)
CHIPPACK:=UZB
 else
# Else we are building for the normal general ZW040x chip.
CHIPPACK:=$(CHIP)
 endif
endif

ifneq ($(IF),)
IFNAME:=_$(IF)
endif

# Target name (Name of your target directory and base name of your target files)
TARGET:=$(APP)_$(LIB)_$(CHIPPACK)$(IFNAME)_$(COUNTRY)$(TEST)

#---------------------------------------------------------------------------------------------------------

ifeq ($(CODE_MEMORY_MODE),banking)
# List ZW_library modules, which you want forced into COMMON bank
COMMON_ZWLIB_MODULES:=

# List ZW_library modules, which you want forced into BANK1
BANK1_ZWLIB_MODULES:=\
 ZW_DEBUG,\
 ZW_MEM,\
 ZW_MEM_SUPPORT,\
 ZW_MEM_API,\
 ZW_UART0_SUPPORT,\
 ZW_UART1_SUPPORT,\
 ZW_ROUTING,\
 ZW_TRANSPORT,\
 ZW_TRANSPORT_SUPPORT,\
 ZW_TRANSPORT_TIMER,\
 ZW_VERSION,\
 ZW_PHY_LED_040X,\
 ZW_EVENT

# List ZW_library modules, which you want forced into BANK2
BANK2_ZWLIB_MODULES:=

# List ZW_library modules, which you want forced into BANK3
BANK3_ZWLIB_MODULES:=
endif

# List of object modules constituting the program for the different code memory modes.

ifeq ($(CODE_MEMORY_MODE),banking)
# build the project the original way for OTP. "serialapi"
COMMON_RELFILES:=

COMMON_RELFILES+=\
 ZW_L51_BANK.obj\
 App_RFSetup.obj

BANK1_RELFILES:=

BANK2_RELFILES:=\
 serialappl.obj\
 conhandle.obj\
 led_control.obj

BANK3_RELFILES:=

RELFILES:=$(COMMON_RELFILES) $(BANK1_RELFILES) $(BANK2_RELFILES) $(BANK3_RELFILES)

endif

ifeq ($(CODE_MEMORY_MODE),normal)
# build the project the original way for OTP. "serialapi"
RELFILES:=\
 serialappl.obj\
 conhandle.obj\
 led_control.obj\
 App_RFSetup.obj
endif

ifeq ($(CODE_MEMORY_MODE),devmode)
# build the project for development mode for OTP. "serialapi_devmode"
RELFILES:=\
 serialappl.obj\
 conhandle.obj\
 led_control.obj\
 App_RFSetup.obj
endif

ifeq ($(CODE_MEMORY_MODE),devmode_patch)
# build patch code for development mode RAM. "serialapi_devmode_patch" (all patched modules)
RELFILES:=\
 serialappl_patch.obj
endif

ifeq ($(CODE_MEMORY_MODE),starter_devmode)
# build the project for development mode for OTP. "starter_devmode" (empty program with library)
RELFILES:=\
 starter.obj\
 conhandle.obj\
 led_control.obj\
 App_RFSetup.obj
endif

ifeq ($(CODE_MEMORY_MODE),starter_devmode_patch)
# build patch code for development mode RAM. "starter_devmode_serialapi_patch" (all application except library)
RELFILES:=\
 serialappl_patch.obj\
 starter_patch.obj
endif

#---------------------------------------------------------------------------------------------------------

# Find the current directory
# used as a base for all other paths.
BASEDIR=$(shell cd)

# Find the product name from the subdirectoryname
BASEDIRWORDLIST=$(subst \, ,$(BASEDIR))
BASEDIRLASTWORDINDEX=$(words $(BASEDIRWORDLIST))
PRODUCTDIR=$(word $(BASEDIRLASTWORDINDEX),$(BASEDIRWORDLIST))

# Find the Z-Wave library location (if not forced by setting the environment variable ZWLIBROOT)
ifndef ZWLIBROOT
ZWLIBROOT:=$(subst \$(PRODUCTDIR),,$(subst \Product,\Z-Wave,$(BASEDIR)))
endif

# Library location when making release of application
ifdef SVNLIBRELEASEDIRBIN
ZWLIBROOT:=$(SVNLIBRELEASEDIRBIN)
endif

# Export the variables declared above to the other makefiles.
export BASEDIR ZWLIBROOT UNDERSTAND_C

include $(ZWLIBROOT)\Common\Makefile.common

UVBATHEADERFILES:=
UVBATHEADERFILESPLUS:=
# "uVision" project creation is optional.
# use "make UVISION=1 ZWxxxxx...."
ifeq ($(UVISION),1)

# If we are building a patch, then don't generate lib_supported_func.h
ifeq ($(PATCH),)
UVBATHEADERFILES:=$(LIB)_supported_func.huvbat
UVBATHEADERFILESPLUS:=$(UVBATHEADERFILES) +
endif

$(LIB)_supported_func.huvbat: $(LIB)_supported_func.h
	@echo $(UVISIONPROJECTGENERATOR) TARGET="$(TARGET)" FREQUENCY="$(FREQUENCY)" RELSDIR="$(RELSDIR)" LISTDIR="$(LISTDIR)" CREATE_SUPPORTED_FUNC="$(LIBFILE)"  > $@

endif

-include Makefile.common_ZW0x0x_supported_functions

# Generate SerialAPI defines for supported functions based on what exists in library.
lib_supported_func.h: $(LIB)_supported_func.h $(UVBATHEADERFILES)
	@echo #include "$(LIB)_supported_func.h" > $(subst \,\\,$(RELSDIRFULL)\lib_supported_func.h)
$(LIB)_supported_func.h: $(LIBFILE)
	make-supported-functions-include.bat $(subst \,\\,$(LIBFILE)) $(subst \,\\,$(TARGETBASEDIRFULL)\$(LIB)_supported_func.h)

# Make sure lib_supported_func.h is generated before we generate dependencies
Makefile.d: lib_supported_func.h

CDEFINES+=,\
 ZW_ID_SERVER,\
 SUC_SUPPORT

CDEFINES+=,\
 $(BOARD)

ifeq ($(IF),USBVCP)
CDEFINES+=,\
 $(IF)
endif

## Missing C defines common for all ZW0102 based controller libraries should be defined below
CDEFINES+=,\
 ZW_EXPLORE

# ifeq ("$(APP)$(CHIP)", "bridgeZW040x")
# CDEFINES+=,REDUCE_BRIDGE
# else
ifeq ("$(APP)$(CHIP)", "bridgeZW030x")
CDEFINES+=,REDUCE_BRIDGE
else
ifeq ("$(APP)$(CHIP)", "bridgeZW020x")
CDEFINES+=,REDUCE_BRIDGE
else
ifeq ("$(APP)", "controller_single")
else
CDEFINES+=,ENABLE_LEDS
endif
endif
endif
# endif

ifeq ($(BOARD),ZM4102)
CDEFINES+=,\
 ZM4102
ADEFINES+=,\
 ZM4102
endif
