error handling

This commit is contained in:
2025-09-13 15:02:15 +02:00
parent 31d52e2441
commit 981a6cd65c
6 changed files with 22 additions and 14 deletions

View File

@@ -35,3 +35,8 @@ 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,
}

View File

@@ -1,4 +1,4 @@
use crate::{mmio_read, mmio_write, print};
use crate::{mmio_read, mmio_write, NovaError};
const MBOX_BASE: u32 = 0x3F00_0000 + 0xB880;
@@ -25,9 +25,11 @@ macro_rules! max {
#[macro_export]
macro_rules! mailbox_command {
($name:ident,$tag:expr, $request_len:expr,$response_len:expr) => {
($name:ident, $tag:expr, $request_len:expr,$response_len:expr) => {
/// More information at: https://github.com/raspberrypi/firmware/wiki/Mailbox-property-interface
pub fn $name(request_data: [u32; $request_len / 4]) -> [u32; $response_len / 4] {
pub fn $name(
request_data: [u32; $request_len / 4],
) -> Result<[u32; $response_len / 4], 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
@@ -35,8 +37,8 @@ macro_rules! mailbox_command {
mailbox[2] = $tag; // Command Tag
mailbox[3] = max!($request_len, $response_len) as u32; // Max value buffer size
mailbox[4] = $request_len;
mailbox[5..(5 + ($request_len / 4))].copy_from_slice(&request_data);
mailbox[5..(5 + ($request_len / 4))].copy_from_slice(&request_data);
mailbox[(5 + ($request_len / 4))..].fill(0);
let addr = core::ptr::addr_of!(mailbox[0]) as u32;
@@ -46,17 +48,17 @@ macro_rules! mailbox_command {
let _ = read_mailbox(8);
if mailbox[1] == 0 {
println!("Mailbox Command Failed!");
return Err(NovaError::Mailbox);
}
let mut out = [0u32; $response_len / 4]; // TODO: Can this be improved?
out.copy_from_slice(&mailbox[5..(5 + $response_len / 4)]);
out
Ok(out)
}
};
}
mailbox_command!(read_soc_temp, 0x00030006, 4, 8);
mailbox_command!(mb_read_soc_temp, 0x00030006, 4, 8);
pub fn read_mailbox(channel: u32) -> u32 {
// Wait until mailbox is not empty

View File

@@ -11,7 +11,7 @@ use core::{
use nova::{
framebuffer::{print_display_resolution, FrameBuffer, BLUE, GREEN, ORANGE, RED, YELLOW},
irq_interrupt::enable_irq_source,
mailbox::read_soc_temp,
mailbox::mb_read_soc_temp,
math::polar_to_cartesian,
peripherals::{
gpio::{
@@ -72,12 +72,13 @@ pub extern "C" fn main() -> ! {
el2_to_el1();
}
#[allow(clippy::empty_loop)]
loop {}
}
unsafe fn zero_bss() {
let mut bss: *mut u32 = &raw mut __bss_start as *mut u32;
while bss < &raw mut __bss_end as *mut u32 {
let mut bss: *mut u32 = &raw mut __bss_start;
while bss < &raw mut __bss_end {
write_volatile(bss, 0);
bss = bss.add(1);
}
@@ -123,7 +124,7 @@ pub extern "C" fn kernel_main() -> ! {
fb.draw_function(cos, 100, 105, BLUE);
loop {
let temp = read_soc_temp([0]);
let temp = mb_read_soc_temp([0]).unwrap();
println!("{} °C", temp[1] / 1000);
blink_gpio(SpecificGpio::OnboardLed as u8, 500);

View File

@@ -77,7 +77,7 @@ pub fn gpio_get_state(gpio: u8) -> u8 {
let register_addr = GPLEV_BASE + (register_index as u32 * 4);
let state = mmio_read(register_addr);
return ((state >> register_offset) & 0b1) as u8;
((state >> register_offset) & 0b1) as u8
}
/// Pull GPIO up

View File

@@ -20,7 +20,7 @@ const UART0_CR: u32 = 0x3F20_1030;
const UART0_CR_UARTEN: u32 = 1 << 0;
const UART0_CR_TXE: u32 = 1 << 8;
const UART0_LCRH: u32 = 0x3F20_102c;
const UART0_LCRH: u32 = 0x3F20_102C;
const UART0_LCRH_FEN: u32 = 1 << 4;
pub struct Uart;

View File

@@ -3,7 +3,7 @@ use crate::mmio_read;
const TIMER_CLO: u32 = 0x3F00_3004;
fn read_clo() -> u32 {
return mmio_read(TIMER_CLO);
mmio_read(TIMER_CLO)
}
/// Sleep for `us` microseconds