From 948cbf758f196a866fa07519e75dedbc200e4929 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Neuh=C3=A4user?= Date: Mon, 19 May 2025 18:33:13 +0200 Subject: [PATCH] Blink an LED on pi --- .gitignore | 1 + config.txt | 3 ++ kernel.img | Bin 66600 -> 0 bytes link.ld | 4 +-- src/gpio.rs | 60 ++++++--------------------------- src/main.rs | 22 ++++++------ tools/build_debug.sh | 4 +++ tools/build_release.sh | 4 +++ tools/generate_kernel.sh | 3 ++ tools/start_simulator.sh | 2 +- tools/start_simulator_debug.sh | 3 ++ 11 files changed, 44 insertions(+), 62 deletions(-) create mode 100755 config.txt delete mode 100755 kernel.img create mode 100755 tools/build_debug.sh create mode 100755 tools/build_release.sh create mode 100755 tools/generate_kernel.sh create mode 100755 tools/start_simulator_debug.sh diff --git a/.gitignore b/.gitignore index ea8c4bf..571c3bc 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +kernel8.img diff --git a/config.txt b/config.txt new file mode 100755 index 0000000..2bfa2a7 --- /dev/null +++ b/config.txt @@ -0,0 +1,3 @@ +kernel=kernel8.img +arm_64bit=1 +enable_uart=1 diff --git a/kernel.img b/kernel.img deleted file mode 100755 index eecf22d2cea2a574b1a8376f5940b55b7ed5a3a2..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 66600 zcmeIwPj4GV6aetIb`rc%Kq%^g3IUb)BN7p}QC&4T;Y*QHAtWp~M+vo{hXge?sO3V` zTkm`a;K~L0h{RXm%mog)!6z`Y-dUP#X*t00xAN|rdGp@P`e%QA>;3mSMG?)_i65h$ z^%kjNUihx_xMmnT(T!oO#L}X6c~R4>*L@Ln9OQHpBkx|NSJFMmXAg>|zUi+IUaLcT z)*F5J`}9uL=ORFW009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C7 z2oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N z0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+ z009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBly zK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF z5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7csf&W)v-aY!Ei1?-#4@bAVr=#9) z-=37)!`a4z*VfDJn3Y@A_~rF-D`tCfI$C`*W~(opj>?S`Q*QH08dvqj>gJ;U=SQPo z-|hagu^jh)n$JHfFAryD^Q$+@GSwAnzdZkb?c)5=^Ut22wbsj}i}h!#t$C|{uX^Wm z=Ra{?>wPv_`|lNXf6~A7tY08m#!~z_Z`ZuCNK~u63rkmWzSqjXp7QJQxW4Jy%=xDN zovI{VSJVAsbYfUEm5X%jJ|%zaDf!!r{FCn={ptArH2Q~=N%SWVj;05PDSkGYp46i{ z#_{K09`p}yZ0{b9cduW&adT&UeP?&;lTW|sUyC>HSD*dK^x$L~M+bM0V}Czww?96< zlSlQ^`NZ9?#`X93ug{99x>x?*sBx+R2idV%UTOw8K9^$E(arNgb{-UU<66!)p|xF9 z>z!utT-?e1f8^Y9iq+Z8+r54Xt4|4y<|I+^# DGVh&P diff --git a/link.ld b/link.ld index dfbf022..a37e62b 100644 --- a/link.ld +++ b/link.ld @@ -1,7 +1,7 @@ SECTIONS { - . = 0x80000; /* Kernel load address for AArch64 */ - .text : { KEEP(*(.text.boot)) *(.text .text.* .gnu.linkonce.t*) } + . = 0x80000; + .text : { KEEP(*(.text._start)) *(.text .text.* .gnu.linkonce.t*) } .rodata : { *(.rodata .rodata.* .gnu.linkonce.r*) } PROVIDE(_data = .); .data : { *(.data .data.* .gnu.linkonce.d*) } diff --git a/src/gpio.rs b/src/gpio.rs index 4f483d0..483e812 100644 --- a/src/gpio.rs +++ b/src/gpio.rs @@ -3,66 +3,28 @@ use crate::{ uart::{self}, }; +const GPFSEL2: u32 = 0x3F20_0008; //GPIO 20-29 const GPFSEL4: u32 = 0x3F20_0010; //GPIO 40-49 -const GPPUD: u32 = 0x3F20_0094; -const GPPUD_CLK: u32 = 0x3F20_0098; // GPIO 32-53 +const GPSET0: u32 = 0x3F20_001C; +const GPCLR0: u32 = 0x3F20_0028; -unsafe fn set_gpio47_to_output() { - let value: u32 = 0b001 << 21; +unsafe fn set_gpio29_to_output() { + let value: u32 = 0b001 << 27; - core::ptr::write_volatile(GPFSEL4 as *mut u32, value); + core::ptr::write_volatile(GPFSEL2 as *mut u32, value); } -unsafe fn enable_pull_up() { - let value: u32 = 0b10; - - core::ptr::write_volatile(GPPUD as *mut u32, value); -} - -unsafe fn enable_pull_down() { - let value: u32 = 0b01; - - core::ptr::write_volatile(GPPUD as *mut u32, value); -} - -unsafe fn disable_GPPUD() { - core::ptr::write_volatile(GPPUD as *mut u32, 0); -} - -unsafe fn enable_clock_gpio47() { - let value: u32 = 0b1 << 13; - - core::ptr::write_volatile(GPPUD as *mut u32, value); -} - -unsafe fn disable_GPPUD_CLK() { - core::ptr::write_volatile(GPPUD as *mut u32, 0); -} - -pub fn pull_up_gpio47() { +pub fn pull_up_gpio29() { unsafe { uart::print("Pull Up\n"); - set_gpio47_to_output(); - enable_pull_up(); - // Wait 150 cycles - delay(150); - enable_clock_gpio47(); - delay(150); - disable_GPPUD(); - disable_GPPUD_CLK(); + set_gpio29_to_output(); + core::ptr::write_volatile(GPSET0 as *mut u32, 1 << 29); } } -pub fn pull_down_gpio47() { +pub fn pull_down_gpio29() { unsafe { uart::print("Pull Down\n"); - set_gpio47_to_output(); - enable_pull_down(); - // Wait 150 cycles - delay(150); - enable_clock_gpio47(); - delay(150); - disable_GPPUD(); - disable_GPPUD_CLK(); + core::ptr::write_volatile(GPCLR0 as *mut u32, 1 << 29); } } diff --git a/src/main.rs b/src/main.rs index 705027f..51832bb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,9 +2,9 @@ #![no_std] #![feature(asm_experimental_arch)] -use core::panic::PanicInfo; +use core::{arch::asm, panic::PanicInfo}; -use gpio::{pull_down_gpio47, pull_up_gpio47}; +use gpio::{pull_down_gpio29, pull_up_gpio29}; mod gpio; mod uart; @@ -12,18 +12,20 @@ mod uart; #[panic_handler] fn panic(_panic: &PanicInfo) -> ! { loop { + pull_up_gpio29(); uart::print("Panic"); } } #[no_mangle] -#[unsafe(naked)] -pub extern "C" fn _start() -> ! { - core::arch::naked_asm!("mov sp, #0x80000", "bl main"); +#[link_section = ".text._start"] +pub unsafe extern "C" fn _start() { + asm!("ldr x0, =0x8004000", "mov sp, x0"); + main(); } #[no_mangle] -fn main() { +extern "C" fn main() { uart::configure_uart(); // Delay so clock speed can stabilize @@ -31,10 +33,10 @@ fn main() { uart::print("Hello World!\n"); loop { - pull_up_gpio47(); - unsafe { delay(10_000_000) } // ~0.5s - pull_down_gpio47(); - unsafe { delay(10_000_000) } + pull_up_gpio29(); + unsafe { delay(1_000_000) } + pull_down_gpio29(); + unsafe { delay(1_000_000) } } } diff --git a/tools/build_debug.sh b/tools/build_debug.sh new file mode 100755 index 0000000..fae3ba2 --- /dev/null +++ b/tools/build_debug.sh @@ -0,0 +1,4 @@ +cd "$(dirname "$0")" +cd ".." + +cargo build --target aarch64-unknown-none diff --git a/tools/build_release.sh b/tools/build_release.sh new file mode 100755 index 0000000..d4ecfba --- /dev/null +++ b/tools/build_release.sh @@ -0,0 +1,4 @@ +cd "$(dirname "$0")" +cd ".." + +cargo build --target aarch64-unknown-none --release diff --git a/tools/generate_kernel.sh b/tools/generate_kernel.sh new file mode 100755 index 0000000..f778711 --- /dev/null +++ b/tools/generate_kernel.sh @@ -0,0 +1,3 @@ +cd "$(dirname "$0")" + +llvm-objcopy -O binary ../target/aarch64-unknown-none/release/nova ../kernel8.img diff --git a/tools/start_simulator.sh b/tools/start_simulator.sh index 353c915..906718d 100755 --- a/tools/start_simulator.sh +++ b/tools/start_simulator.sh @@ -1,3 +1,3 @@ cd "$(dirname "$0")" -qemu-system-aarch64 -M raspi3b -cpu cortex-a53 -serial stdio -display none -kernel ../target/aarch64-unknown-none/debug/nova -s +qemu-system-aarch64 -M raspi3b -cpu cortex-a53 -serial stdio -display none -kernel ../target/aarch64-unknown-none/release/nova -s diff --git a/tools/start_simulator_debug.sh b/tools/start_simulator_debug.sh new file mode 100755 index 0000000..353c915 --- /dev/null +++ b/tools/start_simulator_debug.sh @@ -0,0 +1,3 @@ +cd "$(dirname "$0")" + +qemu-system-aarch64 -M raspi3b -cpu cortex-a53 -serial stdio -display none -kernel ../target/aarch64-unknown-none/debug/nova -s