From c1867efd924ad63c1d52258f6dc443ea56f92fff Mon Sep 17 00:00:00 2001 From: Rauhul Varma Date: Wed, 19 Mar 2025 22:38:31 -0700 Subject: [PATCH 1/2] First step converting stm32-lcd to mmio 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. --- Tools/Toolsets/stm32f74x-lcd.json | 25 +++++ stm32-lcd-logo/Makefile | 94 +++++++++---------- stm32-lcd-logo/Package.swift | 28 ++++++ .../Application}/Board.swift | 2 + .../Application}/GPIO.swift | 0 .../Application}/HAL.swift | 2 + .../Application}/LTDC.swift | 0 .../Application}/Libc.swift | 0 .../{ => Sources/Application}/Main.swift | 0 .../Application}/RCC.swift | 0 .../Application}/USART.swift | 0 .../Application}/Volatile.swift | 2 + .../{ => Sources}/Support/PixelData.c | 0 .../{ => Sources}/Support/Startup.c | 0 .../Support/include/Support.h} | 2 +- 15 files changed, 104 insertions(+), 51 deletions(-) create mode 100644 Tools/Toolsets/stm32f74x-lcd.json create mode 100644 stm32-lcd-logo/Package.swift rename stm32-lcd-logo/{Support => Sources/Application}/Board.swift (99%) rename stm32-lcd-logo/{Support => Sources/Application}/GPIO.swift (100%) rename stm32-lcd-logo/{Support => Sources/Application}/HAL.swift (99%) rename stm32-lcd-logo/{Support => Sources/Application}/LTDC.swift (100%) rename stm32-lcd-logo/{Support => Sources/Application}/Libc.swift (100%) rename stm32-lcd-logo/{ => Sources/Application}/Main.swift (100%) rename stm32-lcd-logo/{Support => Sources/Application}/RCC.swift (100%) rename stm32-lcd-logo/{Support => Sources/Application}/USART.swift (100%) rename stm32-lcd-logo/{Support => Sources/Application}/Volatile.swift (97%) rename stm32-lcd-logo/{ => Sources}/Support/PixelData.c (100%) rename stm32-lcd-logo/{ => Sources}/Support/Startup.c (100%) rename stm32-lcd-logo/{Support/BridgingHeader.h => Sources/Support/include/Support.h} (90%) diff --git a/Tools/Toolsets/stm32f74x-lcd.json b/Tools/Toolsets/stm32f74x-lcd.json new file mode 100644 index 00000000..d9c1790b --- /dev/null +++ b/Tools/Toolsets/stm32f74x-lcd.json @@ -0,0 +1,25 @@ +{ + "schemaVersion": "1.0", + "swiftCompiler": { + "extraCLIOptions": [ + "-Xcc", "-D__APPLE__", + "-Xcc", "-D__MACH__", + "-Xfrontend", "-disable-stack-protector", + "-enable-experimental-feature", "Embedded" + ] + }, + "linker": { + "extraCLIOptions": [ + "-arch", "armv7em", + "-dead_strip", + "-static", + "-e", "_reset", + "-no_zero_fill_sections", + "-segalign", "4", + "-segaddr", "__VECTORS", "0x00200000", + "-seg1addr", "0x00200200", + "-pagezero_size", "0", + "-allow_dead_duplicates" + ] + } +} diff --git a/stm32-lcd-logo/Makefile b/stm32-lcd-logo/Makefile index 4bda625a..b04fafbd 100644 --- a/stm32-lcd-logo/Makefile +++ b/stm32-lcd-logo/Makefile @@ -9,53 +9,47 @@ ## ##===----------------------------------------------------------------------===## -# Determine file paths -REPOROOT := $(shell git rev-parse --show-toplevel) -TOOLSROOT := $(REPOROOT)/Tools -SRCROOT := $(REPOROOT)/stm32-lcd-logo -BUILDROOT := $(SRCROOT)/.build - -# Setup tools and build flags -TARGET := armv7-apple-none-macho -BASEADDRESS := 0x00200000 - -SWIFT_EXEC := $(shell xcrun -f swiftc) -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 - -CLANG_EXEC := $(shell xcrun -f clang) -CLANG_FLAGS := -target $(TARGET) -Oz - -LD_EXEC := $(CLANG_EXEC) -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 - -PYTHON_EXEC := $(shell xcrun -f python3) -MACHO2BIN := $(TOOLSROOT)/macho2bin.py - -.PHONY: all -all: $(BUILDROOT)/lcd-logo.bin - -$(BUILDROOT): - # Create build directory - mkdir -p $(BUILDROOT) - -$(BUILDROOT)/lcd-logo.o: $(SRCROOT)/Main.swift $(SRCROOT)/Support/*.swift | $(BUILDROOT) - # Build Swift sources - $(SWIFT_EXEC) $(SWIFT_FLAGS) -c $^ -o $@ - -$(BUILDROOT)/Startup.o: $(SRCROOT)/Support/Startup.c | $(BUILDROOT) - # Build C sources - $(CLANG_EXEC) $(CLANG_FLAGS) -c $^ -o $@ - -$(BUILDROOT)/PixelData.o: $(SRCROOT)/Support/PixelData.c | $(BUILDROOT) - # Build C sources - $(CLANG_EXEC) $(CLANG_FLAGS) -c $^ -o $@ - -$(BUILDROOT)/lcd-logo: $(BUILDROOT)/lcd-logo.o $(BUILDROOT)/Startup.o $(BUILDROOT)/PixelData.o - # Link objects into executable - $(LD_EXEC) $(LD_FLAGS) $^ -o $@ - -$(BUILDROOT)/lcd-logo.bin: $(BUILDROOT)/lcd-logo - # Extract sections from executable into flashable binary - $(PYTHON_EXEC) $(MACHO2BIN) $^ $@ --base-address 0x00200000 --segments '__TEXT,__DATA,__VECTORS' - # Echo final binary path - ls -al $(BUILDROOT)/lcd-logo.bin +# Paths +REPOROOT := $(shell git rev-parse --show-toplevel) +TOOLSROOT := $(REPOROOT)/Tools +TOOLSET := $(TOOLSROOT)/Toolsets/stm32f74x-lcd.json +MACHO2BIN := $(TOOLSROOT)/macho2bin.py +SWIFT_BUILD := swift build + +# Flags +ARCH := armv7em +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..." + $(MACHO2BIN) \ + $(BUILDROOT)/Application $(BUILDROOT)/Application.bin --base-address 0x00200000 --segments '__TEXT,__DATA,__VECTORS' + +.PHONY: clean +clean: + @echo "cleaning..." + @swift package clean + @rm -rf .build diff --git a/stm32-lcd-logo/Package.swift b/stm32-lcd-logo/Package.swift new file mode 100644 index 00000000..20220d38 --- /dev/null +++ b/stm32-lcd-logo/Package.swift @@ -0,0 +1,28 @@ +// swift-tools-version: 6.2 + +import PackageDescription + +let package = Package( + name: "stm32-lcd-logo", + platforms: [ + .macOS(.v10_15) + ], + products: [ + .executable(name: "Application", targets: ["Application"]) + ], + dependencies: [ + // .package(url: "https://github.com/apple/swift-mmio", branch: "main") + ], + targets: [ + // SVD2Swift \ + // --input Tools/SVDs/stm32f7x6.patched.svd \ + // --output stm32-lcd-logo/Sources/STM32F7x6 \ + // --peripherals LTDC RCC GPIOA GPIOB GPIOC GPIOD GPIOE GPIOF GPIOG GPIOH GPIOI GPIOJ GPIOK + .executableTarget( + name: "Application", + dependencies: [ + // .product(name: "MMIO", package: "swift-mmio"), + "Support", + ]), + .target(name: "Support"), + ]) diff --git a/stm32-lcd-logo/Support/Board.swift b/stm32-lcd-logo/Sources/Application/Board.swift similarity index 99% rename from stm32-lcd-logo/Support/Board.swift rename to stm32-lcd-logo/Sources/Application/Board.swift index bf850d29..33f92bb3 100644 --- a/stm32-lcd-logo/Support/Board.swift +++ b/stm32-lcd-logo/Sources/Application/Board.swift @@ -9,6 +9,8 @@ // //===----------------------------------------------------------------------===// +import Support + struct STM32F746Board { var led: HALGPIO diff --git a/stm32-lcd-logo/Support/GPIO.swift b/stm32-lcd-logo/Sources/Application/GPIO.swift similarity index 100% rename from stm32-lcd-logo/Support/GPIO.swift rename to stm32-lcd-logo/Sources/Application/GPIO.swift diff --git a/stm32-lcd-logo/Support/HAL.swift b/stm32-lcd-logo/Sources/Application/HAL.swift similarity index 99% rename from stm32-lcd-logo/Support/HAL.swift rename to stm32-lcd-logo/Sources/Application/HAL.swift index 278d183d..7fe7e484 100644 --- a/stm32-lcd-logo/Support/HAL.swift +++ b/stm32-lcd-logo/Sources/Application/HAL.swift @@ -9,6 +9,8 @@ // //===----------------------------------------------------------------------===// +import Support + public protocol GPIOPlatform { associatedtype Pin static func configure(_ pin: Pin, _ configuration: GPIOConfiguration) diff --git a/stm32-lcd-logo/Support/LTDC.swift b/stm32-lcd-logo/Sources/Application/LTDC.swift similarity index 100% rename from stm32-lcd-logo/Support/LTDC.swift rename to stm32-lcd-logo/Sources/Application/LTDC.swift diff --git a/stm32-lcd-logo/Support/Libc.swift b/stm32-lcd-logo/Sources/Application/Libc.swift similarity index 100% rename from stm32-lcd-logo/Support/Libc.swift rename to stm32-lcd-logo/Sources/Application/Libc.swift diff --git a/stm32-lcd-logo/Main.swift b/stm32-lcd-logo/Sources/Application/Main.swift similarity index 100% rename from stm32-lcd-logo/Main.swift rename to stm32-lcd-logo/Sources/Application/Main.swift diff --git a/stm32-lcd-logo/Support/RCC.swift b/stm32-lcd-logo/Sources/Application/RCC.swift similarity index 100% rename from stm32-lcd-logo/Support/RCC.swift rename to stm32-lcd-logo/Sources/Application/RCC.swift diff --git a/stm32-lcd-logo/Support/USART.swift b/stm32-lcd-logo/Sources/Application/USART.swift similarity index 100% rename from stm32-lcd-logo/Support/USART.swift rename to stm32-lcd-logo/Sources/Application/USART.swift diff --git a/stm32-lcd-logo/Support/Volatile.swift b/stm32-lcd-logo/Sources/Application/Volatile.swift similarity index 97% rename from stm32-lcd-logo/Support/Volatile.swift rename to stm32-lcd-logo/Sources/Application/Volatile.swift index f7041c3b..eae2274a 100644 --- a/stm32-lcd-logo/Support/Volatile.swift +++ b/stm32-lcd-logo/Sources/Application/Volatile.swift @@ -9,6 +9,8 @@ // //===----------------------------------------------------------------------===// +import Support + extension UnsafeMutablePointer where Pointee == UInt32 { func volatileLoad() -> Pointee { volatile_load_uint32_t(self) diff --git a/stm32-lcd-logo/Support/PixelData.c b/stm32-lcd-logo/Sources/Support/PixelData.c similarity index 100% rename from stm32-lcd-logo/Support/PixelData.c rename to stm32-lcd-logo/Sources/Support/PixelData.c diff --git a/stm32-lcd-logo/Support/Startup.c b/stm32-lcd-logo/Sources/Support/Startup.c similarity index 100% rename from stm32-lcd-logo/Support/Startup.c rename to stm32-lcd-logo/Sources/Support/Startup.c diff --git a/stm32-lcd-logo/Support/BridgingHeader.h b/stm32-lcd-logo/Sources/Support/include/Support.h similarity index 90% rename from stm32-lcd-logo/Support/BridgingHeader.h rename to stm32-lcd-logo/Sources/Support/include/Support.h index 7713eba8..f962f6fa 100644 --- a/stm32-lcd-logo/Support/BridgingHeader.h +++ b/stm32-lcd-logo/Sources/Support/include/Support.h @@ -25,4 +25,4 @@ static inline void nop() { asm volatile("nop"); } -extern uint32_t *logoPixelDataStartPointer; +extern uint32_t const * const logoPixelDataStartPointer; From 59b6ad8ed862f9aef0e9fc5f62238d452049849c Mon Sep 17 00:00:00 2001 From: Rauhul Varma Date: Wed, 19 Mar 2025 22:47:38 -0700 Subject: [PATCH 2/2] formatting --- stm32-lcd-logo/Package.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stm32-lcd-logo/Package.swift b/stm32-lcd-logo/Package.swift index 20220d38..796e1779 100644 --- a/stm32-lcd-logo/Package.swift +++ b/stm32-lcd-logo/Package.swift @@ -22,7 +22,7 @@ let package = Package( name: "Application", dependencies: [ // .product(name: "MMIO", package: "swift-mmio"), - "Support", + "Support" ]), .target(name: "Support"), ])