From 74e3120ba2f1de91adc3e40b40c9c136755557ca Mon Sep 17 00:00:00 2001 From: bunnie Date: Wed, 20 Aug 2025 01:46:18 +0800 Subject: [PATCH] move console over to UART2 and set speed to 1Mbps this makes on the go debugging a bit easier. the memory initialiazation also has to be moved earlier because the static initialization of the bool that determines which console mode to use has to be clean early on. It may even need to move into the asm stub, but would prefer to avoid that if I can. --- baremetal/src/platform/cramium/cramium.rs | 34 ++++++++++++----------- baremetal/src/platform/cramium/debug.rs | 32 +++++++++++++++++---- 2 files changed, 44 insertions(+), 22 deletions(-) diff --git a/baremetal/src/platform/cramium/cramium.rs b/baremetal/src/platform/cramium/cramium.rs index 2c47d7115..2c6003b80 100644 --- a/baremetal/src/platform/cramium/cramium.rs +++ b/baremetal/src/platform/cramium/cramium.rs @@ -32,6 +32,22 @@ pub const SYSTEM_CLOCK_FREQUENCY: u32 = 800_000_000; pub const SYSTEM_TICK_INTERVAL_MS: u32 = 1; pub fn early_init() { + // Define the .data region - bootstrap baremetal using these hard-coded parameters. + const DATA_ORIGIN: usize = 0x61080000; + const DATA_SIZE_BYTES: usize = 0x5000; + const DATA_INIT: [(usize, u32); 1] = [(0x0, 0x1)]; + + // Clear .data, .bss, .stack, .heap regions & setup .data values + unsafe { + let data_ptr = DATA_ORIGIN as *mut u32; + for i in 0..DATA_SIZE_BYTES / size_of::() { + data_ptr.add(i).write_volatile(0); + } + for (offset, data) in DATA_INIT { + data_ptr.add(offset).write_volatile(data); + } + } + let iox = Iox::new(utra::iox::HW_IOX_BASE as *mut u32); #[cfg(not(feature = "nto-evb"))] { @@ -191,22 +207,6 @@ pub fn early_init() { */ crate::println!("scratch page: {:x}, heap start: {:x}", SCRATCH_PAGE, HEAP_START); - // Define the .data region - bootstrap baremetal using these hard-coded parameters. - const DATA_ORIGIN: usize = 0x61080000; - const DATA_SIZE_BYTES: usize = 0x5000; - const DATA_INIT: [(usize, u32); 1] = [(0x0, 0x1)]; - - // Clear .data, .bss, .stack, .heap regions & setup .data values - unsafe { - let data_ptr = DATA_ORIGIN as *mut u32; - for i in 0..DATA_SIZE_BYTES / size_of::() { - data_ptr.add(i).write_volatile(0); - } - for (offset, data) in DATA_INIT { - data_ptr.add(offset).write_volatile(data); - } - } - // setup heap alloc setup_alloc(); @@ -218,6 +218,8 @@ pub fn early_init() { enable_irq(utra::irqarray5::IRQARRAY5_IRQ); udma_uart.write("console up\r\n".as_bytes()); + crate::debug::USE_CONSOLE.store(true, core::sync::atomic::Ordering::SeqCst); + crate::println!("This debug print should be on the UDMA UART"); // Setup I/Os so things that should be powered off are actually off cramium_hal::board::setup_display_pins(&iox); diff --git a/baremetal/src/platform/cramium/debug.rs b/baremetal/src/platform/cramium/debug.rs index 4eb2455c9..abd2bfcf7 100644 --- a/baremetal/src/platform/cramium/debug.rs +++ b/baremetal/src/platform/cramium/debug.rs @@ -1,9 +1,13 @@ +use core::sync::atomic::{AtomicBool, Ordering::SeqCst}; + use cramium_api::*; use cramium_hal::iox::Iox; use cramium_hal::udma::UartIrq; use cramium_hal::{udma, udma::GlobalConfig}; use utralib::generated::*; +pub static USE_CONSOLE: AtomicBool = AtomicBool::new(false); + /// A trait for serial like drivers which allows reading from a source. #[allow(dead_code)] pub trait SerialRead { @@ -15,13 +19,29 @@ pub struct Uart {} #[allow(dead_code)] impl Uart { pub fn putc(&self, c: u8) { - let base = utra::duart::HW_DUART_BASE as *mut u32; - let mut uart = CSR::new(base); - if uart.rf(utra::duart::SFR_CR_SFR_CR) == 0 { - uart.wfo(utra::duart::SFR_CR_SFR_CR, 1); + if !USE_CONSOLE.load(SeqCst) { + let base = utra::duart::HW_DUART_BASE as *mut u32; + let mut uart = CSR::new(base); + if uart.rf(utra::duart::SFR_CR_SFR_CR) == 0 { + uart.wfo(utra::duart::SFR_CR_SFR_CR, 1); + } + while uart.r(utra::duart::SFR_SR) != 0 {} + uart.wo(utra::duart::SFR_TXD, c as u32); + } else { + let buf: [u8; 1] = [c]; + let uart_buf_addr = crate::platform::UART_IFRAM_ADDR; + #[cfg(feature = "nto-evb")] + let mut udma_uart = unsafe { + // safety: this is safe to call, because we set up clock and events prior to calling new. + udma::Uart::get_handle(utra::udma_uart_1::HW_UDMA_UART_1_BASE, uart_buf_addr, uart_buf_addr) + }; + #[cfg(not(feature = "nto-evb"))] + let mut udma_uart = unsafe { + // safety: this is safe to call, because we set up clock and events prior to calling new. + udma::Uart::get_handle(utra::udma_uart_2::HW_UDMA_UART_2_BASE, uart_buf_addr, uart_buf_addr) + }; + udma_uart.write(&buf); } - while uart.r(utra::duart::SFR_SR) != 0 {} - uart.wo(utra::duart::SFR_TXD, c as u32); } fn put_digit(&mut self, d: u8) {