cmake_minimum_required(VERSION 3.22)
project(iap_demo)
enable_language(C ASM)

# Setup compiler settings
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS ON)

# Define the build type
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE "Debug")
endif()

# Include toolchain file
include("gcc-arm-none-eabi.cmake")

# Enable compile command to ease indexing with e.g. clangd
set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE)
message("Build type: " ${CMAKE_BUILD_TYPE})

add_executable(iap_demo)

target_sources(iap_demo PRIVATE

        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/common/cpu_endian.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/common/os_port_none.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/common/date_time.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/common/str.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/common/path.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/common/resource_manager.c

        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_adc.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_adc_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_cec.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_comp.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_cordic.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_cortex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_crc.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_crc_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_cryp.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_cryp_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_dac.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_dac_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_dcache.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_dcmi.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_dma.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_dma_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_dts.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_eth.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_eth_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_exti.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_fdcan.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_flash.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_flash_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_fmac.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_gpio.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_gtzc.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_hash.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_hcd.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_i2c.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_i2c_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_i2s.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_i2s_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_i3c.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_icache.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_irda.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_iwdg.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_lptim.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_mmc.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_mmc_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_nand.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_nor.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_opamp.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_opamp_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_otfdec.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_pcd.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_pcd_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_pka.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_pssi.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_pwr.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_pwr_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_ramcfg.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_rcc.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_rcc_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_rng.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_rng_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_rtc.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_rtc_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_sai.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_sai_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_sd.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_sdram.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_sd_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_smartcard.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_smartcard_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_smbus.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_smbus_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_spi.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_spi_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_sram.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_tim.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_tim_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_uart.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_uart_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_usart.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_usart_ex.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_wwdg.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_hal_xspi.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_ll_adc.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_ll_comp.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_ll_cordic.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_ll_crc.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_ll_crs.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_ll_dac.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_ll_dlyb.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_ll_dma.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_ll_exti.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_ll_fmac.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_ll_fmc.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_ll_gpio.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_ll_i2c.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_ll_i3c.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_ll_icache.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_ll_lptim.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_ll_lpuart.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_ll_opamp.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_ll_pka.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_ll_pwr.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_ll_rcc.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_ll_rng.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_ll_rtc.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_ll_sdmmc.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_ll_spi.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_ll_tim.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_ll_ucpd.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_ll_usart.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_ll_usb.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_ll_utils.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/src/stm32h5xx_util_i3c.c

        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/boards2/stm32h573i_discovery/stm32h573i_discovery.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/boards2/stm32h573i_discovery/stm32h573i_discovery_audio.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/boards2/stm32h573i_discovery/stm32h573i_discovery_bus.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/boards2/stm32h573i_discovery/stm32h573i_discovery_lcd.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/boards2/stm32h573i_discovery/stm32h573i_discovery_sd.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/boards2/stm32h573i_discovery/stm32h573i_discovery_ts.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/boards2/stm32h573i_discovery/stm32h573i_discovery_usbpd_pwr.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/boards2/components/cs42l51/cs42l51.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/boards2/components/cs42l51/cs42l51_reg.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/boards2/components/ft6x06/ft6x06.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/boards2/components/ft6x06/ft6x06_reg.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/boards2/components/st7789h2/st7789h2.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/boards2/components/st7789h2/st7789h2_reg.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/boards2/components/tcpp0203/tcpp0203.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/boards2/components/tcpp0203/tcpp0203_reg.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/boards2/utilities/lcd/stm32_lcd.c

		# common
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/tinyusb/src/tusb.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/tinyusb/src/common/tusb_fifo.c
		# device
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/tinyusb/src/device/usbd.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/tinyusb/src/device/usbd_control.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/tinyusb/src/class/audio/audio_device.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/tinyusb/src/class/cdc/cdc_device.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/tinyusb/src/class/dfu/dfu_device.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/tinyusb/src/class/dfu/dfu_rt_device.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/tinyusb/src/class/hid/hid_device.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/tinyusb/src/class/midi/midi_device.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/tinyusb/src/class/msc/msc_device.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/tinyusb/src/class/mtp/mtp_device.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/tinyusb/src/class/net/ecm_rndis_device.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/tinyusb/src/class/net/ncm_device.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/tinyusb/src/class/usbtmc/usbtmc_device.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/tinyusb/src/class/vendor/vendor_device.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/tinyusb/src/class/video/video_device.c
		# host
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/tinyusb/src/host/usbh.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/tinyusb/src/host/hub.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/tinyusb/src/class/cdc/cdc_host.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/tinyusb/src/class/hid/hid_host.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/tinyusb/src/class/midi/midi_host.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/tinyusb/src/class/msc/msc_host.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/tinyusb/src/class/vendor/vendor_host.c
		# typec
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/tinyusb/src/typec/usbc.c

		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/hardware/stm32h5xx/stm32h5xx_crypto.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/hardware/stm32h5xx/stm32h5xx_crypto_trng.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/hash/md5.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/hash/sha1.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/hash/sha224.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/hash/sha256.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/hash/sha384.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/hash/sha512.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/mac/hmac.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/cipher/rc4.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/cipher/idea.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/cipher/des.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/cipher/des3.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/cipher/aes.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/cipher/camellia.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/cipher/seed.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/cipher/aria.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/cipher_modes/cbc.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/aead/ccm.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/aead/gcm.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/cipher/chacha.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/mac/poly1305.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/aead/chacha20_poly1305.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/pkc/dh.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/pkc/rsa.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/pkc/dsa.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/ecc/ec.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/ecc/ec_curves.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/ecc/ecdh.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/ecc/ecdsa.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/ecc/eddsa.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/ecc/curve25519.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/ecc/curve448.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/ecc/x25519.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/ecc/x448.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/ecc/ed25519.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/ecc/ed448.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/mpi/mpi.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/mpi/mpi_misc.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/encoding/base64.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/encoding/asn1.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/encoding/oid.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/pkix/pem_import.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/pkix/pem_key_import.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/pkix/pem_decrypt.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/pkix/pem_common.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/pkix/pkcs5_decrypt.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/pkix/pkcs5_common.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/pkix/pkcs8_key_parse.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/pkix/x509_key_parse.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/pkix/x509_cert_parse.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/pkix/x509_cert_validate.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/pkix/x509_cert_ext_parse.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/pkix/x509_common.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/pkix/x509_sign_parse.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/pkix/x509_sign_verify.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/kdf/hkdf.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/rng/yarrow.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/ecc/ec_misc.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto/pkc/rsa_misc.c

		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_boot/core/crc32.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_boot/core/mailbox.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_boot/drivers/mcu_core/stm32h5xx_mcu_driver.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_boot/drivers/flash/internal/stm32h5xx_flash_driver.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_boot/modules/image/image.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_boot/modules/image/image_process.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_boot/modules/image/image_utils.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_boot/modules/memory/memory.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_boot/modules/memory/memory_ex.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_boot/core/verify.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_boot/modules/security/verify_auth.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_boot/modules/security/verify_sign.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_boot/modules/security/cipher.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_boot/update/update.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_boot/update/update_misc.c
		${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_boot/update/update_fallback.c

        ${CMAKE_CURRENT_SOURCE_DIR}/startup_stm32h573xx.S
        ${CMAKE_CURRENT_SOURCE_DIR}/syscalls.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../src/system_stm32h5xx.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../src/stm32h5xx_it.c
        #${CMAKE_CURRENT_SOURCE_DIR}/../src/stm32h5xx_hal_timebase_tim.c

		${CMAKE_CURRENT_SOURCE_DIR}/../src/usb_descriptors.c
		${CMAKE_CURRENT_SOURCE_DIR}/../src/family.c
		${CMAKE_CURRENT_SOURCE_DIR}/../src/dcd_stm32_fsdev.c
		${CMAKE_CURRENT_SOURCE_DIR}/../src/msc_disk.c

        ${CMAKE_CURRENT_SOURCE_DIR}/../src/main.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../src/res.c
        ${CMAKE_CURRENT_SOURCE_DIR}/../src/debug.c
)

target_include_directories(iap_demo PRIVATE
        ${CMAKE_CURRENT_SOURCE_DIR}/../src
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/common
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_crypto
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_boot
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_boot/update
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../src/cyclone_boot/modules
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/cmsis/include
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/devices/stm32h5xx
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/drivers/stm32h5xx_hal_driver/inc
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/boards2/stm32h573i_discovery
	    ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/boards2/components/common
	    ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/st/boards2/utilities/lcd
	    ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party/tinyusb/src
)

target_compile_definitions(iap_demo PRIVATE
    STM32H573xx
    USE_HAL_DRIVER
    USE_STM32H573I_DK
    __USE_C99_MATH

    $<$<CONFIG:Debug>:DEBUG>
)

# Add the map file to the list of files to be removed with 'clean' target
set_target_properties(iap_demo PROPERTIES ADDITIONAL_CLEAN_FILES ${CMAKE_PROJECT_NAME}.map)

# Convert .elf to .bin
add_custom_command(TARGET iap_demo POST_BUILD
        COMMAND ${CMAKE_OBJCOPY} -O binary
        ${CMAKE_CURRENT_BINARY_DIR}/iap_demo.elf
        ${CMAKE_CURRENT_BINARY_DIR}/iap_demo.bin
        COMMENT "Generating binary file from ELF"
)

 # Pre-build step: Generate res.c using ResourceCompiler
 if(WIN32)
     add_custom_command(
         OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/../src/res.c
         COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../tools/ResourceCompiler/bin/rc.exe
                 ${CMAKE_CURRENT_SOURCE_DIR}/../resources/
                 ${CMAKE_CURRENT_SOURCE_DIR}/../src/res.c
         DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/../resources/
         COMMENT "Running Resource Compiler to generate res.c"
     )
 elseif(LINUX)
     add_custom_command(
         OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/../src/res.c
         COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../tools/ResourceCompiler/bin/rc
                 ${CMAKE_CURRENT_SOURCE_DIR}/../resources/
                 ${CMAKE_CURRENT_SOURCE_DIR}/../src/res.c
         DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/../resources/
         COMMENT "Running Resource Compiler to generate res.c"
     )
 endif()

# Variables (set these appropriately or pass via -D flags)
set(IMAGE_BUILDER_PATH ../../../../../../../tools/ImageBuilder/bin/image_builder_linux)
set(INPUT_FILE ${CMAKE_CURRENT_BINARY_DIR}/iap_demo.bin)
set(OUTPUT_FILE ${CMAKE_CURRENT_SOURCE_DIR}/../iap_demo_bootable.bin)
set(OUTPUT_IMAGE_FILE ${CMAKE_CURRENT_SOURCE_DIR}/../iap_demo.img)

# Custom command to build the bootable image
add_custom_command(
		OUTPUT ${OUTPUT_FILE}
		COMMAND ${IMAGE_BUILDER_PATH}
		--input ${INPUT_FILE}
		--output ${OUTPUT_FILE}
		--runtime-sign-algo
		rsa-sha256
		--runtime-sign-key
		../../resources/keys/my_rsa_private_key.pem
		--vtor-align

		# Remove temporary input file after execution
		COMMAND ${CMAKE_COMMAND} -E rm -f ${INPUT_FILE}

		COMMENT "Running Image Builder to generate ${OUTPUT_FILE}"
		VERBATIM
)

# Custom command to build the update image
add_custom_command(
		OUTPUT ${OUTPUT_IMAGE_FILE}
		COMMAND ${IMAGE_BUILDER_PATH}
		--input ${INPUT_FILE}
		--output ${OUTPUT_IMAGE_FILE}
		--runtime-sign-algo
		rsa-sha256
		--runtime-sign-key
		../../resources/keys/my_rsa_private_key.pem
		--sign-algo
		rsa-sha256
		--sign-key
		../../resources/keys/my_rsa_private_key.pem
		--enc-algo
		aes-cbc
		--enc-key
		aa3ff7d43cc015682c7dfd00de9379e7
		--vtor-align

		# Remove temporary input file after execution
		COMMAND ${CMAKE_COMMAND} -E rm -f ${INPUT_FILE}

		COMMENT "Running Image Builder to generate ${OUTPUT_IMAGE_FILE}"
		VERBATIM
)

# A target to run the command
add_custom_target(build_image ALL
		DEPENDS ${OUTPUT_FILE}
		DEPENDS ${OUTPUT_IMAGE_FILE}
)

# Copy the generated .bin file to ../
add_custom_command(TARGET iap_demo POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy
            ${CMAKE_CURRENT_BINARY_DIR}/iap_demo.bin
            ${CMAKE_CURRENT_SOURCE_DIR}/../
    COMMENT "Copying iap_demo.bin to parent directory"
)
