cmake_minimum_required(VERSION 3.21)
include($ENV{XMOS_CMAKE_PATH}/xcommon.cmake)
project(app_usb_aud_sy102)

set(APP_HW_TARGET synido.xn)
include(${CMAKE_CURRENT_LIST_DIR}/../deps.cmake)
set(APP_PCA_ENABLE ON)

###=========================================================================###
set(TARGET_HW_PHATEN_GSV2 ON)
set(CODEC_IS_AIC3204 ON)

# Configure host os detection here... (Windows or Non-Windows)
#-----------------------------------------------------------------------------#
# Host OS detection, set HOST_OS_DETECTION to ON

set(HOST_OS_DETECTION ON)
###=========================================================================###
# Customer
if (TARGET_HW_PHATEN_GSV2)
    message("-- Building for PHATEN_GSv2 ---")
    set(TARGET_BOARD PHATEN_GSV2)
    set(APP_HW_TARGET synido.xn)
endif()

# CODEC IC
if (CODEC_IS_AIC3204)
    message("-- DAC: AIC3204 ---")
    set(CODEC_IC CODEC_AIC3204)
endif()

# Host OS detection
if (HOST_OS_DETECTION)
    message("-- Host OS detection (Windows/Non-Windows) enabled ---")
    set(EXTRA_BUILD_FLAGS ${EXTRA_BUILD_FLAGS} -DHOST_OS_DETECTION)
endif()

# Firmware version (maps to BCD_DEVICE_J.M.N in USB descriptor)
set(FW_VER_J 1)
set(FW_VER_M 2)
set(FW_VER_N 1)
set(FW_VERSION "v${FW_VER_J}.${FW_VER_M}.${FW_VER_N}")

set(SW_USB_AUDIO_FLAGS ${EXTRA_BUILD_FLAGS} -Os
                                            -report
                                            -L${CMAKE_CURRENT_LIST_DIR}/../../lib_ex3d/lib_ex3d/lib
                                            -lquadflash
                                            -g
                                            #-fxscope
                                            -DUSB_TILE=tile[0]
                                            -DXUA_QUAD_SPI_FLASH=1
                                            -D${TARGET_BOARD}
                                            -DBCD_DEVICE_J=${FW_VER_J}
                                            -DBCD_DEVICE_M=${FW_VER_M}
                                            -DBCD_DEVICE_N=${FW_VER_N}
                                            -D${CODEC_IC}
                                            -DWINDOWS_OS_DESCRIPTOR_SUPPORT)

set(SW_USB_FACT_FLAGS ${EXTRA_BUILD_FLAGS} -Os
                                            -report
                                            -L${CMAKE_CURRENT_LIST_DIR}/../../lib_ex3d/lib_ex3d/lib
                                            -lquadflash
                                            -g
                                            #-fxscope
                                            -DUSB_TILE=tile[0]
                                            -DXUA_QUAD_SPI_FLASH=1
                                            -D${TARGET_BOARD}
                                            -DBCD_DEVICE_J=5
                                            -DBCD_DEVICE_M=5
                                            -DBCD_DEVICE_N=7
                                            -D${CODEC_IC}
                                            -DWINDOWS_OS_DESCRIPTOR_SUPPORT)



LINK_DIRECTORIES(${CMAKE_CURRENT_LIST_DIR}/../../lib_dnr/lib_dnr)

set(APP_COMPILER_FLAGS_f5_music_uac1 ${SW_USB_AUDIO_FLAGS}       -DI2S_CHANS_DAC=2
                                                        -DI2S_CHANS_ADC=2
                                                        -DAUDIO_CLASS=1
                                                        -DMIN_FREQ=48000
                                                        -DMAX_FREQ=48000
                                                        -DMCLK_441=512*44100
                                                        -DMCLK_48=512*48000
                                                        -DUAC1_MODE=1
                                                        -DF5_MUSIC_UAC1=1
                                                        #-DUSE_EX3D
                                                        -DMIXER=0
                                                        #-ldnr_11ms
                                                        #-DDNR_ENABLE=1
                                                        #-llib_ex3d_all
                                                        -DEQ_EN=1
                                                        -DEX3D_SF_NUM=3
                                                        -DNUM_USB_CHAN_OUT=8
                                                        -DNUM_USB_CHAN_OUT_FS=2
                                                        -DNUM_USB_CHAN_IN=2
                                                        -DSTREAM_FORMAT_OUTPUT_1_RESOLUTION_BITS=16
                                                        -DSTREAM_FORMAT_INPUT_1_RESOLUTION_BITS=16
                                                        -DINPUT_FORMAT_COUNT=1
                                                        -DOUTPUT_FORMAT_COUNT=1
                                                        #-DNUM_EX3D_CHAN_OUT=2
                                                        -DMIN_VOLUME=0xE000
                                                        -DINPUT_VOLUME_CONTROL=0
                                                        -DOUTPUT_VOLUME_CONTROL=0
                                                        #-DDEBUG_MEMORY_LOG_ENABLED=1
                                                        -DXUA_DFU_EN=0
                                                        -DHID_DFU_EN=1
                                                        -DHID_CONTROLS_UAC1=1
                                                        #-DIR_SWITCHING_MODE
                                                        -DHID_CONTROLS=1)



set(APP_COMPILER_FLAGS_fact ${SW_USB_FACT_FLAGS}       -DI2S_CHANS_DAC=2
                                                        -DI2S_CHANS_ADC=0
                                                        -DMIN_FREQ=48000
                                                        -DMAX_FREQ=48000
                                                        -DFACT=1
                                                        #-DUSE_EX3D
                                                        -DMIXER=0
                                                        -DUAC2_MODE=1
                                                        #-ldnr_50ms
                                                        #-llib_ex3d_all
                                                        #-DEQ_EN=1
                                                        #-DEX3D_SF_NUM=3
                                                        -DNUM_USB_CHAN_OUT=2
                                                        -DNUM_USB_CHAN_IN=0
                                                        #-DNUM_EX3D_CHAN_OUT=2
                                                        -DMIN_VOLUME=0xE000
                                                        -DINPUT_VOLUME_CONTROL=0
                                                        -DOUTPUT_VOLUME_CONTROL=0
                                                        -DDEBUG_MEMORY_LOG_ENABLED=1
                                                        -DHID_DFU_EN=1
                                                        -DXUA_DFU_EN=1
                                                        #-DIR_SWITCHING_MODE
                                                        -DHID_CONTROLS=1)



set(APP_COMPILER_FLAGS_f1_music_uac2 ${SW_USB_AUDIO_FLAGS}       -DI2S_CHANS_DAC=2
                                                        -DI2S_CHANS_ADC=2
                                                        -DMIN_FREQ=192000
                                                        -DMAX_FREQ=192000
                                                        -DF1_MUSIC_UAC2=1
                                                        -DSTREAM_FORMAT_OUTPUT_1_RESOLUTION_BITS=24
                                                        -DSTREAM_FORMAT_OUTPUT_2_RESOLUTION_BITS=24
                                                        -DSTREAM_FORMAT_INPUT_1_RESOLUTION_BITS=24
                                                        -DSTREAM_FORMAT_INPUT_2_RESOLUTION_BITS=24
                                                        -DINPUT_FORMAT_COUNT=1
                                                        -DOUTPUT_FORMAT_COUNT=1
                                                        #-DUSE_EX3D
                                                        -DMIXER=0
                                                        -DUAC2_MODE=1
                                                        -ldnr_50ms
                                                        -DDNR_ENABLE=1
                                                        #-llib_ex3d_all
                                                        -DEQ_EN=1
                                                        #-DEX3D_SF_NUM=3
                                                        -DNUM_USB_CHAN_OUT=2
                                                        -DNUM_USB_CHAN_IN=2
                                                        #-DNUM_EX3D_CHAN_OUT=2
                                                        -DMIN_VOLUME=0xE000
                                                        -DINPUT_VOLUME_CONTROL=1
                                                        -DOUTPUT_VOLUME_CONTROL=1
                                                        #-DDEBUG_MEMORY_LOG_ENABLED=1
                                                        -DXUA_DFU_EN=1
                                                        -DHID_DFU_EN=1
                                                        #-DIR_SWITCHING_MODE
                                                        -DHID_CONTROLS=1)


#set(APP_COMPILER_FLAGS_f3_f4_fps_uac2 ${SW_USB_AUDIO_FLAGS}    -DI2S_CHANS_DAC=2
#                                                        -DI2S_CHANS_ADC=2
#                                                        -DMIN_FREQ=48000
#                                                        -DMAX_FREQ=48000
#                                                        -DUSE_EX3D=1
#                                                        -DF3_F4_FPS_UAC2=1
#                                                        -DMIXER=0
#                                                        -DUAC2_MODE=1
#                                                        -ldnr_50ms
#                                                        -llib_ex3d_all
#                                                        -DEQ_EN=1
#                                                        -DDNR_ENABLE=1
#                                                        -DSTREAM_FORMAT_OUTPUT_1_RESOLUTION_BITS=16
#                                                        -DSTREAM_FORMAT_INPUT_1_RESOLUTION_BITS=16
#                                                        -DINPUT_FORMAT_COUNT=1
#                                                        -DOUTPUT_FORMAT_COUNT=1
#                                                        -DEX3D_SF_NUM=3
#                                                        -DNUM_USB_CHAN_OUT=8
#                                                        -DNUM_USB_CHAN_IN=2
#                                                        -DNUM_EX3D_CHAN_OUT=2
#                                                        -DMIN_VOLUME=0xE000
#                                                        -DINPUT_VOLUME_CONTROL=1
#                                                        -DOUTPUT_VOLUME_CONTROL=1
#                                                        #-DDEBUG_MEMORY_LOG_ENABLED=1
#                                                        -DXUA_DFU_EN=1
#                                                        -DHID_DFU_EN=1
#                                                        -DIR_SWITCHING_MODE
#                                                        -DHID_CONTROLS=1)
#
set(APP_COMPILER_FLAGS_f6_f7_fps_uac1 ${SW_USB_AUDIO_FLAGS}  -DI2S_CHANS_DAC=2
                                                        -DI2S_CHANS_ADC=2
                                                        -DAUDIO_CLASS=1
                                                        -DMIN_FREQ=48000
                                                        -DMAX_FREQ=48000
                                                        -DMCLK_441=512*44100
                                                        -DMCLK_48=512*48000
                                                        -DSTREAM_FORMAT_OUTPUT_1_RESOLUTION_BITS=16
                                                        -DSTREAM_FORMAT_INPUT_1_RESOLUTION_BITS=16
                                                        -DINPUT_FORMAT_COUNT=1
                                                        -DOUTPUT_FORMAT_COUNT=1
                                                        -DUAC1_MODE=1
                                                        -DUSE_EX3D=1
                                                        -DF6_F7_FPS_UAC1=1
                                                        -DMIXER=0
                                                        -ldnr_50ms
                                                        -DDNR_ENABLE=1
                                                        -llib_ex3d_all
                                                        -DEQ_EN=1
                                                        -DEX3D_SF_NUM=3
                                                        -DNUM_USB_CHAN_OUT=8
                                                        -DNUM_USB_CHAN_IN=2
                                                        -DNUM_EX3D_CHAN_OUT=2
                                                        -DMIN_VOLUME=0xE000
                                                        -DINPUT_VOLUME_CONTROL=0
                                                        -DOUTPUT_VOLUME_CONTROL=0
                                                        #-DDEBUG_MEMORY_LOG_ENABLED=1
                                                        -DXUA_DFU_EN=1
                                                        -DHID_DFU_EN=1
                                                        -DHID_CONTROLS_UAC1=1
                                                        -DIR_SWITCHING_MODE
                                                        -DHID_CONTROLS=1)

set(APP_INCLUDES src src/core src/extensions src/extensions/ssrc ../../lib_dnr/lib_dnr)
set(XMOS_SANDBOX_DIR ${CMAKE_CURRENT_LIST_DIR}/../..)

XMOS_REGISTER_APP()

###=========================================================================###
# Flash image generation
# Slot assignment (matches MODE_Fxx flag values in audiohw.xc):
#   slot 1 = f3_f4_fps_uac2  (COAX_IN_FLAG  = MODE_F3_F4_FPS_UAC2)
#   slot 2 = f5_music_uac1   (UAC1_IN_FLAG  = MODE_F5_MUSIC_UAC1)   <- factory base
#   slot 3 = f1_music_uac2   (OPT_IN_FLAG   = MODE_F1_MUSIC_UAC2)
#   slot 4 = f6_f7_fps_uac1  (USB_IN_FLAG   = MODE_F6_F7_FPS_UAC1)
###=========================================================================###

set(APP_BIN_DIR   ${CMAKE_CURRENT_LIST_DIR}/bin)
set(APP_BASE      ${PROJECT_NAME})
set(XE_FACT       ${APP_BIN_DIR}/fact/${APP_BASE}_fact.xe)
set(XE_F1         ${APP_BIN_DIR}/f1_music_uac2/${APP_BASE}_f1_music_uac2.xe)
set(XE_F3F4       ${APP_BIN_DIR}/f3_f4_fps_uac2/${APP_BASE}_f3_f4_fps_uac2.xe)
set(XE_F5         ${APP_BIN_DIR}/f5_music_uac1/${APP_BASE}_f5_music_uac1.xe)
set(XE_F6F7       ${APP_BIN_DIR}/f6_f7_fps_uac1/${APP_BASE}_f6_f7_fps_uac1.xe)
set(LOADER_OBJ    ${CMAKE_CURRENT_LIST_DIR}/loader.o)
set(TARGET_XN     ${CMAKE_CURRENT_LIST_DIR}/src/core/synido.xn)

# factory_<project>_<version>.bin  — full factory image (base + 4 upgrade slots)
add_custom_target(factory_bin
    COMMAND ${CMAKE_COMMAND} -E echo "xflash ${XE_FACT} --loader ${LOADER_OBJ} --upgrade 1 ${XE_F3F4} --upgrade 2 ${XE_F5} --upgrade 3 ${XE_F1} --upgrade 4 ${XE_F6F7} -o ${CMAKE_CURRENT_LIST_DIR}/factory_${APP_BASE}_${FW_VERSION}.bin"
    COMMAND xflash ${XE_FACT} --loader ${LOADER_OBJ}
            --upgrade 2 ${XE_F5}
            --upgrade 3 ${XE_F1}
            --upgrade 1 ${XE_F3F4}
            --upgrade 4 ${XE_F6F7}
            -o ${CMAKE_CURRENT_LIST_DIR}/factory_${APP_BASE}_${FW_VERSION}.bin
    #DEPENDS f1_music_uac2 f3_f4_fps_uac2 f5_music_uac1 f6_f7_fps_uac1
    COMMENT "Generating factory image: factory_${APP_BASE}_${FW_VERSION}.bin"
    VERBATIM
)

# update_<project>_<version>.bin  — DFU upgrade package (upgrade slots only)
add_custom_target(update_bin
    COMMAND ${CMAKE_COMMAND} -E echo "xflash --factory-version 15.2 --target-file ${TARGET_XN} --upgrade 1 ${XE_F3F4} --upgrade 2 ${XE_F5} --upgrade 3 ${XE_F1} --upgrade 4 ${XE_F6F7} -o ${CMAKE_CURRENT_LIST_DIR}/update_${APP_BASE}_${FW_VERSION}.bin"
    COMMAND xflash --factory-version 15.2 --target-file ${TARGET_XN}
            --upgrade 2 ${XE_F5}
            --upgrade 3 ${XE_F1}
            --upgrade 1 ${XE_F3F4}
            --upgrade 4 ${XE_F6F7}
            -o ${CMAKE_CURRENT_LIST_DIR}/update_${APP_BASE}_${FW_VERSION}.bin
    #DEPENDS f1_music_uac2 f3_f4_fps_uac2 f5_music_uac1 f6_f7_fps_uac1
    COMMENT "Generating update image: update_${APP_BASE}_${FW_VERSION}.bin"
    VERBATIM
)
