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..796e1779 --- /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;