cmake_minimum_required(VERSION 3.22)

#
# This file is generated only once,
# and is not re-generated if converter is called multiple times.
#
# User is free to modify the file as much as necessary
#

# 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()

# Set the project name
set(CMAKE_PROJECT_NAME polarimeter)


# Enable compile command to ease indexing with e.g. clangd
set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE)

# Core project settings
project(${CMAKE_PROJECT_NAME})
message("Build type: " ${CMAKE_BUILD_TYPE})

# Enable CMake support for ASM and C languages
enable_language(C ASM)

# Create an executable object type
add_executable(${CMAKE_PROJECT_NAME})

# Add STM32CubeMX generated sources
add_subdirectory(cmake/stm32cubemx)

# Link directories setup
target_link_directories(${CMAKE_PROJECT_NAME} PRIVATE
    # Add user defined library search paths
)

# Specify entry symbol as entry
target_link_options(${CMAKE_PROJECT_NAME} PRIVATE

)

# Floating point hardware support (e.g. Cortex-M4F/M7F), add compile and link options
# STM32F103ZET6 is Cortex-M3 core, no hardware FPU, should use soft float
target_compile_options(${CMAKE_PROJECT_NAME} PRIVATE
    -mfloat-abi=softfp
    -u _printf_float
)
target_link_options(${CMAKE_PROJECT_NAME} PRIVATE
    -mfloat-abi=softfp
    -u _printf_float
)

# Add sources to executable
target_sources(${CMAKE_PROJECT_NAME} PRIVATE
    # Add user sources here
    ${CMAKE_SOURCE_DIR}/User/board/bsp_misc.c
    ${CMAKE_SOURCE_DIR}/User/board/bsp_encoder.c
    ${CMAKE_SOURCE_DIR}/User/board/bsp_collect.c
    ${CMAKE_SOURCE_DIR}/User/board/bsp_tec.c
    ${CMAKE_SOURCE_DIR}/User/board/bsp_temper_sampling.c
    ${CMAKE_SOURCE_DIR}/User/board/bsp_hmi.c
     ${CMAKE_SOURCE_DIR}/User/board/bsp_motor.c
    ${CMAKE_SOURCE_DIR}/User/driver/at24cx/at24cx.c
    ${CMAKE_SOURCE_DIR}/User/driver/ad779x/ad7793.c
    ${CMAKE_SOURCE_DIR}/segger_rtt/RTT/SEGGER_RTT_printf.c
    ${CMAKE_SOURCE_DIR}/segger_rtt/RTT/SEGGER_RTT.c
    ${CMAKE_SOURCE_DIR}/User/driver/sv630p/h0c_comm.c
    ${CMAKE_SOURCE_DIR}/User/driver/sv630p/sv_common.c
    ${CMAKE_SOURCE_DIR}/User/driver/sv630p/h00_motor_args.c
    ${CMAKE_SOURCE_DIR}/User/driver/sv630p/h03_di_do.c
    ${CMAKE_SOURCE_DIR}/User/driver/sv630p/h02_basic_control.c
    ${CMAKE_SOURCE_DIR}/User/driver/sv630p/h05_pos_control.c
    ${CMAKE_SOURCE_DIR}/User/driver/sv630p/h06_speed_control.c
    ${CMAKE_SOURCE_DIR}/User/driver/sv630p/h0d_af.c
    ${CMAKE_SOURCE_DIR}/User/driver/sv630p/h31_comm_related_var.c
    ${CMAKE_SOURCE_DIR}/User/driver/sv630p/sv_device.c
    ${CMAKE_SOURCE_DIR}/User/driver/pt_res/pt100x.c

    ${CMAKE_SOURCE_DIR}/User/app/pm_common.c
    ${CMAKE_SOURCE_DIR}/User/app/storage.c
    ${CMAKE_SOURCE_DIR}/User/app/tec_control.c
    ${CMAKE_SOURCE_DIR}/User/app/servo.c
    ${CMAKE_SOURCE_DIR}/User/app/pm_device.c
    ${CMAKE_SOURCE_DIR}/User/app/data_process.c
    ${CMAKE_SOURCE_DIR}/User/app/data_sampling.c
    ${CMAKE_SOURCE_DIR}/User/app/mb_hmi/mb_command.c
    ${CMAKE_SOURCE_DIR}/User/app/mb_hmi/mb_interface.c
    ${CMAKE_SOURCE_DIR}/User/app/mb_hmi/hmi_server.c
    ${CMAKE_SOURCE_DIR}/User/app/pm_meas.c
    ${CMAKE_SOURCE_DIR}/User/app/pm_params.c
    ${CMAKE_SOURCE_DIR}/User/app/bsp_debug.c
    ${CMAKE_SOURCE_DIR}/nanoMODBUS/nanomodbus.c
    ${CMAKE_SOURCE_DIR}/etk/src/drivers/encoder/src/et_encoder.c
    ${CMAKE_SOURCE_DIR}/etk/src/drivers/encoder/src/et_encoder_utils.c
    ${CMAKE_SOURCE_DIR}/etk/src/logging/et_log.c
    ${CMAKE_SOURCE_DIR}/etk/src/algorithm/filter/src/et_ema_filter.c
    ${CMAKE_SOURCE_DIR}/etk/src/algorithm/pid/src/etk_pid.c
    ${CMAKE_SOURCE_DIR}/etk/src/algorithm/dsp_utils/src/et_goertzel.c
    ${CMAKE_SOURCE_DIR}/etk/src/algorithm/math/src/et_zero_cross.c
    ${CMAKE_SOURCE_DIR}/etk/src/utils/byte_conv/src/etk_byte_conv.c
    ${CMAKE_SOURCE_DIR}/etk/src/utils/ringbuffer/src/etk_ringbuffer.c
    ${CMAKE_SOURCE_DIR}/etk/src/algorithm/trend/src/et_trend.c
    ${CMAKE_SOURCE_DIR}/etk/src/algorithm/math/src/etk_slope.c
)


# Add include paths
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
    ${CMAKE_SOURCE_DIR}/User/board
    ${CMAKE_SOURCE_DIR}/segger_rtt/Config/
    ${CMAKE_SOURCE_DIR}/segger_rtt/RTT/
    ${CMAKE_SOURCE_DIR}/User/driver/at24cx/
    ${CMAKE_SOURCE_DIR}/User/driver/soft_spi/
    ${CMAKE_SOURCE_DIR}/User/driver/ad779x/
    ${CMAKE_SOURCE_DIR}/User/driver/sv630p/
    ${CMAKE_SOURCE_DIR}/User/driver/pt_res/
    ${CMAKE_SOURCE_DIR}/User/
    ${CMAKE_SOURCE_DIR}/User/app/
    ${CMAKE_SOURCE_DIR}/User/app/mb_hmi/

    ${CMAKE_SOURCE_DIR}/nanoMODBUS
    ${CMAKE_SOURCE_DIR}/etk/src/logging
    ${CMAKE_SOURCE_DIR}/etk/src/algorithm/math/include
    ${CMAKE_SOURCE_DIR}/etk/src/drivers/encoder/include
    ${CMAKE_SOURCE_DIR}/etk/src/algorithm/filter/include
    ${CMAKE_SOURCE_DIR}/etk/src/algorithm/pid/include
    ${CMAKE_SOURCE_DIR}/etk/src/algorithm/dsp_utils/include
    ${CMAKE_SOURCE_DIR}/etk/src/common/include
    ${CMAKE_SOURCE_DIR}/etk/src/algorithm/math/include
    ${CMAKE_SOURCE_DIR}/etk/src/utils/byte_conv/include
    ${CMAKE_SOURCE_DIR}/etk/src/utils/ringbuffer/include
    ${CMAKE_SOURCE_DIR}/etk/src/algorithm/trend/include
)

# Add project symbols (macros)
target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE
    # Add user defined symbols
    ARM_MATH
    SYSTEM_TYPE=2
    ET_SLIDING_FILTER_SIZE=4
    #NMBS_DEBUG
    USING_RTT_AS_CONSOLE
    RT_DEBUG
)

# Add linked libraries
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE
    stm32cubemx
)