From 818717e2366b30a92bfa52a4b9a2f123a3046a41 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 14 Sep 2025 02:15:21 +0800 Subject: [PATCH] add avalanche generator characterization routine to baremetal --- baremetal/Cargo.toml | 7 +- baremetal/src/platform/cramium.rs | 2 + baremetal/src/platform/cramium/avtrng.rs | 35 +++++++++ .../src/platform/cramium/usb/handlers.rs | 9 +++ baremetal/src/repl.rs | 74 ++++++++++++++++++- 5 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 baremetal/src/platform/cramium/avtrng.rs diff --git a/baremetal/Cargo.toml b/baremetal/Cargo.toml index 50c6ebe6f..cbe65e317 100644 --- a/baremetal/Cargo.toml +++ b/baremetal/Cargo.toml @@ -65,7 +65,12 @@ nto-usb = [ # "cramium-hal/verbose-debug", ] nto-buttons = [] -nto-trng = ["nto-usb", "base64"] +nto-trng = [ + "nto-usb", + "base64", + "xous-bio-bdma/baremetal", + "xous-bio-bdma/cramium-soc", +] verbose-debug = [] verilator-only = [] diff --git a/baremetal/src/platform/cramium.rs b/baremetal/src/platform/cramium.rs index ceb01ced4..779048156 100644 --- a/baremetal/src/platform/cramium.rs +++ b/baremetal/src/platform/cramium.rs @@ -1,5 +1,7 @@ pub mod cramium; pub use cramium::*; +#[cfg(feature = "nto-trng")] +pub mod avtrng; #[cfg(feature = "nto-bio")] pub mod bio; pub mod debug; diff --git a/baremetal/src/platform/cramium/avtrng.rs b/baremetal/src/platform/cramium/avtrng.rs new file mode 100644 index 000000000..5183a1103 --- /dev/null +++ b/baremetal/src/platform/cramium/avtrng.rs @@ -0,0 +1,35 @@ +use utralib::*; +use xous_bio_bdma::*; + +pub fn setup(bio_ss: &mut BioSharedState, trng_pin: u8) { + // stop all the machines, so that code can be loaded + bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0x0); + bio_ss.load_code(avtrng_bio_code(), 0, BioCore::Core0); + + // don't use QDIV + bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV0, 0x1_0000); + + // use extclock on channel 0, tied to the trng pin + bio_ss.bio.wo( + utra::bio_bdma::SFR_EXTCLOCK, + bio_ss.bio.ms(utra::bio_bdma::SFR_EXTCLOCK_USE_EXTCLK, 0b0001) + | bio_ss.bio.ms(utra::bio_bdma::SFR_EXTCLOCK_EXTCLK_GPIO_0, trng_pin as u32), + ); + + // start the machine + bio_ss.set_core_run_states([true, false, false, true]); + bio_ss.bio.wo(utra::bio_bdma::SFR_TXF0, trng_pin as u32); // start the sampling +} + +#[rustfmt::skip] +bio_code!(avtrng_bio_code, BM_AVTRNG_BIO_START, BM_AVTRNG_BIO_END, + "mv x1, x16", // get pin for trng input + "li x2, 1", + "sll x1, x2, x1", // shift the pin into a bitmask + "mv x25, x1", // make it an input +"10:", + "mv x20, x0", // wait for quantum: this time, the toggle from the TRNG + "mv x1, x31", // remember aclk time + "mv x16, x1", // save result to FIFO + "j 10b" // and do it again +); diff --git a/baremetal/src/platform/cramium/usb/handlers.rs b/baremetal/src/platform/cramium/usb/handlers.rs index 01b4eb2b0..2dfd79254 100644 --- a/baremetal/src/platform/cramium/usb/handlers.rs +++ b/baremetal/src/platform/cramium/usb/handlers.rs @@ -334,6 +334,15 @@ pub fn usb_ep3_bulk_out_complete( this.bulk_xfer(3, USB_RECV, acm_buf.as_ptr() as usize, acm_buf.len(), 0, 0); } +pub fn flush() { + unsafe { + if let Some(ref mut usb_ref) = crate::platform::usb::USB { + let usb = &mut *core::ptr::addr_of_mut!(*usb_ref); + flush_tx(usb); + } + } +} + pub fn flush_tx(this: &mut CorigineUsb) { let mut written = 0; diff --git a/baremetal/src/repl.rs b/baremetal/src/repl.rs index 09c30818c..69e08322b 100644 --- a/baremetal/src/repl.rs +++ b/baremetal/src/repl.rs @@ -852,6 +852,78 @@ impl Repl { } } } + #[cfg(feature = "nto-trng")] + "trngav" => { + use base64::{Engine as _, engine::general_purpose}; + fn encode_base64(input: &[u8]) -> String { general_purpose::STANDARD.encode(input) } + fn as_u8_slice(slice: &[u32]) -> &[u8] { + let len = slice.len() * size_of::(); + unsafe { core::slice::from_raw_parts(slice.as_ptr() as *const u8, len) } + } + + use cramium_hal::iox::Iox; + use xous_bio_bdma::*; + + let iox = Iox::new(utra::iox::HW_IOX_BASE as *mut u32); + let (power_port, power_pin) = cramium_hal::board::setup_trng_power_pin(&iox); + let bio_input = cramium_hal::board::setup_trng_input_pin(&iox); + // turn on the system + iox.set_gpio_pin_value(power_port, power_pin, IoxValue::High); + + let mut bio_ss = BioSharedState::new(); + bio_ss.init(); + crate::println!("bio_input: {}", bio_input); + crate::avtrng::setup(&mut bio_ss, bio_input); + + crate::println!("====AVSTART===="); + const BUFLEN: usize = 256; + + crate::usb::flush(); + + const BITS_PER_SAMPLE: usize = 4; + loop { + let mut buf = [0u32; BUFLEN / size_of::()]; + for d in buf.iter_mut() { + let mut raw32 = 0; + for _ in 0..(size_of::() * 8) / BITS_PER_SAMPLE { + // wait for the next interval to arrive + while bio_ss.bio.rf(utra::bio_bdma::SFR_FLEVEL_PCLK_REGFIFO_LEVEL0) == 0 {} + let raw = bio_ss.bio.r(utra::bio_bdma::SFR_RXF0); + // crate::println!("raw: {:x}", raw); + raw32 <<= BITS_PER_SAMPLE; + raw32 |= (raw >> 1) & ((1 << BITS_PER_SAMPLE) - 1) + } + *d = raw32; + } + let buf_u8 = as_u8_slice(&buf); + let b64 = encode_base64(buf_u8); + unsafe { + if let Some(ref mut usb_ref) = crate::platform::usb::USB { + let usb = &mut *core::ptr::addr_of_mut!(*usb_ref); + let tx_buf = usb.cdc_acm_tx_slice(); + let b64_as_slice = b64.as_bytes(); + assert!(b64_as_slice.len() + 1 < tx_buf.len()); // this is ensured by adjusting BUFLEN + tx_buf[..b64_as_slice.len()].copy_from_slice(b64_as_slice); + tx_buf[b64_as_slice.len()] = '\n' as char as u32 as u8; + while !crate::platform::usb::TX_IDLE + .swap(false, core::sync::atomic::Ordering::SeqCst) + { + // wait for tx to go idle + } + usb.bulk_xfer( + 3, + cramium_hal::usb::driver::USB_SEND, + tx_buf.as_ptr() as usize, + b64_as_slice.len() + 1, + 0, + 0, + ); + } else { + panic!("USB core not allocated, can't proceed!"); + } + } + } + } "echo" => { for word in args { crate::print!("{} ", word); @@ -872,7 +944,7 @@ impl Repl { #[cfg(feature = "nto-usb")] crate::print!(", usb"); #[cfg(feature = "nto-trng")] - crate::print!(", trngro"); + crate::print!(", trngro, trngav"); crate::println!(""); } }