diff --git a/Tools/Toolsets/pico.json b/Tools/Toolsets/pico.json new file mode 100644 index 00000000..52b60199 --- /dev/null +++ b/Tools/Toolsets/pico.json @@ -0,0 +1,26 @@ +{ + "schemaVersion": "1.0", + "swiftCompiler": { + "extraCLIOptions": [ + "-Xfrontend", "-disable-stack-protector", + "-enable-experimental-feature", "Embedded", + "-Xcc", "-mcpu=cortex-m0plus" + ] + }, + "linker": { + "extraCLIOptions": [ + "-arch", "armv6m", + "-dead_strip", + "-static", + "-e", "_reset", + "-no_zero_fill_sections", + "-segalign", "4", + "-segaddr", "__RESET", "0x20000000", + "-segaddr", "__VECTORS", "0x20000100", + "-seg1addr", "0x20000200", + "-pagezero_size", "0", + "-allow_dead_duplicates" + ] + } + } + \ No newline at end of file diff --git a/pico-blink/Makefile b/pico-blink/Makefile new file mode 100755 index 00000000..cfeb3bba --- /dev/null +++ b/pico-blink/Makefile @@ -0,0 +1,61 @@ +##===----------------------------------------------------------------------===## +## +## This source file is part of the Swift open source project +## +## Copyright (c) 2025 Apple Inc. and the Swift project authors. +## Licensed under Apache License v2.0 with Runtime Library Exception +## +## See https://swift.org/LICENSE.txt for license information +## +##===----------------------------------------------------------------------===## + +# Paths +REPOROOT := $(shell git rev-parse --show-toplevel) +TOOLSROOT := $(REPOROOT)/Tools +TOOLSET := $(TOOLSROOT)/Toolsets/pico.json +MACHO2UF2 := $(TOOLSROOT)/macho2uf2.py +SWIFT_BUILD := swift build + +# Flags +PICO_FAMILY := rp2040 +ARCH := armv6m +TARGET := $(ARCH)-apple-none-macho +SWIFT_BUILD_ARGS := \ + --configuration release \ + --triple $(TARGET) \ + --toolset $(TOOLSET) \ + --disable-local-rpath +BUILDROOT := $(shell $(SWIFT_BUILD) $(SWIFT_BUILD_ARGS) --show-bin-path) + +.PHONY: build +build: + @echo "building..." + $(SWIFT_BUILD) \ + $(SWIFT_BUILD_ARGS) \ + -Xlinker -map -Xlinker $(BUILDROOT)/Application.mangled.map \ + --verbose + + @echo "demangling linker map..." + cat $(BUILDROOT)/Application.mangled.map \ + | c++filt | swift demangle > $(BUILDROOT)/Application.map + + @echo "disassembling..." + otool \ + -arch $(ARCH) -v -V -d -t \ + $(BUILDROOT)/Application \ + | c++filt | swift demangle > $(BUILDROOT)/Application.disassembly + + @echo "extracting binary..." + $(MACHO2UF2) \ + --pico-family "$(PICO_FAMILY)" \ + "$(BUILDROOT)/Application" \ + "$(BUILDROOT)/Application.uf2" \ + --base-address 0x20000000 \ + --segments '__TEXT,__DATA,__VECTORS,__RESET' + + +.PHONY: clean +clean: + @echo "cleaning..." + @swift package clean + @rm -rf .build diff --git a/pico-blink/Package.swift b/pico-blink/Package.swift index efb184b2..20d1f1cd 100644 --- a/pico-blink/Package.swift +++ b/pico-blink/Package.swift @@ -5,11 +5,10 @@ import PackageDescription let package = Package( name: "RP2040", products: [ - .library(name: "Blinky", type: .static, targets: ["Blinky"]) + .executable(name: "Application", targets: ["Application"]) ], targets: [ - .target(name: "Blinky", dependencies: ["RP2040"]), + .executableTarget(name: "Application", dependencies: ["RP2040"]), .target(name: "Support"), .target(name: "RP2040", dependencies: ["Support"]), - ] -) + ]) diff --git a/pico-blink/README.md b/pico-blink/README.md index a9ae654a..a7de8602 100644 --- a/pico-blink/README.md +++ b/pico-blink/README.md @@ -11,9 +11,11 @@ - Connect the Pico board via a USB cable to your Mac, and make sure it's in the USB Mass Storage firmware upload mode (either hold the BOOTSEL button while plugging the board, or make sure your Flash memory doesn't contain any valid firmware). - Make sure you have a recent nightly Swift toolchain that has Embedded Swift support. - Build and copy the program in the UF2 format to the Mass Storage device to trigger flashing the program into memory (after which the device will reboot and run the firmware): + ``` console $ cd pico-blink -$ TOOLCHAINS='' ./build.sh -$ cp .build/blink.uf2 /Volumes/RP2040 +$ make +$ cp .build/Application.uf2 /Volumes/RP2040 ``` + - The green LED should now be blinking in a pattern. diff --git a/pico-blink/Sources/Blinky/Blinky.swift b/pico-blink/Sources/Application/Application.swift similarity index 99% rename from pico-blink/Sources/Blinky/Blinky.swift rename to pico-blink/Sources/Application/Application.swift index 8db4889b..a27b8b76 100644 --- a/pico-blink/Sources/Blinky/Blinky.swift +++ b/pico-blink/Sources/Application/Application.swift @@ -12,7 +12,7 @@ import RP2040 @main -struct Main { +struct Application { // swift-format-ignore: NeverUseImplicitlyUnwrappedOptionals static var board: RP2040! = nil diff --git a/pico-blink/build.sh b/pico-blink/build.sh deleted file mode 100755 index 8fbf37c7..00000000 --- a/pico-blink/build.sh +++ /dev/null @@ -1,44 +0,0 @@ -#!/bin/sh - -set -vex - -# Either rp2040 or rp2350 -PICO_FAMILY=rp2040 - -# Determine file paths -REPOROOT=$(git rev-parse --show-toplevel) -TOOLSROOT=$REPOROOT/Tools - -# Setup tools and build flags -SWIFT_EXEC=${SWIFT_EXEC:-$(xcrun -f swift)} -CLANG=${CLANG:-$(xcrun -f clang)} -SWIFT_FLAGS="-enable-experimental-feature Embedded -disable-stack-protector" -CLANG_FLAGS="-D__MACH__ -ffreestanding -mcpu=cortex-m0plus -mthumb" -LD_FLAGS="-static -Wl,-e,_reset -dead_strip -Wl,-no_zero_fill_sections -Wl,-segalign,4 -Wl,-segaddr,__RESET,0x20000000 -Wl,-segaddr,__VECTORS,0x20000100 -Wl,-seg1addr,0x20000200 -Wl,-pagezero_size,0" - -SWIFT_BUILD_FLAGS="--triple armv6m-apple-none-macho --configuration release --verbose" -for SWIFT_FLAG in $SWIFT_FLAGS; do - SWIFT_BUILD_FLAGS="$SWIFT_BUILD_FLAGS -Xswiftc $SWIFT_FLAG" -done - -for CLANG_FLAG in $CLANG_FLAGS; do - SWIFT_BUILD_FLAGS="$SWIFT_BUILD_FLAGS -Xcc $CLANG_FLAG" -done - -PYTHON_EXEC=${PYTHON_EXEC:-$(xcrun -f python3)} -MACHO2UF2=$TOOLSROOT/macho2uf2.py - -# Build with Swift package manager -$SWIFT_EXEC build "$SWIFT_BUILD_FLAGS" - -# Get the output directory -BUILDROOT=$($SWIFT_EXEC build "$SWIFT_BUILD_FLAGS" --show-bin-path) - -# Link -$CLANG .build/release/Support.build/Support.c.o .build/release/Support.build/crt0.S.o .build/release/Blinky.build/*.o -target armv6m-apple-none-macho -o "$BUILDROOT"/blinky "$LD_FLAGS" - -# Extract sections from executable into flashable binary -$PYTHON_EXEC "$MACHO2UF2" --pico-family $PICO_FAMILY "$BUILDROOT"/blinky "$BUILDROOT"/blinky.uf2 --base-address 0x20000000 --segments '__TEXT,__DATA,__VECTORS,__RESET' - -# Echo final binary path -ls -al "$BUILDROOT"/blinky.uf2 diff --git a/stm32-neopixel/Makefile b/stm32-neopixel/Makefile index 51885ce5..8880d741 100755 --- a/stm32-neopixel/Makefile +++ b/stm32-neopixel/Makefile @@ -2,7 +2,7 @@ ## ## This source file is part of the Swift open source project ## -## Copyright (c) 2023 Apple Inc. and the Swift project authors. +## Copyright (c) 2025 Apple Inc. and the Swift project authors. ## Licensed under Apache License v2.0 with Runtime Library Exception ## ## See https://swift.org/LICENSE.txt for license information