cmake_minimum_required(VERSION 3.16...3.24)

set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_VERBOSE_MAKEFILE 0)
set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")

set(CMAKE_C_COMPILER arm-none-eabi-gcc) #tested with: clang, arm-none-eabi-gcc
set(CMAKE_SYSROOT "/opt/gcc-arm-none-eabi/arm-none-eabi")
set(ARM_CORTEXM_MULTI_DIR "/opt/gcc-arm-none-eabi/arm-none-eabi/lib/thumb/v7e-m+fp/hard")
set(ARM_CORTEXM_BUILTINS_DIR "/opt/gcc-arm-none-eabi/lib/gcc/arm-none-eabi/11.2.1/thumb/v7e-m+fp/hard")

set(SRC_FILES
    USB_Device/Target/usbd_conf.c
    USB_Device/App/usb_device.c
    USB_Device/App/usbd_cdc_if.c
    USB_Device/App/usbd_desc.c
    Middlewares/Third_Party/FreeRTOS/Source/portable/MemMang/heap_4.c
    Middlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM33_NTZ/non_secure/port.c
    Middlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM33_NTZ/non_secure/portasm.c
    Middlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS_V2/cmsis_os2.c
    Middlewares/Third_Party/FreeRTOS/Source/croutine.c
    Middlewares/Third_Party/FreeRTOS/Source/event_groups.c
    Middlewares/Third_Party/FreeRTOS/Source/list.c
    Middlewares/Third_Party/FreeRTOS/Source/queue.c
    Middlewares/Third_Party/FreeRTOS/Source/stream_buffer.c
    Middlewares/Third_Party/FreeRTOS/Source/tasks.c
    Middlewares/Third_Party/FreeRTOS/Source/timers.c
    Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_core.c
    Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_ctlreq.c
    Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_ioreq.c
    Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Src/usbd_cdc.c
    Drivers/STM32L5xx_HAL_Driver/Src/stm32l5xx_hal.c
    Drivers/STM32L5xx_HAL_Driver/Src/stm32l5xx_hal_adc.c
    Drivers/STM32L5xx_HAL_Driver/Src/stm32l5xx_hal_adc_ex.c
    Drivers/STM32L5xx_HAL_Driver/Src/stm32l5xx_hal_cortex.c
    Drivers/STM32L5xx_HAL_Driver/Src/stm32l5xx_hal_dma.c
    Drivers/STM32L5xx_HAL_Driver/Src/stm32l5xx_hal_dma_ex.c
    Drivers/STM32L5xx_HAL_Driver/Src/stm32l5xx_hal_exti.c
    Drivers/STM32L5xx_HAL_Driver/Src/stm32l5xx_hal_fdcan.c
    Drivers/STM32L5xx_HAL_Driver/Src/stm32l5xx_hal_flash.c
    Drivers/STM32L5xx_HAL_Driver/Src/stm32l5xx_hal_flash_ex.c
    Drivers/STM32L5xx_HAL_Driver/Src/stm32l5xx_hal_flash_ramfunc.c
    Drivers/STM32L5xx_HAL_Driver/Src/stm32l5xx_hal_gpio.c
    Drivers/STM32L5xx_HAL_Driver/Src/stm32l5xx_hal_gtzc.c
    Drivers/STM32L5xx_HAL_Driver/Src/stm32l5xx_hal_i2c.c
    Drivers/STM32L5xx_HAL_Driver/Src/stm32l5xx_hal_i2c_ex.c
    Drivers/STM32L5xx_HAL_Driver/Src/stm32l5xx_hal_icache.c
    Drivers/STM32L5xx_HAL_Driver/Src/stm32l5xx_hal_pcd.c
    Drivers/STM32L5xx_HAL_Driver/Src/stm32l5xx_hal_pcd_ex.c
    Drivers/STM32L5xx_HAL_Driver/Src/stm32l5xx_hal_pwr.c
    Drivers/STM32L5xx_HAL_Driver/Src/stm32l5xx_hal_pwr_ex.c
    Drivers/STM32L5xx_HAL_Driver/Src/stm32l5xx_hal_rcc.c
    Drivers/STM32L5xx_HAL_Driver/Src/stm32l5xx_hal_rcc_ex.c
    Drivers/STM32L5xx_HAL_Driver/Src/stm32l5xx_hal_spi.c
    Drivers/STM32L5xx_HAL_Driver/Src/stm32l5xx_hal_spi_ex.c
    Drivers/STM32L5xx_HAL_Driver/Src/stm32l5xx_hal_sram.c
    Drivers/STM32L5xx_HAL_Driver/Src/stm32l5xx_hal_tim.c
    Drivers/STM32L5xx_HAL_Driver/Src/stm32l5xx_hal_tim_ex.c
    Drivers/STM32L5xx_HAL_Driver/Src/stm32l5xx_hal_uart.c
    Drivers/STM32L5xx_HAL_Driver/Src/stm32l5xx_hal_uart_ex.c
    Drivers/STM32L5xx_HAL_Driver/Src/stm32l5xx_ll_fmc.c
    Drivers/STM32L5xx_HAL_Driver/Src/stm32l5xx_ll_usb.c
    Core/Startup/startup_stm32l552zetx.s
    Core/Src/sc03mpd/sc03mpd.c
    Core/Src/ImC/decoder.c
    Core/Src/ImC/imc_api.c
    Core/Src/ImC/imc_kernel.c
    # Core/Src/ImC/uartBufferIT.c
    Core/Src/app_freertos.c
    Core/Src/main.c
    Core/Src/stm32l5xx_hal_msp.c
    Core/Src/stm32l5xx_hal_timebase_tim.c
    Core/Src/stm32l5xx_it.c
    Core/Src/syscalls.c
    Core/Src/sysmem.c
    Core/Src/system_stm32l5xx.c
)

include_directories(
    Core/Inc 
    Drivers/STM32L5xx_HAL_Driver/Inc 
    Drivers/STM32L5xx_HAL_Driver/Inc/Legacy 
    Drivers/CMSIS/Device/ST/STM32L5xx/Include 
    Drivers/CMSIS/Include 
    USB_Device/App 
    USB_Device/Target 
    Middlewares/ST/STM32_USB_Device_Library/Core/Inc 
    Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc 
    Middlewares/Third_Party/FreeRTOS/Source/include 
    Middlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS_V2 
    Middlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM33_NTZ/non_secure 
)

# compile flags

add_compile_definitions(USE_HAL_DRIVER STM32L552xx)
set(CMAKE_SYSTEM_NAME Generic)

set(CPU "-mcpu=cortex-m33")
set(FPU "-mfpu=fpv5-sp-d16")
set(FLOAT-ABI "-mfloat-abi=hard")
set(MCU "${CPU} -mthumb ${FPU} ${FLOAT-ABI}")
set(OPT "-O1")
#set(C_FLAGS_NO_ASM "-g -gdwarf-2")
set(C_FLAGS_NO_ASM )
set(C_FLAGS_EXTRA "-Wall -Wno-char-subscripts -Wno-unused-function -Wno-unused-variable -Wno-unused-but-set-variable -nostdlib -fdata-sections -ffunction-sections")

set(COMPILER_SPECIFIC_CFLAGS )
if (CMAKE_C_COMPILER MATCHES "clang")
    set(TARGET-TRIPPLE "--target=arm-none-eabi --sysroot=${CMAKE_SYSROOT}")
    set(COMPILER_SPECIFIC_CFLAGS "${TARGET-TRIPPLE}")
endif()

set(COMMON_CC_FLAGS "${MCU} ${TARGET-TRIPPLE} ${OPT} ${C_FLAGS_EXTRA} ${COMPILER_SPECIFIC_CFLAGS}")

set(CMAKE_C_FLAGS "${COMMON_CC_FLAGS} ${C_FLAGS_NO_ASM}")

# asm options
set(ASM_OPTIONS "-x assembler-with-cpp -Wno-null-character")
set(CMAKE_ASM_FLAGS "${COMMON_CC_FLAGS} ${ASM_OPTIONS}")

# linker options
set(ARM_CORTEXM_BUILTINS "-L${ARM_CORTEXM_BUILTINS_DIR}")
set(LD_ADDITIONAL_FILES "${ARM_CORTEXM_BUILTINS_DIR}/crti.o ${ARM_CORTEXM_BUILTINS_DIR}/crtn.o")

set(LINKER_SCRIPT "${CMAKE_SOURCE_DIR}/STM32L552ZETX_FLASH.ld")

set(LIBDIR "-L${ARM_CORTEXM_MULTI_DIR} ${ARM_CORTEXM_BUILTINS}")
set(VERBOSE_LINKER_OUTPUT "") #-v for verbose output
#set(LDFLAGS "${VERBOSE_LINKER_OUTPUT} -lc -lm -lnosys -lgcc -u _printf_float")
set(LDFLAGS "${VERBOSE_LINKER_OUTPUT} -lc -lm -lnosys -lgcc")

set(COMPILER_SPECIFIC_LDFLAGS )

set(CMAKE_EXE_LINKER_FLAGS "${LD_ADDITIONAL_FILES} ${LIBDIR} ${LDFLAGS} ${COMPILER_SPECIFIC_LDFLAGS} -T ${LINKER_SCRIPT}")

project(imc_freeRTOS C ASM)

add_executable(imc_freeRTOS ${SRC_FILES})
target_compile_options(imc_freeRTOS PRIVATE "-g")
set_target_properties(imc_freeRTOS PROPERTIES LINK_DEPENDS ${LINKER_SCRIPT})