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 eecf22d..0000000 Binary files a/kernel.img and /dev/null differ 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