### Blink LED Example (C) Source: https://github.com/raspberrypi/pico-sdk/blob/master/docs/footer.html A basic example demonstrating how to blink the onboard LED of the Raspberry Pi Pico using the SDK. This involves GPIO setup and a delay loop. ```c #include "pico/stdlib.h" int main() { const uint LED_PIN = PICO_DEFAULT_LED_PIN; gpio_init(LED_PIN); gpio_set_dir(LED_PIN, GPIO_OUT); while (true) { gpio_put(LED_PIN, 1); sleep_ms(1000); gpio_put(LED_PIN, 0); sleep_ms(1000); } } ``` -------------------------------- ### Install Prerequisites (Linux) Source: https://github.com/raspberrypi/pico-sdk/blob/master/README.md Installs necessary tools like CMake, Python 3, a native compiler, and the ARM GCC cross-compiler using the apt package manager on Debian-based systems. ```shell sudo apt install cmake python3 build-essential gcc-arm-none-eabi libnewlib-arm-none-eabi libstdc++-arm-none-eabi-newlib ``` -------------------------------- ### Include Directories and Installation Setup Source: https://github.com/raspberrypi/pico-sdk/blob/master/tools/pioasm/CMakeLists.txt Configures include directories for the pioasm build and sets up installation paths based on the PIOASM_FLAT_INSTALL variable. It defines where the executable and CMake configuration files will be installed. ```cmake target_include_directories(pioasm PRIVATE ${CMAKE_CURRENT_LIST_DIR} ${CMAKE_CURRENT_LIST_DIR}/gen ${CMAKE_BINARY_DIR}) if (MSVC OR (WIN32 AND NOT MINGW AND (CMAKE_CXX_COMPILER_ID STREQUAL "Clang"))) target_compile_definitions(pioasm PRIVATE YY_NO_UNISTD_H) endif() if (MSVC) target_compile_options(pioasm PRIVATE "/std:c++latest") endif() # allow installing to flat dir include(GNUInstallDirs) if (PIOASM_FLAT_INSTALL) set(INSTALL_CONFIGDIR pioasm) set(INSTALL_BINDIR pioasm) else() set(INSTALL_CONFIGDIR lib/cmake/pioasm) set(INSTALL_BINDIR ${CMAKE_INSTALL_BINDIR}) endif() # allow `make install` install(TARGETS pioasm EXPORT pioasm-targets RUNTIME DESTINATION ${INSTALL_BINDIR} ) #Export the targets to a script install(EXPORT pioasm-targets FILE pioasmTargets.cmake DESTINATION ${INSTALL_CONFIGDIR} ) ``` -------------------------------- ### Initialize Picotool for Pico SDK Source: https://github.com/raspberrypi/pico-sdk/blob/master/tools/CMakeLists.txt Ensures the picotool is installed or downloads and builds it if not found. It handles finding existing installations or fetching from Git, setting up the build environment, and making the picotool target available. ```cmake function(pico_init_picotool) set(picotool_VERSION_REQUIRED 2.1.1) if (NOT TARGET picotool AND NOT DEFINED picotool_FOUND) # Build path of local install dir if (DEFINED ENV{PICOTOOL_FETCH_FROM_GIT_PATH} AND (NOT PICOTOOL_FETCH_FROM_GIT_PATH)) set(PICOTOOL_FETCH_FROM_GIT_PATH $ENV{PICOTOOL_FETCH_FROM_GIT_PATH}) message("Using PICOTOOL_FETCH_FROM_GIT_PATH from environment ('${PICOTOOL_FETCH_FROM_GIT_PATH}')") endif () include(FetchContent) if (PICOTOOL_FETCH_FROM_GIT_PATH) get_filename_component(picotool_INSTALL_DIR "${PICOTOOL_FETCH_FROM_GIT_PATH}" ABSOLUTE) else() set(picotool_INSTALL_DIR ${FETCHCONTENT_BASE_DIR}) endif () if (NOT PICOTOOL_FORCE_FETCH_FROM_GIT AND NOT ENV{PICOTOOL_FORCE_FETCH_FROM_GIT}) if (NOT picotool_DIR AND EXISTS ${picotool_INSTALL_DIR}/picotool) set(picotool_DIR ${picotool_INSTALL_DIR}/picotool) endif() # Find package - will find installed picotool, either at picotool_DIR or system find_package(picotool ${picotool_VERSION_REQUIRED} QUIET CONFIG NO_CMAKE_FIND_ROOT_PATH) if (NOT picotool_FOUND AND picotool_CONSIDERED_VERSIONS) message(FATAL_ERROR "Incompatible picotool installation found: " "Requires version ${picotool_VERSION_REQUIRED}, " "you have version ${picotool_CONSIDERED_VERSIONS}\n" "Update your installation, or set PICOTOOL_FORCE_FETCH_FROM_GIT " "to download and build the correct version" ) endif() else() message("Forcing fetch of picotool from git") endif() if (NOT picotool_FOUND) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PICO_SDK_PATH}/tools) find_package(picotool MODULE) endif() endif () if (TARGET picotool AND NOT DEFINED ENV{_PICOTOOL_FOUND_THIS_RUN}) # Internal environment variable to prevent printing multiple times set(ENV{_PICOTOOL_FOUND_THIS_RUN} 1) get_property(picotool_location TARGET picotool PROPERTY LOCATION) message("Using picotool from ${picotool_location}") endif () if (TARGET picotool) set(picotool_FOUND 1 PARENT_SCOPE) else () message("No picotool found") endif () endfunction() ``` -------------------------------- ### Download and Install RISC-V Toolchain Source: https://github.com/raspberrypi/pico-sdk/blob/master/README.md Downloads a prebuilt RISC-V toolchain from GitHub and installs it into a specified directory on Raspberry Pi OS. This is a prerequisite for building RISC-V code for the RP2350. ```sh wget https://github.com/raspberrypi/pico-sdk-tools/releases/download/v2.0.0-5/riscv-toolchain-14-aarch64-lin.tar.gz sudo mkdir -p /opt/riscv/riscv-toolchain-14 sudo chown $USER /opt/riscv/riscv-toolchain-14 tar xvf riscv-toolchain-14-aarch64-lin.tar.gz -C /opt/riscv/riscv-toolchain-14 ``` -------------------------------- ### Build hello_world Target with CMake Source: https://github.com/raspberrypi/pico-sdk/blob/master/README.md Builds the 'hello_world' example project using the CMake build system. This command is executed from the build directory and generates the executable and UF2 file for the Raspberry Pi Pico. ```sh cmake --build build --target hello_world ``` -------------------------------- ### CMake: Define pico_sync_sem Library Source: https://github.com/raspberrypi/pico-sdk/blob/master/src/common/pico_sync/CMakeLists.txt Configures the semaphore synchronization library. It includes the `sem.c` source file and links against the `pico_sync_core` library. ```CMake if (NOT TARGET pico_sync_sem) pico_add_library(pico_sync_sem) target_sources(pico_sync_sem INTERFACE ${CMAKE_CURRENT_LIST_DIR}/sem.c ) pico_mirrored_target_link_libraries(pico_sync_sem INTERFACE pico_sync_core) endif() ``` -------------------------------- ### CMake: Define pico_sync_mutex Library Source: https://github.com/raspberrypi/pico-sdk/blob/master/src/common/pico_sync/CMakeLists.txt Sets up the mutex synchronization library. It includes the `mutex.c` source file and depends on the `pico_sync_core` library. ```CMake if (NOT TARGET pico_sync_mutex) pico_add_library(pico_sync_mutex) target_sources(pico_sync_mutex INTERFACE ${CMAKE_CURRENT_LIST_DIR}/mutex.c ) pico_mirrored_target_link_libraries(pico_sync_mutex INTERFACE pico_sync_core) endif() ``` -------------------------------- ### Pico SDK Standard Library Setup Source: https://github.com/raspberrypi/pico-sdk/blob/master/src/host/pico_stdlib/CMakeLists.txt This CMake code snippet defines the `pico_stdlib` target, which is a core component of the Raspberry Pi Pico SDK. It ensures the library is added only if it doesn't already exist, specifies its source files, and links it against other necessary Pico SDK libraries like `pico_platform`, `pico_time`, and `hardware_gpio`. This setup is crucial for building applications for the Raspberry Pi Pico. ```cmake if (NOT TARGET pico_stdlib) pico_add_impl_library(pico_stdlib) target_sources(pico_stdlib INTERFACE ${CMAKE_CURRENT_LIST_DIR}/stdlib.c ) target_link_libraries(pico_stdlib INTERFACE pico_stdlib_headers pico_platform pico_time pico_divider pico_binary_info pico_printf pico_runtime pico_stdio hardware_gpio hardware_uart ) endif() ``` -------------------------------- ### CMake: Configure and Build Raspberry Pi Pico SDK Doxygen Docs Source: https://github.com/raspberrypi/pico-sdk/blob/master/docs/CMakeLists.txt This CMake script manages the Doxygen documentation build process for the Raspberry Pi Pico SDK. It checks for Doxygen installation, allows configuration of documentation building via an option, handles fetching example code from a specified path or GitHub repository, and sets up custom targets to run Doxygen with generated configuration files. ```cmake find_package(Doxygen QUIET) if (PICO_SDK_TOP_LEVEL_PROJECT AND ${DOXYGEN_FOUND}) set(PICO_BUILD_DOCS_DEFAULT 1) endif() option(PICO_BUILD_DOCS "Build HTML Doxygen docs" ${PICO_BUILD_DOCS_DEFAULT}) if (DEFINED ENV{PICO_EXAMPLES_PATH} AND NOT PICO_EXAMPLES_PATH) set(PICO_EXAMPLES_PATH $ENV{PICO_EXAMPLES_PATH}) message("Using PICO_EXAMPLES_PATH from environment ('${PICO_EXAMPLES_PATH}')") endif() if(PICO_BUILD_DOCS) if(NOT DOXYGEN_FOUND) message(FATAL_ERROR "Doxygen is needed to build the documentation.") endif() include(ExternalProject) if(PICO_EXAMPLES_PATH) get_filename_component(PICO_EXAMPLES_PATH "${PICO_EXAMPLES_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}") if (EXISTS ${PICO_EXAMPLES_PATH}) message("Documentation example code will come from ${PICO_EXAMPLES_PATH}") else() message(WARNING "Documentation example code configured to come from ${PICO_EXAMPLES_PATH}, but that path does not exist") endif() add_custom_target(doc-pico-examples) else() ExternalProject_Add(doc-pico-examples GIT_REPOSITORY https://github.com/raspberrypi/pico-examples GIT_TAG master CONFIGURE_COMMAND "" BUILD_COMMAND "" INSTALL_COMMAND "" ) ExternalProject_Get_property(doc-pico-examples SOURCE_DIR) ExternalProject_Get_property(doc-pico-examples GIT_REPOSITORY) ExternalProject_Get_property(doc-pico-examples GIT_TAG) set(PICO_EXAMPLES_PATH ${SOURCE_DIR}) message("Documentation example code will come from git repo ${GIT_REPOSITORY}, branch ${GIT_TAG}") endif() set(DOXY_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/doxygen") string(REPLACE ";" " " DOXY_INPUT_DIRS "${PICO_DOXYGEN_PATHS}") string(REPLACE ";" " " DOXY_EXCLUDE_DIRS "${PICO_DOXYGEN_EXCLUDE_PATHS}") string(REPLACE ";" " " DOXY_PREDEFINED "${PICO_DOXYGEN_PRE_DEFINES}") string(REPLACE ";" " " DOXY_ENABLED_SECTIONS "${PICO_DOXYGEN_ENABLED_SECTIONS}") set(DOXY_EXAMPLE_DIR "${PICO_EXAMPLES_PATH}") # auto genereate additional section enables from library paths foreach (DIR IN LISTS PICO_DOXYGEN_PATHS) get_filename_component(NAME "${DIR}" NAME) if (NOT DIR STREQUAL "src") set(DOXY_ENABLED_SECTIONS "${DOXY_ENABLED_SECTIONS} ${NAME}") endif () endforeach () set(doxyfile_in ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in) set(doxyfile ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile) if(DOXYGEN_VERSION VERSION_GREATER_EQUAL "1.9.8") # see https://github.com/doxygen/doxygen/issues/10562 set(DOXY_API_DOCS_TAB_TYPE "topics") else() set(DOXY_API_DOCS_TAB_TYPE "modules") endif() set(doxylayout_in ${CMAKE_CURRENT_SOURCE_DIR}/DoxygenLayout.xml.in) set(doxylayout ${CMAKE_CURRENT_BINARY_DIR}/DoxygenLayout.xml) if (PICO_PLATFORM STREQUAL "rp2040") set(PICO_DOXYGEN_TAG "(RP2040)") elseif (PICO_PLATFORM STREQUAL "rp2350-arm-s" OR PICO_PLATFORM STREQUAL "rp2350-riscv") set(PICO_DOXYGEN_TAG "(RP2350)") endif () configure_file(${doxylayout_in} ${doxylayout} @ONLY) configure_file(${doxyfile_in} ${doxyfile} @ONLY) add_custom_target(docs COMMAND ${DOXYGEN_EXECUTABLE} ${doxyfile} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} COMMENT "Generating API documentation with Doxygen" VERBATIM) add_dependencies(docs doc-pico-examples) endif() ``` -------------------------------- ### CMake: Define pico_sync Implementation Library Source: https://github.com/raspberrypi/pico-sdk/blob/master/src/common/pico_sync/CMakeLists.txt Creates the main implementation library for synchronization primitives. It links various synchronization components and sets an include directory for header files. ```CMake if (NOT TARGET pico_sync) pico_add_impl_library(pico_sync) target_include_directories(pico_sync_headers SYSTEM INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include) pico_mirrored_target_link_libraries(pico_sync INTERFACE pico_sync_sem pico_sync_mutex pico_sync_critical_section pico_time hardware_sync) endif() ``` -------------------------------- ### UART Communication Example (C++) Source: https://github.com/raspberrypi/pico-sdk/blob/master/docs/footer.html Demonstrates how to send and receive data over UART using the Pico SDK. This is useful for serial communication with other devices. ```cpp #include "pico/stdlib.h" #include "hardware/uart.h" // Example using UART 0 #define UART_ID uart0 #define BAUD_RATE 115200 // Pins for UART0 #define UART_TX_PIN 0 #define UART_RX_PIN 1 int main() { stdio_init_all(); // Initialize UART uart_init(UART_ID, BAUD_RATE); // Set the GPIO function for the UART pins gpio_set_function(UART_TX_PIN, GPIO_FUNC_UART); gpio_set_function(UART_RX_PIN, GPIO_FUNC_UART); // Send a message uart_puts(UART_ID, "Hello from Pico!\n"); while (true) { // Check if data is available to read if (uart_is_readable(UART_ID)) { char ch = uart_getc(UART_ID); uart_putc(UART_ID, ch); } } return 0; } ``` -------------------------------- ### CMake Setup for Pico USB Reset Interface Source: https://github.com/raspberrypi/pico-sdk/blob/master/src/common/pico_usb_reset_interface_headers/CMakeLists.txt This CMake code defines and configures the `pico_usb_reset_interface` library. It creates interface libraries for headers and the main interface, links them, and specifies system include directories for the header files, essential for USB functionality on the Raspberry Pi Pico. ```cmake add_library(pico_usb_reset_interface_headers INTERFACE) add_library(pico_usb_reset_interface INTERFACE) target_link_libraries(pico_usb_reset_interface INTERFACE pico_usb_reset_interface_headers) target_include_directories(pico_usb_reset_interface_headers SYSTEM INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include) ``` -------------------------------- ### Pico SDK pico_stdio Library Setup Source: https://github.com/raspberrypi/pico-sdk/blob/master/src/rp2_common/pico_stdio/CMakeLists.txt Sets up the `pico_stdio` library, including wrapping standard C stdio functions like `printf` and `puts` to ensure thread-safe access via mutexes. It also links against `pico_printf` if available. ```CMake if (NOT TARGET pico_stdio) pico_add_library(pico_stdio) target_include_directories(pico_stdio_headers SYSTEM INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include) target_sources(pico_stdio INTERFACE ${CMAKE_CURRENT_LIST_DIR}/stdio.c ) pico_wrap_function(pico_stdio printf) # here not pico_printf as we do mutex pico_wrap_function(pico_stdio vprintf) # here not pico_printf as we do mutex pico_wrap_function(pico_stdio puts) pico_wrap_function(pico_stdio putchar) pico_wrap_function(pico_stdio getchar) if (TARGET pico_printf) pico_mirrored_target_link_libraries(pico_stdio INTERFACE pico_printf) endif() endif() ``` -------------------------------- ### Raspberry Pi Pico SDK API Documentation Overview Source: https://github.com/raspberrypi/pico-sdk/blob/master/README.md This entry summarizes the scope and availability of API documentation for the Raspberry Pi Pico SDK. It guides users to detailed resources for programming the RP-series microcontrollers. ```APIDOC Raspberry Pi Pico SDK API Documentation: Purpose: Provides comprehensive reference for C/C++ libraries and hardware register definitions for the Raspberry Pi Pico and RP-series microcontrollers. Content: - Low-level hardware register definitions and access methods. - High-level libraries for timers, synchronization, Wi-Fi, Bluetooth, USB, and multicore programming. - Guidance on using the SDK for various application types, from simple programs to complex runtime environments. Access: - PDF-based API documentation: https://rptl.io/pico-c-sdk - HTML-based API documentation (Doxygen): https://rptl.io/pico-doxygen Related Resources: - Getting Started: https://rptl.io/pico-get-started - Connecting to the Internet with Pico W: https://rptl.io/picow-connect - Example Code: https://github.com/raspberrypi/pico-examples - pico-extras library: https://github.com/raspberrypi/pico-extras Note: Users are encouraged to consult the linked resources for detailed API specifications, usage examples, and setup instructions. ``` -------------------------------- ### Project Setup and Core Configuration Source: https://github.com/raspberrypi/pico-sdk/blob/master/tools/pioasm/CMakeLists.txt Sets the minimum required CMake version, defines the project name, and configures the default build type and C++ standard. Ensures a consistent build environment for the pioasm tool. ```cmake cmake_minimum_required(VERSION 3.13...3.27) project(pioasm CXX) if (NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Release) endif() set(CMAKE_CXX_STANDARD 11) ``` -------------------------------- ### Pico SDK: CYW43 Bluetooth Library Setup Source: https://github.com/raspberrypi/pico-sdk/blob/master/src/rp2_common/pico_cyw43_driver/CMakeLists.txt Configures the pico_btstack_cyw43 library for Bluetooth functionality. It links against necessary base libraries and defines sources and include directories for the CYW43 chipset. ```cmake pico_add_library(pico_btstack_cyw43) target_sources(pico_btstack_cyw43 INTERFACE ${CMAKE_CURRENT_LIST_DIR}/btstack_cyw43.c ) target_include_directories(pico_btstack_cyw43_headers SYSTEM INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include ) pico_mirrored_target_link_libraries(pico_btstack_cyw43 INTERFACE pico_btstack_base pico_btstack_flash_bank pico_btstack_run_loop_async_context pico_cyw43_arch pico_btstack_hci_transport_cyw43 ) ``` -------------------------------- ### CMake: Define pico_sync_critical_section Library Source: https://github.com/raspberrypi/pico-sdk/blob/master/src/common/pico_sync/CMakeLists.txt Configures the critical section synchronization library. It includes the `critical_section.c` source file and links against the `pico_sync_core` library. ```CMake if (NOT TARGET pico_sync_critical_section) pico_add_library(pico_sync_critical_section) target_sources(pico_sync_critical_section INTERFACE ${CMAKE_CURRENT_LIST_DIR}/critical_section.c ) pico_mirrored_target_link_libraries(pico_sync_critical_section INTERFACE pico_sync_core) endif() ``` -------------------------------- ### CMake: Define pico_sync_core Library Source: https://github.com/raspberrypi/pico-sdk/blob/master/src/common/pico_sync/CMakeLists.txt Defines the core synchronization library, which includes the `lock_core.c` source file. This library serves as a foundational component for other synchronization primitives. ```CMake if (NOT TARGET pico_sync_core) pico_add_library(pico_sync_core NOFLAG) target_sources(pico_sync_core INTERFACE ${CMAKE_CURRENT_LIST_DIR}/lock_core.c ) endif() ``` -------------------------------- ### Pico SDK CMake Library Setup Source: https://github.com/raspberrypi/pico-sdk/blob/master/src/rp2_common/hardware_base/CMakeLists.txt This snippet demonstrates how to define CMake interface libraries for hardware base components and configure include directories and library linking for the Raspberry Pi Pico SDK. ```cmake add_library(hardware_base INTERFACE) add_library(hardware_base_headers INTERFACE) target_include_directories(hardware_base_headers SYSTEM INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include) target_link_libraries(hardware_base_headers INTERFACE pico_base_headers) target_link_libraries(hardware_base INTERFACE hardware_base_headers) ``` -------------------------------- ### pico_multicore CMake Setup Source: https://github.com/raspberrypi/pico-sdk/blob/master/src/rp2_common/pico_multicore/CMakeLists.txt This CMake script defines and configures the `pico_multicore` library. It specifies include directories, source files, and links against `pico_sync` and `hardware_irq`. It also includes conditional linking for `hardware_riscv` when `PICO_RISCV` is defined. ```cmake if (NOT TARGET pico_multicore) pico_add_library(pico_multicore) target_include_directories(pico_multicore_headers SYSTEM INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include) target_sources(pico_multicore INTERFACE ${CMAKE_CURRENT_LIST_DIR}/multicore.c) pico_mirrored_target_link_libraries(pico_multicore INTERFACE pico_sync hardware_irq) if (PICO_RISCV) pico_mirrored_target_link_libraries(pico_multicore INTERFACE hardware_riscv) endif() endif() ``` -------------------------------- ### Build Pico Semaphore Test Executable Source: https://github.com/raspberrypi/pico-sdk/blob/master/test/pico_sem_test/CMakeLists.txt This snippet defines a CMake build process for a Raspberry Pi Pico SDK project. It specifies the executable target 'pico_sem_test' to be built from 'pico_sem_test.c', links it against necessary Pico SDK libraries ('pico_test', 'pico_sync'), and adds extra build outputs. ```cmake add_executable(pico_sem_test pico_sem_test.c) target_link_libraries(pico_sem_test PRIVATE pico_test pico_sync) pico_add_extra_outputs(pico_sem_test) ``` -------------------------------- ### Add pico_rand Library using CMake Source: https://github.com/raspberrypi/pico-sdk/blob/master/src/host/pico_rand/CMakeLists.txt This CMake snippet demonstrates how to add a library named 'pico_rand' to your project. It specifies the source files for the library, sets system include directories, and links it against hardware libraries like 'hardware_timer' and 'hardware_sync'. ```cmake pico_add_library(pico_rand) target_sources(pico_rand INTERFACE ${CMAKE_CURRENT_LIST_DIR}/rand.c ) target_include_directories(pico_rand_headers SYSTEM INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include) pico_mirrored_target_link_libraries(pico_rand INTERFACE hardware_timer hardware_sync ) ``` -------------------------------- ### Hello World C Program Source: https://github.com/raspberrypi/pico-sdk/blob/master/README.md A basic 'Hello, World!' program for the Raspberry Pi Pico, utilizing the `pico/stdlib.h` library for standard I/O initialization and printing to the console. ```c #include #include "pico/stdlib.h" int main() { stdio_init_all(); printf("Hello, world!\n"); return 0; } ``` -------------------------------- ### CMakeLists.txt: Build Executable Source: https://github.com/raspberrypi/pico-sdk/blob/master/README.md Configures the CMake build system to create an executable named 'hello_world' from the source file 'hello_world.c', linking against the `pico_stdlib` library and enabling extra output formats. ```cmake add_executable(hello_world hello_world.c ) # Add pico_stdlib library which aggregates commonly used features target_link_libraries(hello_world pico_stdlib) # create map/bin/hex/uf2 file in addition to ELF. pico_add_extra_outputs(hello_world) ``` -------------------------------- ### CMakeLists.txt: Direct SDK Include Source: https://github.com/raspberrypi/pico-sdk/blob/master/README.md Configures a CMake project to use a locally cloned Raspberry Pi Pico SDK by directly including the `pico_sdk_init.cmake` script from its path. ```cmake cmake_minimum_required(VERSION 3.13) # initialize the SDK directly include(/path/to/pico-sdk/pico_sdk_init.cmake) project(my_project) # initialize the Raspberry Pi Pico SDK pico_sdk_init() # rest of your project ``` -------------------------------- ### Setup UART Hardware Target and Link Libraries Source: https://github.com/raspberrypi/pico-sdk/blob/master/src/rp2_common/hardware_uart/CMakeLists.txt Configures the UART peripheral as a hardware target and links essential interface libraries for hardware resets and clocks. This setup is crucial for enabling UART communication on the Pico. ```cmake pico_simple_hardware_target(uart) pico_mirrored_target_link_libraries(hardware_uart INTERFACE hardware_resets hardware_clocks) ``` -------------------------------- ### Pico SDK: Add Semihosting Library Source: https://github.com/raspberrypi/pico-sdk/blob/master/src/rp2_common/pico_stdio_semihosting/CMakeLists.txt This CMake script defines a new library target for semihosting standard I/O support. It specifies the source files and include directories required for the library, and links it against the pico_stdio library. ```cmake pico_add_library(pico_stdio_semihosting) target_sources(pico_stdio_semihosting INTERFACE ${CMAKE_CURRENT_LIST_DIR}/stdio_semihosting.c ) target_include_directories(pico_stdio_semihosting_headers SYSTEM INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include) pico_mirrored_target_link_libraries(pico_stdio_semihosting INTERFACE pico_stdio) ``` -------------------------------- ### pico_btstack_run_loop_async_context Library Configuration Source: https://github.com/raspberrypi/pico-sdk/blob/master/src/rp2_common/pico_btstack/CMakeLists.txt Sets up the pico_btstack_run_loop_async_context library, defining its source file, include paths, and linking dependencies like pico_btstack_base and pico_async_context_base. ```CMake pico_add_library(pico_btstack_run_loop_async_context NOFLAG) target_sources(pico_btstack_run_loop_async_context INTERFACE ${CMAKE_CURRENT_LIST_DIR}/btstack_run_loop_async_context.c ) target_include_directories(pico_btstack_run_loop_async_context_headers SYSTEM INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include) pico_mirrored_target_link_libraries(pico_btstack_run_loop_async_context INTERFACE pico_btstack_base pico_async_context_base) ``` -------------------------------- ### Link Pico Libraries Source: https://github.com/raspberrypi/pico-sdk/blob/master/src/rp2_common/hardware_adc/CMakeLists.txt Links specified libraries to the target, defining interface dependencies. This example links 'hardware_adc' with dependencies on 'hardware_gpio' and 'hardware_resets'. ```cmake pico_mirrored_target_link_libraries(hardware_adc INTERFACE hardware_gpio hardware_resets) ``` -------------------------------- ### Enable USB and Semihosting STDOUT/STDERR Source: https://github.com/raspberrypi/pico-sdk/blob/master/test/kitchen_sink/CMakeLists.txt Configures the 'kitchen_sink_extra_stdio' executable to enable both USB and semihosting for standard input/output streams. It also links necessary libraries and adds extra outputs. ```cmake add_executable(kitchen_sink_extra_stdio ${CMAKE_CURRENT_LIST_DIR}/kitchen_sink.c) target_link_libraries(kitchen_sink_extra_stdio kitchen_sink_libs kitchen_sink_options) pico_add_extra_outputs(kitchen_sink_extra_stdio) pico_enable_stdio_usb(kitchen_sink_extra_stdio 1) pico_enable_stdio_semihosting(kitchen_sink_extra_stdio 1) ``` -------------------------------- ### CMake: Configure pico_stdio_test_uart for UART Source: https://github.com/raspberrypi/pico-sdk/blob/master/test/pico_stdio_test/CMakeLists.txt Builds the `pico_stdio_test_uart` executable, enabling STDIO over UART and disabling it for USB and RTT. This is useful for testing serial communication. ```cmake if (TARGET pico_multicore AND NOT PICO_TIME_NO_ALARM_SUPPORT) add_executable(pico_stdio_test_uart pico_stdio_test.c) target_link_libraries(pico_stdio_test_uart PRIVATE pico_stdlib pico_test pico_multicore) pico_add_extra_outputs(pico_stdio_test_uart) pico_enable_stdio_uart(pico_stdio_test_uart 1) pico_enable_stdio_usb(pico_stdio_test_uart 0) pico_enable_stdio_rtt(pico_stdio_test_uart 0) endif() ``` -------------------------------- ### CMake: Configure pico_stdio_test_rtt for RTT Source: https://github.com/raspberrypi/pico-sdk/blob/master/test/pico_stdio_test/CMakeLists.txt Builds the `pico_stdio_test_rtt` executable, enabling STDIO over RTT and disabling it for USB and UART. RTT is often used with debug probes. ```cmake if (TARGET pico_multicore AND NOT PICO_TIME_NO_ALARM_SUPPORT) add_executable(pico_stdio_test_rtt pico_stdio_test.c) target_link_libraries(pico_stdio_test_rtt PRIVATE pico_stdlib pico_test pico_multicore) pico_add_extra_outputs(pico_stdio_test_rtt) pico_enable_stdio_uart(pico_stdio_test_rtt 0) pico_enable_stdio_usb(pico_stdio_test_rtt 0) pico_enable_stdio_rtt(pico_stdio_test_rtt 1) endif() ``` -------------------------------- ### Configure Pico Runtime Initialization Library (CMake) Source: https://github.com/raspberrypi/pico-sdk/blob/master/src/rp2_common/pico_runtime_init/CMakeLists.txt Defines the `pico_runtime_init` library for the Raspberry Pi Pico SDK. It specifies the source files, include directories, and links against core SDK libraries and optional hardware modules like clocks, timers, and voltage regulators. ```CMake pico_add_library(pico_runtime_init) target_sources(pico_runtime_init INTERFACE ${CMAKE_CURRENT_LIST_DIR}/runtime_init.c ${CMAKE_CURRENT_LIST_DIR}/runtime_init_clocks.c ${CMAKE_CURRENT_LIST_DIR}/runtime_init_stack_guard.c ) target_include_directories(pico_runtime_init_headers SYSTEM INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include) pico_mirrored_target_link_libraries(pico_runtime_init INTERFACE pico_base ) if (TARGET hardware_clocks) pico_mirrored_target_link_libraries(pico_runtime_init INTERFACE hardware_clocks) endif() if (TARGET hardware_timer) pico_mirrored_target_link_libraries(pico_runtime_init INTERFACE hardware_timer) endif() if (TARGET hardware_vreg) pico_mirrored_target_link_libraries(pico_runtime_init INTERFACE hardware_vreg) endif() # pico/runtime_init.h includes pico/runtime.h target_link_libraries(pico_runtime_init_headers INTERFACE pico_runtime_headers) ``` -------------------------------- ### CMake: Define pico_sync_headers Library Source: https://github.com/raspberrypi/pico-sdk/blob/master/src/common/pico_sync/CMakeLists.txt Defines an interface library for synchronization headers, linking against hardware and time header libraries. This is typically used for header-only targets. ```CMake if (NOT TARGET pico_sync_headers) add_library(pico_sync_headers INTERFACE) target_link_libraries(pico_sync_headers INTERFACE hardware_sync_headers pico_time_headers) endif() ``` -------------------------------- ### CMake: Configure pico_stdio_test_usb for USB Source: https://github.com/raspberrypi/pico-sdk/blob/master/test/pico_stdio_test/CMakeLists.txt Builds the `pico_stdio_test_usb` executable, enabling STDIO over USB and disabling it for UART and RTT. It also sets a timeout for USB connection waiting. ```cmake if (TARGET pico_multicore AND NOT PICO_TIME_NO_ALARM_SUPPORT) add_executable(pico_stdio_test_usb pico_stdio_test.c) target_link_libraries(pico_stdio_test_usb PRIVATE pico_stdlib pico_test pico_multicore) target_compile_definitions(pico_stdio_test_usb PRIVATE PICO_STDIO_USB_CONNECT_WAIT_TIMEOUT_MS=-1) # wait for USB connect pico_add_extra_outputs(pico_stdio_test_usb) pico_enable_stdio_uart(pico_stdio_test_usb 0) pico_enable_stdio_usb(pico_stdio_test_usb 1) pico_enable_stdio_rtt(pico_stdio_test_usb 0) endif() ``` -------------------------------- ### pico_set_binary_type CMake Function Source: https://github.com/raspberrypi/pico-sdk/blob/master/src/rp2_common/pico_standard_link/CMakeLists.txt Sets the binary type for a CMake target. This function is used to configure how the binary is built, for example, whether it runs from flash or RAM, by setting the `PICO_TARGET_BINARY_TYPE` property. ```cmake # pico_set_binary_type(TARGET TYPE) # \brief\ Set the binary type for the target # # \param\ TYPE The binary type to set function(pico_set_binary_type TARGET TYPE) set_target_properties(${TARGET} PROPERTIES PICO_TARGET_BINARY_TYPE ${TYPE}) endfunction() ``` -------------------------------- ### pico_add_uf2_output CMake Function Source: https://github.com/raspberrypi/pico-sdk/blob/master/tools/CMakeLists.txt This CMake function defines a post-build custom command to generate a UF2 bootloader file for a given target. It uses the `picotool` to convert the built executable into the UF2 format, supporting custom output paths, additional arguments, and board-specific features like RP2350 A2 support. Dependencies include `picotool` being found and the target having necessary properties set. ```CMAKE function(pico_add_uf2_output TARGET) pico_init_picotool() get_target_property(${TARGET}_archive_directory ${TARGET} ARCHIVE_OUTPUT_DIRECTORY) if (${TARGET}_archive_directory) get_filename_component(output_path "${${TARGET}_archive_directory}" REALPATH BASE_DIR "${CMAKE_CURRENT_BINARY_DIR}") file(MAKE_DIRECTORY "${output_path}") set(output_path "${output_path}/") else() set(output_path "") endif() pico_get_output_name(${TARGET} output_name) get_target_property(${TARGET}_uf2_package_addr ${TARGET} PICOTOOL_UF2_PACKAGE_ADDR) if (${TARGET}_uf2_package_addr) set(uf2_package_args "-o;${${TARGET}_uf2_package_addr}") endif() get_target_property(extra_uf2_args ${TARGET} PICOTOOL_EXTRA_UF2_ARGS) if (PICO_RP2350_A2_SUPPORTED) if (NOT extra_uf2_args) set(extra_uf2_args "--abs-block") elseif(NOT "--abs-block" IN_LIST extra_uf2_args) list(APPEND extra_uf2_args "--abs-block") endif() else() if (NOT extra_uf2_args) set(extra_uf2_args "") endif() endif() if (picotool_FOUND) get_target_property(picotool_family ${TARGET} PICOTOOL_UF2_FAMILY) if (NOT picotool_family) if (PICOTOOL_DEFAULT_FAMILY) set(picotool_family ${PICOTOOL_DEFAULT_FAMILY}) else() set(picotool_family ${PICO_PLATFORM}) endif() endif() add_custom_command(TARGET ${TARGET} POST_BUILD COMMAND picotool ARGS uf2 convert --quiet ${uf2_package_args} $ "${output_path}$>,$,$>.uf2" --family ${picotool_family} ${extra_uf2_args} COMMAND_EXPAND_LISTS VERBATIM BYPRODUCTS "${output_path}$>,$,$>.uf2") endif() endfunction() ``` -------------------------------- ### CMake: Define and Link Libraries Source: https://github.com/raspberrypi/pico-sdk/blob/master/test/kitchen_sink/CMakeLists.txt This snippet demonstrates how to define an interface library ('kitchen_sink_libs') and link various hardware and pico-specific libraries to it. It also includes logic to generate a combined header file with necessary includes based on linked libraries. ```CMake add_library(kitchen_sink_libs INTERFACE) set(KITCHEN_SINK_LIBS hardware_adc hardware_boot_lock hardware_claim hardware_clocks hardware_dcp hardware_divider hardware_dma hardware_exception hardware_flash hardware_gpio hardware_hazard3 hardware_i2c hardware_interp hardware_irq hardware_pio hardware_pll hardware_powman hardware_pwm hardware_rcp hardware_resets hardware_riscv hardware_riscv_platform_timer hardware_rtc hardware_sha256 hardware_spi hardware_sync hardware_sync_spin_lock hardware_ticks hardware_timer hardware_uart hardware_vreg hardware_watchdog hardware_xip_cache hardware_xosc pico_aon_timer pico_bit_ops pico_bootrom pico_bootsel_via_double_reset pico_divider pico_double pico_fix_rp2040_usb_device_enumeration pico_flash pico_float pico_i2c_slave pico_int64_ops pico_malloc pico_mem_ops pico_multicore pico_platform pico_printf pico_rand pico_runtime pico_runtime_init pico_sha256 pico_status_led pico_stdio pico_stdlib pico_sync pico_time pico_unique_id pico_util ) set(KITCHEN_SINK_NO_HEADER_LIBS hardware_dcp pico_bootsel_via_double_reset pico_int64_ops # currently empty, and only included by _pico variant pico_mem_ops # currently empty, and only included by _pico variant ) set(KITCHEN_SINK_INCLUDES "#pragma once\n") foreach(LIB IN LISTS KITCHEN_SINK_LIBS) if (TARGET ${LIB}) target_link_libraries(kitchen_sink_libs INTERFACE ${LIB}) string(REGEX MATCH "([a-z]+)_(.+)" HAS_MATCH ${LIB}) if (HAS_MATCH AND NOT LIB IN_LIST KITCHEN_SINK_NO_HEADER_LIBS) # these are few, so just hack fixing for now if (LIB STREQUAL "pico_util") string(APPEND KITCHEN_SINK_INCLUDES "#include \"pico/util/datetime.h\"\n") string(APPEND KITCHEN_SINK_INCLUDES "#include \"pico/util/pheap.h\"\n") string(APPEND KITCHEN_SINK_INCLUDES "#include \"pico/util/queue.h\"\n") else() if ("${CMAKE_MATCH_2}" STREQUAL "fix_rp2040_usb_device_enumeration") set(CMAKE_MATCH_2 "fix/rp2040_usb_device_enumeration") elseif ("${CMAKE_MATCH_2}" STREQUAL "sync_spin_lock") set(CMAKE_MATCH_2 "sync/spin_lock") endif() string(APPEND KITCHEN_SINK_INCLUDES "#include \"${CMAKE_MATCH_1}/${CMAKE_MATCH_2}.h\"\n") endif() endif() endif() endforeach () set(KITCHEN_SINK_INCLUDE_HEADER "${CMAKE_CURRENT_BINARY_DIR}/kitchen_sink_includes.h") file(GENERATE OUTPUT ${KITCHEN_SINK_INCLUDE_HEADER} CONTENT ${KITCHEN_SINK_INCLUDES}) ``` -------------------------------- ### Check Picotool Default Keys Source: https://github.com/raspberrypi/pico-sdk/blob/master/tools/CMakeLists.txt Checks if a target is using default signing or encryption keys. It utilizes `picotool_compare_keys` to compare the target's specified signing and encryption files against the default example keys. ```cmake function(picotool_check_default_keys TARGET) get_target_property(picotool_sigfile ${TARGET} PICOTOOL_SIGFILE) picotool_compare_keys(${TARGET} ${picotool_sigfile} private.pem "signing") get_target_property(picotool_aesfile ${TARGET} PICOTOOL_AESFILE) picotool_compare_keys(${TARGET} ${picotool_aesfile} privateaes.bin "encryption") get_target_property(picotool_enc_sigfile ${TARGET} PICOTOOL_ENC_SIGFILE) picotool_compare_keys(${TARGET} ${picotool_enc_sigfile} private.pem "encrypted signing") endfunction() ``` -------------------------------- ### Pico SDK Simple Hardware Target Source: https://github.com/raspberrypi/pico-sdk/blob/master/src/rp2_common/hardware_sha256/CMakeLists.txt This function likely configures the Pico SDK to target a simple hardware setup. The SHA256 hash is provided for integrity verification of the SDK or a specific build. ```c pico_simple_hardware_target(sha256) ``` -------------------------------- ### CMakeLists.txt: Automatic Download from GitHub Source: https://github.com/raspberrypi/pico-sdk/blob/master/README.md Configures a CMake project to automatically download the Raspberry Pi Pico SDK from GitHub using `pico_sdk_import.cmake` and setting `PICO_SDK_FETCH_FROM_GIT`. ```cmake cmake_minimum_required(VERSION 3.13) # initialize pico-sdk from GIT # (note this can come from environment, CMake cache etc) set(PICO_SDK_FETCH_FROM_GIT on) # pico_sdk_import.cmake is a single file copied from this SDK # note: this must happen before project() include(pico_sdk_import.cmake) project(my_project) # initialize the Raspberry Pi Pico SDK pico_sdk_init() # rest of your project ``` -------------------------------- ### Pico SDK Float Library Setup Source: https://github.com/raspberrypi/pico-sdk/blob/master/src/rp2_common/pico_float/CMakeLists.txt Configures the core pico_float library, including its default implementation and linking. It sets up aliases and determines the default floating-point implementation based on project properties. ```cmake if (NOT TARGET pico_float) # library to be depended on - we make this depend on particular implementations using per target generator expressions pico_add_library(pico_float) # no custom implementation; falls thru to compiler pico_add_library(pico_float_compiler) target_include_directories(pico_float_headers SYSTEM INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include) # add alias "default" which is just pico. add_library(pico_float_default INTERFACE) target_link_libraries(pico_float_default INTERFACE pico_float_pico) set(PICO_DEFAULT_FLOAT_IMPL pico_float_default) target_link_libraries(pico_float INTERFACE $>,$,${PICO_DEFAULT_FLOAT_IMPL}>) endif() ``` -------------------------------- ### Pico Float None Library Setup Source: https://github.com/raspberrypi/pico-sdk/blob/master/src/rp2_common/pico_float/CMakeLists.txt Defines and configures the `pico_float_none` library, which uses a specific assembly source file (`float_none.S`) and links against the `pico_float_headers`. It also applies the `wrap_float_functions` utility to this library. ```cmake pico_add_library(pico_float_none) target_sources(pico_float_none INTERFACE ${CMAKE_CURRENT_LIST_DIR}/float_none.S ) target_link_libraries(pico_float_none INTERFACE pico_float_headers) wrap_float_functions(pico_float_none) # we wrap all functions ``` -------------------------------- ### Include Tools Subdirectory and Promote Variables (CMake) Source: https://github.com/raspberrypi/pico-sdk/blob/master/src/rp2_common/pico_lwip/CMakeLists.txt Includes the 'tools' subdirectory and promotes common scope variables, which are standard build system operations for managing project structure and build configurations. ```cmake pico_add_subdirectory(tools) pico_promote_common_scope_vars() ``` -------------------------------- ### Pico SDK: CYW43 HCI Transport Library Setup Source: https://github.com/raspberrypi/pico-sdk/blob/master/src/rp2_common/pico_cyw43_driver/CMakeLists.txt Sets up the pico_btstack_hci_transport_cyw43 library, which provides the HCI transport layer for the CYW43 chip. It defines sources and compile definitions for Bluetooth support. ```cmake pico_add_library(pico_btstack_hci_transport_cyw43 NOFLAG) target_sources(pico_btstack_hci_transport_cyw43 INTERFACE ${CMAKE_CURRENT_LIST_DIR}/btstack_hci_transport_cyw43.c ${CMAKE_CURRENT_LIST_DIR}/btstack_chipset_cyw43.c ) target_include_directories(pico_btstack_hci_transport_cyw43_headers SYSTEM INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include ) target_compile_definitions(pico_btstack_hci_transport_cyw43_headers INTERFACE CYW43_ENABLE_BLUETOOTH=1 ) ```