###############################################################################
#
# MODULE:   Makefile
#
# DESCRIPTION: Makefile for the simple test app
# 
###############################################################################
# 
# This software is owned by Jennic 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 Jennic products. 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 "AS IS". JENNIC MAKES NO WARRANTIES, WHETHER
# EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE,
# ACCURACY OR LACK OF NEGLIGENCE. JENNIC SHALL NOT, IN ANY CIRCUMSTANCES,
# BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, SPECIAL,
# INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY REASON WHATSOEVER.
# 
# Copyright Jennic Ltd 2009. All rights reserved
#
###############################################################################
# Subversion variables
# $HeadURL$
# $Revision$
# $LastChangedBy$
# $LastChangedDate$
# $Id$ 
#
###############################################################################

# Application target name

TARGET = ValidImage_TestApp

###############################################################################
#User definable make parameters that may be overwritten from the command line

# Default target device is the JN5172

#JENNIC_CHIP ?= JN5179
JENNIC_CHIP ?= JN5168

###############################################################################
# Default DK2 development kit target hardware

JENNIC_PCB ?= DEVKIT4

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

JENNIC_STACK ?= None

JENNIC_MAC ?= None

JENNIC_SDK ?= JN-SW-4168

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

###############################################################################
# Define TRACE to use with DBG module
#TRACE ?=1
ifeq ($(TRACE), 1)	
CFLAGS  += -DDBG_ENABLE
endif

###############################################################################
# Path definitions

# Use if application directory contains multiple targets
SDK_BASE_DIR        ?= ../../../../sdk/$(JENNIC_SDK)
APP_SRC_DIR 	   	= ../Source
TOOLS_DIR           = ../../Tools

###############################################################################
# Application Source files
APPSRC  = $(APP_SRC_DIR)/ValidImage_TestApp.c
APPSRC += $(APP_SRC_DIR)/Printf.c

###############################################################################
# Standard Application header search paths
INCFLAGS += -I$(APP_SRC_DIR)
INCFLAGS += -I$(SDK_BASE_DIR)/Components/RegisterMap/Include

# Application specific include files


###############################################################################
# Application libraries
# Specify additional Component libraries
#APPLIBS +=


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

# 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)/Stack/Common/Build/config.mk

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

TEMP = $(APPSRC:.c=.o)
APPOBJS = $(TEMP:.S=.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 buildimage

all: $(TARGET)_$(JENNIC_CHIP)$(BIN_SUFFIX).bin

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

%.o: %.S
	$(info Assembling $< ...)
	$(CC) -c -o $@ $(CFLAGS) $(INCFLAGS) $<
	@echo

%.o: %.c 
	$(info Compiling $< ...)
	$(CC) -c -o $@ $(CFLAGS) $(INCFLAGS) $<
	@echo

$(TARGET)_$(JENNIC_CHIP)$(BIN_SUFFIX).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)) -lm -Wl,--end-group -Wl,-Map,$(TARGET)_$(JENNIC_CHIP)$(BIN_SUFFIX).map 
	@echo
	$(OBJDUMP) -d $(TARGET)_$(JENNIC_CHIP)$(BIN_SUFFIX).elf > $(TARGET)_$(JENNIC_CHIP)$(BIN_SUFFIX).dis
	$(info Image size information ...)
	$(SIZE) -B $(TARGET)_$(JENNIC_CHIP)$(BIN_SUFFIX).elf
	@echo

$(TARGET)_$(JENNIC_CHIP)$(BIN_SUFFIX).bin: $(TARGET)_$(JENNIC_CHIP)$(BIN_SUFFIX).elf 
	$(info Generating binary ...)
	$(OBJCOPY) -S -O binary $< $@
	
###############################################################################
buildimage:
		$(TOOLS_DIR)/ImageMerge -a $(TARGET)_$(JENNIC_CHIP)$(BIN_SUFFIX).bin -b $(TARGET)_$(JENNIC_CHIP)$(BIN_SUFFIX).bin -c $(TARGET)_$(JENNIC_CHIP)$(BIN_SUFFIX).bin -d $(TARGET)_$(JENNIC_CHIP)$(BIN_SUFFIX).bin -e $(TARGET)_$(JENNIC_CHIP)$(BIN_SUFFIX).bin -f $(TARGET)_$(JENNIC_CHIP)$(BIN_SUFFIX).bin -g $(TARGET)_$(JENNIC_CHIP)$(BIN_SUFFIX).bin -h $(TARGET)_$(JENNIC_CHIP)$(BIN_SUFFIX).bin -o $(TARGET)_$(JENNIC_CHIP)$(BIN_SUFFIX)_Joined.bin -p 0 -p 1 -p 2 -p 3 -p 4 -p 5 -p 6 -s 	
clean:
	rm -f $(APPOBJS) $(TARGET)_$(JENNIC_CHIP)*.bin $(TARGET)_$(JENNIC_CHIP)$(BIN_SUFFIX).elf $(TARGET)_$(JENNIC_CHIP)$(BIN_SUFFIX).map $(TARGET)_$(JENNIC_CHIP)$(BIN_SUFFIX).dis

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