-
Notifications
You must be signed in to change notification settings - Fork 66
Raspberry Pi 4B & 5 baremetal blink example #67
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
Changes from 42 commits
Commits
Show all changes
48 commits
Select commit
Hold shift + click to select a range
5c16244
Create README.md
iCMDdev d7ee0fb
Update README.md
iCMDdev b82c7f1
Added Blink example code
iCMDdev 595b02e
Update MainApp.swift
iCMDdev 6f680c0
Delete RPi4B-blink/Makefile
iCMDdev b15606f
Update link.ld
iCMDdev 555bdbd
Update boot.S
iCMDdev 6e9cf61
Add files via upload
iCMDdev 0fd00dd
Update build.sh
iCMDdev bd61e35
Update link.ld
iCMDdev c4ea37d
Added RPi 5 example
iCMDdev 72347f6
Delete rpi5-blink/Package.resolved
iCMDdev 7a9af2f
Delete RPi4B-blink directory
iCMDdev f8f64ae
Updated RPi 4 example
iCMDdev ec9afca
Simplified README.md (removed redundent chmod command)
iCMDdev 9403cb3
Added LLVM instructions
iCMDdev 471c4a5
Update description
iCMDdev b2e3601
Typo fix
iCMDdev f560449
Merge branch 'main' into RPi-4B-BCM2711
iCMDdev 8f1ca70
Update pico-w-blink-sdk README.md
iCMDdev 7451fe2
Added CI support
iCMDdev 7426575
Merge pull request #3 from iCMDdev/RPi-4B-BCM2711
iCMDdev e9a3971
Update build-rpi-baremetal.yml
iCMDdev c60a427
Rename workflow
iCMDdev f768d03
Update build-rpi-baremetal.yml
iCMDdev 669fcc8
Update build-rpi-baremetal.yml
iCMDdev 30def4e
Update build-rpi-baremetal.yml
iCMDdev e7076ae
Update build-rpi-baremetal.yml
iCMDdev 0dee67c
Makefile typo fix
iCMDdev 290c928
Merge pull request #5 from iCMDdev/RPi-4B-BCM2711
iCMDdev a6e2b92
Makefile fixes
iCMDdev ce46d5e
Merge pull request #6 from iCMDdev/RPi-4B-BCM2711
iCMDdev bda28bb
Makefile fixes for Linux host
iCMDdev b1c238c
Merge pull request #7 from iCMDdev/RPi-4B-BCM2711
iCMDdev 92b775c
Fix styling
iCMDdev b677708
Update README.md
iCMDdev 47894c7
Merge branch 'main' into RPi-4B-BCM2711
iCMDdev 7f99773
Update build-rpi-baremetal.yml
iCMDdev 3e623d8
Merge branch 'main' into RPi-4B-BCM2711
iCMDdev c202dd5
Merge branch 'main' into RPi-4B-BCM2711
iCMDdev dc4024f
Merge branch 'main' into RPi-4B-BCM2711
iCMDdev 92dd75b
Merge branch 'main' into RPi-4B-BCM2711
rauhul 8b33b8b
Merge branch 'main' into RPi-4B-BCM2711
iCMDdev dab55e1
Update build-rpi-baremetal.yml
iCMDdev 7e539ed
Update build-rpi-baremetal.yml
iCMDdev 6e71205
Update build-rpi-baremetal.yml
iCMDdev 691635e
Update build-rpi-baremetal.yml
iCMDdev da8dc49
Update build-rpi-baremetal.yml
iCMDdev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
name: Build Raspberry Pi Baremetal Examples | ||
|
||
on: | ||
push: | ||
branches: ["main"] | ||
pull_request: | ||
branches: ["main"] | ||
schedule: | ||
# Build on Mondays at 9am PST every week | ||
- cron: '0 17 * * 1' | ||
|
||
jobs: | ||
build-pico-sdk: | ||
runs-on: ubuntu-22.04 | ||
container: swiftlang/swift:nightly-main-jammy | ||
iCMDdev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
strategy: | ||
fail-fast: false | ||
matrix: | ||
example: [rpi5-blink, rpi4b-blink] | ||
|
||
steps: | ||
- name: Checkout repo | ||
uses: actions/checkout@v4 | ||
|
||
- name: Install apt dependencies | ||
run: apt-get -qq update && apt-get -qq -y install make llvm | ||
|
||
- name: Build ${{ matrix.example }} | ||
run: | | ||
cd ${{ matrix.example }} | ||
make |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
SWIFT_EXEC ?= $(shell if [ "$(shell uname)" = "Darwin" ]; then xcrun -f swift; else which swift; fi) | ||
CLANG ?= $(shell if [ "$(shell uname)" = "Darwin" ]; then xcrun -f clang; else which clang; fi) | ||
LLVM_OBJCOPY ?= $(shell if [ "$(shell uname)" = "Darwin" ]; then xcrun -f llvm-objcopy; else which llvm-objcopy; fi) | ||
|
||
BUILDROOT := $(shell $(SWIFT_EXEC) build --triple aarch64-none-none-elf -Xswiftc -Xfrontend -Xswiftc -disable-stack-protector --show-bin-path) | ||
|
||
.PHONY: all clean | ||
|
||
all: kernel8.img | ||
|
||
kernel8.img: kernel8.elf | ||
@echo "💾 Converting to binary kernel image with llvm-objcopy..." | ||
$(LLVM_OBJCOPY) -O binary kernel8.elf kernel8.img | ||
@echo "" | ||
@echo "🥳 Done! kernel8.img was saved to this directory." | ||
|
||
kernel8.elf: $(BUILDROOT)/libMainApp.a $(BUILDROOT)/Support.build/boot.S.o link.ld | ||
@echo "🔗 Linking with clang..." | ||
$(CLANG) --target=aarch64-elf -o kernel8.elf $< $^ -fuse-ld=lld -nostdlib -Wl,--unresolved-symbols=ignore-in-object-files -Wl,-T ./link.ld | ||
@echo "" | ||
|
||
$(BUILDROOT)/libMainApp.a $(BUILDROOT)/Support.build/boot.S.o: | ||
@echo "🛠️ Building with Swift Package Manager..." | ||
$(SWIFT_EXEC) build --triple aarch64-none-none-elf -Xswiftc -Xfrontend -Xswiftc -disable-stack-protector | ||
@echo "" | ||
|
||
clean: | ||
rm -rf kernel8.elf kernel8.img .build |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// swift-tools-version: 6.1 | ||
// The swift-tools-version declares the minimum version of Swift required to build this package. | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "RPi4B-Blink", | ||
platforms: [ | ||
.macOS(.v14) | ||
], | ||
products: [ | ||
.library( | ||
name: "MainApp", | ||
type: .static, | ||
targets: ["MainApp"]) | ||
], | ||
dependencies: [ | ||
.package( | ||
url: "https://github.com/apple/swift-mmio.git", | ||
branch: "swift-embedded-examples") | ||
], | ||
targets: [ | ||
.target( | ||
name: "MainApp", | ||
dependencies: [ | ||
.product(name: "MMIO", package: "swift-mmio") | ||
], | ||
swiftSettings: [ | ||
.enableExperimentalFeature("Embedded"), | ||
.unsafeFlags(["-Xfrontend", "-function-sections"]), | ||
] | ||
), | ||
.target(name: "Support"), | ||
|
||
] | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# rpi4b-blink | ||
|
||
<img src="assets/rpi4.png"> | ||
|
||
## Requirements | ||
|
||
- A Raspberry Pi 4B board | ||
- An SD Card, with a Raspberry Pi OS installed (this way, we don't need to create the configuration files from scratch). You may backup `kernel8.img` and `config.txt` if you need the Linux install later, since we will change these files. | ||
- LLVM installed (`brew install llvm`) and added to PATH. This is needed to convert the resulted ELF file to binary image format using `llvm-objcopy`. | ||
|
||
## How to build and run this example: | ||
|
||
- Make sure you have a recent nightly Swift toolchain that has Embedded Swift support. | ||
- Build the program, then copy the kernel image to the SD card. | ||
``` console | ||
$ cd rpi4b-blink | ||
$ export TOOLCHAINS='<toolchain-identifier>' # Your Swift nightly toolchain identifier | ||
$ make | ||
$ cp kernel8.img /Volumes/bootfs | ||
``` | ||
- If your original OS is not 64-bit, make sure to set `arm_64bit=1` in `config.txt`. | ||
- Place the SD card in your Raspberry Pi 4B, and connect it to power. | ||
- After the boot sequence, the green (ACT) led will start blinking in a regular pattern. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift open source project | ||
// | ||
// Copyright (c) 2024 Apple Inc. and the Swift project authors. | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import MMIO | ||
|
||
@Register(bitWidth: 32) | ||
struct GPSET1 { | ||
@ReadWrite(bits: 10..<11, as: Bool.self) | ||
var set: SET | ||
} | ||
|
||
@Register(bitWidth: 32) | ||
struct GPCLR1 { | ||
@ReadWrite(bits: 10..<11, as: Bool.self) | ||
var clear: CLEAR | ||
} | ||
|
||
@Register(bitWidth: 32) | ||
struct GPFSEL4 { | ||
@ReadWrite(bits: 6..<7, as: Bool.self) | ||
var fsel42b1: FSEL42b1 | ||
@ReadWrite(bits: 7..<8, as: Bool.self) | ||
var fsel42b2: FSEL42b2 | ||
@ReadWrite(bits: 8..<9, as: Bool.self) | ||
var fsel42b3: FSEL42b3 | ||
} | ||
|
||
@RegisterBlock | ||
struct GPIO { | ||
@RegisterBlock(offset: 0x200020) | ||
var gpset1: Register<GPSET1> | ||
@RegisterBlock(offset: 0x20002c) | ||
var gpclr1: Register<GPCLR1> | ||
@RegisterBlock(offset: 0x200010) | ||
var gpfsel4: Register<GPFSEL4> | ||
} | ||
|
||
let gpio = GPIO(unsafeAddress: 0xFE00_0000) | ||
|
||
func setLedOutput() { | ||
gpio.gpfsel4.modify { | ||
// setFunction Select 42 (fsel42) to 001 | ||
$0.fsel42b1 = true | ||
$0.fsel42b2 = false | ||
$0.fsel42b3 = false | ||
} | ||
} | ||
|
||
func ledOn() { | ||
gpio.gpset1.modify { | ||
$0.set = true | ||
} | ||
} | ||
|
||
func ledOff() { | ||
gpio.gpclr1.modify { | ||
$0.clear = true | ||
} | ||
} | ||
|
||
@main | ||
struct Main { | ||
|
||
static func main() { | ||
setLedOutput() | ||
|
||
while true { | ||
ledOn() | ||
for _ in 1..<100000 {} // just a delay | ||
ledOff() | ||
for _ in 1..<100000 {} // just a delay | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift open source project | ||
// | ||
// Copyright (c) 2024 Apple Inc. and the Swift project authors. | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
.section ".text.boot" | ||
|
||
.global _start | ||
|
||
_start: | ||
// Check processor ID is zero (executing on main core), else hang | ||
mrs x1, mpidr_el1 | ||
and x1, x1, #3 | ||
cbz x1, 2f | ||
// We're not on the main core, so hang in an infinite wait loop | ||
1: wfe | ||
b 1b | ||
2: // We're on the main core! | ||
|
||
// Set stack to start below our code | ||
ldr x1, =_start | ||
mov sp, x1 | ||
|
||
// Clean the BSS section | ||
ldr x1, =__bss_start // Start address | ||
ldr w2, =__bss_size // Size of the section | ||
3: cbz w2, 4f // Quit loop if zero | ||
str xzr, [x1], #8 | ||
sub w2, w2, #1 | ||
cbnz w2, 3b // Loop if non-zero | ||
|
||
// Jump to Swift! | ||
4: bl main | ||
// Halt if Swift returns | ||
b 1b |
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
SECTIONS | ||
{ | ||
. = 0x80000; /* Kernel load address for AArch64 */ | ||
.text : { KEEP(*(.text.boot)) *(.text .text.* .gnu.linkonce.t*) } | ||
.rodata : { *(.rodata .rodata.* .gnu.linkonce.r*) } | ||
PROVIDE(_data = .); | ||
.data : { *(.data .data.* .gnu.linkonce.d*) } | ||
.bss (NOLOAD) : { | ||
. = ALIGN(16); | ||
__bss_start = .; | ||
*(.bss .bss.*) | ||
*(COMMON) | ||
__bss_end = .; | ||
} | ||
_end = .; | ||
|
||
/DISCARD/ : { *(.comment) *(.gnu*) *(.note*) *(.eh_frame*) } | ||
} | ||
__bss_size = (__bss_end - __bss_start)>>3; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
SWIFT_EXEC ?= $(shell if [ "$(shell uname)" = "Darwin" ]; then xcrun -f swift; else which swift; fi) | ||
CLANG ?= $(shell if [ "$(shell uname)" = "Darwin" ]; then xcrun -f clang; else which clang; fi) | ||
LLVM_OBJCOPY ?= $(shell if [ "$(shell uname)" = "Darwin" ]; then xcrun -f llvm-objcopy; else which llvm-objcopy; fi) | ||
|
||
BUILDROOT := $(shell $(SWIFT_EXEC) build --triple aarch64-none-none-elf -Xswiftc -Xfrontend -Xswiftc -disable-stack-protector --show-bin-path) | ||
|
||
.PHONY: all clean | ||
|
||
all: kernel8.img | ||
|
||
kernel8.img: kernel8.elf | ||
@echo "💾 Converting to binary kernel image with llvm-objcopy..." | ||
$(LLVM_OBJCOPY) -O binary kernel8.elf kernel8.img | ||
@echo "" | ||
@echo "🥳 Done! kernel8.img was saved to this directory." | ||
|
||
kernel8.elf: $(BUILDROOT)/libMainApp.a $(BUILDROOT)/Support.build/boot.S.o link.ld | ||
@echo "🔗 Linking with clang..." | ||
$(CLANG) --target=aarch64-elf -o kernel8.elf $< $^ -fuse-ld=lld -nostdlib -Wl,--unresolved-symbols=ignore-in-object-files -Wl,-T ./link.ld | ||
@echo "" | ||
|
||
$(BUILDROOT)/libMainApp.a $(BUILDROOT)/Support.build/boot.S.o: | ||
@echo "🛠️ Building with Swift Package Manager..." | ||
$(SWIFT_EXEC) build --triple aarch64-none-none-elf -Xswiftc -Xfrontend -Xswiftc -disable-stack-protector | ||
@echo "" | ||
|
||
clean: | ||
rm -rf kernel8.elf kernel8.img .build |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// swift-tools-version: 6.1 | ||
// The swift-tools-version declares the minimum version of Swift required to build this package. | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "RPi5-Blink", | ||
platforms: [ | ||
.macOS(.v14) | ||
], | ||
products: [ | ||
.library( | ||
name: "MainApp", | ||
type: .static, | ||
targets: ["MainApp"]) | ||
], | ||
dependencies: [ | ||
.package( | ||
url: "https://github.com/apple/swift-mmio.git", | ||
branch: "swift-embedded-examples") | ||
], | ||
targets: [ | ||
.target( | ||
name: "MainApp", | ||
dependencies: [ | ||
.product(name: "MMIO", package: "swift-mmio") | ||
], | ||
swiftSettings: [ | ||
.enableExperimentalFeature("Embedded"), | ||
.unsafeFlags(["-Xfrontend", "-function-sections"]), | ||
] | ||
), | ||
.target(name: "Support"), | ||
|
||
] | ||
) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.