set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_VERSION 1)
cmake_minimum_required(VERSION 3.8)
cmake_policy(SET CMP0069 NEW)

project(ping-pong VERSION 0.1
        DESCRIPTION "ping-pong with rotor light"
        LANGUAGES C CXX ASM)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_STANDARD 11)

# Enable assembler files preprocessing
add_compile_options($<$<COMPILE_LANGUAGE:ASM>:-x$<SEMICOLON>assembler-with-cpp>)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-register")

if (NOT ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "Cortex-M7" ))
    message(FATAL_ERROR "wrong system processor ${CMAKE_SYSTEM_PROCESSOR}, expected: Cortex-M7")
endif()

add_definitions(-DDEBUG
        -DUSE_FULL_LL_DRIVER
        -DHSE_VALUE=8000000
        -DSTM32H743xx
        -DUSE_FULL_ASSERT
        )

add_library(board
    board.cpp
    startup_stm32h743xx.s
    Core/Src/main.c
    Core/Src/gpio.c
    Core/Src/stm32h7xx_it.c
    Core/Src/system_stm32h7xx.c
    Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_ll_utils.c
    Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_ll_rcc.c
    Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_ll_pwr.c
    Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_ll_gpio.c
    Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_ll_exti.c
    ${LINKER_SCRIPT}
)
target_include_directories(rotor_light PUBLIC "${LIBSTDCPP_HOME}/include")
target_include_directories(board PUBLIC
   "${CMAKE_CURRENT_SOURCE_DIR}"
    ${CMAKE_CURRENT_SOURCE_DIR}/Core/Inc
    ${CMAKE_CURRENT_SOURCE_DIR}/Drivers/STM32H7xx_HAL_Driver/Inc
    ${CMAKE_CURRENT_SOURCE_DIR}/Drivers/CMSIS/Device/ST/STM32H7xx/Include
    ${CMAKE_CURRENT_SOURCE_DIR}/Drivers/CMSIS/Include
)
target_link_libraries(board rotor_light)

add_executable(${PROJECT_NAME} ping-pong-poll.cpp)
target_link_libraries(${PROJECT_NAME} board)

target_link_options(${PROJECT_NAME} PUBLIC
        LINKER:-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map
        -T ${LINKER_SCRIPT})

set_target_properties(${PROJECT_NAME} PROPERTIES SUFFIX .elf)
