Skip to content

Commit d94c950

Browse files
authored
First step converting stm32-lcd to mmio (#97)
Switches from manually running various compilers and linkers to using SwiftPM for most of the leg work. Additionally switches from -Osize to -O which has notably better runtime performance.
1 parent 8887cc9 commit d94c950

File tree

15 files changed

+104
-51
lines changed

15 files changed

+104
-51
lines changed

Tools/Toolsets/stm32f74x-lcd.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"schemaVersion": "1.0",
3+
"swiftCompiler": {
4+
"extraCLIOptions": [
5+
"-Xcc", "-D__APPLE__",
6+
"-Xcc", "-D__MACH__",
7+
"-Xfrontend", "-disable-stack-protector",
8+
"-enable-experimental-feature", "Embedded"
9+
]
10+
},
11+
"linker": {
12+
"extraCLIOptions": [
13+
"-arch", "armv7em",
14+
"-dead_strip",
15+
"-static",
16+
"-e", "_reset",
17+
"-no_zero_fill_sections",
18+
"-segalign", "4",
19+
"-segaddr", "__VECTORS", "0x00200000",
20+
"-seg1addr", "0x00200200",
21+
"-pagezero_size", "0",
22+
"-allow_dead_duplicates"
23+
]
24+
}
25+
}

stm32-lcd-logo/Makefile

Lines changed: 44 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,53 +9,47 @@
99
##
1010
##===----------------------------------------------------------------------===##
1111

12-
# Determine file paths
13-
REPOROOT := $(shell git rev-parse --show-toplevel)
14-
TOOLSROOT := $(REPOROOT)/Tools
15-
SRCROOT := $(REPOROOT)/stm32-lcd-logo
16-
BUILDROOT := $(SRCROOT)/.build
17-
18-
# Setup tools and build flags
19-
TARGET := armv7-apple-none-macho
20-
BASEADDRESS := 0x00200000
21-
22-
SWIFT_EXEC := $(shell xcrun -f swiftc)
23-
SWIFT_FLAGS := -target $(TARGET) -Osize -import-bridging-header $(SRCROOT)/Support/BridgingHeader.h -wmo -enable-experimental-feature Embedded -Xcc -D__APPLE__ -Xcc -D__MACH__ -Xcc -ffreestanding
24-
25-
CLANG_EXEC := $(shell xcrun -f clang)
26-
CLANG_FLAGS := -target $(TARGET) -Oz
27-
28-
LD_EXEC := $(CLANG_EXEC)
29-
LD_FLAGS := -target $(TARGET) -static -Wl,-e,_reset -dead_strip -Wl,-no_zero_fill_sections -Wl,-segalign,4 -Wl,-segaddr,__VECTORS,0x00200000 -Wl,-seg1addr,0x00200200 -Wl,-pagezero_size,0
30-
31-
PYTHON_EXEC := $(shell xcrun -f python3)
32-
MACHO2BIN := $(TOOLSROOT)/macho2bin.py
33-
34-
.PHONY: all
35-
all: $(BUILDROOT)/lcd-logo.bin
36-
37-
$(BUILDROOT):
38-
# Create build directory
39-
mkdir -p $(BUILDROOT)
40-
41-
$(BUILDROOT)/lcd-logo.o: $(SRCROOT)/Main.swift $(SRCROOT)/Support/*.swift | $(BUILDROOT)
42-
# Build Swift sources
43-
$(SWIFT_EXEC) $(SWIFT_FLAGS) -c $^ -o $@
44-
45-
$(BUILDROOT)/Startup.o: $(SRCROOT)/Support/Startup.c | $(BUILDROOT)
46-
# Build C sources
47-
$(CLANG_EXEC) $(CLANG_FLAGS) -c $^ -o $@
48-
49-
$(BUILDROOT)/PixelData.o: $(SRCROOT)/Support/PixelData.c | $(BUILDROOT)
50-
# Build C sources
51-
$(CLANG_EXEC) $(CLANG_FLAGS) -c $^ -o $@
52-
53-
$(BUILDROOT)/lcd-logo: $(BUILDROOT)/lcd-logo.o $(BUILDROOT)/Startup.o $(BUILDROOT)/PixelData.o
54-
# Link objects into executable
55-
$(LD_EXEC) $(LD_FLAGS) $^ -o $@
56-
57-
$(BUILDROOT)/lcd-logo.bin: $(BUILDROOT)/lcd-logo
58-
# Extract sections from executable into flashable binary
59-
$(PYTHON_EXEC) $(MACHO2BIN) $^ $@ --base-address 0x00200000 --segments '__TEXT,__DATA,__VECTORS'
60-
# Echo final binary path
61-
ls -al $(BUILDROOT)/lcd-logo.bin
12+
# Paths
13+
REPOROOT := $(shell git rev-parse --show-toplevel)
14+
TOOLSROOT := $(REPOROOT)/Tools
15+
TOOLSET := $(TOOLSROOT)/Toolsets/stm32f74x-lcd.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+
--disable-local-rpath
27+
BUILDROOT := $(shell $(SWIFT_BUILD) $(SWIFT_BUILD_ARGS) --show-bin-path)
28+
29+
.PHONY: build
30+
build:
31+
@echo "building..."
32+
$(SWIFT_BUILD) \
33+
$(SWIFT_BUILD_ARGS) \
34+
-Xlinker -map -Xlinker $(BUILDROOT)/Application.mangled.map \
35+
--verbose
36+
37+
@echo "demangling linker map..."
38+
cat $(BUILDROOT)/Application.mangled.map \
39+
| c++filt | swift demangle > $(BUILDROOT)/Application.map
40+
41+
@echo "disassembling..."
42+
otool \
43+
-arch $(ARCH) -v -V -d -t \
44+
$(BUILDROOT)/Application \
45+
| c++filt | swift demangle > $(BUILDROOT)/Application.disassembly
46+
47+
@echo "extracting binary..."
48+
$(MACHO2BIN) \
49+
$(BUILDROOT)/Application $(BUILDROOT)/Application.bin --base-address 0x00200000 --segments '__TEXT,__DATA,__VECTORS'
50+
51+
.PHONY: clean
52+
clean:
53+
@echo "cleaning..."
54+
@swift package clean
55+
@rm -rf .build

stm32-lcd-logo/Package.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// swift-tools-version: 6.2
2+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "stm32-lcd-logo",
7+
platforms: [
8+
.macOS(.v10_15)
9+
],
10+
products: [
11+
.executable(name: "Application", targets: ["Application"])
12+
],
13+
dependencies: [
14+
// .package(url: "https://github.com/apple/swift-mmio", branch: "main")
15+
],
16+
targets: [
17+
// SVD2Swift \
18+
// --input Tools/SVDs/stm32f7x6.patched.svd \
19+
// --output stm32-lcd-logo/Sources/STM32F7x6 \
20+
// --peripherals LTDC RCC GPIOA GPIOB GPIOC GPIOD GPIOE GPIOF GPIOG GPIOH GPIOI GPIOJ GPIOK
21+
.executableTarget(
22+
name: "Application",
23+
dependencies: [
24+
// .product(name: "MMIO", package: "swift-mmio"),
25+
"Support"
26+
]),
27+
.target(name: "Support"),
28+
])

stm32-lcd-logo/Support/Board.swift renamed to stm32-lcd-logo/Sources/Application/Board.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
//
1010
//===----------------------------------------------------------------------===//
1111

12+
import Support
13+
1214
struct STM32F746Board {
1315
var led: HALGPIO<STM32F746>
1416

stm32-lcd-logo/Support/HAL.swift renamed to stm32-lcd-logo/Sources/Application/HAL.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
//
1010
//===----------------------------------------------------------------------===//
1111

12+
import Support
13+
1214
public protocol GPIOPlatform {
1315
associatedtype Pin
1416
static func configure(_ pin: Pin, _ configuration: GPIOConfiguration)

stm32-lcd-logo/Support/Volatile.swift renamed to stm32-lcd-logo/Sources/Application/Volatile.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
//
1010
//===----------------------------------------------------------------------===//
1111

12+
import Support
13+
1214
extension UnsafeMutablePointer where Pointee == UInt32 {
1315
func volatileLoad() -> Pointee {
1416
volatile_load_uint32_t(self)

stm32-lcd-logo/Support/BridgingHeader.h renamed to stm32-lcd-logo/Sources/Support/include/Support.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ static inline void nop() {
2525
asm volatile("nop");
2626
}
2727

28-
extern uint32_t *logoPixelDataStartPointer;
28+
extern uint32_t const * const logoPixelDataStartPointer;

0 commit comments

Comments
 (0)