# -*- mode: makefile -*-
# This file is executed inside Docker build container.
# It can be used without container too if SDK_PATH is configured.

PLATFORM = CC3200
SDK_PATH ?= /cc3200-sdk
REPO_PATH ?= ../../..
BUILD_DIR ?= ./.build
FW_DIR ?= ./out
SLFS_PATH ?= ./slfs

BINDIR = $(FW_DIR)
OBJDIR = $(BUILD_DIR)

include $(SDK_PATH)/tools/gcc_scripts/makedefs

.PHONY: all clean flash

PROG = cc3200_example

IPATH = . ../.. $(REPO_PATH)

VPATH = ../..

MONGOOSE_FEATURES = -DMG_ENABLE_SSL -DMG_ENABLE_HTTP_STREAMING_MULTIPART

SDK_FLAGS = -DUSE_FREERTOS -DSL_PLATFORM_MULTI_THREADED
# -DTARGET_IS_CC3200 would reduce code size by using functions in ROM
# but then the code won't work on pre-release chips (XCC3200HZ).

CFLAGS += -Os -Wall -Werror \
          $(SDK_FLAGS) -DCS_PLATFORM=4 -DCC3200_FS_SLFS \
          $(MONGOOSE_FEATURES) $(CFLAGS_EXTRA)

FW_ELF = $(FW_DIR)/$(PROG).axf
FW_BIN = $(FW_DIR)/$(PROG).bin
FW_MANIFEST = $(FW_DIR)/manifest.json
FW_ZIP = $(FW_DIR)/firmware.zip
BUILD_INFO_JSON = $(OBJDIR)/build_info.json
SLFS_FILES = $(wildcard $(SLFS_PATH)/*)

.PHONY: all clean flash

all: $(OBJDIR) $(FW_DIR) $(FW_ZIP)

clean:
	@rm -rf $(OBJDIR) $(wildcard *~)
	@rm -rf $(FW_DIR) $(wildcard *~)

$(OBJDIR):
	@echo "  MKDIR $@"
	@mkdir -p $(OBJDIR) $(FS_BUILD_DIR)

$(FW_DIR):
	@echo "  MKDIR $@"
	@mkdir -p $(FW_DIR)

$(FW_ZIP): $(FW_ELF) $(FW_BIN) $(SLFS_FILES)
	@echo "    Code size: $(shell ls -l $(FW_BIN) | awk '{print $$5}')"
	@echo "  GEN   $(FW_MANIFEST)"
	@fw_meta gen_build_info \
	  --json_output=$(BUILD_INFO_JSON)
	@cp -v $(SLFS_FILES) out/
	@cp $(CC3200_SP_FILE)* $(FW_DIR)
	@fw_meta create_manifest \
	  --name=$(PROG) --platform=$(PLATFORM) \
	  --build_info=$(BUILD_INFO_JSON) \
	  --output=$(FW_MANIFEST) \
	  --src_dir=$(FW_DIR) \
	  /sys/servicepack.ucf:type=slfile,src=$(CC3200_SP_FILE),falloc=60416,sign=$(notdir $(CC3200_SP_FILE)).sign \
	  $(notdir $(CC3200_SP_FILE)).sign:type=signature,src=$(CC3200_SP_FILE).sign \
	  /sys/mcuimg.bin:type=app,src=$(notdir $(FW_BIN)) \
	  $(foreach f,$(SLFS_FILES), $(notdir $(f)):type=slfile,src=$(notdir $(f)))
	@echo "  ZIP   $@"
	@fw_meta create_fw \
	  --manifest=$(FW_MANIFEST) \
	  --src_dir=$(FW_DIR) \
	  --output=$@

FREERTOS_SRCS = timers.c list.c queue.c tasks.c port.c heap_3.c osi_freertos.c
DRIVER_SRCS = cpu.c gpio.c gpio_if.c i2c.c i2c_if.c interrupt.c pin.c prcm.c spi.c uart.c udma.c utils.c
SL_SRCS = socket.c wlan.c driver.c device.c netapp.c netcfg.c network_common.c cc_pal.c fs.c
SDK_SRCS = startup_gcc.c $(FREERTOS_SRCS) $(DRIVER_SRCS) $(SL_SRCS)
IPATH += $(SDK_PATH) $(SDK_PATH)/inc $(SDK_PATH)/driverlib \
         $(SDK_PATH)/example/common $(SDK_PATH)/oslib \
         $(SDK_PATH)/simplelink $(SDK_PATH)/simplelink/include \
         $(SDK_PATH)/simplelink_extlib/provisioninglib \
         $(SDK_PATH)/third_party/FreeRTOS/source \
         $(SDK_PATH)/third_party/FreeRTOS/source/include \
         $(SDK_PATH)/third_party/FreeRTOS/source/portable/GCC/ARM_CM4
VPATH += $(SDK_PATH)/driverlib $(SDK_PATH)/example/common $(SDK_PATH)/oslib \
         $(SDK_PATH)/simplelink $(SDK_PATH)/simplelink/source \
         $(SDK_PATH)/third_party/FreeRTOS/source \
         $(SDK_PATH)/third_party/FreeRTOS/source/portable/GCC/ARM_CM4 \
         $(SDK_PATH)/third_party/FreeRTOS/source/portable/MemMang \

APP_SRCS = main.c bm222.c data.c mongoose.c tmp006.c wifi.c $(SDK_SRCS)
APP_OBJS = $(addprefix $(OBJDIR)/,$(patsubst %.c,%.o,$(APP_SRCS)))

$(FW_ELF): $(APP_OBJS)

SCATTERgcc_$(PROG) = $(PROG).ld
ENTRY_$(PROG) = ResetISR

# Disable certain warnings on SDK sources, we have no control over them anyway.

SDK_OBJS = $(addprefix $(OBJDIR)/,$(patsubst %.c,%.o,$(SDK_SRCS)))
$(SDK_OBJS): CFLAGS += -include mongoose.h -Wno-missing-braces -Wno-strict-aliasing -Wno-parentheses -Wno-unused-variable
