# Copyright (C) 2023 Advanced Micro Devices, Inc.  All rights reserved.
# Copyright (c) 2022 Xilinx, Inc.  All rights reserved.
# SPDX-License-Identifier: MIT
cmake_minimum_required(VERSION 3.14.7)
include(${CMAKE_CURRENT_SOURCE_DIR}/UsbExample.cmake)
project(usb)

find_package(common)
enable_language(C ASM)
collect(PROJECT_LIB_DEPS xilstandalone)
collect(PROJECT_LIB_DEPS xil)
collect(PROJECT_LIB_DEPS xiltimer)
collect(PROJECT_LIB_DEPS gcc)
collect(PROJECT_LIB_DEPS c)
collector_list (_deps PROJECT_LIB_DEPS)

SET(DRIVER_INSTANCES "${NUM_DRIVER_INSTANCES}" CACHE STRING "Driver Instances")
SET_PROPERTY(CACHE DRIVER_INSTANCES PROPERTY STRINGS "${NUM_DRIVER_INSTANCES}")
set(index 0)
LIST_INDEX(${index} ${DRIVER_INSTANCES} "${NUM_DRIVER_INSTANCES}")
list(GET TOTAL_EXAMPLE_LIST ${index} ex_list)
list(GET REG_LIST ${index} reg)
SET(XUSB_BASEADDRESS "${reg}")

SET(MASS_STORAGE_DEVICE 1)
SET(MICROPHONE 0)
SET(USB_KEYBOARD 0)
SET(USB_MOUSE 0)

if(USB_KEYBOARD OR USB_MOUSE)
	SET(HID_DEVICES 1)
endif()

configure_file(${CMAKE_SOURCE_DIR}/xusb_example_config.h.in ${CMAKE_SOURCE_DIR}/xusb_example_config.h)

if(MASS_STORAGE_DEVICE)
	SET(xusb_phy_read_write_example xusb_phy_read_write.c;)
	list(APPEND EXAMPLE_LIST xusb_phy_read_write_example)
	SET(xusb_dma_intr_storage_example xusb_dma_intr_storage.c;xusb_cp9.c;)
	list(APPEND EXAMPLE_LIST xusb_dma_intr_storage_example)
	SET(xusb_storage_example xusb_storage.c;xusb_cp9.c;)
	list(APPEND EXAMPLE_LIST xusb_storage_example)
	SET(xusb_storage_polled_mode_example xusb_storage_polled_mode.c;xusb_cp9.c;)
	list(APPEND EXAMPLE_LIST xusb_storage_polled_mode_example)
elseif(MICROPHONE)
	SET(xusb_microphone_example  xusb_microphone.c;xusb_cp9.c;)
	list(APPEND EXAMPLE_LIST xusb_microphone_example)
elseif(HID_DEVICES AND USB_KEYBOARD)
	SET(xusb_keyboard_example xusb_keyboard.c;xusb_cp9.c;)
	list(APPEND EXAMPLE_LIST xusb_keyboard_example)
elseif(HID_DEVICES AND USB_MOUSE)
	SET(xusb_mouse_example  xusb_mouse.c;xusb_cp9.c;)
	list(APPEND EXAMPLE_LIST xusb_mouse_example)
endif()

set(CMAKE_INFILE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../../scripts/linker_files/")
linker_gen(${CMAKE_INFILE_PATH})
gen_exheader(${CMAKE_INFILE_PATH} ${CMAKE_PROJECT_NAME} ${XUSB_BASEADDRESS} x)

foreach(EXAMPLE ${EXAMPLE_LIST})
    message("example ")
    message("${${EXAMPLE}}")
    add_executable(${EXAMPLE}.elf ${${EXAMPLE}})
    target_link_libraries(${EXAMPLE}.elf -Wl,--gc-sections -T\"${CMAKE_SOURCE_DIR}/lscript.ld\" -L\"${CMAKE_SOURCE_DIR}/\" -Wl,--start-group ${_deps} -Wl,--end-group)
endforeach()
