MKFILES := mk
RTL8710_PATH ?= ./rtl8710_openocd

# build dir
BUILD_DIR ?= build
BIN_DIR := $(BUILD_DIR)/bin

# OTA config
OTA_IDX ?= 1
IMAGE2_OTA ?= image2_all_ota$(OTA_IDX).bin
BOOTLOADER ?= $(BUILD_DIR)/boot/boot_all.o

ifneq ($(OTA_IDX),1)
ifneq ($(OTA_IDX),2)
    $(error OTA_IDX must be "1" or "2")
endif
endif

FLASH_ADDRESS := $(if $(OTA_IDX),0x80000,0x0B000)
FLASH_FILE := $(BIN_DIR)/image2_all_ota$(OTA_IDX).bin

.PHONY: all
all: ram_all

# build targets
.PHONY: ram_all mp clean build_info
ram_all:
	$(MAKE) -f $(MKFILES)/application.mk

mp clean build_info:
	$(MAKE) -f $(MKFILES)/application.mk $@

include $(RTL8710_PATH)/rtl8710-openocd.mk

INTERFACE := jlink
OPENOCD_BASE := openocd -f interface/$(INTERFACE).cfg \
	-f $(RTL8710_SCRIPT_PATH)/rtl8710.ocd \
	-c "init" -c "reset halt"

.PHONY: flash
flash:
	@$(OPENOCD_BASE) \
		-c 'rtl8710_flash_auto_erase 1' \
		-c 'rtl8710_flash_auto_verify 1' \
		-c 'rtl8710_flash_write $(FLASH_FILE) $(FLASH_ADDRESS)' \
		-c 'rtl8710_reboot' \
		-c 'reset run' \
		-c 'shutdown'

# flasher targets
.PHONY: rtl-test rtl-mac rtl-dump rtl-full-erase rtl-restore rtl-restore-nae rtl-verify rtl-reset

export MKFILES OTA_IDX BUILD_DIR BIN_DIR IMAGE2_OTA BOOTLOADER
