add crate for BIO - better I/O - driver

This commit is contained in:
bunnie 2024-03-01 00:39:54 +08:00
parent 8622fbdf7a
commit e297c02cf3
4 changed files with 228 additions and 0 deletions

26
libs/xous-bio/Cargo.toml Normal file
View file

@ -0,0 +1,26 @@
[package]
name = "xous-bio"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
log = "0.4.14"
defmt = { version = "0.3", optional = true }
utralib = { version = "0.1.24", default-features = false, features = ["cramium-soc"] }
[target.'cfg(target_os = "xous")'.dependencies]
xous = "0.9.58"
[features]
cramium-soc = ["utralib/cramium-soc"]
cramium-fpga = ["utralib/cramium-fpga"]
precursor = []
hosted = []
renode = []
# may be activated in conjunction with "cramium-soc", "rp2040" targets when not running Xous
baremetal = []
tests = []
default = ["tests", "cramium-soc"]

View file

@ -0,0 +1,82 @@
pub mod units;
#[cfg(not(any(target_os = "xous")))]
mod duart {
pub const UART_DOUT: utralib::Register = utralib::Register::new(0, 0xff);
pub const UART_DOUT_DOUT: utralib::Field = utralib::Field::new(8, 0, UART_DOUT);
pub const UART_CTL: utralib::Register = utralib::Register::new(1, 1);
pub const UART_CTL_EN: utralib::Field = utralib::Field::new(1, 0, UART_CTL);
pub const UART_BUSY: utralib::Register = utralib::Register::new(2, 1);
pub const UART_BUSY_BUSY: utralib::Field = utralib::Field::new(1, 0, UART_BUSY);
pub const HW_DUART_BASE: usize = 0x4004_2000;
}
#[cfg(not(any(target_os = "xous")))]
use utralib::CSR;
#[cfg(not(any(target_os = "xous")))]
pub struct Uart {}
#[cfg(not(any(target_os = "xous")))]
impl Uart {
fn put_digit(&mut self, d: u8) {
let nyb = d & 0xF;
let c = if nyb < 10 { nyb + 0x30 } else { nyb + 0x61 - 10 };
assert!(c >= 0x30, "conversion failed!");
self.putc(c);
}
pub fn put_hex(&mut self, c: u8) {
self.put_digit(c >> 4);
self.put_digit(c & 0xF);
}
pub fn newline(&mut self) { self.putc(0xd); }
pub fn print_hex_word(&mut self, word: u32) {
for &byte in word.to_be_bytes().iter() {
self.put_hex(byte);
}
}
pub fn putc(&self, c: u8) {
let base = duart::HW_DUART_BASE as *mut u32;
let mut uart = CSR::new(base);
if uart.rf(duart::UART_CTL_EN) == 0 {
uart.wfo(duart::UART_CTL_EN, 1);
}
while uart.rf(duart::UART_BUSY_BUSY) != 0 {
// spin wait
}
uart.wfo(duart::UART_DOUT_DOUT, c as u32);
#[cfg(feature = "arty")]
self.putc_litex(c);
}
pub fn tiny_write_str(&mut self, s: &str) {
for c in s.bytes() {
self.putc(c);
}
}
}
#[cfg(not(any(target_os = "xous")))]
pub fn setup_reporting(_rep_adr: *mut u32) {}
pub fn report_api(d: u32) {
#[cfg(not(any(target_os = "xous")))]
{
let mut uart = Uart {};
uart.print_hex_word(d);
uart.newline();
}
#[cfg(target_os = "xous")]
log::info!("report: 0x{:x}", d);
}
pub fn bio_tests() {
report_api(crate::get_id());
units::hello_world();
}

View file

@ -0,0 +1,58 @@
use super::report_api;
use crate::*;
pub fn hello_world() {
report_api(0x0510_0000);
// map the instruction memory
let imem_slice = unsafe {
core::slice::from_raw_parts_mut(
utralib::generated::HW_BIO_RAM_MEM as *mut u32,
utralib::generated::HW_BIO_RAM_MEM_LEN
)
};
let simple_test_ptr = simple_test as *const u8;
let simple_test_slice = unsafe {
core::slice::from_raw_parts(simple_test_ptr,
(simple_test_end as *const u8) as usize - simple_test_ptr as usize
)
};
// copy code to reset vector for 0th machine
for (i, chunk) in simple_test_slice.chunks(4).enumerate() {
if chunk.len() == 4 {
let word: u32 = u32::from_le_bytes(chunk.try_into().unwrap());
imem_slice[i] = word;
report_api(i as u32);
report_api(word);
} else {
let mut ragged_word = 0;
for (j, &b) in chunk.iter().enumerate() {
ragged_word |= (b as u32) << (4 - chunk.len() + j);
}
imem_slice[i] = ragged_word;
}
}
// configure & run the 0th machine
let mut bio = CSR::new(utra::bio::HW_BIO_BASE as *mut u32);
// /16 clock
bio.wo(utra::bio::SFR_QDIV0, 0x10_0000);
// start the machine
bio.wo(utra::bio::SFR_CTRL, 0x111);
report_api(0x0510_0001);
}
pub unsafe fn simple_test() {
core::arch::asm!(
"0:",
"nop",
"j 0b",
"nop"
);
}
// this marks the "end address" of simple_test
pub unsafe fn simple_test_end() {
core::arch::asm!(
"nop"
);
}

62
libs/xous-bio/src/lib.rs Normal file
View file

@ -0,0 +1,62 @@
#![cfg_attr(feature = "baremetal", no_std)]
use utralib::generated::*;
#[cfg(feature = "tests")]
pub mod bio_tests;
#[derive(Debug)]
pub enum BioError {
/// specified state machine is not valid
InvalidSm,
/// program can't fit in memory, for one reason or another
Oom,
/// no more machines available
NoFreeMachines,
}
pub fn get_id() -> u32 {
let bio_ss = BioSharedState::new();
#[cfg(feature = "tests")]
bio_tests::report_api(utra::bio::SFR_CFGINFO.offset() as u32);
#[cfg(feature = "tests")]
bio_tests::report_api(utra::bio::HW_BIO_BASE as u32);
bio_ss.bio.r(utra::bio::SFR_CFGINFO)
}
/// used to generate some test vectors
pub fn lfsr_next(state: u16) -> u16 {
let bit = ((state >> 8) ^ (state >> 4)) & 1;
((state << 1) + bit) & 0x1_FF
}
pub struct BioSharedState {
pub bio: CSR<u32>,
}
impl BioSharedState {
#[cfg(feature = "baremetal")]
pub fn new() -> Self {
BioSharedState {
bio: CSR::new(utra::bio::HW_BIO_BASE as *mut u32),
}
}
#[cfg(not(feature = "baremetal"))]
pub fn new() -> Self {
// Note: this requires a memory region window to be manually specified in create-image
// so that the loader maps the pages for the PIO block. This is because the PIO block is
// an IP block that is created *outside* of the normal LiteX ecosystem. Specifically look in
// xtask/src/builder.rs for a "--extra-svd" argument that refers to precursors/pio.svd.
let csr = xous::syscall::map_memory(
xous::MemoryAddress::new(utra::bio::HW_BIO_BASE),
None,
4096,
xous::MemoryFlags::R | xous::MemoryFlags::W,
)
.unwrap();
BioSharedState {
bio: CSR::new(csr.as_mut_ptr() as *mut u32),
}
}
}