cmake_minimum_required(VERSION 3.12)

# Including the SDK (must be before project)

if (NOT "$ENV{PICO_SDK_PATH_OVERRIDE}" STREQUAL "")
    SET(PICO_SDK_PATH "$ENV{PICO_SDK_PATH_OVERRIDE}")
else()
    SET(PICO_SDK_PATH "${CMAKE_SOURCE_DIR}/lib/pico-sdk")
endif()

#SET(PICO_TOOLCHAIN_PATH "${PICO_SDK_PATH}/cmake/preload/toolchains/")
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")

include(${PICO_SDK_PATH}/pico_sdk_init.cmake)
include(cmake/freeRTOS.cmake)

#include(pico_extras_import_optional.cmake)

project(pico_examples C CXX ASM)
set(CMAKE_C_STANDARD 23)
set(CMAKE_CXX_STANDARD 23)

if (PICO_SDK_VERSION_STRING VERSION_LESS "1.3.0")
    message(FATAL_ERROR "Raspberry Pi Pico SDK version 1.3.0 (or later) required. Your version is ${PICO_SDK_VERSION_STRING}")
endif()

set(PICO_EXAMPLES_PATH ${PROJECT_SOURCE_DIR})

# Initialize the SDK
pico_sdk_init()

include(example_auto_set_url.cmake)

add_compile_options(-Wall
        -Wno-format          # int != int32_t as far as the compiler is concerned because gcc has int32_t as long int
        -Wno-unused-function # we have some for the docs that aren't called
        )
if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
    add_compile_options(-Wno-maybe-uninitialized)
endif()

# adding sources files & includes
add_executable(main
    src/main.cpp
)

target_include_directories(main PUBLIC 
    include/
    include/freeRTOS
    build/
)

include(${PROJECT_SOURCE_DIR}/cmake/tmc2209.cmake)

# Adding PIO
pico_generate_pio_header(main ${CMAKE_CURRENT_LIST_DIR}/src/tmc2209/pulser.pio)

# pull in common dependencies
target_link_libraries(main 
    pico_stdlib
    pico_time
    pico_multicore
    pico_stdio_usb
    hardware_pio

    tmc2209_driver
    FreeRTOS
)

pico_enable_stdio_usb(main 1)
pico_enable_stdio_uart(main 0)

# create map/bin/hex file etc.
pico_add_extra_outputs(main)

# add url via pico_set_program_url
example_auto_set_url(main)
