System Info:

Steps One & Two: VScode Extension and then by hand C

I haven’t used picos much so first proof of concept / toolchain validation using VSCode extension to blink an LED as outlined in the PDF

Super easy. Blink Blink Blink.

Then followed the steps in that same PDF from pages 32 to do the same outside of the VSCode environment:

## I called my folder pico-dev not just pico to make life difficult
mkdir ~/Developer/pico-dev
cd ~/Developer/pico-dev
git clone https://github.com/raspberrypi/pico-sdk.git --branch master
cd pico-sdk
git submodule update --init
cd ..
git clone https://github.com/raspberrypi/pico-examples.git --branch master
cd ~/Developer/pico-dev
ls -la # be in pico-dev with sdk and examples installed
cd pico-examples
mkdir build
cd build
export PICO_SDK_PATH=/Users/MyUserName/Developer/pico-dev/pico-sdk
cmake .. -DPICO_BOARD=pico_w
cd blink
## getconf _NPROCESSORS_ONLN to get the number of cores for threading if wanted.
make -j10
ls 
## should have a blink.uf2 that can be copied over to the pico

Works!

Then did the example starting on page 37 to write my own project:

cd ~/Developer/pico-dev
mkdir test
cd test
touch test.c
touch CMakeLists.txt

In test.c I put the following code as described in the PDF.

#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "pico/binary_info.h"

const uint LED_PIN = 25;

int main() {

    bi_decl(bi_program_description("This is a test binary."));
    bi_decl(bi_1pin_with_name(LED_PIN, "On-board LED"));

    stdio_init_all();

    gpio_init(LED_PIN);
    gpio_set_dir(LED_PIN, GPIO_OUT);

    while (1) {
        gpio_put(LED_PIN, 0);
        sleep_ms(250);
        gpio_put(LED_PIN, 1);
        puts("Hello World\n");
        sleep_ms(1000);
    }
}
cmake_minimum_required(VERSION 3.13)
include(pico_sdk_import.cmake)

project(test_project C CXX ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)

pico_sdk_init()

add_executable(test
  test.c
)
pico_enable_stdio_usb(test 1)
pico_enable_stdio_uart(test 1)
pico_add_extra_outputs(test)
target_link_libraries(test pico_stdlib)

Then on to building

## get the cmake file
cp ../pico-sdk/external/pico_sdk_import.cmake .
## Optional, read what's in it
# cat pico_sdk_import.cmake
mkdir build
cd build
## if in same terminal window this should already be set. check with env
# export PICO_SDK_PATH=/Users/MyUserName/Developer/pico-dev/pico-sdk
cmake .. -DPICO_BOARD=pico_w
make
# brew install tio
ls /dev/tty.*
tio /dev/tty.usbmodem101

Get/select the Swift nightly build

Since embedded swift is still experimental it’s not in the release build, so one has to use the nightly.

On my linux set up I use swiftly, but on the mac laptop I’m going to try doing it by hand.

plutil -extract CFBundleIdentifier raw \
-o - \
/Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2024-10-30-a.xctoolchain/Info.plist

TOOLCHAINS=org.swift.61202410301a swift --version

Get the Embedded swift examples

cd ~/Developer/GitHub/
git clone https://github.com/apple/swift-embedded-examples.git --branch main

In the same window I also followed the directions on Python3 requirements

$ cd swift-embedded-examples
$ python3 -m venv .venv
$ source .venv/bin/activate
$ python3 -m pip install -r Tools/requirements.txt

Updating the Files

https://github.com/apple/swift-embedded-examples/issues/59#issuecomment-2343265666

https://github.com/apple/swift-embedded-examples/issues/62

Examining/Comparing the CMake Files

So, I’ve never used Ninja either so I wanted to see what the difference in the CMake files were in the pico-w-blink-sdk example which calls for it. Honestly it doesn’t really make sense to me to use Ninja since the code you should be building regularly shouldn’t be that big, but I still think CMake is over kill too so don’t listen to me. The way that project is set up is that cmake creates the ninja file and then builds it from what I understand. (write up that explains that dynamic with C++)

## same
cmake_minimum_required(VERSION 3.13)
## same, but in test example moved a copy into the folder
include($ENV{PICO_SDK_PATH}/external/pico_sdk_import.cmake)

project(swift-blinky)

## missing C settings

## same
pico_sdk_init()

## new, to get which command to use for custom command later
if(APPLE)
execute_process(COMMAND xcrun -f swiftc OUTPUT_VARIABLE SWIFTC OUTPUT_STRIP_TRAILING_WHITESPACE)
else()
execute_process(COMMAND which swiftc OUTPUT_VARIABLE SWIFTC OUTPUT_STRIP_TRAILING_WHITESPACE)
endif()

## different in that there is no file list
add_executable(swift-blinky)

## Obviously all new. Replaces C settings? 
add_custom_command(
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o
    COMMAND
        ${SWIFTC}
        -target armv6m-none-none-eabi -Xcc -mfloat-abi=soft -Xcc -fshort-enums
        -Xcc -DCYW43_LWIP
        -Xcc -DPICO_CYW43_ARCH_THREADSAFE_BACKGROUND
        -Xcc -I$ENV{PICO_SDK_PATH}/lib/lwip/src/include
        -Xcc -I${CMAKE_CURRENT_LIST_DIR}/include
        -Xfrontend -function-sections -enable-experimental-feature Embedded -wmo -parse-as-library
        $$\( echo '$<TARGET_PROPERTY:swift-blinky,INCLUDE_DIRECTORIES>' | tr '\;' '\\n' | sed -e 's/\\\(.*\\\)/-Xcc -I\\1/g' \)
        $$\( echo '${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES}'             | tr ' '  '\\n' | sed -e 's/\\\(.*\\\)/-Xcc -I\\1/g' \)
        -import-bridging-header ${CMAKE_CURRENT_LIST_DIR}/BridgingHeader.h
        ${CMAKE_CURRENT_LIST_DIR}/Main.swift
        -c -o ${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o
    DEPENDS
        ${CMAKE_CURRENT_LIST_DIR}/BridgingHeader.h
        ${CMAKE_CURRENT_LIST_DIR}/Main.swift
)

## interim target
add_custom_target(swift-blinky-swiftcode DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o)

#


listed, lists .o file explicitly
target_link_libraries(swift-blinky
    pico_stdlib hardware_uart hardware_gpio pico_lwip_arch pico_cyw43_arch_none
    ${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o
)
add_dependencies(swift-blinky swift-blinky-swiftcode)

## same
pico_add_extra_outputs(swift-blinky)

## missing some pico_enable_* etc

I also went looking at some of the other CMake files in the examples directory.

$ cd ~/Developer/pico-dev $ git clone https://github.com/raspberrypi/pico-sdk.git –branch master

The Code to Replace

MacOS

https://stackoverflow.com/questions/14306642/adding-multiple-executables-in-cmake

https://iamsorush.com/posts/cpp-cmake-build/