mirror of
https://github.com/iceHtwoO/novaOS.git
synced 2026-04-16 20:22:26 +00:00
implement timer
This commit is contained in:
15
src/main.rs
15
src/main.rs
@@ -5,8 +5,10 @@
|
|||||||
use core::{arch::asm, panic::PanicInfo};
|
use core::{arch::asm, panic::PanicInfo};
|
||||||
|
|
||||||
use gpio::{gpio_high, gpio_low, set_gpio_state};
|
use gpio::{gpio_high, gpio_low, set_gpio_state};
|
||||||
|
use timer::{delay_nops, sleep};
|
||||||
|
|
||||||
mod gpio;
|
mod gpio;
|
||||||
|
mod timer;
|
||||||
mod uart;
|
mod uart;
|
||||||
|
|
||||||
#[panic_handler]
|
#[panic_handler]
|
||||||
@@ -31,20 +33,13 @@ extern "C" fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Delay so clock speed can stabilize
|
// Delay so clock speed can stabilize
|
||||||
unsafe { delay(50000) }
|
unsafe { delay_nops(50000) }
|
||||||
uart::print("Hello World!\n");
|
uart::print("Hello World!\n");
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let _ = gpio_high(29);
|
let _ = gpio_high(29);
|
||||||
unsafe { delay(1_000_000) }
|
unsafe { sleep(500_000) } // 0.5s
|
||||||
let _ = gpio_low(29);
|
let _ = gpio_low(29);
|
||||||
unsafe { delay(1_000_000) }
|
unsafe { sleep(500_000) } // 0.5s
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
unsafe fn delay(count: u32) {
|
|
||||||
for _ in 0..count {
|
|
||||||
// Prevent compiler optimizing away the loop
|
|
||||||
core::arch::asm!("nop");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
18
src/timer.rs
Normal file
18
src/timer.rs
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
const TIMER_CLO: u32 = 0x3F00_3004;
|
||||||
|
|
||||||
|
fn read_clo() -> u32 {
|
||||||
|
unsafe { return core::ptr::read_volatile(TIMER_CLO as *const u32) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub unsafe fn sleep(microseconds: u32) {
|
||||||
|
let start = read_clo();
|
||||||
|
while read_clo() - start < microseconds {
|
||||||
|
core::arch::asm!("nop");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub unsafe fn delay_nops(count: u32) {
|
||||||
|
for _ in 0..count {
|
||||||
|
core::arch::asm!("nop");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -23,6 +23,8 @@ pub fn print(s: &str) {
|
|||||||
core::ptr::write_volatile(UART0_DR as *mut u32, byte as u32);
|
core::ptr::write_volatile(UART0_DR as *mut u32, byte as u32);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// wait till uart is not busy anymore
|
||||||
|
unsafe { while (core::ptr::read_volatile(UART0_FR as *const u32) >> 3) & 0b1 != 0 {} }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn configure_uart() {
|
pub fn configure_uart() {
|
||||||
|
|||||||
Reference in New Issue
Block a user