From 68679f41ba72786df8dc68933944bdb9c16eebf2 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 26 Dec 2025 00:58:49 +0800 Subject: [PATCH] add the 'std' implementation side of the abstract BIO API this is essentially the getter/setter that translates BIO api calls into messages that would be sent to the BIO server implementation. The BIO server implementation would have the actual BIO hardware calls in it. These calls, if written correctly, also double as the no-std implemnetation of the BIO API. --- libs/bao1x-hal/src/bio.rs | 158 ++++++++++++++++++++++++++++++++++++++ libs/bao1x-hal/src/lib.rs | 2 + 2 files changed, 160 insertions(+) create mode 100644 libs/bao1x-hal/src/bio.rs diff --git a/libs/bao1x-hal/src/bio.rs b/libs/bao1x-hal/src/bio.rs new file mode 100644 index 000000000..47057ca28 --- /dev/null +++ b/libs/bao1x-hal/src/bio.rs @@ -0,0 +1,158 @@ +//! BIO API +//! +//! For the first draft of this API, we're going to take an approach where all machines +//! and pins are explicitly managed. That is: we don't try and be clever and try to imply any +//! allocations. The developer has to correctly identify which engine to place their code on, +//! and which I/Os, if any, the thing uses. + +use std::marker::PhantomData; + +use bao1x_api::bio::*; +use num_traits::*; +use utralib::*; +use xous::{CID, Message, send_message}; +use xous_ipc::Buffer; + +pub struct Bio { + conn: CID, +} + +impl Bio { + pub fn new() -> Self { + let xns = xous_api_names::XousNames::new().unwrap(); + let conn = xns.request_connection_blocking(BIO_SERVER_NAME).expect("couldn't connect to BIO server"); + Self { conn } + } +} + +pub struct CoreCsr<'a> { + pub csr: CSR, + _lifetime: PhantomData<&'a ()>, +} +impl<'a> CoreCsr<'a> { + pub fn from_handle(handle: CoreHandle<'a>) -> Self { + let (pointer, lifetime) = unsafe { handle.handle() }; + // safety: this structure tracks the lifetime of the handle, and is therefore safe. + Self { csr: CSR::new(pointer as *mut u32), _lifetime: lifetime } + } +} + +impl<'a> BioApi<'a> for Bio { + fn init_core( + &self, + core: BioCore, + code: &[u8], + offset: usize, + config: CoreConfig, + ) -> Result<(), BioError> { + let mut config = CoreInitRkyv { core, offset, config: config.into(), code: [0u8; 4096] }; + config.code[..code.len()].copy_from_slice(code); + // this should automatically allocate 2 pages because the sizeof() type of CoreInitRkyv is over 4096 + let buf = Buffer::into_buf(config).unwrap(); + buf.lend(self.conn, BioOp::InitCore.to_u32().unwrap()) + .map_err(|e| >::into(e))?; + Ok(()) + } + + fn de_init_core(&self, core: BioCore) -> Result<(), BioError> { + send_message( + self.conn, + Message::new_blocking_scalar(BioOp::DeInitCore.to_usize().unwrap(), core as usize, 0, 0, 0), + ) + .map_err(|e| >::into(e))?; + Ok(()) + } + + unsafe fn get_core_handle(&'a self) -> Result, BioError> { + match send_message( + self.conn, + Message::new_blocking_scalar(BioOp::GetCoreHandle.to_usize().unwrap(), 0, 0, 0, 0), + ) + .map_err(|e| >::into(e))? + { + xous::Result::Scalar5(_, handle, _, _, _) => Ok(CoreHandle::new(self.conn, handle)), + _ => unimplemented!("Unhandled return type on get_core_handle()"), + } + } + + fn get_freq(&self) -> u32 { + match send_message( + self.conn, + Message::new_blocking_scalar(BioOp::GetCoreFreq.to_usize().unwrap(), 0, 0, 0, 0), + ) + .map_err(|e| >::into(e)) + .unwrap() + { + xous::Result::Scalar5(_, freq, _, _, _) => freq as u32, + _ => unimplemented!("Unhandled return type"), + } + } + + fn get_version(&self) -> u32 { + match send_message( + self.conn, + Message::new_blocking_scalar(BioOp::GetVersion.to_usize().unwrap(), 0, 0, 0, 0), + ) + .map_err(|e| >::into(e)) + .unwrap() + { + xous::Result::Scalar5(_, version, _, _, _) => version as u32, + _ => unimplemented!("Unhandled return type"), + } + } + + fn set_core_freq(&self, freq: u32) -> Result { + match send_message( + self.conn, + Message::new_blocking_scalar(BioOp::SetCoreFreq.to_usize().unwrap(), freq as usize, 0, 0, 0), + ) + .map_err(|e| >::into(e))? + { + xous::Result::Scalar5(_, previous_freq, _, _, _) => Ok(previous_freq as u32), + _ => unimplemented!("Unhandled return type"), + } + } + + fn set_core_state(&self, which: [CoreRunSetting; 4]) -> Result<(), BioError> { + send_message( + self.conn, + Message::new_blocking_scalar( + BioOp::CoreState.to_usize().unwrap(), + which[0] as usize, + which[1] as usize, + which[2] as usize, + which[3] as usize, + ), + ) + .map_err(|e| >::into(e))?; + Ok(()) + } + + fn setup_dma_windows(&self, windows: DmaFilterWindows) -> Result<(), BioError> { + let buf = Buffer::into_buf(windows).unwrap(); + buf.lend(self.conn, BioOp::DmaWindows.to_u32().unwrap()) + .map_err(|e| >::into(e))?; + Ok(()) + } + + fn setup_fifo_event_triggers(&self, config: FifoEventConfig) -> Result<(), BioError> { + let buf = Buffer::into_buf(config).unwrap(); + buf.lend(self.conn, BioOp::FifoEventTriggers.to_u32().unwrap()) + .map_err(|e| >::into(e))?; + Ok(()) + } + + fn setup_io_config(&self, config: IoConfig) -> Result<(), BioError> { + let buf = Buffer::into_buf(config).unwrap(); + buf.lend(self.conn, BioOp::IoConfig.to_u32().unwrap()) + .map_err(|e| >::into(e))?; + Ok(()) + } + + fn setup_irq_config(&self, config: IrqConfig) -> Result<(), BioError> { + let buf = Buffer::into_buf(config).unwrap(); + buf.lend(self.conn, BioOp::IrqConfig.to_u32().unwrap()) + .map_err(|e| >::into(e))?; + Ok(()) + } +} diff --git a/libs/bao1x-hal/src/lib.rs b/libs/bao1x-hal/src/lib.rs index bb66f02c9..e583100e3 100644 --- a/libs/bao1x-hal/src/lib.rs +++ b/libs/bao1x-hal/src/lib.rs @@ -30,6 +30,8 @@ pub mod usb; pub use shared_csr::*; #[cfg(not(feature = "hosted-baosec"))] pub mod acram; +#[cfg(feature = "std")] +pub mod bio; #[cfg(not(feature = "hosted-baosec"))] pub mod buram; #[cfg(not(feature = "hosted-baosec"))]