Skip to content

Commit dc18937

Browse files
committed
Add a demo for using LVGL, DRAM, LLVM Toolchain, ELF on an STM32F746G discovery board
1 parent c3aaab3 commit dc18937

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+14202
-1
lines changed

Tools/elf2hex.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,17 @@ def main():
3636
parser.add_argument('input')
3737
parser.add_argument('output')
3838
parser.add_argument('--symbol-map')
39+
parser.add_argument('--relocate', action='append')
3940
args = parser.parse_args()
4041

4142
inf = open(args.input, "rb")
4243
outf = open(args.output, "wb")
4344

45+
relocations = {}
46+
for r in args.relocate:
47+
(a, b) = r.split(":")
48+
relocations[int(a, 16)] = int(b, 16)
49+
4450
def emitrecord(record):
4551
checksum = 0
4652
pos = 0
@@ -75,7 +81,16 @@ def emit(vmaddr, data):
7581
continue
7682
vmaddr = segment.header.p_paddr
7783
data = segment.data()
78-
emit(segment.header.p_paddr, data)
84+
flags = ""
85+
flags += "r" if segment.header.p_flags & 0x4 else "-"
86+
flags += "w" if segment.header.p_flags & 0x2 else "-"
87+
flags += "x" if segment.header.p_flags & 0x1 else "-"
88+
print(f"PT_LOAD {flags} at 0x{segment.header.p_paddr:08x} - 0x{segment.header.p_paddr + len(data):08x}, size {len(data)} (0x{len(data):04x})")
89+
placement_addr = segment.header.p_paddr
90+
if segment.header.p_paddr in relocations:
91+
placement_addr = relocations[segment.header.p_paddr]
92+
print(f" ... relocating to 0x{placement_addr:08x}")
93+
emit(placement_addr, data)
7994

8095
chunklen = 0
8196
vmaddr = 0

stm32-lvgl/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lvgl
2+
llvm-toolchain
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"swiftPM": {
3+
"configuration": "release",
4+
"triple": "armv7em-none-none-eabi",
5+
6+
"__comment": "XXX SourceKit-LSP seems to ignore the toolset (relative or absolute path)...",
7+
"toolset": ".../toolset.json",
8+
9+
"swiftCompilerFlags": [
10+
"-enable-experimental-feature", "Embedded",
11+
"-enable-experimental-feature", "Extern",
12+
]
13+
}
14+
}

stm32-lvgl/.swift-format

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"lineLength" : 120
3+
}

stm32-lvgl/.swift-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
main-snapshot-2025-03-28

stm32-lvgl/Makefile

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
##===----------------------------------------------------------------------===##
2+
##
3+
## This source file is part of the Swift open source project
4+
##
5+
## Copyright (c) 2023 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 := $(PWD)/toolset.json
16+
ELF2HEX := $(TOOLSROOT)/elf2hex.py
17+
SWIFT_BUILD := swift build
18+
NM := nm
19+
LLVM_TOOLCHAIN := $(PWD)/llvm-toolchain
20+
21+
# Flags
22+
ARCH := armv7em
23+
TARGET := $(ARCH)-none-none-eabi
24+
SWIFT_BUILD_ARGS := \
25+
--configuration release \
26+
--triple $(TARGET) \
27+
--toolset $(TOOLSET) \
28+
--disable-local-rpath
29+
BUILDROOT := $(shell $(SWIFT_BUILD) $(SWIFT_BUILD_ARGS) --show-bin-path)
30+
31+
.PHONY: build
32+
build:
33+
@echo "checking dependencies..."
34+
35+
# TODO: Check that we have swiftly and recent Swift main toolchain
36+
37+
if [[ ! -d $(PWD)/lvgl ]]; then echo "\n *** LVGL checkout not found, please run ./fetch-dependencies.sh\n" ; exit 1 ; fi
38+
if [[ ! -d $(PWD)/llvm-toolchain ]]; then echo "\n *** LLVM toolchain checkout not found, please run ./fetch-dependencies.sh\n" ; exit 1 ; fi
39+
40+
mkdir -p .build
41+
42+
@echo "configuring LVGL..."
43+
cmake -B .build/lvgl -G Ninja ./lvgl \
44+
-DCMAKE_EXPORT_COMPILE_COMMANDS=On \
45+
-DTOOLCHAIN_PATH=$(LLVM_TOOLCHAIN) \
46+
-DCMAKE_TOOLCHAIN_FILE=../clang-arm-toolchain.cmake \
47+
-DLV_CONF_PATH=../Sources/CLVGL/include/lv_conf.h
48+
49+
@echo "building LVGL..."
50+
cmake --build .build/lvgl
51+
52+
@echo "building..."
53+
$(SWIFT_BUILD) \
54+
$(SWIFT_BUILD_ARGS) \
55+
--verbose
56+
57+
@echo "disassembling..."
58+
$(LLVM_TOOLCHAIN)/bin/llvm-objdump --all-headers --disassemble --mcpu=cortex-m7 \
59+
$(BUILDROOT)/Application \
60+
| c++filt | swift demangle > $(BUILDROOT)/Application.disassembly
61+
62+
@echo "extracting binary..."
63+
$(eval FLASH_DATA_START = `$(NM) -p $(BUILDROOT)/Application | grep __flash_data_start | cut -d' ' -f1`)
64+
@echo " FLASH_DATA_START = 0x$(FLASH_DATA_START)"
65+
$(eval SRAM_DATA_START = `$(NM) -p $(BUILDROOT)/Application | grep __data_start | cut -d' ' -f1`)
66+
@echo " SRAM_DATA_START = 0x$(SRAM_DATA_START)"
67+
$(eval FLASH_DATA_LEN = `$(NM) -p $(BUILDROOT)/Application | grep __flash_data_len | cut -d' ' -f1`)
68+
@echo " FLASH_DATA_LEN = 0x$(FLASH_DATA_LEN)"
69+
$(ELF2HEX) \
70+
$(BUILDROOT)/Application $(BUILDROOT)/Application.hex --relocate "0x$(SRAM_DATA_START):0x$(FLASH_DATA_START)"
71+
ls -al $(BUILDROOT)/Application.hex
72+
@echo "\n *** All done, build succeeded!\n"
73+
74+
flash:
75+
@echo "flashing..."
76+
st-flash --reset --format ihex write $(BUILDROOT)/Application.hex
77+
78+
.PHONY: clean
79+
clean:
80+
@echo "cleaning..."
81+
@swift package clean
82+
@rm -rf .build

stm32-lvgl/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-lvgl/Package.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// swift-tools-version: 5.10
2+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "stm32-lvgl",
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+
.executableTarget(name: "Application",
18+
dependencies: [
19+
"Registers",
20+
"Support",
21+
"CLVGL",
22+
]),
23+
24+
// SVD2Swift \
25+
// --input Tools/SVDs/stm32f7x6.patched.svd \
26+
// --output stm32-lvgl/Sources/STM32F7x6 \
27+
// --peripherals FLASH LTDC RCC PWR FMC SCB DBGMCU USART1 STK NVIC \
28+
// GPIOA GPIOB GPIOC GPIOD GPIOE GPIOF GPIOG GPIOH GPIOI GPIOJ GPIOK \
29+
// --access-level public
30+
.target(name: "Registers",
31+
dependencies: [
32+
.product(name: "MMIO", package: "swift-mmio"),
33+
]),
34+
35+
.target(name: "Support"),
36+
37+
.target(name: "CLVGL"),
38+
])

stm32-lvgl/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# stm32-lvgl
2+
3+
...
4+
5+
## How to build and run this example:
6+
7+
...

0 commit comments

Comments
 (0)