Skip to content

First step converting stm32-lcd to mmio #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Tools/Toolsets/stm32f74x-lcd.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
}
94 changes: 44 additions & 50 deletions stm32-lcd-logo/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
28 changes: 28 additions & 0 deletions stm32-lcd-logo/Package.swift
Original file line number Diff line number Diff line change
@@ -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"),
])
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
//
//===----------------------------------------------------------------------===//

import Support

struct STM32F746Board {
var led: HALGPIO<STM32F746>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
//
//===----------------------------------------------------------------------===//

import Support

public protocol GPIOPlatform {
associatedtype Pin
static func configure(_ pin: Pin, _ configuration: GPIOConfiguration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
//
//===----------------------------------------------------------------------===//

import Support

extension UnsafeMutablePointer where Pointee == UInt32 {
func volatileLoad() -> Pointee {
volatile_load_uint32_t(self)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ static inline void nop() {
asm volatile("nop");
}

extern uint32_t *logoPixelDataStartPointer;
extern uint32_t const * const logoPixelDataStartPointer;
Loading