ifdef CONFIG_ESP32_PHY_INIT_DATA_IN_PARTITION

ESP_WIFI_COMPONENT_PATH := $(COMPONENT_PATH)

ifdef CONFIG_ESP32_SUPPORT_MULTIPLE_PHY_INIT_DATA_BIN
PHY_INIT_DATA_BIN = $(ESP_WIFI_COMPONENT_PATH)/phy_multiple_init_data.bin
else
PHY_INIT_DATA_OBJ = $(BUILD_DIR_BASE)/phy_init_data.o
PHY_INIT_DATA_BIN = $(BUILD_DIR_BASE)/phy_init_data.bin

$(PHY_INIT_DATA_OBJ): $(ESP_WIFI_COMPONENT_PATH)/$(IDF_TARGET)/include/phy_init_data.h $(BUILD_DIR_BASE)/include/sdkconfig.h
	$(summary) CC $(notdir $@)
	printf "#include \"phy_init_data.h\"\n" | $(CC) -I $(BUILD_DIR_BASE)/include -I $(ESP_WIFI_COMPONENT_PATH)/../esp_common/include -I $(ESP_WIFI_COMPONENT_PATH)/include -I $(ESP_WIFI_COMPONENT_PATH)/$(IDF_TARGET)/include -c -o $@ -xc -

$(PHY_INIT_DATA_BIN): $(PHY_INIT_DATA_OBJ)
	$(summary) BIN $(notdir $@)
	$(OBJCOPY) -O binary $< $@
endif

# Command to flash PHY init data partition
PHY_INIT_DATA_FLASH_CMD = $(ESPTOOLPY_SERIAL) write_flash $(PHY_DATA_OFFSET) $(PHY_INIT_DATA_BIN)
ESPTOOL_ALL_FLASH_ARGS += $(PHY_DATA_OFFSET) $(PHY_INIT_DATA_BIN)

phy_init_data: $(PHY_INIT_DATA_BIN)

phy_init_data-flash: $(PHY_INIT_DATA_BIN)
	@echo "Flashing PHY init data..."
	$(PHY_INIT_DATA_FLASH_CMD)

ifndef CONFIG_ESP32_SUPPORT_MULTIPLE_PHY_INIT_DATA_BIN
phy_init_data-clean:
	rm -f $(PHY_INIT_DATA_BIN) $(PHY_INIT_DATA_OBJ)

clean: phy_init_data-clean
endif

all: phy_init_data
flash: phy_init_data

endif # CONFIG_ESP32_PHY_INIT_DATA_IN_PARTITION
