### TinyUSB Dual Examples Setup Source: https://github.com/raspberrypi/pico-examples/blob/master/usb/dual/CMakeLists.txt Configures the build system to include and build dual-role USB examples using the TinyUSB library for the RP2040. ```CMake set(FAMILY rp2040) set(BOARD pico_sdk) set(TINYUSB_FAMILY_PROJECT_NAME_PREFIX "tinyusb_dual_") add_subdirectory(${PICO_TINYUSB_PATH}/examples/dual tinyusb_dual_examples) ``` -------------------------------- ### CMake Project Setup and TinyUSB Configuration Source: https://github.com/raspberrypi/pico-examples/blob/master/usb/device/CMakeLists.txt This snippet demonstrates the initial CMake setup for the Raspberry Pi Pico project. It defines the target family, board, and configures essential paths and variables for the TinyUSB library, including handling potential LWIP path issues and setting the TinyUSB path for specific examples. ```cmake set(FAMILY rp2040) set(BOARD pico_sdk) set(TINYUSB_FAMILY_PROJECT_NAME_PREFIX "tinyusb_dev_") # Provide an LWIP path for net_lwip_webserver in case the module does not exist in tinyusb set(TINYUSB_LWIP_PATH ${PICO_LWIP_PATH}) # Some examples use this, and we need to set this here due to a bug in the TinyUSB CMake config set(TOP ${PICO_TINYUSB_PATH}) ``` -------------------------------- ### Hello Interpolator Example Source: https://github.com/raspberrypi/pico-examples/blob/master/README.md Demonstrates accessing the core-local interpolator hardware and utilizing its features. This example serves as a starting point for understanding and using the interpolator. ```c #include "pico/stdlib.h" #include "hardware/interp.h" int main() { stdio_init_all(); // Initialize interpolator interp_config cfg = interp_default_config(); interp_config_set_wrap(&cfg, false, false); interp_set_config(interp0, interp_cr0, &cfg); // Example usage: linear interpolation interp_set_linear_fraction(interp0, 0.5f); interp_set_accumulator(interp0, 100); printf("Interpolated value: %d\n", interp_get_accumulator(interp0)); return 0; } ``` -------------------------------- ### BTstack Example Configuration Source: https://github.com/raspberrypi/pico-examples/blob/master/pico_w/bt/CMakeLists.txt Configures the inclusion of BTstack examples by iterating through a list of example names. It checks for directory existence, handles dependencies on 'pico-extras', and allows filtering of additional examples based on the 'BTSTACK_EXAMPLES_ALL' variable. Finally, it adds the subdirectory for each valid example and logs the total count. ```cmake set(BTSTACK_EXAMPLE_COUNT 0) list(REMOVE_DUPLICATES BTSTACK_EXAMPLES) foreach(NAME ${BTSTACK_EXAMPLES}) # Ignore if sub folder not found if (NOT IS_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/${NAME}) continue() endif() # Ignore example if it needs pico-extras and pico-extras could not be found if (NOT PICO_EXTRAS_PATH AND ${NAME} IN_LIST BTSTACK_EXAMPLES_NEED_EXTRAS) continue() endif() # Ignore less interesting examples if BTSTACK_EXAMPLES_ALL=1 is not set if (NOT BTSTACK_EXAMPLES_ALL AND ${NAME} IN_LIST BTSTACK_EXAMPLES_ADDITIONAL) continue() endif() # add example add_subdirectory_exclude_platforms(${NAME}) MATH(EXPR BTSTACK_EXAMPLE_COUNT "${BTSTACK_EXAMPLE_COUNT}+1") endforeach() message("Adding ${BTSTACK_EXAMPLE_COUNT} BTstack examples of type '${BTSTACK_EXAMPLE_TYPE}'") suppress_btstack_warnings() ``` -------------------------------- ### PIO Squarewave Div Sync Build Configuration Source: https://github.com/raspberrypi/pico-examples/blob/master/pio/squarewave/CMakeLists.txt Configures the build for the 'pio_squarewave_div_sync' executable. This setup is similar to the main squarewave example, generating PIO headers and linking necessary libraries, but uses a different source file ('squarewave_div_sync.c'). ```cmake add_executable(pio_squarewave_div_sync) pico_generate_pio_header(pio_squarewave_div_sync ${CMAKE_CURRENT_LIST_DIR}/squarewave.pio) target_sources(pio_squarewave_div_sync PRIVATE squarewave_div_sync.c ) target_link_libraries(pio_squarewave_div_sync PRIVATE pico_stdlib hardware_pio ) pico_add_extra_outputs(pio_squarewave_div_sync) example_auto_set_url(pio_squarewave_div_sync) ``` -------------------------------- ### CMake Project Setup and SDK Initialization Source: https://github.com/raspberrypi/pico-examples/blob/master/CMakeLists.txt Configures the CMake build system for Raspberry Pi Pico projects. It sets the minimum required CMake version, includes the Pico SDK, and initializes the SDK. It also enforces a minimum SDK version (2.2.0) and sets C and C++ standards. ```cmake cmake_minimum_required(VERSION 3.12) # Pull in SDK (must be before project) include(pico_sdk_import.cmake) include(pico_extras_import_optional.cmake) project(pico_examples C CXX ASM) set(CMAKE_C_STANDARD 11) set(CMAKE_CXX_STANDARD 17) if (PICO_SDK_VERSION_STRING VERSION_LESS "2.2.0") message(FATAL_ERROR "Raspberry Pi Pico SDK version 2.2.0 (or later) required. Your version is ${PICO_SDK_VERSION_STRING}") endif() pico_sdk_init() include(example_auto_set_url.cmake) ``` -------------------------------- ### TinyUSB Host Examples Source: https://github.com/raspberrypi/pico-examples/blob/master/README.md These examples are directly sourced from the TinyUSB host examples directory. They are automatically included in the pico-examples build if supported on RP2040 and RP2350 devices, prefixed with `tinyusb_host_`. ```c #include "tusb.h" // Example: cdc_msc_hid // This example demonstrates a USB host that can enumerate and interact with // devices that provide CDC (Communication Device Class), MSC (Mass Storage Class), // and HID (Human Interface Device) interfaces. // It's built as `tinyusb_host_cdc_msc_hid`. void main(void) { // Initialize TinyUSB host stack tusb_init(); // Host task loop while (1) { // Poll TinyUSB host stack tud_task(); // Host specific tasks would go here // e.g., enumerating devices, sending/receiving data via specific classes } } ``` -------------------------------- ### Hardware-Specific Example Subdirectories Source: https://github.com/raspberrypi/pico-examples/blob/master/CMakeLists.txt Includes various hardware-specific example directories. Each `add_subdirectory` call integrates the examples within that directory into the build system. ```cmake add_subdirectory(adc) add_subdirectory(binary_info) add_subdirectory(bootloaders) add_subdirectory(clocks) add_subdirectory(cmake) add_subdirectory(dcp) add_subdirectory(divider) add_subdirectory(dma) add_subdirectory(encrypted) add_subdirectory(flash) add_subdirectory(gpio) add_subdirectory(hstx) add_subdirectory(i2c) add_subdirectory(interp) add_subdirectory(multicore) add_subdirectory(otp) add_subdirectory(picoboard) add_subdirectory(pico_w) add_subdirectory(pio) add_subdirectory(pwm) add_subdirectory(reset) add_subdirectory(rtc) add_subdirectory(spi) add_subdirectory(status_led) add_subdirectory(system) add_subdirectory(timer) add_subdirectory(uart) add_subdirectory(universal) add_subdirectory(usb) add_subdirectory(watchdog) add_subdirectory(sha) add_subdirectory(freertos) ``` -------------------------------- ### Configurable Hello World Binary Example Source: https://github.com/raspberrypi/pico-examples/blob/master/README.md Uses `bi_ptr` variables to create a configurable 'Hello World' binary. This allows for customization of the program's behavior at build time. Refer to the separate README for more details. ```c // This is a placeholder. Actual code would involve bi_ptr usage. #include #include "pico/stdlib.h" // Assume bi_ptr is defined and configured elsewhere // extern const char* GREETING_MESSAGE; int main() { // Example usage (assuming GREETING_MESSAGE is configured via bi_ptr) // stdio_init_all(); // printf("%s\n", GREETING_MESSAGE); return 0; } ``` -------------------------------- ### Pico Multicore Examples Setup (CMake) Source: https://github.com/raspberrypi/pico-examples/blob/master/multicore/CMakeLists.txt Configures the build system for various multicore examples on the Raspberry Pi Pico. It includes conditional compilation based on the target platform and excludes specific platforms like RP2350 for certain examples due to known issues. ```cmake if (TARGET pico_multicore) add_subdirectory_exclude_platforms(hello_multicore host) # currently broken on RP2350 due to both cores sharing IRQ add_subdirectory_exclude_platforms(multicore_fifo_irqs host "rp2350.*") add_subdirectory_exclude_platforms(multicore_runner host) add_subdirectory_exclude_platforms(multicore_runner_queue host) add_subdirectory_exclude_platforms(multicore_doorbell host rp2040) else() message("Skipping multicore examples as pico_multicore is unavailable on this platform") endif() ``` -------------------------------- ### Loading Firmware with Picotool Source: https://github.com/raspberrypi/pico-examples/blob/master/pico_w/wifi/ota_update/README.md Demonstrates loading the Wi-Fi firmware and the main application UF2 files onto the Pico W using `picotool`. The `-x` flag for the application UF2 indicates that it should be executed after loading. ```bash picotool load picow_ota_update_wifi_firmware.uf2 picotool load -x picow_ota_update.uf2 ``` -------------------------------- ### PicoW Blink Example Configuration Source: https://github.com/raspberrypi/pico-examples/blob/master/pico_w/wifi/blink/CMakeLists.txt Configures the 'picow_blink' executable using pico_stdlib and pico_cyw43_arch_none. This is a basic setup for blinking an LED on the Pico W. ```cmake add_executable(picow_blink picow_blink.c ) target_link_libraries(picow_blink pico_stdlib # for core functionality pico_cyw43_arch_none # we need Wifi to access the GPIO, but we don't need anything else ) # create map/bin/hex file etc. pico_add_extra_outputs(picow_blink) # add url via pico_set_program_url example_auto_set_url(picow_blink) ``` -------------------------------- ### Pico W BT Example Common Library Configuration Source: https://github.com/raspberrypi/pico-examples/blob/master/pico_w/bt/CMakeLists.txt Defines a common interface library for Pico W Bluetooth examples, including common source files, linking to pico_stdlib and pico_btstack_cyw43, and setting up include directories and compile definitions. It also conditionally links the audio example and defines TEST_AUDIO if available. ```cmake # Adds common stuff for all the examples add_library(picow_bt_example_common INTERFACE) target_sources(picow_bt_example_common INTERFACE ${CMAKE_CURRENT_LIST_DIR}/picow_bt_example_common.c ) target_link_libraries(picow_bt_example_common INTERFACE pico_stdlib pico_btstack_cyw43 ) target_include_directories(picow_bt_example_common INTERFACE ${BT_EXAMPLE_COMMON_DIR}/config # Use our own config ${BTSTACK_EXAMPLE_PATH}/ ) target_compile_definitions(picow_bt_example_common INTERFACE PICO_STDIO_USB_CONNECT_WAIT_TIMEOUT_MS=3000 #WANT_HCI_DUMP=1 # This enables btstack debug #ENABLE_SEGGER_RTT=1 ) if (TARGET pico_btstack_audio_example) target_link_libraries(picow_bt_example_common INTERFACE pico_btstack_audio_example ) target_compile_definitions(picow_bt_example_common INTERFACE TEST_AUDIO=1 ) endif() ``` -------------------------------- ### CMakeLists.txt Configuration for boot_info Source: https://github.com/raspberrypi/pico-examples/blob/master/system/boot_info/CMakeLists.txt This snippet shows the CMakeLists.txt configuration for the 'boot_info' example. It defines the executable, links necessary libraries, and specifies extra output formats. ```cmake add_executable(boot_info boot_info.c ) target_link_libraries(boot_info pico_stdlib ) # create map/bin/hex file etc. pico_add_extra_outputs(boot_info) # add url via pico_set_program_url example_auto_set_url(boot_info) ``` -------------------------------- ### BLE Temperature Sensor with Wi-Fi (Standalone) Source: https://github.com/raspberrypi/pico-examples/blob/master/README.md Similar to the BLE temperature sensor, but also connects to Wi-Fi and starts an 'iperf' server. This example demonstrates dual connectivity. ```c /* * pico_w/bt/standalone/picow_ble_temp_sensor_with_wifi.c * Same as above but also connects to Wi-Fi and starts an "iperf" server. */ #include "pico/stdlib.h" #include "hardware/adc.h" #include "btstack_run_loop.h" #include "btstack_uart_block_driver.h" #include "btstack_chipset_rp2040.h" #include "btstack_config.h" #include "btstack_core.h" #include "btstack_event.h" #include "ble/att_server.h" #include "ble/gatt_client.h" #include "ble/le_device_db.h" #include "ble/sm.h" #include "lwip/netif.h" #include "lwip/ip4_addr.h" #include "lwip/sockets.h" #include "pico/cyw43_arch.h" // ... (rest of the picow_ble_temp_sensor_with_wifi.c code) ``` -------------------------------- ### CMake Build Configuration for hello_adc Source: https://github.com/raspberrypi/pico-examples/blob/master/adc/hello_adc/CMakeLists.txt This snippet shows the CMake commands used to build the 'hello_adc' example. It defines the executable, links the required Pico SDK libraries, and configures the generation of various output formats. ```cmake add_executable(hello_adc hello_adc.c ) target_link_libraries(hello_adc pico_stdlib hardware_adc) # create map/bin/hex file etc. pico_add_extra_outputs(hello_adc) # add url via pico_set_program_url example_auto_set_url(hello_adc) ``` -------------------------------- ### SPI Master Example Source: https://github.com/raspberrypi/pico-examples/blob/master/spi/spi_master_slave/CMakeLists.txt This snippet demonstrates the setup for the SPI master functionality on the Raspberry Pi Pico. It likely involves configuring the SPI peripheral and initializing communication. ```cmake add_subdirectory_exclude_platforms(spi_master) ``` -------------------------------- ### SPI Slave Example Source: https://github.com/raspberrypi/pico-examples/blob/master/spi/spi_master_slave/CMakeLists.txt This snippet demonstrates the setup for the SPI slave functionality on the Raspberry Pi Pico. It likely involves configuring the SPI peripheral to act as a slave device. ```cmake add_subdirectory_exclude_platforms(spi_slave) ``` -------------------------------- ### CMakeLists.txt Configuration for hello_usb Source: https://github.com/raspberrypi/pico-examples/blob/master/hello_world/usb/CMakeLists.txt This snippet configures the CMake build system for the hello_usb example. It defines the executable, links necessary libraries, and configures standard output to use USB. ```cmake if (TARGET tinyusb_device) add_executable(hello_usb hello_usb.c ) # pull in common dependencies target_link_libraries(hello_usb pico_stdlib) # enable usb output, disable uart output pico_enable_stdio_usb(hello_usb 1) pico_enable_stdio_uart(hello_usb 0) # create map/bin/hex/uf2 file etc. pico_add_extra_outputs(hello_usb) # add url via pico_set_program_url example_auto_set_url(hello_usb) elseif(PICO_ON_DEVICE) message("Skipping hello_usb because TinyUSB submodule is not initialized in the SDK") endif() ``` -------------------------------- ### Integrate lwIP DHCP Server with Pico Examples Source: https://github.com/raspberrypi/pico-examples/blob/master/pico_w/bt/pan_lwip_http_server/CMakeLists.txt This section demonstrates how to integrate the lwIP DHCP server with various Pico examples, specifically focusing on HTTP server and Bluetooth stack configurations. It utilizes custom macros like `picow_bt_example_target_name`, `picow_bt_example_poll`, `picow_bt_example_background`, and `picow_bt_example_freertos` for streamlined setup. ```cmake picow_bt_example_target_name(pan_lwip_http_server TARGET_NAME) picow_bt_example_poll(pan_lwip_http_server ${TARGET_NAME} picow_bt_example_btstack_lwip_poll pan_lwip_dhserver pico_lwip_http pico_btstack_classic) picow_bt_example_background(pan_lwip_http_server ${TARGET_NAME} picow_bt_example_btstack_lwip_background pan_lwip_dhserver pico_lwip_http pico_btstack_classic) picow_bt_example_freertos(pan_lwip_http_server ${TARGET_NAME} picow_bt_example_btstack_lwip_freertos pan_lwip_dhserver pico_lwip_http pico_btstack_classic) ``` -------------------------------- ### PIO SPI Loopback Example Configuration Source: https://github.com/raspberrypi/pico-examples/blob/master/pio/spi/CMakeLists.txt Configures the 'pio_spi_loopback' executable for the Raspberry Pi Pico. This setup involves generating the PIO header, linking pico_stdlib and hardware_pio libraries, and defining the source files. ```cmake add_executable(pio_spi_loopback) pico_generate_pio_header(pio_spi_loopback ${CMAKE_CURRENT_LIST_DIR}/spi.pio) target_sources(pio_spi_loopback PRIVATE spi_loopback.c pio_spi.c pio_spi.h ) target_link_libraries(pio_spi_loopback PRIVATE pico_stdlib hardware_pio) pico_add_extra_outputs(pio_spi_loopback) example_auto_set_url(pio_spi_loopback) ``` -------------------------------- ### BTstack SCO Demo Utility Library Configuration Source: https://github.com/raspberrypi/pico-examples/blob/master/pico_w/bt/CMakeLists.txt Sets up an interface library for SCO (Synchronous Connection-Oriented) demo utilities, linking the necessary source files from the BTstack examples directory. ```cmake add_library(pico_btstack_sco_demo_util INTERFACE) target_sources(pico_btstack_sco_demo_util INTERFACE # sco demo utils ${BTSTACK_EXAMPLE_PATH}/sco_demo_util.c ) ``` -------------------------------- ### FreeRTOS HTTP Client (nosys) Source: https://github.com/raspberrypi/pico-examples/blob/master/pico_w/wifi/freertos/http_client/CMakeLists.txt Configures the HTTP client example using the `pico_cyw43_arch_lwip_threadsafe_background` architecture. This setup is noted to have potential stack issues with mbedtls and FreeRTOS SMP. WiFi and LwIP activity occur in an IRQ. ```cmake if (0) add_executable(picow_freertos_http_client_nosys picow_freertos_http_client.c ) target_compile_definitions(picow_freertos_http_client_nosys PRIVATE WIFI_SSID="${WIFI_SSID}" WIFI_PASSWORD="${WIFI_PASSWORD}" ALTCP_MBEDTLS_AUTHMODE=MBEDTLS_SSL_VERIFY_REQUIRED ) target_include_directories(picow_freertos_http_client_nosys PRIVATE ${CMAKE_CURRENT_LIST_DIR} ${CMAKE_CURRENT_LIST_DIR}/.. # for our common FreeRTOSConfig ${CMAKE_CURRENT_LIST_DIR}/../.. # for our common lwipopts and mbedtls_config ) target_link_libraries(picow_freertos_http_client_nosys pico_cyw43_arch_lwip_threadsafe_background pico_stdlib example_lwip_http_util FreeRTOS-Kernel-Heap4 # FreeRTOS kernel and dynamic heap ) pico_add_extra_outputs(picow_freertos_http_client_nosys) endif() ``` -------------------------------- ### Universal Build Process Explanation Source: https://github.com/raspberrypi/pico-examples/blob/master/universal/CMakeLists.txt This section explains the core concept of the universal build process. It details how separate binaries are compiled for each platform and then linked into a single block loop. This universal binary is designed to execute on any compatible platform, with the bootrom handling the selection and execution of the appropriate binary based on the hardware. ```comment # The way this universal build works is it builds separate binaries for each platform, # then links them into a single block loop. This universal binary will then run on any # platform # - On RP2040 the bootrom will just execute the RP2040 binary at the start of flash # - On RP2350 the bootrom will search the block loop for the appropriate IMAGE_DEF for # Arm/RISC-V, translate it's address to the start of flash using the rolling window # delta, and execute it on the appropriate CPU # # The build will output a TARGET.bin file which can be written using picotool, and a ``` -------------------------------- ### Bluetooth GATT Counter Setup Source: https://github.com/raspberrypi/pico-examples/blob/master/pico_w/bt/gatt_counter_with_wifi/CMakeLists.txt Configures the Bluetooth GATT counter example by setting the target name and associating it with Wi-Fi and BLE functionalities. It specifies the polling, background, and FreeRTOS functions to be used. ```cmake set(NAME gatt_counter) picow_bt_example_target_name(${NAME}_with_wifi TARGET_NAME) picow_bt_example_poll(${NAME} ${TARGET_NAME} picow_bt_example_cyw43_lwip_poll pico_btstack_ble) picow_bt_example_background(${NAME} ${TARGET_NAME} picow_bt_example_cyw43_lwip_background pico_btstack_ble) picow_bt_example_freertos(${NAME} ${TARGET_NAME} picow_bt_example_cyw43_lwip_freertos pico_btstack_ble) ``` -------------------------------- ### CMake Build Configuration for hello_otp Source: https://github.com/raspberrypi/pico-examples/blob/master/otp/hello_otp/CMakeLists.txt Configures the build process for the hello_otp executable. It defines the source file, links the pico_stdlib library, and sets up extra output formats like bin, hex, and uf2 files. It also automatically sets a program URL. ```cmake add_executable(hello_otp hello_otp.c ) # pull in common dependencies target_link_libraries(hello_otp pico_stdlib) # create map/bin/hex/uf2 file etc. pico_add_extra_outputs(hello_otp) # add url via pico_set_program_url example_auto_set_url(hello_otp) ``` -------------------------------- ### Universal Binary Examples Source: https://github.com/raspberrypi/pico-examples/blob/master/README.md Examples for creating universal binaries that run on both RP2040 and RP2350 architectures (Arm and RISC-V). Requires setting PICO_ARM_TOOLCHAIN_PATH and PICO_RISCV_TOOLCHAIN_PATH. Includes universal versions of blink and nuke examples, and a hello world program with cross-architecture behavior on RP2350. ```cmake # universal/CMakeLists.txt # Example for universal binaries # set(PICO_ARM_TOOLCHAIN_PATH "path/to/arm/toolchain") # set(PICO_RISCV_TOOLCHAIN_PATH "path/to/riscv/toolchain") # add_executable(blink_universal universal/blink_universal.c) # target_link_libraries(blink_universal pico_stdlib) # add_executable(hello_universal universal/hello_universal.c) # target_link_libraries(hello_universal pico_stdlib) # add_executable(nuke_universal universal/nuke_universal.c) # target_link_libraries(nuke_universal pico_stdlib) ``` -------------------------------- ### MMA8451 Accelerometer I2C Example (C) Source: https://github.com/raspberrypi/pico-examples/blob/master/i2c/mma8451_i2c/README.adoc C code to interface with the MMA8451 accelerometer via I2C on the Raspberry Pi Pico. Reads 3-axis acceleration data and allows configuration of range and precision. Requires I2C setup and MMA8451 driver functions. ```c #include "pico/stdlib.h" #include "hardware/i2c.h" #include "mma8451.h" // Assuming this header contains MMA8451 functions #define I2C_PORT i2c0 #define SDA_PIN 4 #define SCL_PIN 5 int main() { stdio_init_all(); // Initialize I2C i2c_init(I2C_PORT, 400000); gpio_set_function(SDA_PIN, GPIO_FUNC_I2C); gpio_set_function(SCL_PIN, GPIO_FUNC_I2C); gpio_pull_up(SDA_PIN); gpio_pull_up(SCL_PIN); // Initialize MMA8451 sensor (assuming mma8451_init function exists) if (mma8451_init(I2C_PORT) < 0) { printf("MMA8451 initialization failed\n"); return 1; } printf("MMA8451 I2C Example\n"); while (1) { float x, y, z; // Read acceleration data (assuming mma8451_read_acceleration function exists) if (mma8451_read_acceleration(I2C_PORT, &x, &y, &z) < 0) { printf("Failed to read acceleration data\n"); } else { printf("X: %.2f, Y: %.2f, Z: %.2f\n", x, y, z); } sleep_ms(500); } return 0; } ``` -------------------------------- ### CMake Configuration for RP2040 USB Host Examples Source: https://github.com/raspberrypi/pico-examples/blob/master/usb/host/CMakeLists.txt This snippet demonstrates CMake configuration for the Raspberry Pi Pico SDK, specifically setting up the project for the RP2040 family and the TinyUSB host examples. It includes setting a prefix for TinyUSB family project names and defining the top-level path for TinyUSB examples. ```Cmake set(FAMILY rp2040) set(BOARD pico_sdk) set(TINYUSB_FAMILY_PROJECT_NAME_PREFIX "tinyusb_host_") # Hack as some host examples use $TOP in their path set(TOP ${PICO_TINYUSB_PATH}) add_subdirectory(${PICO_TINYUSB_PATH}/examples/host tinyusb_host_examples) ``` -------------------------------- ### Max7219 32x8 SPI Example (C) Source: https://github.com/raspberrypi/pico-examples/blob/master/spi/max7219_32x8_spi/README.adoc The C code for interfacing the Raspberry Pi Pico with a Max7219 driving a 32x8 LED display via SPI. It assumes a setup with 4 cascaded Max7219 chips, each controlling an 8x8 matrix. ```c #include "pico/stdlib.h" #include "hardware/spi.h" #include // Define SPI pins and instance #define SPI_PORT spi0 #define SPI_SCK 18 #define SPI_MOSI 19 #define SPI_CS 17 // Max7219 commands #define MAX7219_REG_NOOP 0x00 #define MAX7219_REG_DIGIT0 0x01 #define MAX7219_REG_DIGIT1 0x02 #define MAX7219_REG_DIGIT2 0x03 #define MAX7219_REG_DIGIT3 0x04 #define MAX7219_REG_DIGIT4 0x05 #define MAX7219_REG_DIGIT5 0x06 #define MAX7219_REG_DIGIT6 0x07 #define MAX7219_REG_DIGIT7 0x08 #define MAX7219_REG_DECODEMODE 0x09 #define MAX7219_REG_INTENSITY 0x0A #define MAX7219_REG_SCANLIMIT 0x0B #define MAX7219_REG_SHUTDOWN 0x0C #define MAX7219_REG_DISPLAYTEST 0x0F // Number of Max7219 devices (cascaded) #define NUM_DEVICES 4 // Function to send a command to a specific Max7219 device void max7219_send_cmd(uint8_t cmd, uint8_t data, int device_index) { uint8_t buffer[2]; buffer[0] = cmd; buffer[1] = data; // Select the specific device (adjusting for cascading) // In a cascaded setup, the CS line is typically held low, and data is shifted through. // For simplicity in this example, we'll assume a single CS line controls all, // and the command is sent to all devices in sequence. // A more robust implementation would manage CS per device or use a different approach. gpio_put(SPI_CS, 0); spi_write_blocking(SPI_PORT, buffer, 2); // For cascaded devices, the data needs to be shifted through all devices. // This simplified example sends to one logical device at a time. // A real cascaded implementation would require careful timing and potentially multiple CS pulses or a single long CS pulse. gpio_put(SPI_CS, 1); } // Function to initialize the Max7219 display void max7219_init() { // Initialize SPI spi_init(SPI_PORT, 1000000); // 1 MHz SPI clock gpio_set_function(SPI_SCK, GPIO_FUNC_SPI); gpio_set_function(SPI_MOSI, GPIO_FUNC_SPI); gpio_set_function(SPI_CS, GPIO_FUNC_SIO); gpio_set_dir(SPI_CS, GPIO_OUT); gpio_put(SPI_CS, 1); // Initialize all Max7219 devices for (int i = 0; i < NUM_DEVICES; ++i) { max7219_send_cmd(MAX7219_REG_SCANLIMIT, 7, i); // Scan all 8 digits max7219_send_cmd(MAX7219_REG_DECODEMODE, 0x00, i); // No decode for all digits max7219_send_cmd(MAX7219_REG_DISPLAYTEST, 0x00, i); // Turn off display test max7219_send_cmd(MAX7219_REG_SHUTDOWN, 0x01, i); // Turn on display max7219_send_cmd(MAX7219_REG_INTENSITY, 0x0F, i); // Max intensity } } // Function to display a pattern on the 32x8 display void display_pattern(uint8_t pattern[8]) { // For a 32x8 display with 4 cascaded devices, each device controls 8 columns. // The pattern array should represent the state of each row across all 32 columns. // This example assumes 'pattern' is an array of 8 bytes, where each byte represents a row. // Each bit in the byte corresponds to a pixel in that row. for (int row = 0; row < 8; ++row) { // Send the row data to each device for (int device_index = 0; device_index < NUM_DEVICES; ++device_index) { // In a cascaded setup, the data for a specific row needs to be sent to the correct digit register // of each device. For a 32x8 display, device 0 controls digits 0-7, device 1 controls digits 8-15, etc. // This requires careful mapping of the pattern data to the correct digit registers. // The following is a simplified representation. uint8_t digit_data = pattern[row]; max7219_send_cmd(MAX7219_REG_DIGIT0 + device_index, digit_data, device_index); } } } int main() { stdio_init_all(); max7219_init(); // Example pattern: a simple horizontal line uint8_t test_pattern[8] = { 0xFF, // Row 0: all pixels on 0x81, // Row 1: pixels at ends on 0x42, // Row 2: pixels second from ends on 0x24, // Row 3: pixels third from ends on 0x18, // Row 4: center pixels on 0x24, // Row 5: mirroring Row 3 0x42, // Row 6: mirroring Row 2 0x81 // Row 7: mirroring Row 1 }; while (1) { display_pattern(test_pattern); sleep_ms(500); // Example: clear display uint8_t clear_pattern[8] = {0}; display_pattern(clear_pattern); sleep_ms(500); } return 0; } ``` -------------------------------- ### CMake Build Configuration for hello_serial Source: https://github.com/raspberrypi/pico-examples/blob/master/hello_world/serial/CMakeLists.txt Configures the CMake build system for the hello_serial C example. It defines the executable, links necessary libraries (pico_stdlib), and sets up extra output formats like bin, hex, and uf2 files. It also automatically sets a program URL. ```cmake add_executable(hello_serial hello_serial.c ) # pull in common dependencies target_link_libraries(hello_serial pico_stdlib) # create map/bin/hex/uf2 file etc. pico_add_extra_outputs(hello_serial) # add url via pico_set_program_url example_auto_set_url(hello_serial) ``` -------------------------------- ### FreeRTOS Tasks on One Core Source: https://github.com/raspberrypi/pico-examples/blob/master/README.md Demonstrates the basic setup and execution of FreeRTOS tasks on a single core of the Raspberry Pi Pico. ```c #include "FreeRTOS.h" #include "task.h" #include "pico/stdlib.h" #include // Task function void vTask1(void *pvParameters) { for (;;) { printf ``` -------------------------------- ### Pico Bluetooth HFP HF Demo CMake Configuration Source: https://github.com/raspberrypi/pico-examples/blob/master/pico_w/bt/hfp_hf_demo/CMakeLists.txt Configures the Bluetooth HFP HF demo for Raspberry Pi Pico. It defines audio interface pins and links necessary libraries and examples. This setup is crucial for enabling Bluetooth audio functionalities. ```cmake add_library(hfp_hf_demo_pins INTERFACE) target_compile_definitions(hfp_hf_demo_pins INTERFACE PICO_AUDIO_I2S_DATA_PIN=9 PICO_AUDIO_I2S_CLOCK_PIN_BASE=10 ) picow_bt_example(hfp_hf_demo pico_btstack_sco_demo_util pico_btstack_sbc_encoder hfp_hf_demo_pins) ``` -------------------------------- ### CMake Build Configuration for hello_timer Source: https://github.com/raspberrypi/pico-examples/blob/master/timer/hello_timer/CMakeLists.txt Configures the CMake build system for the hello_timer C example. It defines the executable, links necessary libraries (pico_stdlib), and sets up extra outputs and program URLs. ```cmake add_executable(hello_timer hello_timer.c ) # pull in common dependencies target_link_libraries(hello_timer pico_stdlib) # create map/bin/hex file etc. pico_add_extra_outputs(hello_timer) # add url via pico_set_program_url example_auto_set_url(hello_timer) ``` -------------------------------- ### RTC Alarm Setup Source: https://github.com/raspberrypi/pico-examples/blob/master/rtc/rtc_alarm/CMakeLists.txt Configures the RTC alarm functionality on the Raspberry Pi Pico. This involves setting up the necessary libraries and linking them to the executable. ```c add_executable(rtc_alarm rtc_alarm.c ) # pull in common dependencies and additional rtc hardware support target_link_libraries(rtc_alarm pico_stdlib hardware_rtc) # create map/bin/hex file etc. pico_add_extra_outputs(rtc_alarm) # add url via pico_set_program_url example_auto_set_url(rtc_alarm) ``` -------------------------------- ### MPU6050 I2C Example (C) Source: https://github.com/raspberrypi/pico-examples/blob/master/i2c/mpu6050_i2c/README.adoc This C code snippet demonstrates how to initialize and read data from an MPU6050 sensor connected via I2C to a Raspberry Pi Pico. It covers basic I2C communication setup and raw data retrieval. Dependencies include the Pico SDK's I2C library. The output is raw sensor readings. ```c #include "pico/stdlib.h" #include "hardware/i2c.h" #include // MPU6050 registers #define MPU6050_ADDRESS 0x68 #define PWR_MGMT_1 0x6B #define ACCEL_XOUT_H 0x3B void mpu6050_init() { uint8_t data[2]; // Wake up MPU6050 data[0] = 0x00; i2c_write_blocking(i2c_default, MPU6050_ADDRESS, data, 1, false); } int main() { stdio_init_all(); // Initialize I2C i2c_init(i2c_default, 400 * 1000); // Set I2C pins gpio_set_function(PICO_DEFAULT_I2C_SDA_PIN, GPIO_FUNC_I2C); gpio_set_function(PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C); gpio_pull_up(PICO_DEFAULT_I2C_SDA_PIN); gpio_pull_up(PICO_DEFAULT_I2C_SCL_PIN); mpu6050_init(); while (1) { uint8_t buffer[6]; int16_t ax, ay, az; // Read accelerometer data buffer[0] = ACCEL_XOUT_H; i2c_write_blocking(i2c_default, MPU6050_ADDRESS, buffer, 1, true); i2c_read_blocking(i2c_default, MPU6050_ADDRESS, buffer, 6, false); ax = (buffer[0] << 8) | buffer[1]; ay = (buffer[2] << 8) | buffer[3]; az = (buffer[4] << 8) | buffer[5]; printf("Accel: X=%d, Y=%d, Z=%d\n", ax, ay, az); sleep_ms(100); } return 0; } ``` -------------------------------- ### CYW43 LWIP Integration for BT Examples Source: https://github.com/raspberrypi/pico-examples/blob/master/pico_w/bt/CMakeLists.txt Configures an interface library for integrating CYW43 Wi-Fi with LWIP for Bluetooth examples. It links the common library, defines Wi-Fi credentials and LWIP usage, and includes necessary header paths. ```cmake # Adds stuff needed for cyw43 lwip add_library(picow_bt_example_cyw43_lwip INTERFACE) target_link_libraries(picow_bt_example_cyw43_lwip INTERFACE picow_bt_example_common ) target_compile_definitions(picow_bt_example_cyw43_lwip INTERFACE WIFI_SSID="${WIFI_SSID}" WIFI_PASSWORD="${WIFI_PASSWORD}" CYW43_LWIP=1 ) target_link_libraries(picow_bt_example_cyw43_lwip INTERFACE pico_lwip_iperf ) target_include_directories(picow_bt_example_cyw43_lwip INTERFACE ${CMAKE_CURRENT_LIST_DIR}/../wifi # for our common lwipopts ) ``` -------------------------------- ### CMakeLists.txt Configuration for Multicore Source: https://github.com/raspberrypi/pico-examples/blob/master/multicore/hello_multicore/CMakeLists.txt This snippet details the CMake configuration for the 'hello_multicore' project. It defines the executable, links the required Pico SDK libraries for multicore functionality, and sets up additional build outputs like binary and hex files. It also configures a program URL for easy access. ```cmake add_executable(hello_multicore multicore.c ) # Add pico_multicore which is required for multicore functionality target_link_libraries(hello_multicore pico_stdlib pico_multicore) # create map/bin/hex file etc. pico_add_extra_outputs(hello_multicore) # add url via pico_set_program_url example_auto_set_url(hello_multicore) ``` -------------------------------- ### PIO Assembler for NEC Carrier Control Source: https://github.com/raspberrypi/pico-examples/blob/master/pio/ir_nec/README.adoc The PIO assembler code used by the NEC transmit library to control the transmission of IR frames, including start pulses and data bits. ```pioasm ; Copyright (c) 2021 Raspberry Pi (Trading) Ltd. ; SPDX-License-Identifier: BSD-3-Clause .program nec_carrier_control ; State machine for controlling NEC frame transmission set(y, 0) ; Initialize data register set(x, 31) ; Initialize bit counter ; Start pulse set(pins, 1) ; Turn on carrier burst nop ; Wait for preamble duration set(pins, 0) ; Turn off carrier burst nop ; Wait for preamble gap duration ; Transmit data bits loop: out(pins, 1) ; Output the least significant bit of the frame jmp(x, end) ; If all bits sent, jump to end nop ; Wait for next bit duration jmp(loop) ; Continue loop end: nop ; Final wait nop ; End of transmission ``` -------------------------------- ### CMake Build Configuration for PWM Example Source: https://github.com/raspberrypi/pico-examples/blob/master/pwm/hello_pwm/CMakeLists.txt This snippet demonstrates a typical CMakeLists.txt configuration for a Raspberry Pi Pico C/C++ project. It defines an executable, links necessary libraries (pico_stdlib, hardware_pwm), and sets up output formats and program URLs. ```cmake add_executable(hello_pwm hello_pwm.c ) # pull in common dependencies and additional pwm hardware support target_link_libraries(hello_pwm pico_stdlib hardware_pwm) # create map/bin/hex file etc. pico_add_extra_outputs(hello_pwm) # add url via pico_set_program_url example_auto_set_url(hello_pwm) ``` -------------------------------- ### List of General Bluetooth Examples Source: https://github.com/raspberrypi/pico-examples/blob/master/pico_w/bt/CMakeLists.txt Includes the CMakeLists.txt from the btstack examples directory to define general Bluetooth examples. This variable `BTSTACK_EXAMPLES` is populated with various categories of examples. ```cmake include(${BTSTACK_ROOT}/example/CMakeLists.txt) # Full list of bluetooth examples set(BTSTACK_EXAMPLES ${EXAMPLES_GENERAL} ${EXAMPLES_CLASSIC_ONLY} ${EXAMPLES_LE_ONLY} ${EXAMPLES_DUAL_MODE} pan_lwip_http_server ) ``` -------------------------------- ### TinyUSB Examples for Raspberry Pi Pico Source: https://github.com/raspberrypi/pico-examples/blob/master/usb/README.md This section highlights examples that are direct copies from the tinyusb examples found within the pico-sdk. These examples showcase various USB functionalities supported by the Raspberry Pi Pico. ```c #include "pico/stdlib.h" #include "tusb.h" int main() { stdio_init_all(); tusb_init(); while (1) { tuh_task(); // Your application logic here } return 0; } ``` -------------------------------- ### HTTP Client Utility Library Setup Source: https://github.com/raspberrypi/pico-examples/blob/master/pico_w/wifi/http_client/CMakeLists.txt Configures a CMake library target for HTTP client utility functions. It specifies source files and links necessary Pico SDK libraries like lwIP and Mbed TLS for network communication and TLS encryption. ```CMake pico_add_library(example_lwip_http_util NOFLAG) target_sources(example_lwip_http_util INTERFACE ${CMAKE_CURRENT_LIST_DIR}/example_http_client_util.c ) pico_mirrored_target_link_libraries(example_lwip_http_util INTERFACE pico_lwip_http pico_lwip_mbedtls pico_mbedtls ) target_include_directories(example_lwip_http_util INTERFACE ${CMAKE_CURRENT_LIST_DIR} ) ``` -------------------------------- ### Basic Examples Subdirectories Source: https://github.com/raspberrypi/pico-examples/blob/master/CMakeLists.txt Includes basic examples such as blink and hello world. The `add_subdirectory_exclude_platforms` function is used to conditionally include these based on platform compatibility. ```cmake add_subdirectory_exclude_platforms(blink) add_subdirectory_exclude_platforms(blink_simple) add_subdirectory_exclude_platforms(hello_world) ``` -------------------------------- ### CMakeLists.txt Configuration for GPIO Output Source: https://github.com/raspberrypi/pico-examples/blob/master/clocks/hello_gpout/CMakeLists.txt This snippet shows the CMakeLists.txt file used to build the hello_gpout C example. It defines the executable, links necessary libraries like pico_stdlib, and configures output formats and program URLs. ```cmake add_executable(hello_gpout hello_gpout.c ) # pull in common dependencies target_link_libraries(hello_gpout pico_stdlib) # create map/bin/hex file etc. pico_add_extra_outputs(hello_gpout) # add url via pico_set_program_url example_auto_set_url(hello_gpout) ``` -------------------------------- ### Conditional DMA Example Compilation Source: https://github.com/raspberrypi/pico-examples/blob/master/dma/CMakeLists.txt This CMake script conditionally includes DMA-related example subdirectories. It checks for the 'hardware_dma' target and includes specific examples if available, otherwise, it prints a message indicating that DMA examples are skipped. ```cmake if (TARGET hardware_dma) add_subdirectory_exclude_platforms(channel_irq) add_subdirectory_exclude_platforms(control_blocks) add_subdirectory_exclude_platforms(hello_dma) add_subdirectory_exclude_platforms(sniff_crc) else() message("Skipping DMA examples as hardware_dma is unavailable on this platform") endif() ``` -------------------------------- ### Build Configuration for hello_universal Source: https://github.com/raspberrypi/pico-examples/blob/master/universal/hello_universal/CMakeLists.txt This snippet shows the CMake configuration for the 'hello_universal' project. It defines the executable, links the pico_stdlib library, enables UART and USB stdio output, and specifies the generation of additional output formats like bin, hex, and uf2. ```cmake add_executable(hello_universal hello_universal.c ) # pull in common dependencies target_link_libraries(hello_universal pico_stdlib) # enable usb output and uart output pico_enable_stdio_uart(hello_universal 1) pico_enable_stdio_usb(hello_universal 1) # create map/bin/hex/uf2 file etc. pico_add_extra_outputs(hello_universal) ``` -------------------------------- ### CMakeLists.txt Configuration for hello_dcp Source: https://github.com/raspberrypi/pico-examples/blob/master/dcp/hello_dcp/CMakeLists.txt This snippet shows the CMakeLists.txt file used to build the 'hello_dcp' project for the Raspberry Pi Pico. It defines the executable, links necessary libraries, sets the printf implementation, and configures additional build outputs and program URL. ```cmake add_executable(hello_dcp hello_dcp.c dcp_examples.S ) # pull in common dependencies target_link_libraries(hello_dcp pico_stdlib) # we need a high-precision printf to demonstrate dotx properly pico_set_printf_implementation(hello_dcp compiler) # create map/bin/hex file etc. pico_add_extra_outputs(hello_dcp) # add url via pico_set_program_url example_auto_set_url(hello_dcp) ```