TARGET = spiboot_pcieinit.spi.dat


HEX6X = $(CGT_INSTALL_DIR)/bin/hex6x
B2I2C = $(PDK_INSTALL_PATH)/ti/boot/ibl/src/util/btoccs/b2i2c
B2CCS = $(PDK_INSTALL_PATH)/ti/boot/ibl/src/util/btoccs/b2ccs
ROMPARSE = $(PDK_INSTALL_PATH)/ti/boot/ibl/src/util/romparse/romparse
CCS2BIN = $(PDK_INSTALL_PATH)/ti/boot/ibl/src/util/btoccs/ccs2bin
BYTESWAPCCS = $(REPOSITORY_ROOT)/byteswapccs/byteswapccs


%.spi.dat: %.spi.dat_le
	@echo "Swapping bytes of CCS data file."
	@$(BYTESWAPCCS) $< $@

%.spi.dat_le: %.spi.map
	@echo "Adding boot parameter table to to CCS data format."
	@$(ROMPARSE) $<
	@awk 'NR==9 { sub("51$$","00") } { print }' i2crom.ccs > $@
	@rm i2crom.ccs

%.spi.map: %.spi.ccs
	@echo "Creating boot parameter table."
	@echo -e "section { \n\
	    boot_mode = 50 \n\
	    param_index = 0 \n\
	    options = 1 \n\
	    core_freq_mhz = 1000 \n\
	    exe_file = \"$<\" \n\
	    next_dev_addr_ext = 0x0 \n\
	    sw_pll_prediv = 5 \n\
	    sw_pll_mult = 32 \n\
	    sw_pll_postdiv = 2 \n\
	    sw_pll_flags = 1 \n\
	    addr_width = 24 \n\
	    n_pins = 4 \n\
	    csel = 0 \n\
	    mode = 0 \n\
	    c2t_delay = 0 \n\
	    bus_freq_mhz = 5 \n\
	    bus_freq_khz = 0 \n\
	}" > $@

%.spi.ccs: %.spi
	@echo "Converting I2C/SPI format to CCS data format."
	@$(B2CCS) $< $@

%.spi: %.btbl
	@echo "Converting boot table to I2C/SPI format."
	@$(B2I2C) $< $@

%.btbl: %.out
	@echo "Generating boot table from LE binary."
	@$(HEX6X) -a -order L --memwidth 32 --romwidth 32 -boot -e _c_int00 -bootorg 0x800000 \
		$< -o $@

all: $(TARGET)

clean:
	@rm -rf *.btbl
	@rm -rf *.spi
	@rm -rf *.spi.ccs
	@rm -rf *.spi.map
	@rm -rf *.spi.dat_le
	@rm -rf *.spi.dat

