cmake_minimum_required(VERSION 3.12)

# Project name
project(Robohand)

# Set the MCU and toolchain parameters
set(MCU_MODEL STM32F407xT)
set(MCU_FAMILY STM32F4xx)
set(CPU_PARAMETERS
    -mcpu=cortex-m4
    -mthumb
    -mfpu=fpv4-sp-d16
    -mfloat-abi=hard)

# Startup and linker scripts
set(STARTUP_SCRIPT ${CMAKE_SOURCE_DIR}/startup_stm32f407zgtx.s)
set(LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/STM32F407ZGTX_FLASH.ld)

# Standard and language settings
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
enable_language(C ASM)

# Include directories

#include_directories(${CMAKE_SOURCE_DIR}/Driver)
#include_directories(${CMAKE_SOURCE_DIR}/Driver/ADS131)
include_directories(${CMAKE_SOURCE_DIR}/Driver/AS5047)
include_directories(${CMAKE_SOURCE_DIR}/Platform)
include_directories(${CMAKE_SOURCE_DIR}/Platform/COM)
#include_directories(${CMAKE_SOURCE_DIR}/Platform/Flash)
include_directories(${CMAKE_SOURCE_DIR}/Platform/PID)
include_directories(${CMAKE_SOURCE_DIR}/Platform/GPIO)
include_directories(${CMAKE_SOURCE_DIR}/Platform/UART)
include_directories(${CMAKE_SOURCE_DIR}/Platform/Scheduler)
include_directories(${CMAKE_SOURCE_DIR}/Platform/Scheduler/UserTasks)
include_directories(${CMAKE_SOURCE_DIR}/Platform/SPI)
include_directories(${CMAKE_SOURCE_DIR}/Platform/Timer)
include_directories(${CMAKE_SOURCE_DIR}/Src)
include_directories(${CMAKE_SOURCE_DIR}/Src/HWconf)



# Source files
file(GLOB_RECURSE PROJECT_SOURCES
    #${CMAKE_SOURCE_DIR}/Driver/*.c
    #${CMAKE_SOURCE_DIR}/Driver/ADS131/*.c
    ${CMAKE_SOURCE_DIR}/Driver/AS5047/*.c
    ${CMAKE_SOURCE_DIR}/Platform/*.c
    ${CMAKE_SOURCE_DIR}/Platform/COM/*.c
    #${CMAKE_SOURCE_DIR}/Platform/Flash/*.c
    ${CMAKE_SOURCE_DIR}/Platform/PID/*.c
    ${CMAKE_SOURCE_DIR}/Platform/GPIO/*.c
    ${CMAKE_SOURCE_DIR}/Platform/UART/*.c
    ${CMAKE_SOURCE_DIR}/Platform/Scheduler/*.c
    ${CMAKE_SOURCE_DIR}/Platform/Scheduler/UserTasks/*.c
    ${CMAKE_SOURCE_DIR}/Platform/SPI/*.c
    ${CMAKE_SOURCE_DIR}/Platform/Timer/*.c
    ${CMAKE_SOURCE_DIR}/Src/*.c
    ${CMAKE_SOURCE_DIR}/Src/HWconf/*.c
    )

file(GLOB_RECURSE PROJECT_ASM
    ${CMAKE_SOURCE_DIR}/Platform/Scheduler/*.s
)

# Final output name
set(EXECUTABLE ${PROJECT_NAME}.elf)

# Create executable
add_executable(${EXECUTABLE}
    ${PROJECT_SOURCES}
    ${PROJECT_ASM}
    ${STARTUP_SCRIPT})

# Preprocessor definitions
target_compile_definitions(${EXECUTABLE} PRIVATE
    ${MCU_MODEL}
    USE_HAL_DRIVER)

# Compiler flags
target_compile_options(${EXECUTABLE} PRIVATE
    ${CPU_PARAMETERS}
    -Wall
    -Wextra
    -Wpedantic
    -Wno-unused-parameter
    $<$<CONFIG:Debug>:-O -g3>
    $<$<CONFIG:Release>:-O>)

# Linker flags
target_link_options(${EXECUTABLE} PRIVATE
    -T${LINKER_SCRIPT}
    ${CPU_PARAMETERS}
    -Wl,-Map=${PROJECT_NAME}.map
    -Wl,--gc-sections
    --specs=nosys.specs
    -Wl,--start-group
    -lc -lm
    -Wl,--end-group
    -Wl,--print-memory-usage)

set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# Post-build steps: generate .hex and .bin
add_custom_command(TARGET ${EXECUTABLE} POST_BUILD
    COMMAND ${CMAKE_SIZE} $<TARGET_FILE:${EXECUTABLE}>
    COMMENT "Size info")

add_custom_command(TARGET ${EXECUTABLE} POST_BUILD
    COMMAND ${CMAKE_OBJCOPY} -O ihex $<TARGET_FILE:${EXECUTABLE}> ${PROJECT_NAME}.hex
    COMMAND ${CMAKE_OBJCOPY} -O binary $<TARGET_FILE:${EXECUTABLE}> ${PROJECT_NAME}.bin
    COMMENT "Generating HEX and BIN files")
