diff --git a/kernel/src/platform/bao1x/mod.rs b/kernel/src/platform/bao1x/mod.rs index 856ed6bfe..e649e6f65 100644 --- a/kernel/src/platform/bao1x/mod.rs +++ b/kernel/src/platform/bao1x/mod.rs @@ -2,7 +2,11 @@ // SPDX-FileCopyrightText: 2024 bunnie // SPDX-License-Identifier: Apache-2.0 -#[cfg(feature = "bao1x")] +#[cfg(all(feature = "bao1x", not(feature = "verilator-only")))] +#[path = "rand.rs"] +pub mod rand; +#[cfg(all(feature = "bao1x", feature = "verilator-only"))] +#[path = "rand_fake.rs"] pub mod rand; #[cfg(any(feature = "debug-print", feature = "print-panics"))] diff --git a/kernel/src/platform/bao1x/rand_fake.rs b/kernel/src/platform/bao1x/rand_fake.rs index 217671f63..3e6932b7b 100644 --- a/kernel/src/platform/bao1x/rand_fake.rs +++ b/kernel/src/platform/bao1x/rand_fake.rs @@ -24,7 +24,9 @@ static LOCAL_RNG_STATE: [AtomicU32; 8] = [ pub fn init() {} -/// This a fully deterministic PRNG that relies on Chacha8 for state evolution. +/// This a fully deterministic PRNG that relies on Chacha8 for state evolution. This is +/// used in verilator simulation environments. +#[cfg(feature = "verilator-only")] pub fn get_u32() -> u32 { // Local storage for the seed. let mut seed = [0u8; 32]; diff --git a/libs/bao1x-hal/Cargo.toml b/libs/bao1x-hal/Cargo.toml index 17014cf47..18a8e4654 100644 --- a/libs/bao1x-hal/Cargo.toml +++ b/libs/bao1x-hal/Cargo.toml @@ -51,6 +51,7 @@ board-baosec = [ "bmp180", "utralib/bao1x", "ux-api/board-baosec", + "ux-api/std", ] # USB form factor token loader-baosec = [ "utralib/bao1x", @@ -66,9 +67,11 @@ kernel-dabao = ["utralib/bao1x"] test-baosec = ["ux-api/loader-baosec", "camera-gc2145"] hosted-baosec = ["std"] # emulated hardware on x86 platform board-baosor = [ + "std", "camera-gc2145", "axp2101", "utralib/bao1x", + "ux-api/std", ] # Precursor form factor loader-baosor = [] board-dabao = ["utralib/bao1x"] # Dev board form factor @@ -89,7 +92,6 @@ std = [ "xous-api-names", "usb-device", "xous-api-ticktimer", - "ux-api/std", "bao1x-api/std", ] derive-rkyv = ["rkyv"] diff --git a/libs/bao1x-hal/src/board/dabao.rs b/libs/bao1x-hal/src/board/dabao.rs index 687277c52..430c0dda9 100644 --- a/libs/bao1x-hal/src/board/dabao.rs +++ b/libs/bao1x-hal/src/board/dabao.rs @@ -1,6 +1,6 @@ // Constants that define pin locations, RAM offsets, etc. for the DaBao basic breakout board -use crate::iox::IoSetup; -use crate::iox::*; + +use bao1x_api::*; // console uart buffer pub const UART_DMA_TX_BUF_PHYS: usize = utralib::HW_IFRAM0_MEM + utralib::HW_IFRAM0_MEM_LEN - 4096; @@ -42,7 +42,7 @@ pub fn setup_usb_pins(iox: &T) -> (IoxPort, u8) { (IoxPort::PB, SE0_PIN) } -pub fn setup_i2c_pins(iox: &dyn IoSetup) -> crate::udma::I2cChannel { +pub fn setup_i2c_pins(iox: &dyn IoSetup) -> bao1x_api::I2cChannel { // I2C_SCL_B[0] iox.setup_pin( IoxPort::PB, @@ -65,5 +65,12 @@ pub fn setup_i2c_pins(iox: &dyn IoSetup) -> crate::udma::I2cChannel { Some(IoxEnable::Enable), Some(IoxDriveStrength::Drive2mA), ); - crate::udma::I2cChannel::Channel0 + bao1x_api::I2cChannel::Channel0 } + +// these are a bodge to allow some UDMA imports to work - they need to be present even if +// there currently no SPINOR devices attached. +pub const SPINOR_PAGE_LEN: u32 = 0x100; +pub const SPINOR_ERASE_SIZE: u32 = 0x1000; // this is the smallest sector size. +pub const SPINOR_BULK_ERASE_SIZE: u32 = 0x1_0000; // this is the bulk erase size. +pub const SPINOR_LEN: u32 = 16384 * 1024; diff --git a/libs/bao1x-hal/src/board/mod.rs b/libs/bao1x-hal/src/board/mod.rs index aae709edc..cdb1ff099 100644 --- a/libs/bao1x-hal/src/board/mod.rs +++ b/libs/bao1x-hal/src/board/mod.rs @@ -16,9 +16,9 @@ pub use baosec::*; pub mod baosor; #[cfg(any(feature = "board-baosor", feature = "loader-baosor"))] pub use baosor::*; -#[cfg(any(feature = "board-dabao", feature = "loader-dabao"))] +#[cfg(any(feature = "board-dabao", feature = "loader-dabao", feature = "kernel-dabao"))] pub mod dabao; -#[cfg(any(feature = "board-dabao", feature = "loader-dabao"))] +#[cfg(any(feature = "board-dabao", feature = "loader-dabao", feature = "kernel-dabao"))] pub use dabao::*; #[cfg(feature = "hosted-baosec")] pub mod hosted; diff --git a/libs/bao1x-hal/src/debug.rs b/libs/bao1x-hal/src/debug.rs index 8332b8bbc..6ad106d8b 100644 --- a/libs/bao1x-hal/src/debug.rs +++ b/libs/bao1x-hal/src/debug.rs @@ -66,13 +66,13 @@ impl Uart { use core::fmt::{Error, Write}; impl Write for Uart { - fn write_str(&mut self, s: &str) -> Result<(), Error> { + fn write_str(&mut self, _s: &str) -> Result<(), Error> { #[cfg(not(all(feature = "std", not(feature = "debug-print-usb"))))] - for c in s.bytes() { + for c in _s.bytes() { self.putc(c); } // #[cfg(all(feature = "std", not(feature = "debug-print-usb")))] - // log::info!("{}", s); + // log::info!("{}", _s); Ok(()) } } diff --git a/loader/Cargo.toml b/loader/Cargo.toml index 522832874..a442dcac9 100644 --- a/loader/Cargo.toml +++ b/loader/Cargo.toml @@ -106,7 +106,7 @@ board-baosec = [ "ux-api/loader-baosec", ] board-baosor = ["bao1x-hal/loader-baosor"] -board-dabao = ["bao1x-hal/loader-dabao"] +board-dabao = ["bao1x-hal/loader-dabao", "bao1x-hal/board-dabao", "linked_list_allocator",] atsama5d27 = ["utralib/atsama5d27", "armv7", "dep:atsama5d27"] # precursor flags diff --git a/loader/src/platform/bao1x/bao1x.rs b/loader/src/platform/bao1x/bao1x.rs index a4aa4386a..7e53c6d37 100644 --- a/loader/src/platform/bao1x/bao1x.rs +++ b/loader/src/platform/bao1x/bao1x.rs @@ -10,6 +10,7 @@ use bao1x_hal::udma; #[cfg(any(feature = "board-baosec", feature = "board-baosor"))] use bao1x_hal::{axp2101::Axp2101, udma::GlobalConfig}; use utralib::generated::*; +#[cfg(not(feature = "verilator-only"))] use utralib::utra::sysctrl; #[cfg(feature = "qr")] @@ -2199,6 +2200,7 @@ pub fn coreuser_config() { /// Takes in the top clock in MHz, desired perclk in MHz, and returns a tuple of /// (min cycle, fd, actual freq) /// *tested* +#[allow(dead_code)] pub fn clk_to_per(top_in_mhz: u32, perclk_in_mhz: u32) -> Option<(u8, u8, u32)> { let fd_platonic = ((256 * perclk_in_mhz) / (top_in_mhz / 2)).min(256); if fd_platonic > 0 { @@ -2218,6 +2220,7 @@ pub fn clk_to_per(top_in_mhz: u32, perclk_in_mhz: u32) -> Option<(u8, u8, u32)> } /// Delay with a given system clock frequency. Useful during power mode switching. +#[allow(dead_code)] pub fn delay_at_sysfreq(ms: usize, sysclk_freq: u32) { let mut timer = utralib::CSR::new(utra::timer0::HW_TIMER0_BASE as *mut u32); timer.wfo(utra::timer0::EN_EN, 0b0); // disable the timer diff --git a/services/xous-log/Cargo.toml b/services/xous-log/Cargo.toml index 663628dac..ba747df7c 100644 --- a/services/xous-log/Cargo.toml +++ b/services/xous-log/Cargo.toml @@ -44,6 +44,7 @@ board-baosec = ["bao1x-hal/board-baosec"] board-baosor = ["bao1x-hal/board-baosor"] board-dabao = ["bao1x-hal/board-dabao"] hosted-baosec = [] +hwsim = [] precursor = ["utralib/precursor"] hosted = ["utralib/hosted"] diff --git a/services/xous-log/src/platform/bao1x/debug.rs b/services/xous-log/src/platform/bao1x/debug.rs index 038dee00b..b9803b411 100644 --- a/services/xous-log/src/platform/bao1x/debug.rs +++ b/services/xous-log/src/platform/bao1x/debug.rs @@ -1,6 +1,6 @@ use core::fmt::{Error, Write}; -#[cfg(feature = "bao1x")] +#[cfg(all(feature = "bao1x", not(feature = "hwsim")))] use bao1x_hal::udma; #[macro_export] @@ -30,7 +30,7 @@ pub struct Uart {} // this is a hack to bypass an explicit initialization/allocation step for the debug structure pub static mut DEFAULT_UART_ADDR: *mut usize = 0x0000_0000 as *mut usize; -#[cfg(feature = "bao1x")] +#[cfg(all(feature = "bao1x", not(feature = "hwsim")))] impl Uart { pub fn putc(&self, c: u8) { // check that we've been initialized before attempting to send any characters... @@ -56,6 +56,18 @@ impl Uart { uart.write(&[c]); } } +#[cfg(all(feature = "bao1x", feature = "hwsim"))] +impl Uart { + pub fn putc(&self, c: u8) { + // check that we've been initialized before attempting to send any characters... + if unsafe { DEFAULT_UART_ADDR } as usize == 0 { + return; + } + let mut uart_csr = unsafe { utralib::CSR::new(DEFAULT_UART_ADDR) }; + while uart_csr.r(utralib::utra::duart::SFR_SR) != 0 {} + uart_csr.wo(utralib::utra::duart::SFR_TXD, c as usize); + } +} impl Write for Uart { fn write_str(&mut self, s: &str) -> Result<(), Error> { diff --git a/services/xous-log/src/platform/bao1x/implementation.rs b/services/xous-log/src/platform/bao1x/implementation.rs index fba90152d..04af4bb48 100644 --- a/services/xous-log/src/platform/bao1x/implementation.rs +++ b/services/xous-log/src/platform/bao1x/implementation.rs @@ -1,28 +1,28 @@ use core::fmt::{Error, Write}; -#[cfg(feature = "bao1x")] +#[cfg(all(feature = "bao1x", not(feature = "hwsim")))] use std::pin::Pin; -#[cfg(feature = "bao1x")] +#[cfg(all(feature = "bao1x", not(feature = "hwsim")))] use bao1x_hal::board::UART_DMA_TX_BUF_PHYS; -#[cfg(feature = "bao1x")] +#[cfg(all(feature = "bao1x", not(feature = "hwsim")))] use bao1x_hal::udma; use utralib::generated::*; pub struct Output {} -#[cfg(feature = "bao1x")] +#[cfg(all(feature = "bao1x", not(feature = "hwsim")))] pub static mut UART_DMA_TX_BUF_VIRT: *mut u8 = 0x0000_0000 as *mut u8; -#[cfg(feature = "bao1x")] +#[cfg(all(feature = "bao1x", not(feature = "hwsim")))] pub static mut UART_IRQ: Option>> = None; -#[cfg(feature = "bao1x")] +#[cfg(all(feature = "bao1x", not(feature = "hwsim")))] pub static mut KBD_CONN: u32 = 0; pub fn init() -> Output { // TODO: migrate this to a "proper" UART that is available on SoC hardware, but for now all we have access // to is the DUART. - #[cfg(feature = "bao1x")] + #[cfg(all(feature = "bao1x", not(feature = "hwsim")))] let uart = xous::syscall::map_memory( xous::MemoryAddress::new(utra::udma_uart_2::HW_UDMA_UART_2_BASE), None, @@ -30,8 +30,11 @@ pub fn init() -> Output { xous::MemoryFlags::R | xous::MemoryFlags::W, ) .expect("couldn't map serial port"); - unsafe { crate::platform::debug::DEFAULT_UART_ADDR = uart.as_mut_ptr() as _ }; - #[cfg(feature = "bao1x")] + #[cfg(all(feature = "bao1x", not(feature = "hwsim")))] + unsafe { + crate::platform::debug::DEFAULT_UART_ADDR = uart.as_mut_ptr() as _ + }; + #[cfg(all(feature = "bao1x", not(feature = "hwsim")))] { // Note: for the TX buf, we allocate a pre-reserved portion of IFRAM as our // DMA buffer. We do *not* use the IFRAM allocator in `bao1x-hal-service` because @@ -65,13 +68,25 @@ pub fn init() -> Output { }; udma_uart.setup_async_read(); } + #[cfg(all(feature = "bao1x", feature = "hwsim"))] + let uart = xous::syscall::map_memory( + xous::MemoryAddress::new(utra::duart::HW_DUART_BASE), + None, + 4096, + xous::MemoryFlags::R | xous::MemoryFlags::W, + ) + .expect("couldn't map UDMA buffer"); + #[cfg(all(feature = "bao1x", feature = "hwsim"))] + unsafe { + crate::platform::debug::DEFAULT_UART_ADDR = uart.as_mut_ptr() as _ + }; println!("Mapped UART @ {:08x}", uart.as_ptr() as usize); println!("Process: map success!"); Output {} } -#[cfg(feature = "bao1x")] +#[cfg(all(feature = "bao1x", not(feature = "hwsim")))] fn uart_handler(_irq_no: usize, _arg: *mut usize) { let mut uart = unsafe { udma::Uart::get_handle( @@ -117,7 +132,7 @@ pub struct OutputWriter {} #[allow(dead_code)] impl OutputWriter { pub fn putc(&self, c: u8) { - #[cfg(feature = "bao1x")] + #[cfg(all(feature = "bao1x", not(feature = "hwsim")))] { // safety: safe to call as long as the raw parts are initialized and we exclusively // own it; and the UART has been initialized. For this peripheral, initialization @@ -134,13 +149,19 @@ impl OutputWriter { // enqueue our character to send via DMA uart.write(&[c]); } + #[cfg(all(feature = "bao1x", feature = "hwsim"))] + { + let mut uart_csr = unsafe { CSR::new(crate::platform::debug::DEFAULT_UART_ADDR) }; + while uart_csr.r(utra::duart::SFR_SR) != 0 {} + uart_csr.wo(utra::duart::SFR_TXD, c as usize); + } } /// Write a buffer to the output and return the number of /// bytes written. This is mostly compatible with `std::io::Write`, /// except it is infallible. pub fn write(&mut self, buf: &[u8]) -> usize { - #[cfg(feature = "bao1x")] + #[cfg(all(feature = "bao1x", not(feature = "hwsim")))] { // safety: safe to call as long as the raw parts are initialized and we exclusively // own it; and the UART has been initialized. For this peripheral, initialization @@ -155,6 +176,15 @@ impl OutputWriter { }; uart.write(buf) } + #[cfg(all(feature = "bao1x", feature = "hwsim"))] + { + let mut uart_csr = unsafe { CSR::new(crate::platform::debug::DEFAULT_UART_ADDR) }; + for &c in buf.iter() { + while uart_csr.r(utra::duart::SFR_SR) != 0 {} + uart_csr.wo(utra::duart::SFR_TXD, c as usize); + } + buf.len() + } } pub fn write_all(&mut self, buf: &[u8]) -> core::result::Result { Ok(self.write(buf)) } diff --git a/utralib/bao1x/.gitignore b/utralib/bao1x/.gitignore new file mode 100644 index 000000000..e13bc9fa2 --- /dev/null +++ b/utralib/bao1x/.gitignore @@ -0,0 +1,2 @@ +# this is only present during simulations +soc.svd \ No newline at end of file diff --git a/utralib/build.rs b/utralib/build.rs index 43405a621..b05bd810c 100644 --- a/utralib/build.rs +++ b/utralib/build.rs @@ -123,7 +123,11 @@ fn main() { let generated_filename = "src/generated/atsama5d27.rs"; #[cfg(feature = "bao1x")] - let svd_filenames = vec!["bao1x/core.svd", "bao1x/bao1x_peri.svd"]; + let mut svd_filenames = vec!["bao1x/core.svd", "bao1x/bao1x_peri.svd"]; + #[cfg(feature = "bao1x")] + if std::path::Path::new("bao1x/soc.svd").exists() { + svd_filenames.push("bao1x/soc.svd"); + } #[cfg(feature = "bao1x")] let generated_filename = "src/generated/bao1x.rs"; diff --git a/xtask/src/builder.rs b/xtask/src/builder.rs index 576a98d97..418b65969 100644 --- a/xtask/src/builder.rs +++ b/xtask/src/builder.rs @@ -224,7 +224,7 @@ impl Builder { self } - /// Disable default features on the loader + /// Disable default features on the kernel #[allow(dead_code)] pub fn kernel_disable_defaults(&mut self) -> &mut Builder { self.kernel_disable_defaults = true; diff --git a/xtask/src/main.rs b/xtask/src/main.rs index ba85f54df..a7708dc25 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -499,7 +499,15 @@ fn main() -> Result<(), Box> { builder.add_loader_feature(board); builder.add_kernel_feature(board); } - _ => (), // don't add any board because this is simulation + Some("bao1x-sim") => { + let board = "board-dabao"; + // select the board + builder.add_feature(board); + builder.add_loader_feature(board); + builder.add_kernel_feature(board); + builder.kernel_disable_defaults(); // need to turn of kernel DUART exclusive access so console can work! + } + _ => panic!("unhandled configuration"), }; // placement in flash is a tension between dev convenience and RAM usage. Things in flash