CMakeLists.txt for Raspberry Pi Pico Project

I would like to perform the initialisation of a PIO in a function in the raik060spilib.c instead of in raik060spicom.c.
So I have include “raik060spicom.pio.h” in raik060spilib.h and raik060spilib.c.
However, I don’t understand how to set my CMakeLists.txt to be able to compile my code.

My file tree is :

  • RAIK060SPI

    • include

      • raik060spilib.h
    • src

      • raik060spilib.c
    • CMakeLists.txt

    • pico_sdk_import.cmake

    • raik060spicom.c

    • raik060spicom.pio

Do you have a solution to update my CMakeLists.txt below?

# Generated Cmake Pico project file

cmake_minimum_required(VERSION 3.13)

set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# Initialise pico_sdk from installed location
# (note this can come from environment, CMake cache etc)

# == DO NOT EDIT THE FOLLOWING LINES for the Raspberry Pi Pico VS Code Extension to work ==
if(WIN32)
    set(USERHOME $ENV{USERPROFILE})
else()
    set(USERHOME $ENV{HOME})
endif()
set(sdkVersion 2.1.1)
set(toolchainVersion 14_2_Rel1)
set(picotoolVersion 2.1.1)
set(picoVscode ${USERHOME}/.pico-sdk/cmake/pico-vscode.cmake)
if (EXISTS ${picoVscode})
    include(${picoVscode})
endif()
# ====================================================================================
set(PICO_BOARD pico_w CACHE STRING "Board type")

# Pull in Raspberry Pi Pico SDK (must be before project)
include(pico_sdk_import.cmake)

project(raik060spicom C CXX ASM)

# Initialise the Raspberry Pi Pico SDK
pico_sdk_init()

# Add executable. Default name is the project name, version 0.1

add_executable(raik060spicom
    raik060spicom.c
    )

# Create file .h from pio file
pico_generate_pio_header(raik060spicom ${CMAKE_CURRENT_LIST_DIR}/raik060spicom.pio)



add_library(raik060spilib INTERFACE) #add and change here
#add_library(name of the .h file1 INTERFACE) #add and change here

# Add the source file raik060spilib.c to the raik060spilib interface
target_sources(raik060spilib INTERFACE
    ${CMAKE_CURRENT_SOURCE_DIR}/src/raik060spilib.c
)

target_include_directories(raik060spilib INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include
       #name of the .h file1 INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include

) #add and change here


# pull in common dependencies
target_link_libraries(raik060spicom pico_stdlib hardware_pio hardware_clocks raik060spilib)

if (PICO_CYW43_SUPPORTED)
    target_link_libraries(raik060spicom raik060spilib pico_cyw43_arch_none)
endif()

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


pico_enable_stdio_usb(raik060spicom 1)
pico_enable_stdio_uart(raik060spicom 0)

# add url via pico_set_program_url

Without debugging your specific problem, I’ll say that I did a presentation on programming the Pico for Utah C++ Programmers. Perhaps it will help.

Embedded Programming with Raspberry Pi Pico

Dear Richard,

Thank you for your reply but even if I have quicly watch you rpresentation, I have not found a solution to my problem.

Have you asked in a Pico dedicated forum?

Dear Richard,

Yes, I have start to ask the question on Raspberry Pi forum (https://forums.raspberrypi.com/viewtopic.php?t=392807) but as I seems to be a CMakeLists.txt issue, I thought that I can have more help on this forum with CMake expert.