mirror of
https://github.com/iceHtwoO/novaOS.git
synced 2026-04-17 04:32:27 +00:00
Compare commits
8 Commits
logger
...
abdef70198
| Author | SHA1 | Date | |
|---|---|---|---|
| abdef70198 | |||
| 31e68b9bd2 | |||
| 4e79832e00 | |||
| 326a779692 | |||
| 27b185239f | |||
| f0f71ea490 | |||
| 4dbbfa1fcf | |||
| 44cfcd9f69 |
@@ -1,5 +1,2 @@
|
||||
[build]
|
||||
target = "aarch64-unknown-none"
|
||||
|
||||
[target.aarch64-unknown-none]
|
||||
rustflags = ["-C", "link-arg=-Tlink.ld"]
|
||||
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -3,3 +3,4 @@ kernel8.img
|
||||
.env
|
||||
sd.img
|
||||
settings.json
|
||||
.DS_Store
|
||||
|
||||
2
.vscode/tasks.json
vendored
2
.vscode/tasks.json
vendored
@@ -14,7 +14,7 @@
|
||||
{
|
||||
"label": "Run QEMU",
|
||||
"type": "shell",
|
||||
"command": "qemu-system-aarch64 -M raspi3b -cpu cortex-a53 -serial stdio -sd sd.img -display none -kernel ${workspaceFolder}/target/aarch64-unknown-none/debug/kernel8.img -S -s -m 1024",
|
||||
"command": "llvm-objcopy -O binary target/aarch64-unknown-none/debug/nova target/aarch64-unknown-none/debug/kernel8.img && qemu-system-aarch64 -M raspi3b -cpu cortex-a53 -serial stdio -sd sd.img -kernel ${workspaceFolder}/target/aarch64-unknown-none/debug/kernel8.img -S -s -m 1024",
|
||||
"isBackground": true,
|
||||
"dependsOn": ["Build"]
|
||||
}
|
||||
|
||||
13
Cargo.lock
generated
13
Cargo.lock
generated
@@ -2,6 +2,17 @@
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "NovaError"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "heap"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"NovaError",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libm"
|
||||
version = "0.2.15"
|
||||
@@ -12,5 +23,7 @@ checksum = "f9fbbcab51052fe104eb5e5d351cf728d30a5be1fe14d9be8a3b097481fb97de"
|
||||
name = "nova"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"NovaError",
|
||||
"heap",
|
||||
"libm",
|
||||
]
|
||||
|
||||
@@ -14,3 +14,11 @@ panic = "abort"
|
||||
|
||||
[dependencies]
|
||||
libm = "0.2.15"
|
||||
heap = {path = "heap"}
|
||||
NovaError = {path = "NovaError"}
|
||||
|
||||
[workspace]
|
||||
|
||||
members = [ "NovaError",
|
||||
"heap"
|
||||
]
|
||||
|
||||
4
NovaError/Cargo.toml
Normal file
4
NovaError/Cargo.toml
Normal file
@@ -0,0 +1,4 @@
|
||||
[package]
|
||||
name = "NovaError"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
11
NovaError/src/lib.rs
Normal file
11
NovaError/src/lib.rs
Normal file
@@ -0,0 +1,11 @@
|
||||
#![no_std]
|
||||
|
||||
use core::fmt::Debug;
|
||||
use core::prelude::rust_2024::derive;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum NovaError {
|
||||
Mailbox,
|
||||
HeapFull,
|
||||
EmptyHeapSegmentNotAllowed,
|
||||
}
|
||||
@@ -8,9 +8,14 @@ NovaOS is a expository project where I build a kernel from scratch for a Raspber
|
||||
|
||||
- Delay and sleep ✓
|
||||
- UART ✓
|
||||
- Switching ELs ✓
|
||||
- GPIOs ✓
|
||||
- GPIO Interrupts ✓
|
||||
- Communicate with peripherals via mailboxes ✓
|
||||
- Frame Buffer ✓
|
||||
- Heap Memory allocation ✓
|
||||
- Multi Core
|
||||
- Dynamic clock speed
|
||||
- MMU
|
||||
- Multiprocessing
|
||||
- Basic Terminal over UART
|
||||
|
||||
7
heap/Cargo.toml
Normal file
7
heap/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "heap"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
NovaError = {path = "../NovaError"}
|
||||
194
heap/src/lib.rs
Normal file
194
heap/src/lib.rs
Normal file
@@ -0,0 +1,194 @@
|
||||
#![allow(static_mut_refs)]
|
||||
#![cfg_attr(not(test), no_std)]
|
||||
|
||||
use core::{
|
||||
alloc::GlobalAlloc,
|
||||
default::Default,
|
||||
mem::size_of,
|
||||
prelude::v1::*,
|
||||
ptr::{self, null_mut, read_volatile},
|
||||
result::Result,
|
||||
};
|
||||
|
||||
use NovaError::NovaError;
|
||||
|
||||
#[cfg(not(target_os = "none"))]
|
||||
extern crate std;
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
#[repr(C, align(16))]
|
||||
pub struct HeapHeader {
|
||||
pub next: *mut HeapHeader,
|
||||
before: *mut HeapHeader,
|
||||
pub size: usize,
|
||||
free: bool,
|
||||
}
|
||||
|
||||
const HEAP_HEADER_SIZE: usize = size_of::<HeapHeader>();
|
||||
const MIN_BLOCK_SIZE: usize = 16;
|
||||
|
||||
// TODO: This implementation has to be reevaluated when implementing multiprocessing
|
||||
// Spinlock could be a solution but has its issues:
|
||||
// https://matklad.github.io/2020/01/02/spinlocks-considered-harmful.html
|
||||
|
||||
pub struct Heap {
|
||||
pub start_address: *mut HeapHeader,
|
||||
pub end_address: *mut HeapHeader,
|
||||
pub raw_size: usize,
|
||||
}
|
||||
impl Heap {
|
||||
pub const fn empty() -> Self {
|
||||
Self {
|
||||
start_address: null_mut() as *mut HeapHeader,
|
||||
end_address: null_mut() as *mut HeapHeader,
|
||||
raw_size: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init(&mut self, heap_start: usize, heap_end: usize) {
|
||||
self.start_address = heap_start as *mut HeapHeader;
|
||||
self.end_address = heap_end as *mut HeapHeader;
|
||||
|
||||
self.raw_size = heap_end - heap_start;
|
||||
|
||||
unsafe {
|
||||
ptr::write(
|
||||
self.start_address,
|
||||
HeapHeader {
|
||||
next: null_mut(),
|
||||
before: null_mut(),
|
||||
size: self.raw_size - HEAP_HEADER_SIZE,
|
||||
free: true,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn find_first_fit(&self, size: usize) -> Result<*mut HeapHeader, NovaError> {
|
||||
let mut current = self.start_address;
|
||||
while !fits(size, current) {
|
||||
if (*self.start_address).next.is_null() {
|
||||
return Err(NovaError::HeapFull);
|
||||
}
|
||||
current = (*current).next;
|
||||
}
|
||||
Ok(current)
|
||||
}
|
||||
|
||||
pub fn malloc(&self, mut size: usize) -> Result<*mut u8, NovaError> {
|
||||
if size == 0 {
|
||||
return Err(NovaError::EmptyHeapSegmentNotAllowed);
|
||||
}
|
||||
|
||||
if size < MIN_BLOCK_SIZE {
|
||||
size = MIN_BLOCK_SIZE;
|
||||
}
|
||||
|
||||
// Align size to the next 16 bytes
|
||||
size += (16 - (size % 16)) % 16;
|
||||
|
||||
unsafe {
|
||||
// Find First-Fit memory segment
|
||||
let current = self.find_first_fit(size)?;
|
||||
|
||||
// Return entire block WITHOUT generating a new header
|
||||
// if the current block doesn't have enough space to hold: requested size + HEAP_HEADER_SIZE + MIN_BLOCK_SIZE
|
||||
if (*current).size < size + HEAP_HEADER_SIZE + MIN_BLOCK_SIZE {
|
||||
(*current).free = false;
|
||||
return Ok(current.byte_add(HEAP_HEADER_SIZE) as *mut u8);
|
||||
}
|
||||
|
||||
Self::fragment_segment(current, size);
|
||||
|
||||
let data_start_address = current.byte_add(HEAP_HEADER_SIZE);
|
||||
|
||||
Ok(data_start_address as *mut u8)
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn fragment_segment(current: *mut HeapHeader, size: usize) {
|
||||
let byte_offset = HEAP_HEADER_SIZE + size;
|
||||
let new_address = unsafe { current.byte_add(byte_offset) };
|
||||
|
||||
// Handle case where fragmenting center free space
|
||||
let next = (*current).next;
|
||||
if !(*current).next.is_null() {
|
||||
(*next).before = new_address;
|
||||
}
|
||||
|
||||
unsafe {
|
||||
ptr::write(
|
||||
new_address as *mut HeapHeader,
|
||||
HeapHeader {
|
||||
next,
|
||||
before: current,
|
||||
size: (*current).size - size - HEAP_HEADER_SIZE,
|
||||
free: true,
|
||||
},
|
||||
)
|
||||
};
|
||||
(*current).next = new_address;
|
||||
(*current).free = false;
|
||||
(*current).size = size;
|
||||
}
|
||||
|
||||
pub fn free(&self, pointer: *mut u8) -> Result<(), NovaError> {
|
||||
let mut segment = unsafe { pointer.sub(HEAP_HEADER_SIZE) as *mut HeapHeader };
|
||||
unsafe {
|
||||
// IF prev is free:
|
||||
// Delete header, add size to previous and fix pointers.
|
||||
// Move Head left
|
||||
if !(*segment).before.is_null() && (*(*segment).before).free {
|
||||
let before_head = (*segment).before;
|
||||
(*before_head).size += (*segment).size + HEAP_HEADER_SIZE;
|
||||
delete_header(segment);
|
||||
segment = before_head;
|
||||
}
|
||||
// IF next is free:
|
||||
// Delete next header and merge size, fix pointers
|
||||
if !(*segment).next.is_null() && (*(*segment).next).free {
|
||||
let next_head = (*segment).next;
|
||||
(*segment).size += (*next_head).size + HEAP_HEADER_SIZE;
|
||||
delete_header(next_head);
|
||||
}
|
||||
|
||||
// Neither: Set free
|
||||
(*segment).free = true;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl GlobalAlloc for Heap {
|
||||
unsafe fn alloc(&self, layout: core::alloc::Layout) -> *mut u8 {
|
||||
self.malloc(layout.size()).unwrap()
|
||||
}
|
||||
|
||||
unsafe fn dealloc(&self, ptr: *mut u8, _: core::alloc::Layout) {
|
||||
self.free(ptr).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl Sync for Heap {}
|
||||
|
||||
unsafe fn fits(size: usize, header: *mut HeapHeader) -> bool {
|
||||
(*header).free && size <= (*header).size
|
||||
}
|
||||
|
||||
unsafe fn delete_header(header: *mut HeapHeader) {
|
||||
let before = (*header).before;
|
||||
let next = (*header).next;
|
||||
|
||||
if !before.is_null() {
|
||||
(*before).next = next;
|
||||
}
|
||||
|
||||
if !next.is_null() {
|
||||
(*next).before = before;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {}
|
||||
10
link.ld
10
link.ld
@@ -27,9 +27,17 @@ SECTIONS {
|
||||
KEEP(*(.vector_table))
|
||||
}
|
||||
|
||||
.stack 0x8018000 : ALIGN(16)
|
||||
.heap 0x8000000 : ALIGN(16)
|
||||
{
|
||||
__heap_start = .;
|
||||
. += 0x10000; #10kB
|
||||
__heap_end = .;
|
||||
}
|
||||
|
||||
.stack : ALIGN(16)
|
||||
{
|
||||
__stack_start = .;
|
||||
. += 0x10000; #10kB stack
|
||||
__stack_end = .;
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,6 @@ use crate::mailbox::{read_mailbox, write_mailbox};
|
||||
struct Mailbox([u32; 36]);
|
||||
|
||||
const ALLOCATE_BUFFER: u32 = 0x0004_0001;
|
||||
const GET_PHYSICAL_DISPLAY_WH: u32 = 0x0004_0003;
|
||||
const SET_PHYSICAL_DISPLAY_WH: u32 = 0x0004_8003;
|
||||
const SET_VIRTUAL_DISPLAY_WH: u32 = 0x0004_8004;
|
||||
const SET_PIXEL_DEPTH: u32 = 0x0004_8005;
|
||||
@@ -242,26 +241,3 @@ impl FrameBuffer {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn print_display_resolution() {
|
||||
let mut mailbox: [u32; 8] = [0; 8];
|
||||
mailbox[0] = 8 * 4;
|
||||
mailbox[1] = 0;
|
||||
mailbox[2] = GET_PHYSICAL_DISPLAY_WH;
|
||||
mailbox[3] = 8;
|
||||
mailbox[4] = 0;
|
||||
mailbox[5] = 0;
|
||||
mailbox[6] = 0;
|
||||
mailbox[7] = 0;
|
||||
|
||||
let addr = core::ptr::addr_of!(mailbox[0]) as u32;
|
||||
|
||||
write_mailbox(8, addr);
|
||||
|
||||
let _ = read_mailbox(8);
|
||||
if mailbox[1] == 0 {
|
||||
println!("Failed");
|
||||
}
|
||||
|
||||
println!("Width x Height: {}x{}", mailbox[5], mailbox[6]);
|
||||
}
|
||||
|
||||
35
src/lib.rs
35
src/lib.rs
@@ -1,6 +1,33 @@
|
||||
#![no_std]
|
||||
|
||||
use core::ptr::{read_volatile, write_volatile};
|
||||
use core::{
|
||||
panic::PanicInfo,
|
||||
ptr::{read_volatile, write_volatile},
|
||||
};
|
||||
|
||||
use heap::Heap;
|
||||
|
||||
unsafe extern "C" {
|
||||
unsafe static mut __heap_start: u8;
|
||||
unsafe static mut __heap_end: u8;
|
||||
}
|
||||
|
||||
#[global_allocator]
|
||||
pub static mut GLOBAL_ALLOCATOR: Heap = Heap::empty();
|
||||
|
||||
pub unsafe fn init_heap() {
|
||||
let start = core::ptr::addr_of_mut!(__heap_start) as usize;
|
||||
let end = core::ptr::addr_of_mut!(__heap_end) as usize;
|
||||
|
||||
GLOBAL_ALLOCATOR.init(start, end);
|
||||
}
|
||||
|
||||
#[panic_handler]
|
||||
fn panic(_panic: &PanicInfo) -> ! {
|
||||
loop {
|
||||
println!("Panic");
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! print {
|
||||
@@ -25,7 +52,6 @@ pub mod configuration;
|
||||
pub mod framebuffer;
|
||||
pub mod irq_interrupt;
|
||||
pub mod mailbox;
|
||||
pub mod math;
|
||||
pub mod timer;
|
||||
|
||||
pub fn mmio_read(address: u32) -> u32 {
|
||||
@@ -35,8 +61,3 @@ pub fn mmio_read(address: u32) -> u32 {
|
||||
pub fn mmio_write(address: u32, data: u32) {
|
||||
unsafe { write_volatile(address as *mut u32, data) }
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum NovaError {
|
||||
Mailbox,
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::{mmio_read, mmio_write, NovaError};
|
||||
use crate::{mmio_read, mmio_write};
|
||||
|
||||
const MBOX_BASE: u32 = 0x3F00_0000 + 0xB880;
|
||||
|
||||
@@ -29,7 +29,7 @@ macro_rules! mailbox_command {
|
||||
/// More information at: https://github.com/raspberrypi/firmware/wiki/Mailbox-property-interface
|
||||
pub fn $name(
|
||||
request_data: [u32; $request_len / 4],
|
||||
) -> Result<[u32; $response_len / 4], NovaError> {
|
||||
) -> Result<[u32; $response_len / 4], NovaError::NovaError> {
|
||||
let mut mailbox =
|
||||
[0u32; (HEADER_LENGTH + max!($request_len, $response_len) + FOOTER_LENGTH) / 4];
|
||||
mailbox[0] = (HEADER_LENGTH + max!($request_len, $response_len) + FOOTER_LENGTH) as u32; // Total length in Bytes
|
||||
@@ -48,17 +48,20 @@ macro_rules! mailbox_command {
|
||||
let _ = read_mailbox(8);
|
||||
|
||||
if mailbox[1] == 0 {
|
||||
return Err(NovaError::Mailbox);
|
||||
return Err(NovaError::NovaError::Mailbox);
|
||||
}
|
||||
|
||||
let mut out = [0u32; $response_len / 4]; // TODO: Can this be improved?
|
||||
let mut out = [0u32; $response_len / 4];
|
||||
out.copy_from_slice(&mailbox[5..(5 + $response_len / 4)]);
|
||||
Ok(out)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
mailbox_command!(mb_read_soc_temp, 0x00030006, 4, 8);
|
||||
mailbox_command!(mb_read_soc_temp, 0x0003_0006, 4, 8);
|
||||
|
||||
// Framebuffer
|
||||
mailbox_command!(mb_get_display_resolution, 0x0004_0003, 0, 8);
|
||||
|
||||
pub fn read_mailbox(channel: u32) -> u32 {
|
||||
// Wait until mailbox is not empty
|
||||
|
||||
72
src/main.rs
72
src/main.rs
@@ -1,18 +1,19 @@
|
||||
#![no_main]
|
||||
#![no_std]
|
||||
#![feature(asm_experimental_arch)]
|
||||
|
||||
#![allow(static_mut_refs)]
|
||||
use core::{
|
||||
arch::{asm, global_asm},
|
||||
panic::PanicInfo,
|
||||
ptr::write_volatile,
|
||||
};
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
use nova::{
|
||||
framebuffer::{print_display_resolution, FrameBuffer, BLUE, GREEN, ORANGE, RED, YELLOW},
|
||||
framebuffer::{FrameBuffer, BLUE, GREEN, RED},
|
||||
init_heap,
|
||||
irq_interrupt::enable_irq_source,
|
||||
mailbox::mb_read_soc_temp,
|
||||
math::polar_to_cartesian,
|
||||
peripherals::{
|
||||
gpio::{
|
||||
blink_gpio, gpio_pull_up, set_falling_edge_detect, set_gpio_function, GPIOFunction,
|
||||
@@ -22,6 +23,7 @@ use nova::{
|
||||
},
|
||||
print, println,
|
||||
timer::{delay_nops, sleep_us},
|
||||
GLOBAL_ALLOCATOR,
|
||||
};
|
||||
|
||||
global_asm!(include_str!("vector.S"));
|
||||
@@ -32,19 +34,12 @@ extern "C" {
|
||||
static mut __bss_end: u32;
|
||||
}
|
||||
|
||||
#[panic_handler]
|
||||
fn panic(_panic: &PanicInfo) -> ! {
|
||||
loop {
|
||||
println!("Panic");
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
#[link_section = ".text._start"]
|
||||
#[cfg_attr(not(test), link_section = ".text._start")]
|
||||
pub unsafe extern "C" fn _start() {
|
||||
// Set the stack pointer
|
||||
asm!(
|
||||
"ldr x0, =0x8008000",
|
||||
"ldr x0, =__stack_end",
|
||||
"mov sp, x0",
|
||||
"b main",
|
||||
options(noreturn)
|
||||
@@ -61,8 +56,6 @@ pub extern "C" fn main() -> ! {
|
||||
// Set ACT Led to Outout
|
||||
let _ = set_gpio_function(21, GPIOFunction::Output);
|
||||
|
||||
print_current_el_str();
|
||||
|
||||
// Delay so clock speed can stabilize
|
||||
delay_nops(50000);
|
||||
println!("Hello World!");
|
||||
@@ -86,7 +79,12 @@ unsafe fn zero_bss() {
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn kernel_main() -> ! {
|
||||
print_current_el_str();
|
||||
println!("EL: {}", get_current_el());
|
||||
|
||||
unsafe {
|
||||
init_heap();
|
||||
heap_test();
|
||||
};
|
||||
|
||||
sleep_us(500_000);
|
||||
|
||||
@@ -96,20 +94,7 @@ pub extern "C" fn kernel_main() -> ! {
|
||||
gpio_pull_up(26);
|
||||
set_falling_edge_detect(26, true);
|
||||
|
||||
print_display_resolution();
|
||||
let fb = FrameBuffer::new();
|
||||
print_display_resolution();
|
||||
|
||||
for a in 0..360 {
|
||||
let (x, y) = polar_to_cartesian(100.0, a as f32);
|
||||
fb.draw_line(
|
||||
150,
|
||||
150,
|
||||
(150.0 + x) as u32,
|
||||
(150.0 + y) as u32,
|
||||
a * (0x00FFFFFF / 360),
|
||||
);
|
||||
}
|
||||
|
||||
fb.draw_square(500, 500, 600, 700, RED);
|
||||
fb.draw_square_fill(800, 800, 900, 900, GREEN);
|
||||
@@ -118,10 +103,6 @@ pub extern "C" fn kernel_main() -> ! {
|
||||
fb.draw_string("Hello World! :D\nTest next Line", 500, 5, 3, BLUE);
|
||||
|
||||
fb.draw_function(cos, 100, 101, RED);
|
||||
fb.draw_function(cos, 100, 102, ORANGE);
|
||||
fb.draw_function(cos, 100, 103, YELLOW);
|
||||
fb.draw_function(cos, 100, 104, GREEN);
|
||||
fb.draw_function(cos, 100, 105, BLUE);
|
||||
|
||||
loop {
|
||||
let temp = mb_read_soc_temp([0]).unwrap();
|
||||
@@ -131,11 +112,21 @@ pub extern "C" fn kernel_main() -> ! {
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn heap_test() {
|
||||
let a = GLOBAL_ALLOCATOR.malloc(32).unwrap();
|
||||
let b = GLOBAL_ALLOCATOR.malloc(64).unwrap();
|
||||
let c = GLOBAL_ALLOCATOR.malloc(128).unwrap();
|
||||
let _ = GLOBAL_ALLOCATOR.malloc(256).unwrap();
|
||||
GLOBAL_ALLOCATOR.free(b).unwrap();
|
||||
GLOBAL_ALLOCATOR.free(a).unwrap();
|
||||
GLOBAL_ALLOCATOR.free(c).unwrap();
|
||||
}
|
||||
|
||||
fn cos(x: u32) -> f64 {
|
||||
libm::cos(x as f64 * 0.1) * 20.0
|
||||
}
|
||||
|
||||
pub fn get_current_el() -> u64 {
|
||||
fn get_current_el() -> u64 {
|
||||
let el: u64;
|
||||
unsafe {
|
||||
asm!(
|
||||
@@ -153,16 +144,3 @@ fn enable_uart() {
|
||||
let _ = set_gpio_function(14, GPIOFunction::Alternative0);
|
||||
let _ = set_gpio_function(15, GPIOFunction::Alternative0);
|
||||
}
|
||||
|
||||
fn print_current_el_str() {
|
||||
let el = get_current_el();
|
||||
let el_str = match el {
|
||||
0b11 => "Level 3",
|
||||
0b10 => "Level 2",
|
||||
0b01 => "Level 1",
|
||||
0b00 => "Level 0",
|
||||
_ => "Unknown EL",
|
||||
};
|
||||
|
||||
println!("{}", el_str);
|
||||
}
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
pub fn polar_to_cartesian(r: f32, theta_rad: f32) -> (f32, f32) {
|
||||
let x = r * libm::cosf(theta_rad);
|
||||
let y = r * libm::sinf(theta_rad);
|
||||
(x, y)
|
||||
}
|
||||
@@ -170,7 +170,7 @@ pub fn set_rising_edge_detect(gpio: u8, enable: bool) {
|
||||
mmio_write(register_addr, new_val);
|
||||
}
|
||||
|
||||
pub fn blink_gpio(gpio: u8, duration_ms: u32) {
|
||||
pub fn blink_gpio(gpio: u8, duration_ms: u64) {
|
||||
let _ = gpio_high(gpio);
|
||||
|
||||
sleep_ms(duration_ms);
|
||||
|
||||
55
src/timer.rs
55
src/timer.rs
@@ -1,32 +1,61 @@
|
||||
use crate::mmio_read;
|
||||
use core::{hint::spin_loop, ptr::read_volatile};
|
||||
|
||||
const TIMER_CLO: u32 = 0x3F00_3004;
|
||||
const TIMER_CLOCK_LO: u32 = 0x3F00_3004;
|
||||
const TIMER_CLOCK_HI: u32 = 0x3F00_3008;
|
||||
|
||||
fn read_clo() -> u32 {
|
||||
mmio_read(TIMER_CLO)
|
||||
fn read_timer_32() -> u32 {
|
||||
unsafe { read_volatile(TIMER_CLOCK_LO as *const u32) }
|
||||
}
|
||||
|
||||
fn read_timer_64() -> u64 {
|
||||
loop {
|
||||
let clock_hi1 = unsafe { read_volatile(TIMER_CLOCK_HI as *const u32) };
|
||||
let clock_lo = unsafe { read_volatile(TIMER_CLOCK_LO as *const u32) };
|
||||
let clock_hi2 = unsafe { read_volatile(TIMER_CLOCK_HI as *const u32) };
|
||||
|
||||
// account for roll over during read
|
||||
if clock_hi1 == clock_hi2 {
|
||||
return ((clock_hi1 as u64) << 32) | clock_lo as u64;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Sleep for `us` microseconds
|
||||
pub fn sleep_us(us: u32) {
|
||||
let start = read_clo();
|
||||
while read_clo() - start < us {
|
||||
unsafe { core::arch::asm!("nop") }
|
||||
pub fn sleep_us(us: u64) {
|
||||
if us < u32::MAX as u64 {
|
||||
sleep_us_u32(us as u32);
|
||||
} else {
|
||||
sleep_us_u64(us);
|
||||
}
|
||||
}
|
||||
|
||||
fn sleep_us_u32(us: u32) {
|
||||
let start = read_timer_32();
|
||||
while read_timer_32().wrapping_sub(start) < us {
|
||||
spin_loop();
|
||||
}
|
||||
}
|
||||
|
||||
fn sleep_us_u64(us: u64) {
|
||||
let start = read_timer_64();
|
||||
while read_timer_64().wrapping_sub(start) < us {
|
||||
spin_loop();
|
||||
}
|
||||
}
|
||||
|
||||
/// Sleep for `ms` milliseconds
|
||||
pub fn sleep_ms(ms: u32) {
|
||||
sleep_us(ms * 1000);
|
||||
pub fn sleep_ms(ms: u64) {
|
||||
sleep_us(ms * 1_000);
|
||||
}
|
||||
|
||||
/// Sleep for `s` seconds
|
||||
pub fn sleep_s(s: u32) {
|
||||
sleep_us(s * 1000);
|
||||
pub fn sleep_s(s: u64) {
|
||||
sleep_ms(s * 1_000);
|
||||
}
|
||||
|
||||
/// Wait for `count` operations to pass
|
||||
pub fn delay_nops(count: u32) {
|
||||
for _ in 0..count {
|
||||
unsafe { core::arch::asm!("nop") }
|
||||
spin_loop()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user