Reduce clutter by using print macros with fmt

This commit is contained in:
2025-08-30 17:50:07 +02:00
parent 64ca4797bd
commit cc9cf94f5e
7 changed files with 67 additions and 104 deletions

View File

@@ -4,21 +4,18 @@ mod bitmaps;
use bitmaps::BASIC_LEGACY; use bitmaps::BASIC_LEGACY;
use crate::{ use crate::mailbox::{read_mailbox, write_mailbox};
mailbox::{read_mailbox, write_mailbox},
peripherals::uart::{print, print_u32, print_u32_hex},
};
#[repr(align(16))] #[repr(align(16))]
struct Mailbox([u32; 36]); struct Mailbox([u32; 36]);
const ALLOCATE_BUFFER: u32 = 0x00040001; const ALLOCATE_BUFFER: u32 = 0x0004_0001;
const GET_PHYSICAL_DISPLAY_WH: u32 = 0x00040003; const GET_PHYSICAL_DISPLAY_WH: u32 = 0x0004_0003;
const SET_PHYSICAL_DISPLAY_WH: u32 = 0x00048003; const SET_PHYSICAL_DISPLAY_WH: u32 = 0x0004_8003;
const SET_VIRTUAL_DISPLAY_WH: u32 = 0x00048004; const SET_VIRTUAL_DISPLAY_WH: u32 = 0x0004_8004;
const SET_PIXEL_DEPTH: u32 = 0x00048005; const SET_PIXEL_DEPTH: u32 = 0x0004_8005;
const SET_PIXEL_ORDER: u32 = 0x00048006; const SET_PIXEL_ORDER: u32 = 0x0004_8006;
const SET_FB_OFFSET: u32 = 0x00048009; const GET_PITCH: u32 = 0x000_40008;
const GET_PITCH: u32 = 0x00040008; const SET_FB_OFFSET: u32 = 0x0004_8009;
pub struct FrameBuffer { pub struct FrameBuffer {
pixel_depth: u32, // Bits per pixel pixel_depth: u32, // Bits per pixel
@@ -89,7 +86,7 @@ impl FrameBuffer {
let _ = read_mailbox(8); let _ = read_mailbox(8);
if mailbox.0[1] == 0 { if mailbox.0[1] == 0 {
print("Failed\r\n"); println!("Failed");
} }
mailbox.0[28] &= 0x3FFFFFFF; mailbox.0[28] &= 0x3FFFFFFF;
@@ -263,12 +260,8 @@ pub fn print_display_resolution() {
let _ = read_mailbox(8); let _ = read_mailbox(8);
if mailbox[1] == 0 { if mailbox[1] == 0 {
print("Failed\r\n"); println!("Failed");
} }
print("Width x Height: "); println!("Width x Height: {}x{}", mailbox[5], mailbox[6]);
print_u32(mailbox[5]);
print(" x ");
print_u32(mailbox[6]);
print("\r\n");
} }

View File

@@ -5,10 +5,8 @@ use core::{
use crate::{ use crate::{
mmio_read, mmio_write, mmio_read, mmio_write,
peripherals::{ peripherals::gpio::{blink_gpio, SpecificGpio},
gpio::{blink_gpio, SpecificGpio}, print,
uart::print,
},
timer::sleep_s, timer::sleep_s,
}; };
@@ -45,7 +43,7 @@ unsafe extern "C" fn irq_handler() {
#[no_mangle] #[no_mangle]
unsafe extern "C" fn synchronous_interrupt() { unsafe extern "C" fn synchronous_interrupt() {
loop { loop {
print("Sync Exception \r\n"); println!("Sync Exception");
blink_gpio(SpecificGpio::OnboardLed as u8, 100); blink_gpio(SpecificGpio::OnboardLed as u8, 100);
esr_uart_dump(); esr_uart_dump();
sleep_s(200); sleep_s(200);
@@ -62,28 +60,28 @@ fn esr_uart_dump() {
} }
for i in (0..32).rev() { for i in (0..32).rev() {
if ((esr >> i) & 1) == 0 { if ((esr >> i) & 1) == 0 {
print("0"); print!("0");
} else { } else {
print("1"); print!("1");
} }
if i % 4 == 0 && i > 0 { if i % 4 == 0 && i > 0 {
print("_"); print!("_");
} }
if i == 26 || i == 25 || i == 0 { if i == 26 || i == 25 || i == 0 {
print("\n\r"); print!("\n\r");
} }
} }
} }
fn handle_gpio_interrupt() { fn handle_gpio_interrupt() {
print("Interrupt\r\n"); println!("Interrupt");
for i in 0..=53u32 { for i in 0..=53u32 {
let val = read_gpio_event_detect_status(i); let val = read_gpio_event_detect_status(i);
if val { if val {
match i { match i {
26 => print("Button Pressed"), 26 => print!("Button Pressed"),
_ => {} _ => {}
} }
// Reset GPIO Interrupt handler by writing a 1 // Reset GPIO Interrupt handler by writing a 1

View File

@@ -2,6 +2,23 @@
use core::ptr::{read_volatile, write_volatile}; use core::ptr::{read_volatile, write_volatile};
#[macro_export]
macro_rules! print {
() => {};
($($arg:tt)*) => {
$crate::peripherals::uart::_print(format_args!($($arg)*))
};
}
#[macro_export]
macro_rules! println {
() => {};
($($arg:tt)*) => {
print!($($arg)*);
print!("\r\n");
};
}
pub mod peripherals; pub mod peripherals;
pub mod configuration; pub mod configuration;

View File

@@ -1,4 +1,4 @@
use crate::{mmio_read, mmio_write, peripherals::uart::print}; use crate::{mmio_read, mmio_write, print};
const MBOX_BASE: u32 = 0x3F00_0000 + 0xB880; const MBOX_BASE: u32 = 0x3F00_0000 + 0xB880;
@@ -51,7 +51,7 @@ pub fn read_soc_temp() -> u32 {
let _ = read_mailbox(8); let _ = read_mailbox(8);
if mailbox[1] == 0 { if mailbox[1] == 0 {
print("Failed\r\n"); println!("Failed");
} }
let raw_temp = mailbox[6] / 1000; let raw_temp = mailbox[6] / 1000;
raw_temp raw_temp

View File

@@ -18,8 +18,9 @@ use nova::{
blink_gpio, gpio_pull_up, set_falling_edge_detect, set_gpio_function, GPIOFunction, blink_gpio, gpio_pull_up, set_falling_edge_detect, set_gpio_function, GPIOFunction,
SpecificGpio, SpecificGpio,
}, },
uart::{print, print_u32, uart_init}, uart::uart_init,
}, },
print, println,
timer::{delay_nops, sleep_us}, timer::{delay_nops, sleep_us},
}; };
@@ -34,7 +35,7 @@ extern "C" {
#[panic_handler] #[panic_handler]
fn panic(_panic: &PanicInfo) -> ! { fn panic(_panic: &PanicInfo) -> ! {
loop { loop {
print("Panic\r\n"); println!("Panic");
} }
} }
@@ -64,7 +65,7 @@ pub extern "C" fn main() -> ! {
// Delay so clock speed can stabilize // Delay so clock speed can stabilize
delay_nops(50000); delay_nops(50000);
print("Hello World!\r\n"); println!("Hello World!");
unsafe { unsafe {
asm!("mrs x0, SCTLR_EL1"); asm!("mrs x0, SCTLR_EL1");
@@ -123,8 +124,7 @@ pub extern "C" fn kernel_main() -> ! {
loop { loop {
let temp = read_soc_temp(); let temp = read_soc_temp();
print_u32(temp); println!("{} °C", temp);
print("\r\n");
blink_gpio(SpecificGpio::OnboardLed as u8, 500); blink_gpio(SpecificGpio::OnboardLed as u8, 500);
} }
@@ -163,6 +163,5 @@ fn print_current_el_str() {
_ => "Unknown EL", _ => "Unknown EL",
}; };
print(el_str); println!("{}", el_str);
print("\r\n");
} }

View File

@@ -1,2 +1,3 @@
pub mod gpio; pub mod gpio;
#[macro_use]
pub mod uart; pub mod uart;

View File

@@ -1,4 +1,7 @@
use core::arch::asm; use core::{
arch::asm,
fmt::{self, Write},
};
use crate::{mmio_read, mmio_write}; use crate::{mmio_read, mmio_write};
@@ -20,8 +23,10 @@ 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; const UART0_LCRH_FEN: u32 = 1 << 4;
/// Print `s` over UART pub struct Uart;
pub fn print(s: &str) {
impl Write for Uart {
fn write_str(&mut self, s: &str) -> core::fmt::Result {
for byte in s.bytes() { for byte in s.bytes() {
while (mmio_read(UART0_FR) & UART0_FR_TXFF) != 0 { while (mmio_read(UART0_FR) & UART0_FR_TXFF) != 0 {
unsafe { asm!("nop") } unsafe { asm!("nop") }
@@ -30,66 +35,16 @@ pub fn print(s: &str) {
} }
// wait till uart is not busy anymore // wait till uart is not busy anymore
while ((mmio_read(UART0_FR) >> 3) & 0b1) != 0 {} while ((mmio_read(UART0_FR) >> 3) & 0b1) != 0 {}
} Ok(())
pub fn print_u32(mut val: u32) {
let mut last_valid = 0;
let mut values = [0u32; 10];
for (i, c) in (&mut values).iter_mut().enumerate() {
if val == 0 {
break;
}
*c = val % 10;
val /= 10;
if *c != 0 {
last_valid = i;
}
}
for (i, c) in values.iter().enumerate().rev() {
if i > last_valid {
continue;
}
let ascii_byte = b'0' + *c as u8;
let data = [ascii_byte];
let s = str::from_utf8(&data).unwrap();
print(s);
} }
} }
pub fn print_u32_hex(mut val: u32) { pub fn _print(args: fmt::Arguments) {
let mut last_valid = 0; let _ = Uart.write_fmt(args);
let mut values = [0u32; 8]; }
for (i, c) in (&mut values).iter_mut().enumerate() {
if val == 0 {
break;
}
*c = val % 16; pub fn _print_str(st: &str) {
val /= 16; let _ = Uart.write_str(st);
if *c != 0 {
last_valid = i;
}
}
for (i, c) in values.iter().enumerate().rev() {
if i > last_valid {
continue;
}
let ascii_byte = if *c < 10 {
b'0' + *c as u8
} else {
b'A' - 10 + *c as u8
};
let data = [ascii_byte];
let s = str::from_utf8(&data).unwrap();
print(s);
}
} }
/// Initialize UART peripheral /// Initialize UART peripheral