Skip to content

Commit 0ec014a

Browse files
committed
Convert stm32-blink to SwiftPM
Replaces bespoke shell scripts with a SwiftPM based build for the stm32-blink example. Adds CI for MachO based examples. Future: enable ELF variants of all MachO STM32 examples.
1 parent 38b5af0 commit 0ec014a

24 files changed

+4259
-529
lines changed

.github/workflows/build-stm.yml renamed to .github/workflows/build-stm32-elf.yml

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: STM32
1+
name: STM32 ELF
22

33
on:
44
push:
@@ -15,7 +15,7 @@ jobs:
1515
strategy:
1616
fail-fast: false
1717
matrix:
18-
example: [stm32-blink, stm32-lvgl]
18+
example: [stm32-lvgl]
1919

2020
steps:
2121
- name: Checkout repo
@@ -36,21 +36,13 @@ jobs:
3636
- name: Install Swift
3737
uses: ./.github/actions/install-swift
3838

39-
- name: Set environment variables
40-
run: |
41-
echo "STM_BOARD=STM32F746G_DISCOVERY" >> $GITHUB_ENV
42-
4339
- name: Build ${{ matrix.example }}
4440
working-directory: ${{ matrix.example }}
4541
run: |
4642
if [[ -f ./fetch-dependencies.sh ]]; then
4743
./fetch-dependencies.sh
4844
fi
4945
50-
if [[ -f ./build-elf.sh ]]; then
51-
./build-elf.sh
52-
fi
53-
5446
if [[ -f Makefile ]]; then
5547
make
5648
fi
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: STM32 Macho
2+
3+
on:
4+
push:
5+
branches: ["main"]
6+
pull_request:
7+
types: [opened, reopened, synchronize, ready_for_review]
8+
workflow_dispatch:
9+
10+
jobs:
11+
build:
12+
name: Build
13+
runs-on: [self-hosted, macos]
14+
15+
strategy:
16+
fail-fast: false
17+
matrix:
18+
example: [stm32-blink, stm32-lcd-logo, stm32-neopixel, stm32-uart-echo]
19+
20+
steps:
21+
- name: Install Swift
22+
uses: ./.github/actions/install-swift
23+
24+
- name: Build ${{ matrix.example }}
25+
working-directory: ${{ matrix.example }}
26+
run: make

stm32-blink/Board.swift

Lines changed: 0 additions & 115 deletions
This file was deleted.

stm32-blink/Main.swift

Lines changed: 0 additions & 42 deletions
This file was deleted.

stm32-blink/Makefile

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
##===----------------------------------------------------------------------===##
2+
##
3+
## This source file is part of the Swift open source project
4+
##
5+
## Copyright (c) 2025 Apple Inc. and the Swift project authors.
6+
## Licensed under Apache License v2.0 with Runtime Library Exception
7+
##
8+
## See https://swift.org/LICENSE.txt for license information
9+
##
10+
##===----------------------------------------------------------------------===##
11+
12+
# Paths
13+
REPOROOT := $(shell git rev-parse --show-toplevel)
14+
TOOLSROOT := $(REPOROOT)/Tools
15+
TOOLSET := $(TOOLSROOT)/Toolsets/stm32f74x.json
16+
MACHO2BIN := $(TOOLSROOT)/macho2bin.py
17+
SWIFT_BUILD := swift build
18+
19+
# Flags
20+
ARCH := armv7em
21+
TARGET := $(ARCH)-apple-none-macho
22+
SWIFT_BUILD_ARGS := \
23+
--configuration release \
24+
--triple $(TARGET) \
25+
--toolset $(TOOLSET)
26+
BUILDROOT := $(shell $(SWIFT_BUILD) $(SWIFT_BUILD_ARGS) --show-bin-path)
27+
28+
.PHONY: build
29+
build:
30+
@echo "building..."
31+
$(SWIFT_BUILD) \
32+
$(SWIFT_BUILD_ARGS) \
33+
-Xlinker -map -Xlinker $(BUILDROOT)/Application.mangled.map \
34+
--verbose
35+
36+
@echo "demangling linker map..."
37+
cat $(BUILDROOT)/Application.mangled.map \
38+
| c++filt | swift demangle > $(BUILDROOT)/Application.map
39+
40+
@echo "disassembling..."
41+
otool \
42+
-arch $(ARCH) -v -V -d -t \
43+
$(BUILDROOT)/Application \
44+
| c++filt | swift demangle > $(BUILDROOT)/Application.disassembly
45+
46+
@echo "extracting binary..."
47+
$(MACHO2BIN) \
48+
$(BUILDROOT)/Application \
49+
$(BUILDROOT)/Application.bin \
50+
--base-address 0x20010000 \
51+
--segments '__TEXT,__DATA,__VECTORS'
52+
53+
.PHONY: clean
54+
clean:
55+
@echo "cleaning..."
56+
@swift package clean
57+
@rm -rf .build

stm32-blink/Package.resolved

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

stm32-blink/Package.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// swift-tools-version: 6.0
2+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "stm32-blink",
7+
products: [
8+
.executable(name: "Application", targets: ["Application"])
9+
],
10+
dependencies: [
11+
.package(url: "https://github.com/apple/swift-mmio", branch: "main")
12+
],
13+
targets: [
14+
.executableTarget(
15+
name: "Application",
16+
dependencies: ["STM32F7X6", "Support"]),
17+
// SVD2Swift \
18+
// --input ../Tools/SVDs/stm32f7x6.patched.svd \
19+
// --output Sources/Application/Registers \
20+
// --access-level public \
21+
// --indentation-width 2 \
22+
// --peripherals GPIOA GPIOI RCC
23+
.target(
24+
name: "STM32F7X6",
25+
dependencies: [
26+
.product(name: "MMIO", package: "swift-mmio")
27+
],
28+
plugins: [
29+
// Plugin disabled because SwiftPM is slow.
30+
// .plugin(name: "SVD2SwiftPlugin", package: "swift-mmio")
31+
]),
32+
.target(name: "Support"),
33+
],
34+
swiftLanguageModes: [.v5])

stm32-blink/README.md

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,17 @@ This example shows a simple baremetal firmware for an STM32 board that blinks an
1515
- Build and upload the program to flash memory of the microcontroller:
1616
```console
1717
$ cd stm32-blink
18-
$ export STM_BOARD=STM32F746G_DISCOVERY # or NUCLEO_F103RB
19-
$ ./build-macho.sh
20-
$ st-flash --reset write .build/blink.bin 0x08000000
18+
$ make
19+
$ st-flash --reset write .build/Application.bin 0x08000000
2120
```
22-
- The green LED next to the RESET button should now be blinking in a pattern.
23-
24-
## Building and running the firmware as ELF (on either macOS or Linux)
25-
26-
- Build and upload the program to flash memory of the microcontroller:
27-
```console
28-
$ cd stm32-blink
29-
$ export STM_BOARD=STM32F746G_DISCOVERY # or NUCLEO_F103RB
30-
$ ./build-elf.sh
31-
$ st-flash --format ihex --reset write .build/blink.hex
32-
```
33-
- The green LED next to the RESET button should now be blinking in a pattern.
21+
- The green LED next to the RESET button should now be blinking.
3422

3523
## Binary size
3624

3725
The resulting size of the compiled and linked binary is very small (which shouldn't be surprising given that this toy example only blinks an LED), and demonstrates how the Embedded Swift compilation mode doesn't include unnecessary code or data in the resulting program:
3826

3927
```console
40-
$ size -m .build/blink
28+
$ size -m .build/Application
4129
Segment __TEXT: 656
4230
Section __text: 142
4331
total 142
@@ -51,6 +39,6 @@ total 1300
5139
The binary contains only 142 bytes of code! Additionally, the vector table needed by the CPU is actually dominating the size of the final firmware. Note that the `__LINKEDIT` segment is discarded when forming the final `.bin` file, which is 1168 bytes in size:
5240

5341
```console
54-
$ cat .build/blink.bin | wc -c
42+
$ cat .build/Application.bin | wc -c
5543
1168
5644
```

0 commit comments

Comments
 (0)