mirror of
https://github.com/iceHtwoO/novaOS.git
synced 2026-04-16 20:22:26 +00:00
Blink an LED on pi
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
||||
/target
|
||||
kernel8.img
|
||||
|
||||
3
config.txt
Executable file
3
config.txt
Executable file
@@ -0,0 +1,3 @@
|
||||
kernel=kernel8.img
|
||||
arm_64bit=1
|
||||
enable_uart=1
|
||||
BIN
kernel.img
BIN
kernel.img
Binary file not shown.
4
link.ld
4
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*) }
|
||||
|
||||
60
src/gpio.rs
60
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);
|
||||
}
|
||||
}
|
||||
|
||||
22
src/main.rs
22
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) }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
4
tools/build_debug.sh
Executable file
4
tools/build_debug.sh
Executable file
@@ -0,0 +1,4 @@
|
||||
cd "$(dirname "$0")"
|
||||
cd ".."
|
||||
|
||||
cargo build --target aarch64-unknown-none
|
||||
4
tools/build_release.sh
Executable file
4
tools/build_release.sh
Executable file
@@ -0,0 +1,4 @@
|
||||
cd "$(dirname "$0")"
|
||||
cd ".."
|
||||
|
||||
cargo build --target aarch64-unknown-none --release
|
||||
3
tools/generate_kernel.sh
Executable file
3
tools/generate_kernel.sh
Executable file
@@ -0,0 +1,3 @@
|
||||
cd "$(dirname "$0")"
|
||||
|
||||
llvm-objcopy -O binary ../target/aarch64-unknown-none/release/nova ../kernel8.img
|
||||
@@ -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
|
||||
|
||||
3
tools/start_simulator_debug.sh
Executable file
3
tools/start_simulator_debug.sh
Executable file
@@ -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
|
||||
Reference in New Issue
Block a user