copy xous-bio crate to xous-bio-dma

this is the starting point for the rework of the BIO with DMA mods
This commit is contained in:
bunnie 2024-06-25 03:32:49 +08:00
parent d0f94424db
commit bd9500110c
8 changed files with 2093 additions and 0 deletions

View file

@ -0,0 +1,28 @@
[package]
name = "xous-bio-bdma"
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.62"
[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,49 @@
use utralib::generated::*;
pub struct Uart {
// pub base: *mut u32,
}
impl Uart {
pub fn putc(&self, c: u8) {
let base = utra::duart::HW_DUART_BASE as *mut u32;
let mut uart = CSR::new(base);
while uart.r(utra::duart::SFR_SR) != 0 {}
uart.wo(utra::duart::SFR_TXD, c as u32);
}
}
use core::fmt::{Error, Write};
impl Write for Uart {
fn write_str(&mut self, s: &str) -> Result<(), Error> {
for c in s.bytes() {
self.putc(c);
}
Ok(())
}
}
#[macro_use]
pub mod debug_print_hardware {
#[macro_export]
macro_rules! print
{
($($args:tt)+) => ({
use core::fmt::Write;
let _ = write!(crate::bio_tests::debug::Uart {}, $($args)+);
});
}
}
#[macro_export]
macro_rules! println
{
() => ({
$crate::print!("\r\n")
});
($fmt:expr) => ({
$crate::print!(concat!($fmt, "\r\n"))
});
($fmt:expr, $($args:tt)+) => ({
$crate::print!(concat!($fmt, "\r\n"), $($args)+)
});
}

View file

@ -0,0 +1,127 @@
use crate::*;
const PIN_SDA: u32 = 2;
const PIN_SCL: u32 = 3;
pub fn i2c_test() {
print!("I2C tests\r");
// clear prior test config state
let mut test_cfg = CSR::new(utra::csrtest::HW_CSRTEST_BASE as *mut u32);
test_cfg.wo(utra::csrtest::WTEST, 0);
test_cfg.wo(utra::csrtest::WTEST, crate::bio_tests::TEST_I2C_MASK);
// setup the machine & test
let mut bio_ss = BioSharedState::new();
// stop all the machines, so that code can be loaded
bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0x0);
let code = crate::i2c::i2c_driver();
print!("code length {}\r", code.len());
bio_ss.load_code(code, 0);
// configure & run the 0th machine
// 400kHz clock -> 100kHz toggle rate = 0x7D0_0000 @ 800MHz rate FCLK
// 1600kHz clock -> 400kHz toggle rate = 0x1F4_0000 @ 800MHz rate FCLK
bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV0, 0x1F4_0000);
// clear all events
bio_ss.bio.wfo(utra::bio_bdma::SFR_EVENT_CLR_SFR_EVENT_CLR, 0xFFFF_FF);
// reset all the fifos
bio_ss.bio.wo(utra::bio_bdma::SFR_FIFO_CLR, 0xF);
// start core 0
bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0x111);
// don't snap GPIO outputs
bio_ss.bio.wo(utra::bio_bdma::SFR_CONFIG, 0);
// configure interrupts
// T/RX2 is the bank of interest for triggering interrupts
bio_ss.bio.wfo(utra::bio_bdma::SFR_ELEVEL_FIFO_EVENT_LEVEL4, 1); // corresponds to T/RXF2
bio_ss.bio.wfo(utra::bio_bdma::SFR_ETYPE_FIFO_EVENT_EQ_MASK, 0b00_01_00_00); // level4 mask EQ
bio_ss.bio.rmwf(utra::bio_bdma::SFR_ETYPE_FIFO_EVENT_GT_MASK, 0b00_01_00_00); // level4 mask GT
// IRQ 0 should issue the pulse
bio_ss.bio.wo(utra::bio_bdma::SFR_IRQMASK_0, 1 << 28);
// this is where the IRQ is wired to right now
let irqarray18 = CSR::new(utra::irqarray18::HW_IRQARRAY18_BASE as *mut u32);
let mut rxbuf = [0u8];
// expected pass or fails of reads, for automated regression testing
// these are wired directly into the LiteX test harness inside the PioAdapter block
let failing_address = 0x17 >> 1; // 0x17 is exactly what is inside the testbench code, shift right by one to disregard r/w bit
let mut passing = true;
for addr in 10..14 {
print!("reading from {}\r", addr);
// 1 is read, 0 is write
let wr_byte = (addr << 1 | 0x1) as u8;
let i2c_cmd =
PIN_SDA << 27 | PIN_SCL << 22 | 1 << 8 | ((rxbuf.len() & 0x7F) as u32) << 15 | wr_byte as u32;
bio_ss.bio.wo(utra::bio_bdma::SFR_TXF0, i2c_cmd);
let mut timeout = 0;
while irqarray18.r(utra::irqarray18::EV_STATUS) & 1 == 0 {
// wait
timeout += 1;
if (timeout % 1_000) == 0 {
print!(" timeout: {}\r", timeout);
}
}
// read the readback data
for d in rxbuf.iter_mut() {
*d = bio_ss.bio.r(utra::bio_bdma::SFR_RXF1) as u8;
}
// read the result code
let result_code = bio_ss.bio.r(utra::bio_bdma::SFR_RXF2);
if addr == failing_address {
print!("rbk {:x?}, result_code {:x} (intentional fail)\r", rxbuf, result_code);
if result_code != 0x2_0000 {
// expect two NACKs
passing = false;
}
} else {
print!("rbk {:x?}, result_code {:x}\r", rxbuf, result_code);
if result_code != 0x1_0001 {
assert!(rxbuf[0] == wr_byte);
// expect 1 ACK, 1 NACK (last read is always a NACK)
passing = false;
}
}
}
// turn off interrupts after the test, otherwise this interferes with later operations
bio_ss.bio.wo(utra::bio_bdma::SFR_IRQMASK_0, 0);
if passing {
print!("===I2C tests PASS===\r");
} else {
print!("===I2C tests FAIL===\r");
}
}
/// This just generates some more complex I2C waveforms; we don't have a full I2C hardware
/// unit in the upper level test bench, so we use manual waveform checking.
pub fn complex_i2c_test() {
print!("Complex I2C transactions\r");
let mut bio_ss = BioSharedState::new();
let mut i2c =
unsafe { crate::i2c::BioI2C::new_exclusive(&mut bio_ss, PIN_SDA as u8, PIN_SCL as u8, 0, true) }
.unwrap();
// simulate a write-only transaction to device at 0x62, reg addr 0x10, data 0xaa, 0x55
let test_addr = 0x62u8;
let test_tx = [0x10, 0xAA, 0x55];
print!("Write addr {:x}, data {:x?}", test_addr, test_tx);
match i2c.txrx_blocking(test_addr, &test_tx, None, false) {
Ok(_) => print!("Write test waveform generated (check in wave viewer)\r"),
Err(_) => print!("write only test FAIL\r"),
}
// simulate a read transaction to device at 0x34, reg addr 0xC0, with two data
let test_addr = 0x34u8;
let test_tx = [0xc0];
let mut test_rx = [0u8; 2];
print!("Read addr {:x}, tx data {:x?} rx data {:x?}", test_addr, test_tx, test_rx);
match i2c.txrx_blocking(test_addr, &test_tx, Some(&mut test_rx), false) {
Ok(_) => print!("Read test waveform generated (check in wave viewer)\r"),
Err(_) => print!("read test FAIL\r"),
}
print!("===Exit complex I2C transactions===\r");
}

View file

@ -0,0 +1,123 @@
pub(crate) mod debug;
pub mod i2c;
pub mod spi;
pub mod units;
// masks for utra::main::WDATA, which is abused to control GPIO register states for testing this module
// these signals are "pulled through" the hierarchy and only exist in the test configuration, thus
// they don't have a utralib entry
pub const TEST_I2C_MASK: u32 = 0b00001;
pub const TEST_FORCE_MASK: u32 = 0b00010;
pub const TEST_LOOP_OE_MASK: u32 = 0b00100;
pub const TEST_INVERT_MASK: u32 = 0b01000;
pub const TEST_FORCE_OFFSET: usize = 16; // top 16 bits
#[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();
units::hello_multiverse();
spi::spi_test();
units::aclk_tests();
i2c::i2c_test();
i2c::complex_i2c_test();
units::fifo_basic();
units::host_fifo_tests();
units::fifo_level_tests();
}
// Test plan:
// Unit tests:
// -[x] Basic FIFO stall test. Two cores writing to each other to unlock.
// -[x] Host FIFO stall on empty test. Core stalls until host provides data.
// -[x] Host FIFO stall on full test. Core stall until host reads data.
// -[x] GPIO input path test
// -[x] Extclk as x20 stall source
// -[x] Check Extclk:gpio pin mapping (make sure bit ordering is not swapped)
// -[x] GPIO direction control test
// -[x] FIFO level trigger test - eq, gt, lt on various channels, at various fullness levels
// -[x] Stall on event - register bit test, between cores
// -[x] Stall on event - register bit test, to host
// -[x] Stall on event - FIFO level test
// -[x] Host IRQ generation test - some combination with event tests above to confirm IRQ generation
// -[x] Core ID read test
// -[x] Core aclk counter test
// Application tests:
// -[x] SPI loopback test - implement using extclk as spi clk for input
// -[x] I2C loopback test
// -[ ] DMA transfer test -- might have to wait until full-chip integration to get MDMA core?

View file

@ -0,0 +1,452 @@
use utralib::utra::bio_bdma::{SFR_EVENT_STATUS_SFR_EVENT_STATUS, SFR_FLEVEL_PCLK_REGFIFO_LEVEL1};
use super::report_api;
use crate::*;
pub fn spi_test() {
print!("SPI test\r");
// clear prior test config state
let mut test_cfg = CSR::new(utra::csrtest::HW_CSRTEST_BASE as *mut u32);
test_cfg.wo(utra::csrtest::WTEST, 0);
let mut bio_ss = BioSharedState::new();
// stop all the machines, so that code can be loaded
bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0x0);
let code = spi_driver();
print!("SPI code length {}\r", code.len());
bio_ss.load_code(code, 0);
// configure fifo trigger levels
bio_ss.bio.wfo(utra::bio_bdma::SFR_ELEVEL_FIFO_EVENT_LEVEL0, 4);
bio_ss.bio.rmwf(utra::bio_bdma::SFR_ELEVEL_FIFO_EVENT_LEVEL2, 4);
// configure the polarities
bio_ss.bio.wfo(utra::bio_bdma::SFR_ETYPE_FIFO_EVENT_EQ_MASK, 0b00_00_01_01);
bio_ss.bio.rmwf(utra::bio_bdma::SFR_ETYPE_FIFO_EVENT_LT_MASK, 0b00_00_00_01);
bio_ss.bio.rmwf(utra::bio_bdma::SFR_ETYPE_FIFO_EVENT_GT_MASK, 0b00_00_01_00);
// reset all the fifos
bio_ss.bio.wo(utra::bio_bdma::SFR_FIFO_CLR, 0xF);
// tx rate set
bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV2, 0x10_0000);
// this actually shouldn't matter, but set it to be "fast" for testing
bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV3, 0x1_0000);
// snap GPIO outputs
bio_ss.bio.wo(
utra::bio_bdma::SFR_CONFIG,
bio_ss.bio.ms(utra::bio_bdma::SFR_CONFIG_SNAP_OUTPUT_TO_QUANTUM, 1)
| bio_ss.bio.ms(utra::bio_bdma::SFR_CONFIG_SNAP_OUTPUT_TO_WHICH, 2),
);
// bypass sync on all but clock
bio_ss.bio.wo(utra::bio_bdma::SFR_SYNC_BYPASS, 0x500);
// use extclock on channel 3, tied to bit 9
bio_ss.bio.wo(
utra::bio_bdma::SFR_EXTCLOCK,
bio_ss.bio.ms(utra::bio_bdma::SFR_EXTCLOCK_USE_EXTCLK, 0b1000)
| bio_ss.bio.ms(utra::bio_bdma::SFR_EXTCLOCK_EXTCLK_GPIO_3, 9),
);
// start cores 2 & 3
bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0xCCC);
let mut i = 0;
let mut j = 0;
const TEST_LEN: usize = 32;
let mut retvals = [0u32; TEST_LEN];
loop {
// fill the FIFO
while (((bio_ss.bio.rf(SFR_EVENT_STATUS_SFR_EVENT_STATUS) >> 24) & 0x1) != 0) && (i < TEST_LEN) {
// report_api(i as u32 | 0xAA00);
bio_ss.bio.wo(utra::bio_bdma::SFR_TXF0, i as u32 | 0xAA00);
i += 1;
}
if i >= TEST_LEN {
break;
}
// if FIFO is past the full water mark, drain one entry
if ((bio_ss.bio.rf(SFR_EVENT_STATUS_SFR_EVENT_STATUS) >> 24) & 0x4) != 0 {
retvals[j] = bio_ss.bio.r(utra::bio_bdma::SFR_RXF1);
j += 1;
}
}
print!("iter ");
while j < TEST_LEN {
while bio_ss.bio.rf(SFR_FLEVEL_PCLK_REGFIFO_LEVEL1) == 0 {} // wait until there's something there
retvals[j] = bio_ss.bio.r(utra::bio_bdma::SFR_RXF1);
j += 1;
print!("{} ", j);
}
print!("\r");
for (index, &val) in retvals.iter().enumerate() {
print!("SPI rbk {:x}\r", val);
// the XOR to 0xAAAA is just a mask we threw in to test immediate loads
assert!(val == ((0xAA00 | index as u32) ^ 0xAAAA));
}
report_api(0x51C0_600D);
}
// This is written to also test some additional features:
// - loading word out of RAM (the random mask at the top)
// - machine ID based dispatch
// - how fast the SPI could run, hence the unrolled loop
// - Receive clock triggering a code event (quanta register x20 is remapped to a GPIO input instead of the
// clock divider)
// - Branch on quanta read (saves one clock cycle for a tight loop; not strictly necessary but we use the
// idiom here to cap Tx loop)
// This runs at about a 25MHz SPI clock rate, assuming an 800MHz
// core clock for the complex. The Rx loop is potentially much faster
// than the Tx loop.
#[rustfmt::skip]
bio_code!(
spi_driver,
SPI_DRIVER_START,
SPI_DRIVER_END,
"j 90f",
"nop",
"j 90f",
"nop",
"j 90f",
"nop",
"j 90f",
"nop",
"retmask:",
".word 0x0000AAAA", // just a random mask for testing immediate loads
"90:",
// dispatch based on machine ID
"srli x1, x31, 30",
"li x2, 0",
"beq x1, x2, 80f",
"li x2, 1",
"beq x1, x2, 81f",
"li x2, 2",
"beq x1, x2, 82f",
"li x2, 3",
"beq x1, x2, 83f",
"80:", // machine 0
"j 80b",
"81:", // machine 1
"j 81b",
"82:", // machine 2 - tx on bit 8, clock on bit 9, chip select on bit 10
"li x1, 0x700", // setup output mask bits
"mv x26, x1", // mask
"mv x24, x1", // direction (1 = output)
"li x2, 0x100", // bitmask for output data
"not x3, x2",
"li x4, 0x200", // bitmask for clock
"not x5, x4",
"li x6, 0x400", // bitmask for chip select
"not x7, x6",
"mv x21, x6", // setup GPIO with CS high, clock low, data low
"mv x20, x0", // halt to quantum, make sure this takes effect
"20:", // main loop
"mv x15, x16", // load data to transmit from fifo 0 - halts until data is available
"mv x23, x7", // drop CS
"slli x15, x15, 8", // shift so the LSB is at bit 8
"mv x20, x0", // wait quantum for CS hold time
// bit 0
"and x22, x15, x2", // set data bit, if it's 1
"or x23, x15, x3", // clear data bit, if it's 0
"mv x20, x0", // wait quantum for data setup
"mv x22, x4", // clock rise
"srli x15, x15, 1",
"mv x20, x0", // wait quantum for data hold
// bit 1
"and x22, x15, x2", // set data bit, if it's 1
"or x23, x15, x3", // clear data bit, if it's 0
"mv x23, x5", // clock fall
"mv x20, x0", // wait quantum for data setup
"mv x22, x4", // clock rise
"srli x15, x15, 1",
"mv x20, x0", // wait quantum for data hold
// bit 2
"and x22, x15, x2", // set data bit, if it's 1
"or x23, x15, x3", // clear data bit, if it's 0
"mv x23, x5", // clock fall
"mv x20, x0", // wait quantum for data setup
"mv x22, x4", // clock rise
"srli x15, x15, 1",
"mv x20, x0", // wait quantum for data hold
// bit 3
"and x22, x15, x2", // set data bit, if it's 1
"or x23, x15, x3", // clear data bit, if it's 0
"mv x23, x5", // clock fall
"mv x20, x0", // wait quantum for data setup
"mv x22, x4", // clock rise
"srli x15, x15, 1",
"mv x20, x0", // wait quantum for data hold
// bit 4
"and x22, x15, x2", // set data bit, if it's 1
"or x23, x15, x3", // clear data bit, if it's 0
"mv x23, x5", // clock fall
"mv x20, x0", // wait quantum for data setup
"mv x22, x4", // clock rise
"srli x15, x15, 1",
"mv x20, x0", // wait quantum for data hold
// bit 5
"and x22, x15, x2", // set data bit, if it's 1
"or x23, x15, x3", // clear data bit, if it's 0
"mv x23, x5", // clock fall
"mv x20, x0", // wait quantum for data setup
"mv x22, x4", // clock rise
"srli x15, x15, 1",
"mv x20, x0", // wait quantum for data hold
// bit 6
"and x22, x15, x2", // set data bit, if it's 1
"or x23, x15, x3", // clear data bit, if it's 0
"mv x23, x5", // clock fall
"mv x20, x0", // wait quantum for data setup
"mv x22, x4", // clock rise
"srli x15, x15, 1",
"mv x20, x0", // wait quantum for data hold
// bit 7
"and x22, x15, x2", // set data bit, if it's 1
"or x23, x15, x3", // clear data bit, if it's 0
"mv x23, x5", // clock fall
"mv x20, x0", // wait quantum for data setup
"mv x22, x4", // clock rise
"srli x15, x15, 1",
"mv x20, x0", // wait quantum for data hold
// bit 8
"and x22, x15, x2", // set data bit, if it's 1
"or x23, x15, x3", // clear data bit, if it's 0
"mv x23, x5", // clock fall
"mv x20, x0", // wait quantum for data setup
"mv x22, x4", // clock rise
"srli x15, x15, 1",
"mv x20, x0", // wait quantum for data hold
// bit 9
"and x22, x15, x2", // set data bit, if it's 1
"or x23, x15, x3", // clear data bit, if it's 0
"mv x23, x5", // clock fall
"mv x20, x0", // wait quantum for data setup
"mv x22, x4", // clock rise
"srli x15, x15, 1",
"mv x20, x0", // wait quantum for data hold
// bit 10
"and x22, x15, x2", // set data bit, if it's 1
"or x23, x15, x3", // clear data bit, if it's 0
"mv x23, x5", // clock fall
"mv x20, x0", // wait quantum for data setup
"mv x22, x4", // clock rise
"srli x15, x15, 1",
"mv x20, x0", // wait quantum for data hold
// bit 11
"and x22, x15, x2", // set data bit, if it's 1
"or x23, x15, x3", // clear data bit, if it's 0
"mv x23, x5", // clock fall
"mv x20, x0", // wait quantum for data setup
"mv x22, x4", // clock rise
"srli x15, x15, 1",
"mv x20, x0", // wait quantum for data hold
// bit 12
"and x22, x15, x2", // set data bit, if it's 1
"or x23, x15, x3", // clear data bit, if it's 0
"mv x23, x5", // clock fall
"mv x20, x0", // wait quantum for data setup
"mv x22, x4", // clock rise
"srli x15, x15, 1",
"mv x20, x0", // wait quantum for data hold
// bit 13
"and x22, x15, x2", // set data bit, if it's 1
"or x23, x15, x3", // clear data bit, if it's 0
"mv x23, x5", // clock fall
"mv x20, x0", // wait quantum for data setup
"mv x22, x4", // clock rise
"srli x15, x15, 1",
"mv x20, x0", // wait quantum for data hold
// bit 14
"and x22, x15, x2", // set data bit, if it's 1
"or x23, x15, x3", // clear data bit, if it's 0
"mv x23, x5", // clock fall
"mv x20, x0", // wait quantum for data setup
"mv x22, x4", // clock rise
"srli x15, x15, 1",
"mv x20, x0", // wait quantum for data hold
// bit 15
"and x22, x15, x2", // set data bit, if it's 1
"or x23, x15, x3", // clear data bit, if it's 0
"mv x23, x5", // clock fall
"mv x20, x0", // wait quantum for data setup
"mv x22, x4", // clock rise
"srli x15, x15, 1",
"mv x20, x0", // wait quantum for data hold
"mv x23, x5", // clock fall
"mv x20, x0", // CS setup time
"mv x22, x6", // raise CS
"mv x20, x0", // meet CS min-high time (if necessary)
"beqz x20, 20b", // wait quantum & loop back -- x20 reads back as 0
"83:", // machine 3 - rx on bit 8, clock on bit 9, chip select on bit 10
"li x1, 0x700", // setup mask bits
"mv x26, x1", // mask
"not x1, x1",
// "mv x25, x1", // direction (0 = input) -- don't execute this, because we're looping back on the output pins
"li x2, 0x100", // bitmask for input data
"not x3, x2",
"li x4, 0x200", // bitmask for clock
"not x5, x4",
"li x6, 0x400", // bitmask for chip select
"not x7, x6",
"lw x13, retmask", // test loading of words in program text
"30:", // main loop
"mv x14, zero", // zero out the result register
// wait until CS falls
"31:",
"and x8, x6, x21",
"bnez x8, 31b",
// bit 0
"srli x14, x14, 1", // shift bit into place
// wait until clock rises
"mv x20, zero", // halts until rising edge on bit 9 (configured by host)
"and x9, x2, x21", // mask the bit
"slli x9, x9, 7", // move to MSB
"or x14, x9, x14", // OR into the result
// bit 1
"srli x14, x14, 1", // shift bit into place
// wait until clock rises
"mv x20, zero", // halts until rising edge on bit 9 (configured by host)
"and x9, x2, x21", // mask the bit
"slli x9, x9, 7", // move to MSB
"or x14, x9, x14", // OR into the result
// bit 2
"srli x14, x14, 1", // shift bit into place
// wait until clock rises
"mv x20, zero", // halts until rising edge on bit 9 (configured by host)
"and x9, x2, x21", // mask the bit
"slli x9, x9, 7", // move to MSB
"or x14, x9, x14", // OR into the result
// bit 3
"srli x14, x14, 1", // shift bit into place
// wait until clock rises
"mv x20, zero", // halts until rising edge on bit 9 (configured by host)
"and x9, x2, x21", // mask the bit
"slli x9, x9, 7", // move to MSB
"or x14, x9, x14", // OR into the result
// bit 4
"srli x14, x14, 1", // shift bit into place
// wait until clock rises
"mv x20, zero", // halts until rising edge on bit 9 (configured by host)
"and x9, x2, x21", // mask the bit
"slli x9, x9, 7", // move to MSB
"or x14, x9, x14", // OR into the result
// bit 5
"srli x14, x14, 1", // shift bit into place
// wait until clock rises
"mv x20, zero", // halts until rising edge on bit 9 (configured by host)
"and x9, x2, x21", // mask the bit
"slli x9, x9, 7", // move to MSB
"or x14, x9, x14", // OR into the result
// bit 6
"srli x14, x14, 1", // shift bit into place
// wait until clock rises
"mv x20, zero", // halts until rising edge on bit 9 (configured by host)
"and x9, x2, x21", // mask the bit
"slli x9, x9, 7", // move to MSB
"or x14, x9, x14", // OR into the result
// bit 7
"srli x14, x14, 1", // shift bit into place
// wait until clock rises
"mv x20, zero", // halts until rising edge on bit 9 (configured by host)
"and x9, x2, x21", // mask the bit
"slli x9, x9, 7", // move to MSB
"or x14, x9, x14", // OR into the result
// bit 8
"srli x14, x14, 1", // shift bit into place
// wait until clock rises
"mv x20, zero", // halts until rising edge on bit 9 (configured by host)
"and x9, x2, x21", // mask the bit
"slli x9, x9, 7", // move to MSB
"or x14, x9, x14", // OR into the result
// bit 9
"srli x14, x14, 1", // shift bit into place
// wait until clock rises
"mv x20, zero", // halts until rising edge on bit 9 (configured by host)
"and x9, x2, x21", // mask the bit
"slli x9, x9, 7", // move to MSB
"or x14, x9, x14", // OR into the result
// bit 10
"srli x14, x14, 1", // shift bit into place
// wait until clock rises
"mv x20, zero", // halts until rising edge on bit 9 (configured by host)
"and x9, x2, x21", // mask the bit
"slli x9, x9, 7", // move to MSB
"or x14, x9, x14", // OR into the result
// bit 11
"srli x14, x14, 1", // shift bit into place
// wait until clock rises
"mv x20, zero", // halts until rising edge on bit 9 (configured by host)
"and x9, x2, x21", // mask the bit
"slli x9, x9, 7", // move to MSB
"or x14, x9, x14", // OR into the result
// bit 12
"srli x14, x14, 1", // shift bit into place
// wait until clock rises
"mv x20, zero", // halts until rising edge on bit 9 (configured by host)
"and x9, x2, x21", // mask the bit
"slli x9, x9, 7", // move to MSB
"or x14, x9, x14", // OR into the result
// bit 13
"srli x14, x14, 1", // shift bit into place
// wait until clock rises
"mv x20, zero", // halts until rising edge on bit 9 (configured by host)
"and x9, x2, x21", // mask the bit
"slli x9, x9, 7", // move to MSB
"or x14, x9, x14", // OR into the result
// bit 14
"srli x14, x14, 1", // shift bit into place
// wait until clock rises
"mv x20, zero", // halts until rising edge on bit 9 (configured by host)
"and x9, x2, x21", // mask the bit
"slli x9, x9, 7", // move to MSB
"or x14, x9, x14", // OR into the result
// bit 15
"srli x14, x14, 1", // shift bit into place
// wait until clock rises
"mv x20, zero", // halts until rising edge on bit 9 (configured by host)
"and x9, x2, x21", // mask the bit
"slli x9, x9, 7", // move to MSB
"or x14, x9, x14", // OR into the result
"xor x14, x13, x14", // apply the test mask
"mv x17, x14", // move result into output FIFO
"j 30b" // loop to top
);

View file

@ -0,0 +1,789 @@
use utralib::utra::bio_bdma::{
SFR_ETYPE_FIFO_EVENT_EQ_MASK, SFR_ETYPE_FIFO_EVENT_GT_MASK, SFR_ETYPE_FIFO_EVENT_LT_MASK,
};
use super::TEST_INVERT_MASK;
use crate::*;
// this test requires manual inspection of the outputs
// the GPIO pins should toggle with 0x11, 0x12, 0x13...
// at the specified quantum rate of the machine.
pub fn hello_world() {
print!("hello world test\r");
let mut bio_ss = BioSharedState::new();
let simple_test_code = hello_world_code();
// copy code to reset vector for 0th machine
bio_ss.load_code(simple_test_code, 0);
// configure & run the 0th machine
// /32 clock
bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV0, 0x20_0000);
// start the machine
bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0x111);
print!("===hello world PASS===\r");
}
#[rustfmt::skip]
bio_code!(hello_world_code, HELLO_START, HELLO_END,
"add x1, zero, 0x10",
"0:",
"add x1, x1, 0x1",
"mv x21, x1",
"mv x20, zero",
"j 0b",
"nop"
);
// this test requires manual inspection of the outputs
// the GPIO pins should toggle with the following pattern:
// 0x41312111, 0x42322212, 0x43332313, etc.
// and they should be in sync-lock, no ragged transitions
pub fn hello_multiverse() {
print!("multiverse\r");
let mut bio_ss = BioSharedState::new();
// stop all the machines, so that code can be loaded
bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0x0);
let code = multiverse_code();
bio_ss.load_code(code, 0);
// configure & run the 0th machine
// /32 clock
bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV0, 0x20_0000);
bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV1, 0x20_0000);
bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV2, 0x20_0000);
bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV3, 0x20_0000);
// snap GPIO outputs to the quantum
bio_ss.bio.wo(
utra::bio_bdma::SFR_CONFIG,
bio_ss.bio.ms(utra::bio_bdma::SFR_CONFIG_SNAP_OUTPUT_TO_QUANTUM, 1)
| bio_ss.bio.ms(utra::bio_bdma::SFR_CONFIG_SNAP_OUTPUT_TO_QUANTUM, 1), /* arbitrary choice,
* they
* should all be the
* same */
);
// start all the machines, all at once
bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0xfff);
print!("===multiverse PASS===\r");
}
#[rustfmt::skip]
bio_code!(multiverse_code, MULTIVERSE_START, MULTIVERSE_END,
// Reset vectors for each core are aligned to 4-byte boundaries
// As long as the jump target is <2kiB from reset, this will emit
// a C-instruction, so it needs padding with a NOP. Unfortunately,
// I can't seem to figure out a way to force the assembler to always
// encode as uncompressed, so, you have to be aware of the jump destination
// for the assembler output to line up according to your expectation :(
//
// using 'c.j' syntax for the jump causes the assembler to emit an error,
// but the code still compiles, so...avoiding that for now. might be a bug,
// but I am very not interested in fixing that today.
//
// Also note that labels can only be numbers from 0-99, and, due to an llvm
// bug, labels made exclusively of 0 or 1 should be avoided because they get
// interpreted as binary numbers. dat's some jank in the tank!!
"j 90f",
"nop",
"j 91f",
"nop",
"j 92f",
"nop",
"j 93f",
"nop",
// mach 0 code
"90:",
// x26 sets the GPIO mask
"li x2, 0xFF", // load constants into r0-15 bank first
"mv x26, x2", // it's not legal to do anything other than mv to x26
"add x1, zero, 0x10",
"4:",
"add x1, x1, 0x1",
// x21 write clobbers the GPIO bits, ANDed with mask in x26
"mv x21, x1",
// x20 write causes core to wait until next sync quantum
"mv x20, zero",
"j 4b",
// mach 1 code
"91:",
"li x2, 0xFF00",
"mv x26, x2",
"add x1, zero, 0x20",
"5:",
"add x1, x1, 0x1",
"slli x21, x1, 8",
"mv x20, zero",
"j 5b",
// mach 2 code
"92:",
"li x2, 0xFF0000",
"mv x26, x2",
"add x1, zero, 0x30",
"6:",
"add x1, x1, 0x1",
"slli x21, x1, 16",
"mv x20, zero",
"j 6b",
// mach 3 code
"93:",
"li x2, 0xFF000000",
"mv x26, x2",
"add x1, zero, 0x40",
"7:",
"add x1, x1, 0x1",
"slli x21, x1, 24",
"mv x20, zero",
"j 7b"
);
// this test requires manual checking of gpio outputs
// GPIO pins should have the form 0x100n800m
// where n = 2*m. The output is not meant to be fully in sync,
// it will be "ragged" as the output snapping is not turned on.
// so 0x10008000, 0x10048002, 0x10088004, etc...
// but with a glitch before major transitions. The output could
// be sync'd locked, but we leave it off for this test so we have
// a demo of how things look when it's off.
pub fn fifo_basic() {
print!("FIFO basic\r");
// clear any prior test config state
let mut test_cfg = CSR::new(utra::csrtest::HW_CSRTEST_BASE as *mut u32);
test_cfg.wo(utra::csrtest::WTEST, 0);
let mut bio_ss = BioSharedState::new();
// stop all the machines, so that code can be loaded
bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0x0);
let code = fifo_basic_code();
bio_ss.load_code(code, 0);
// configure & run the 0th machine
// / 16. clock
bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV0, 0x23_BE00);
bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV3, 0x23_BE00);
bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV1, 0x33_1200);
bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV2, 0x33_1200);
// don't snap GPIO outputs
bio_ss.bio.wo(utra::bio_bdma::SFR_CONFIG, 0);
// start all the machines, all at once
bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0xfff);
print!("===FIFO basic PASS===\r");
}
#[rustfmt::skip]
bio_code!(fifo_basic_code, FIFO_BASIC_START, FIFO_BASIC_END,
"j 90f",
"nop",
"j 91f",
"nop",
"j 92f",
"nop",
"j 93f",
"nop",
// mach 0 code
"90:",
"li x2, 0xFFFF",
"mv x26, x2",
"li x1, 0x10000000",
"11:",
"mv x16, x1",
"mv x21, x17",
// pass to mach 3 to update the loop counter
"mv x19, x1",
"mv x20, zero",
"mv x1, x19",
"j 11b",
// mach 1 code
"91:",
"li x2, 0xFFFF0000",
"mv x26, x2",
"li x1, 0x8000",
"21:",
"mv x17, x1",
"mv x21, x16",
// pass to mach 2 to update the loop counter
"mv x18, x1",
"mv x20, zero",
"mv x1, x18",
"j 21b",
// mach 2 code
"92:",
"addi x18, x18, 2", // increment the value in fifo by 2
"mv x20, zero",
"j 92b",
// mach 3 code
"93:",
"li x2, 0x40000",
"23:",
"add x19, x19, x2", // increment the value in fifo by 0x4_0000
"mv x20, zero",
"j 23b",
"nop"
);
// this test contains an automated check of the readback, but, in a nutshell,
// the test writes the values "0xF1F0_000m", where m is 0-15 to the GPIO output
// These outputs come from the *host*, and the test runs slow enough that host
// should stall waiting for device to propagate values at least once.
//
// Concurrently, another machine greedily reads the GPIO inputs, at a sync-locked
// rate relative to the outputs, until the FIFO is full and backpressure stops
// reads. The host then drains the FIFO, so some of the output indices should be
// skipped and only the final value is retrieved with future reads.
//
// GPIO outputs are run without snapping in this case, because there is just one
// machine updating outputs and no need to do that.
pub fn host_fifo_tests() {
print!("Host FIFO tests\r");
// clear prior test config state
let mut test_cfg = CSR::new(utra::csrtest::HW_CSRTEST_BASE as *mut u32);
test_cfg.wo(utra::csrtest::WTEST, 0);
let mut bio_ss = BioSharedState::new();
// stop all the machines, so that code can be loaded
bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0x0);
let code = fifo_host_bitbang();
bio_ss.load_code(code, 0);
// reset all the fifos
bio_ss.bio.wo(utra::bio_bdma::SFR_FIFO_CLR, 0xF);
// configure & run the 0th machine
// clock it slowly, so the fifo builds up back pressure
bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV0, 0x400_0000);
bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV1, 0x400_0000);
// don't snap GPIO outputs
bio_ss.bio.wo(utra::bio_bdma::SFR_CONFIG, 0);
// invert readbacks via I/O
test_cfg.wo(utra::csrtest::WTEST, TEST_INVERT_MASK);
// start cores 1 & 2
bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0x333);
// clock some values into the bitbang fifo
let mut stalled = false;
for i in 0..16 {
bio_ss.bio.wo(utra::bio_bdma::SFR_TXF0, i + 0xF1F0_0000);
while bio_ss.bio.rf(utra::bio_bdma::SFR_FLEVEL_PCLK_REGFIFO_LEVEL0) >= 8 {
stalled = true;
}
}
assert!(stalled);
// wait for the write FIFO to drain
while bio_ss.bio.rf(utra::bio_bdma::SFR_FLEVEL_PCLK_REGFIFO_LEVEL0) != 0 {}
// read back some fifo values, and check that back-pressure worked
for i in 0..16 {
let rbk = bio_ss.bio.r(utra::bio_bdma::SFR_RXF1);
// we get indices 0-9: we can capture up to 8+1 entries before backpressure stops captures
// and there is 1 extra value stuck in the CPU itself at the time of the stall.
//
// finally, we're pegged at 15, because, backpressure caused us to miss the rest of
// the entries, and we are stuck at the final written value of the output test
if i <= 9 {
assert!(rbk == !(0xF1F0_0000 + i));
} else {
assert!(rbk == !(0xF1F0_0000 + 15));
}
print!("backpressure {:x}\r", rbk);
}
fn get_gpio_via_core(bio_ss: &mut BioSharedState) -> u32 {
// get the GPIO value by triggering core 1 via event bit 1
bio_ss.bio.wo(utra::bio_bdma::SFR_EVENT_SET, 1);
while bio_ss.bio.rf(utra::bio_bdma::SFR_FLEVEL_PCLK_REGFIFO_LEVEL1) == 0 {
// wait for the core to have reported the GPIO value
}
bio_ss.bio.r(utra::bio_bdma::SFR_RXF1)
}
// load next test
// clear inversions, etc on readbacks via I/O
test_cfg.wo(utra::csrtest::WTEST, 0);
// stop machine & load code
bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0x0);
let code = fifo_host_bitbang_level_trig();
bio_ss.load_code(code, 0);
// clear all events
bio_ss.bio.wfo(utra::bio_bdma::SFR_EVENT_CLR_SFR_EVENT_CLR, 0xFFFF_FF);
// set level trigger to 4
bio_ss.bio.wfo(utra::bio_bdma::SFR_ELEVEL_FIFO_EVENT_LEVEL1, 4);
// set polarities: >= 4 to flip
bio_ss.bio.wfo(utra::bio_bdma::SFR_ETYPE_FIFO_EVENT_EQ_MASK, 0b00_00_00_10);
bio_ss.bio.rmwf(utra::bio_bdma::SFR_ETYPE_FIFO_EVENT_LT_MASK, 0b00_00_00_00);
bio_ss.bio.rmwf(utra::bio_bdma::SFR_ETYPE_FIFO_EVENT_GT_MASK, 0b00_00_00_10);
// reset all the fifos
bio_ss.bio.wo(utra::bio_bdma::SFR_FIFO_CLR, 0xF);
// start cores 1, 2, 3
bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0x777);
// confirm the core booted
let f1_val = get_gpio_via_core(&mut bio_ss);
print!("core booted {:x}\r", f1_val);
assert!(f1_val == 0xfeedface);
// ensure fifo levels are where we think they are
assert!(bio_ss.bio.rf(utra::bio_bdma::SFR_FLEVEL_PCLK_REGFIFO_LEVEL0) == 0);
assert!(bio_ss.bio.rf(utra::bio_bdma::SFR_FLEVEL_PCLK_REGFIFO_LEVEL1) == 0);
assert!(bio_ss.bio.rf(utra::bio_bdma::SFR_FLEVEL_PCLK_REGFIFO_LEVEL2) == 0);
// put 7 items into the output data fifo
let mut final_val: u32 = 0;
for i in 0..7 {
final_val = 0xf1f0_1000 + i;
bio_ss.bio.wo(utra::bio_bdma::SFR_TXF0, final_val);
}
while bio_ss.bio.rf(utra::bio_bdma::SFR_FLEVEL_PCLK_REGFIFO_LEVEL0) != 0 {
// wait for fifo to drain
}
let pause_val = get_gpio_via_core(&mut bio_ss);
print!("pause_val {:x}\r", pause_val);
assert!(pause_val == final_val);
// drop one more value in and confirm it appears
let stop_val = 0xACE0_BACE;
bio_ss.bio.wo(utra::bio_bdma::SFR_TXF0, stop_val);
let stop_confirm_val = get_gpio_via_core(&mut bio_ss);
print!("stop_confirm_val {:x}\r", stop_confirm_val);
assert!(stop_val == stop_confirm_val);
// fifo2 should have the entire log of all values in it. make sure that's the case
for i in 0..7 {
let f2_val = bio_ss.bio.r(utra::bio_bdma::SFR_RXF2);
print!("f2_val {:x}\r", f2_val);
assert!(f2_val == 0xf1f0_1000 + i);
}
let stop_check = bio_ss.bio.r(utra::bio_bdma::SFR_RXF2);
print!("stop_check {:x}\r", stop_check);
assert!(stop_check == stop_val);
print!("===Host FIFO PASS===\r");
}
#[rustfmt::skip]
bio_code!(fifo_host_bitbang, FIFO_HOST_BITBANG_START, FIFO_HOST_BITBANG_END,
"j 90f",
"nop",
"j 91f",
"nop",
"90:",
"mv x21, x16",
"mv x20, zero",
"j 90b",
"91:",
"mv x20, zero",
"mv x17, x21",
"j 91b"
);
// core 0:
// - sets GPIO to 0xFEEDFACE on boot
// - sets to trigger on fifo0 level, channel B
// - waits for the fifo0 to meet its full-enough condition
// - moves fifo content -> gpio as fast as possible
// - notifies core 2 via event bit 2 every fifo move
// - waits for ack on event bit 3
// - restores fifo trigger mask
// core 1: should only capture the data on host command (so the buffered fifo entries will be missed)
// - waits on event bit 1 (0x1 mask)
// - takes gpio in and writes it to fifo1
// core 2: should capture *all* the data written to core 0
// - waits on event bit 2 (0x2 mask)
// - takes gpio in and writes it to fifo2
// - acks core 0 on event bit 3
#[rustfmt::skip]
bio_code!(
fifo_host_bitbang_level_trig,
FIFO_HOST_BITBANG_LEVEL_TRIG_START,
FIFO_HOST_BITBANG_LEVEL_TRIG_END,
"j 90f",
"nop",
"j 91f",
"nop",
"j 92f",
"nop",
"90:", // machine 0
"li x2, 0xfeedface",
"mv x21, x2", // init gpio to the "i'm here" sentinel
"li x1, 0x02000000", // event trigger fifo0, trigger channel B
"li x5, 0x4", // event mask for event bit 3
"mv x27, x1", // set event mask
"mv x3, x30", // wait on event trigger by reading x30 -- code will halt until fifo trigger condition is met
"li x4, 0x2", // trigger for channel used by machine 2
"mv x27, x5", // set event mask for the ack from machine 2, indicating gpio pin is sampled
"20:",
"mv x21, x16", // fifo0 -> gpio out
"mv x28, x4", // set the bit that machine 2 is listening to
"mv x3, x30", // wait for ack that gpio was sampled
"mv x29, x3", // clear the trigger
"j 20b",
"91:", // machine 1
"li x1, 0x1", // event bit 0
"mv x27, x1", // set trigger mask
"21:",
"mv x2, x30", // wait for event trigger
"and x2, x2, x1", // mask event result
"mv x29, x2", // clear the event trigger
"mv x17, x21", // gpio in -> fifo1
"j 21b",
"92:", // machine 2
"li x2, 0x2", // event bit 1
"mv x27, x2", // set trigger mask
"li x4, 0x4", // ack trigger mask
"22:",
"mv x3, x30", // wait for event
"and x3, x2, x2", // mask event
"mv x29, x3", // clear trigger
"mv x18, x21", // gpio in -> fifo2
"mv x28, x4", // set event based on ack trigger mask (x4)
"j 22b"
);
#[derive(Clone, Copy)]
struct FifoLevelTestConfig {
tx_reg: crate::Register,
rx_reg: crate::Register,
levels: [crate::Field; 2],
event_masks: [u32; 2],
}
// This can be done without any code running on the machines; the host can
// set and observe all fifo levels and triggers directly.
pub fn fifo_level_tests() {
print!("FIFO level comprehensive\r");
// clear prior test config state
let mut test_cfg = CSR::new(utra::csrtest::HW_CSRTEST_BASE as *mut u32);
test_cfg.wo(utra::csrtest::WTEST, 0);
let mut bio_ss = BioSharedState::new();
// stop all the machines, so that code can be loaded
bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0x0);
// let code = fifo_level_tests_code();
// bio_ss.load_code(code, 0);
// configure fifo trigger levels
bio_ss.bio.wfo(utra::bio_bdma::SFR_ELEVEL_FIFO_EVENT_LEVEL0, 0);
bio_ss.bio.wfo(utra::bio_bdma::SFR_ELEVEL_FIFO_EVENT_LEVEL1, 9);
bio_ss.bio.wfo(utra::bio_bdma::SFR_ELEVEL_FIFO_EVENT_LEVEL2, 1);
bio_ss.bio.wfo(utra::bio_bdma::SFR_ELEVEL_FIFO_EVENT_LEVEL3, 8);
bio_ss.bio.wfo(utra::bio_bdma::SFR_ELEVEL_FIFO_EVENT_LEVEL4, 2);
bio_ss.bio.wfo(utra::bio_bdma::SFR_ELEVEL_FIFO_EVENT_LEVEL5, 7);
bio_ss.bio.wfo(utra::bio_bdma::SFR_ELEVEL_FIFO_EVENT_LEVEL6, 4);
bio_ss.bio.wfo(utra::bio_bdma::SFR_ELEVEL_FIFO_EVENT_LEVEL7, 4);
// configure the polarities
bio_ss.bio.wfo(utra::bio_bdma::SFR_ETYPE_FIFO_EVENT_EQ_MASK, 0b11_00_11_11);
bio_ss.bio.rmwf(utra::bio_bdma::SFR_ETYPE_FIFO_EVENT_LT_MASK, 0b11_01_01_00);
bio_ss.bio.rmwf(utra::bio_bdma::SFR_ETYPE_FIFO_EVENT_GT_MASK, 0b10_10_10_00);
/*
The structure of the FIFO events is that there are two event level configurations
per FIFO, structured as fifo [N] gets event level [N*2, N*2+1].
Each event level could trigger on equals, less than, greater than, or any combination
of the three.
*/
let fifo_test_configs: [FifoLevelTestConfig; 4] = [
FifoLevelTestConfig {
tx_reg: utra::bio_bdma::SFR_TXF0,
rx_reg: utra::bio_bdma::SFR_RXF0,
levels: [
utra::bio_bdma::SFR_ELEVEL_FIFO_EVENT_LEVEL0,
utra::bio_bdma::SFR_ELEVEL_FIFO_EVENT_LEVEL1,
],
event_masks: [0x1, 0x2],
},
FifoLevelTestConfig {
tx_reg: utra::bio_bdma::SFR_TXF1,
rx_reg: utra::bio_bdma::SFR_RXF1,
levels: [
utra::bio_bdma::SFR_ELEVEL_FIFO_EVENT_LEVEL2,
utra::bio_bdma::SFR_ELEVEL_FIFO_EVENT_LEVEL3,
],
event_masks: [0x4, 0x8],
},
FifoLevelTestConfig {
tx_reg: utra::bio_bdma::SFR_TXF2,
rx_reg: utra::bio_bdma::SFR_RXF2,
levels: [
utra::bio_bdma::SFR_ELEVEL_FIFO_EVENT_LEVEL4,
utra::bio_bdma::SFR_ELEVEL_FIFO_EVENT_LEVEL5,
],
event_masks: [0x10, 0x20],
},
FifoLevelTestConfig {
tx_reg: utra::bio_bdma::SFR_TXF3,
rx_reg: utra::bio_bdma::SFR_RXF3,
levels: [
utra::bio_bdma::SFR_ELEVEL_FIFO_EVENT_LEVEL6,
utra::bio_bdma::SFR_ELEVEL_FIFO_EVENT_LEVEL7,
],
event_masks: [0x40, 0x80],
},
];
const FIFO_MAX: u32 = 9;
let mut rx_checks = [0u32; FIFO_MAX as usize];
let mut tx_state = 0x1;
let irq_masks: [Register; 4] = [
utra::bio_bdma::SFR_IRQMASK_0,
utra::bio_bdma::SFR_IRQMASK_1,
utra::bio_bdma::SFR_IRQMASK_2,
utra::bio_bdma::SFR_IRQMASK_3,
];
let irqarray18 = CSR::new(utra::irqarray18::HW_IRQARRAY18_BASE as *mut u32);
for (bank, config) in fifo_test_configs.iter().enumerate() {
let irq_mask_reg = irq_masks[bank];
let irq_mask = (1 << bank) as u32;
print!("irq_mask {:x}\r", irq_mask);
// we want to check that less than, equals, and greater than triggers work individually
// then we want to check that lt+eq and gt+eq work together
// lt+gt trigger doesn't make sense, we just don't check that
for (&level, &mask) in config.levels.iter().zip(config.event_masks.iter()) {
bio_ss.bio.wo(irq_mask_reg, mask << 24);
// reset all the fifos
bio_ss.bio.wo(utra::bio_bdma::SFR_FIFO_CLR, 0xF);
for test_level in 0..FIFO_MAX {
print!("test_level {:x} bank {:x}\r", test_level, bank);
// test eq at level
bio_ss.bio.wfo(level, test_level);
bio_ss.bio.rmwf(SFR_ETYPE_FIFO_EVENT_EQ_MASK, mask);
bio_ss.bio.rmwf(SFR_ETYPE_FIFO_EVENT_LT_MASK, 0);
bio_ss.bio.rmwf(SFR_ETYPE_FIFO_EVENT_GT_MASK, 0);
// fill
for check_level in 0..FIFO_MAX {
let ev_check = bio_ss.bio.r(utra::bio_bdma::SFR_EVENT_STATUS) >> 24;
// report_api(ev_check);
if check_level == test_level {
assert!(ev_check & mask == mask);
// report_api(irqarray18.r(utra::irqarray18::EV_STATUS));
assert!(irqarray18.r(utra::irqarray18::EV_STATUS) & irq_mask == irq_mask);
} else {
assert!(ev_check & mask != mask);
// report_api(irqarray18.r(utra::irqarray18::EV_STATUS));
assert!(irqarray18.r(utra::irqarray18::EV_STATUS) & irq_mask != irq_mask);
}
bio_ss.bio.wo(config.tx_reg, tx_state);
// report_api(tx_state);
rx_checks[check_level as usize] = tx_state;
tx_state = crate::lfsr_next_u32(tx_state);
}
// drain
// report_api(0xdddd_dddd);
for check_level in 0..FIFO_MAX {
let rx = bio_ss.bio.r(config.rx_reg);
// report_api(rx);
assert!(rx == rx_checks[check_level as usize]);
let ev_check = bio_ss.bio.r(utra::bio_bdma::SFR_EVENT_STATUS) >> 24;
if FIFO_MAX - check_level - 1 == test_level {
assert!(ev_check & mask == mask);
assert!(irqarray18.r(utra::irqarray18::EV_STATUS) & irq_mask == irq_mask);
} else {
assert!(ev_check & mask != mask);
assert!(irqarray18.r(utra::irqarray18::EV_STATUS) & irq_mask != irq_mask);
}
}
// test lt at level
bio_ss.bio.wfo(level, test_level);
bio_ss.bio.rmwf(SFR_ETYPE_FIFO_EVENT_EQ_MASK, 0);
bio_ss.bio.rmwf(SFR_ETYPE_FIFO_EVENT_LT_MASK, mask);
bio_ss.bio.rmwf(SFR_ETYPE_FIFO_EVENT_GT_MASK, 0);
// fill
for check_level in 0..FIFO_MAX {
let ev_check = bio_ss.bio.r(utra::bio_bdma::SFR_EVENT_STATUS) >> 24;
if check_level < test_level {
assert!(ev_check & mask == mask);
assert!(irqarray18.r(utra::irqarray18::EV_STATUS) & irq_mask == irq_mask);
} else {
assert!(ev_check & mask != mask);
assert!(irqarray18.r(utra::irqarray18::EV_STATUS) & irq_mask != irq_mask);
}
bio_ss.bio.wo(config.tx_reg, tx_state);
rx_checks[check_level as usize] = tx_state;
tx_state = crate::lfsr_next_u32(tx_state);
}
// drain
for check_level in 0..FIFO_MAX {
let rx = bio_ss.bio.r(config.rx_reg);
assert!(rx == rx_checks[check_level as usize]);
let ev_check = bio_ss.bio.r(utra::bio_bdma::SFR_EVENT_STATUS) >> 24;
if FIFO_MAX - check_level - 1 < test_level {
assert!(ev_check & mask == mask);
assert!(irqarray18.r(utra::irqarray18::EV_STATUS) & irq_mask == irq_mask);
} else {
assert!(ev_check & mask != mask);
assert!(irqarray18.r(utra::irqarray18::EV_STATUS) & irq_mask != irq_mask);
}
}
// test gt at level
bio_ss.bio.wfo(level, test_level);
bio_ss.bio.rmwf(SFR_ETYPE_FIFO_EVENT_EQ_MASK, 0);
bio_ss.bio.rmwf(SFR_ETYPE_FIFO_EVENT_LT_MASK, 0);
bio_ss.bio.rmwf(SFR_ETYPE_FIFO_EVENT_GT_MASK, mask);
// fill
for check_level in 0..FIFO_MAX {
let ev_check = bio_ss.bio.r(utra::bio_bdma::SFR_EVENT_STATUS) >> 24;
if check_level > test_level {
assert!(ev_check & mask == mask);
assert!(irqarray18.r(utra::irqarray18::EV_STATUS) & irq_mask == irq_mask);
} else {
assert!(ev_check & mask != mask);
assert!(irqarray18.r(utra::irqarray18::EV_STATUS) & irq_mask != irq_mask);
}
bio_ss.bio.wo(config.tx_reg, tx_state);
rx_checks[check_level as usize] = tx_state;
tx_state = crate::lfsr_next_u32(tx_state);
}
// drain
for check_level in 0..FIFO_MAX {
let rx = bio_ss.bio.r(config.rx_reg);
assert!(rx == rx_checks[check_level as usize]);
let ev_check = bio_ss.bio.r(utra::bio_bdma::SFR_EVENT_STATUS) >> 24;
if FIFO_MAX - check_level - 1 > test_level {
assert!(ev_check & mask == mask);
assert!(irqarray18.r(utra::irqarray18::EV_STATUS) & irq_mask == irq_mask);
} else {
assert!(ev_check & mask != mask);
assert!(irqarray18.r(utra::irqarray18::EV_STATUS) & irq_mask != irq_mask);
}
}
// test lt eq at level
bio_ss.bio.wfo(level, test_level);
bio_ss.bio.rmwf(SFR_ETYPE_FIFO_EVENT_EQ_MASK, mask);
bio_ss.bio.rmwf(SFR_ETYPE_FIFO_EVENT_LT_MASK, mask);
bio_ss.bio.rmwf(SFR_ETYPE_FIFO_EVENT_GT_MASK, 0);
// fill
for check_level in 0..FIFO_MAX {
let ev_check = bio_ss.bio.r(utra::bio_bdma::SFR_EVENT_STATUS) >> 24;
if check_level <= test_level {
assert!(ev_check & mask == mask);
assert!(irqarray18.r(utra::irqarray18::EV_STATUS) & irq_mask == irq_mask);
} else {
assert!(ev_check & mask != mask);
assert!(irqarray18.r(utra::irqarray18::EV_STATUS) & irq_mask != irq_mask);
}
bio_ss.bio.wo(config.tx_reg, tx_state);
rx_checks[check_level as usize] = tx_state;
tx_state = crate::lfsr_next_u32(tx_state);
}
// drain
for check_level in 0..FIFO_MAX {
let rx = bio_ss.bio.r(config.rx_reg);
assert!(rx == rx_checks[check_level as usize]);
let ev_check = bio_ss.bio.r(utra::bio_bdma::SFR_EVENT_STATUS) >> 24;
if FIFO_MAX - check_level - 1 <= test_level {
assert!(ev_check & mask == mask);
assert!(irqarray18.r(utra::irqarray18::EV_STATUS) & irq_mask == irq_mask);
} else {
assert!(ev_check & mask != mask);
assert!(irqarray18.r(utra::irqarray18::EV_STATUS) & irq_mask != irq_mask);
}
}
// test gt eq at level
bio_ss.bio.wfo(level, test_level);
bio_ss.bio.rmwf(SFR_ETYPE_FIFO_EVENT_EQ_MASK, mask);
bio_ss.bio.rmwf(SFR_ETYPE_FIFO_EVENT_LT_MASK, 0);
bio_ss.bio.rmwf(SFR_ETYPE_FIFO_EVENT_GT_MASK, mask);
// fill
for check_level in 0..FIFO_MAX {
let ev_check = bio_ss.bio.r(utra::bio_bdma::SFR_EVENT_STATUS) >> 24;
if check_level >= test_level {
assert!(ev_check & mask == mask);
assert!(irqarray18.r(utra::irqarray18::EV_STATUS) & irq_mask == irq_mask);
} else {
assert!(ev_check & mask != mask);
assert!(irqarray18.r(utra::irqarray18::EV_STATUS) & irq_mask != irq_mask);
}
bio_ss.bio.wo(config.tx_reg, tx_state);
rx_checks[check_level as usize] = tx_state;
tx_state = crate::lfsr_next_u32(tx_state);
}
// drain
for check_level in 0..FIFO_MAX {
let rx = bio_ss.bio.r(config.rx_reg);
assert!(rx == rx_checks[check_level as usize]);
let ev_check = bio_ss.bio.r(utra::bio_bdma::SFR_EVENT_STATUS) >> 24;
if FIFO_MAX - check_level - 1 >= test_level {
assert!(ev_check & mask == mask);
assert!(irqarray18.r(utra::irqarray18::EV_STATUS) & irq_mask == irq_mask);
} else {
assert!(ev_check & mask != mask);
assert!(irqarray18.r(utra::irqarray18::EV_STATUS) & irq_mask != irq_mask);
}
}
}
bio_ss.bio.wo(irq_mask_reg, 0);
}
}
print!("===FIFO level comprehensive PASS===\r")
}
pub fn aclk_tests() {
print!("ACLK test\r");
// clear any prior test config state
let mut test_cfg = CSR::new(utra::csrtest::HW_CSRTEST_BASE as *mut u32);
test_cfg.wo(utra::csrtest::WTEST, 0);
let mut bio_ss = BioSharedState::new();
// stop all the machines, so that code can be loaded
bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0x0);
let code = aclk_code();
bio_ss.load_code(code, 0);
// configure & run the 0th machine
bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV1, 0xA_0000);
// don't snap GPIO outputs
bio_ss.bio.wo(utra::bio_bdma::SFR_CONFIG, 0);
// start machine 1
bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0x222);
while bio_ss.bio.rf(utra::bio_bdma::SFR_FLEVEL_PCLK_REGFIFO_LEVEL1) < 7 {
print!("waiting {}\r", bio_ss.bio.rf(utra::bio_bdma::SFR_FLEVEL_PCLK_REGFIFO_LEVEL1));
// wait
}
let mut results = [0u32; 7];
for d in results.iter_mut() {
*d = bio_ss.bio.r(utra::bio_bdma::SFR_RXF1) & 0x3FFF_FFFF;
}
for (i, r) in results.iter().enumerate() {
print!("{}: {} cycles\r", i, r);
}
assert!(results[1] - results[0] == 3);
assert!(results[2] - results[1] == 3);
assert!(results[3] - results[2] == 6);
assert!(results[4] - results[3] == 3);
assert!(results[6] - results[5] == 10); // related to the clock divider
print!("===ACLK test PASS===\r");
}
#[rustfmt::skip]
bio_code!(aclk_code, ACLK_START, ACLK_END,
"j 90f",
"nop",
"j 91f",
"nop",
"j 92f",
"nop",
"j 93f",
"nop",
// mach 0 code
"90:",
"j 90b",
// mach 1 code
"91:",
"mv x17, x31",
"mv x17, x31",
"mv x17, x31",
"nop",
"mv x17, x31",
"mv x17, x31",
"mv x20, x0",
"mv x17, x31",
"mv x20, x0",
"mv x17, x31",
"40:",
"j 40b",
// mach 2 code
"92:",
"j 92b",
// mach 3 code
"93:",
"j 93b"
);

View file

@ -0,0 +1,350 @@
use core::fmt::Error;
use crate::*;
pub const MAX_TX_LEN: usize = 126;
pub const MAX_RX_LEN: usize = 127;
const FIFO_HIGH_WATER_MARK: u32 = 7;
pub struct BioI2C<'a> {
pin_sda: u8,
pin_scl: u8,
irq_no: u8,
bio_ss: &'a mut BioSharedState,
}
impl<'a> BioI2C<'a> {
/// This is unsafe because it will clear all of the memory and state in the BIO.
/// If a cooperative machine is required a new routine needs to be coded.
///
/// The routine requires the user to specify an IRQ channel so it knows where to poll,
/// but it does not install an interrupt handler.
///
/// An interrupt-driven version of the routine would need to enable interrupts from e.g
/// IRQARRAY18
pub unsafe fn new_exclusive(
bio_ss: &'a mut BioSharedState,
sda: u8,
scl: u8,
irq_no: u8,
hs: bool,
) -> Option<Self> {
if sda > 31 || scl > 31 {
return None;
}
if irq_no > 3 {
return None;
}
// stop all the machines, so that code can be loaded
bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0x0);
let code = i2c_driver();
print!("I2C code length: {} bytes\r", code.len());
bio_ss.load_code(code, 0);
if hs {
// 1600kHz clock -> 400kHz toggle rate = 0x1F4_0000 @ 800MHz rate FCLK
bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV0, 0x1F4_0000);
} else {
// 400kHz clock -> 100kHz toggle rate = 0x7D0_0000 @ 800MHz rate FCLK
bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV0, 0x7D0_0000);
}
// clear all events
bio_ss.bio.wfo(utra::bio_bdma::SFR_EVENT_CLR_SFR_EVENT_CLR, 0xFFFF_FF);
// reset all the fifos
bio_ss.bio.wo(utra::bio_bdma::SFR_FIFO_CLR, 0xF);
// start core 0
bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0x111);
// don't snap GPIO outputs
bio_ss.bio.wo(utra::bio_bdma::SFR_CONFIG, 0);
// configure interrupts
// T/RX2 is the bank of interest for triggering interrupts
bio_ss.bio.wfo(utra::bio_bdma::SFR_ELEVEL_FIFO_EVENT_LEVEL4, 1); // corresponds to T/RXF2
bio_ss.bio.wfo(utra::bio_bdma::SFR_ETYPE_FIFO_EVENT_EQ_MASK, 0b00_01_00_00); // level4 mask EQ
bio_ss.bio.rmwf(utra::bio_bdma::SFR_ETYPE_FIFO_EVENT_GT_MASK, 0b00_01_00_00); // level4 mask GT
bio_ss.bio.wo(utra::bio_bdma::SFR_IRQMASK_0, 1 << (28 + irq_no) as u32);
Some(BioI2C { pin_sda: sda, pin_scl: scl, irq_no, bio_ss })
}
/// This will block until the full transaction completes. On Xous, `do_yield` can be
/// specified which causes the process to yield its quantum during the wait loop so that
/// other processes can make progress.
///
/// `dev_addr` is the 7-bit version of the target address (i.e. shifted without the r/w bit)
///
/// Note that this can easily be made multi-bank/multi-I2C interface by simply making the SCL/SDA
/// pin mappings an argument to this function. Every transaction can use a different GPIO mapping,
/// so we could potentially have up to 16 free-from I2C units controlled with a single code block.
pub fn txrx_blocking(
&mut self,
dev_addr: u8,
tx: &[u8],
rx: Option<&mut [u8]>,
do_yield: bool,
) -> Result<(), Error> {
// 1 is read, 0 is write
if tx.len() > 0 {
let wr_byte = (dev_addr << 1 | 0x0) as u8;
let i2c_cmd = (self.pin_sda as u32) << 27
| (self.pin_scl as u32) << 22
| (tx.len() as u32 + 1).min(MAX_TX_LEN as u32) << 8
| 0 << 15
| wr_byte as u32;
// start the Tx
self.bio_ss.bio.wo(utra::bio_bdma::SFR_TXF0, i2c_cmd);
// write the bytes to send
for &data in tx.iter() {
while self.bio_ss.bio.rf(utra::bio_bdma::SFR_FLEVEL_PCLK_REGFIFO_LEVEL1)
> FIFO_HIGH_WATER_MARK
{
if do_yield {
#[cfg(feature = "std")]
xous::yield_slice();
}
}
self.bio_ss.bio.wfo(utra::bio_bdma::SFR_TXF1_FDIN, data as u32);
}
// wait for done
while self.bio_ss.bio.r(utra::bio_bdma::SFR_EVENT_STATUS) & (1 << self.irq_no + 28) == 0 {
if do_yield {
#[cfg(feature = "std")]
xous::yield_slice();
}
}
let result_code = self.bio_ss.bio.r(utra::bio_bdma::SFR_RXF2);
if result_code != (tx.len() as u32 + 1) {
#[cfg(not(feature = "tests"))]
return Err(Error);
}
#[cfg(feature = "tests")]
{
if result_code == (tx.len() as u32 + 1) {
print!(" Tx pass, Result code: {:x}\r", result_code);
} else {
print!(" Tx fail, Result code: {:x}\r", result_code);
}
}
}
if let Some(rx) = rx {
if rx.len() > 0 {
let wr_byte = (dev_addr << 1 | 0x1) as u8;
let i2c_cmd = (self.pin_sda as u32) << 27
| (self.pin_scl as u32) << 22
| 1 << 8
| (rx.len() as u32) << 15
| wr_byte as u32;
// start the Rx
self.bio_ss.bio.wo(utra::bio_bdma::SFR_TXF0, i2c_cmd);
// read the data
for data in rx.iter_mut() {
while self.bio_ss.bio.rf(utra::bio_bdma::SFR_FLEVEL_PCLK_REGFIFO_LEVEL1) == 0 {
if do_yield {
#[cfg(feature = "std")]
xous::yield_slice();
}
}
*data = self.bio_ss.bio.rf(utra::bio_bdma::SFR_RXF1_FDOUT) as u8;
}
// wait for the status code
while self.bio_ss.bio.r(utra::bio_bdma::SFR_EVENT_STATUS) & (1 << self.irq_no + 28) == 0 {
if do_yield {
#[cfg(feature = "std")]
xous::yield_slice();
}
}
let result_code = self.bio_ss.bio.r(utra::bio_bdma::SFR_RXF2);
// should be 1 NACK on rx
if result_code != ((1 + rx.len() as u32 - 1) | 0x1_0000) {
#[cfg(not(feature = "tests"))]
return Err(Error);
}
#[cfg(feature = "tests")]
{
if result_code == ((1 + rx.len() as u32 - 1) | 0x1_0000) {
print!(" Rx pass, Result code: {:x}\r", result_code);
} else {
print!(" Rx fail, Result code: {:x}\r", result_code);
}
print!(" Rx data: {:x?}\r", rx);
}
}
}
Ok(())
}
}
// An I2C test that gives us coverage of GPIO direction control
// I2C is initiated by writing a 32-bit word to x16 (FIFO 0) that has the following format:
// bit[0..1] - r/w. R=1, W=0
// bit[1..8] - device address
// bit[8..15] - bytes to write (0-127); amount includes device addressing write
// bit[15..22] - bytes to read (0-127)
// bit[22..27] - I/O pin for SCL
// bit[27..32] - I/O pin for SDA
//
// Outgoing data is written into x17; incoming data is read from x17 (FIFO 1)
//
// Host determines if I2C is done by IRQ & value appearing on x18 (FIFO 2) which contains ack/nack status
// Format is:
// bit[0..9] - # of words successfully written or (read - 1)
// bit[16..25] - # of words NACK'd - if a read, the last word is always NACK so this should be 1
//
// Quantum is assumed to be 4x of final I2C clock rate
#[rustfmt::skip]
bio_code!(
i2c_driver,
I2C_DRIVER_START,
I2C_DRIVER_END,
"j 90f",
"nop",
"j 91f",
"nop",
"j 92f",
"nop",
"j 93f",
"nop",
"90:", // machine 0 code
"mv x1, x16", // x1 <- initiation command
"andi x2, x1, 1", // x2 <- r/w bit. write if 0.
"andi x3, x1, 0xff", // x3 <- word to send: initially, device address with r/w bit in place
"mv x9, x1", // x9 <- writes to transfer
"srli x9, x9, 8",
"andi x9, x9, 0x7f",
"mv x10, x1", // x10 <- reads to transfer
"srli x10, x10, 15",
"andi x10, x10, 0x7f",
"mv x15, x1", // x15 <- SCL pin temp
"srli x15, x15, 22",
"andi x15, x15, 0x1f",
"mv x14, x1", // x14 <- SDA pin temp
"srli x14, x14, 27",
"andi x14, x14, 0x1f",
"li x5, 1", // setup GPIO masks
"sll x5, x5, x15", // x5 <- SCL bitmask
"li x4, 1",
"sll x4, x4, x14", // x4 <- SDA bitmask
// setup gpios
"mv x20, x0", // sync to clock stream
"or x26, x4, x5", // set GPIO mask
"or x23, x4, x5", // clear SCL/SDA pins to 0 for "output low"
"or x25, x4, x5", // SCL/SDA to inputs
// setup done
"mv x20, x0", // sync to clock stream
"li x7, 0", // x7 <- ACK counter
"li x8, 0", // x8 <- NACK counter
// main loop
"26:",
// START bit
"mv x20, x0", // SCL HS
"mv x24, x4", // SDA <- 0 while SCL is high (start)
"mv x20, x0", // SCL HS+1 -- extra time for repeated start hold, some devices require this
"li x6, 8", // x6 <- set loop counter = 8
"mv x11, x0", // x11 is read shift register; initialize with 0
"23:",
"mv x20, x0", // SCL L0
"mv x24, x5", // SCL <- 0
"beq x9, x0, 30f", // go to 30f if read cycle
// write path
"mv x20, x0", // SCL L1
"andi x14, x3, 0x80",// x14 (temp) <- extract MSB for sending
"slli x3, x3, 1", // shift data for next iteration
"beq x14, x0, 20f", // 20f: send 0
"mv x25, x4", // not taken SDA <- 1
"j 21f", // go past send 0
"20:",
"mv x24, x4", // taken SDA <- 0
"21:",
"mv x20, x0", // SCL H0
"mv x25, x5", // SCL <- 1
"mv x20, x0", // SCL H1
"addi x6, x6, -1", // decrement bit counter
"bne x6, x0, 23b", // loop back if we haven't shifted all the bits
// incoming ACK
"mv x20, x0", // SCL L0
"mv x24, x5", // SCL <- 0
"mv x20, x0", // SCL L1
"mv x25, x4", // SDA is input
"mv x20, x0", // SCL H0
"mv x25, x5", // SCL <- 1
"mv x13, x21", // read GPIO pins
"and x13, x4, x13", // mask just SDA
"beq x13, x0, 24f", // if ack, go to 24f
"addi x8, x8, 1", // NACK += 1
"j 25f",
"24:",
"addi x7, x7, 1", // ACK += 1
"j 25f",
// read path
"30:",
"mv x25, x4", // SDA is input
"mv x20, x0", // SCL L1
"mv x20, x0", // SCL H0
"mv x25, x5", // SCL <- 1
"mv x20, x0", // SCL H1
"mv x14, x21", // x14 (temp) <- GPIO pins
"and x14, x14, x4", // mask just SDA
"slli x11, x11, 1", // x11 <<= x11
"beq x14, x0, 40f", // don't OR in a 1 if SDA is 0
"ori x11, x11, 1", // x11 <- x11 | 1
"40:",
"addi x6, x6, -1", // decrement bit counter
"bne x6, x0, 23b", // loop back if we haven't shifted all the bits
"mv x17, x11", // x17 <- read data
// outgoing ACK or NACK
"beq x10, x0, 28f", // if last iteration, do NACK
"mv x24, x4", // otherwise ACK by SDA as output
"addi x7, x7, 1", // ACK += 1
"j 29f",
"28:",
"mv x25, x4", // SDA is input (SDA goes high; NACK)
"addi x8, x8, 1", // NACK += 1
"29:",
"mv x20, x0", // SCL L0
"mv x24, x5", // SCL <- 0
"mv x20, x0", // SCL L1
"mv x20, x0", // SCL H0
"mv x25, x5", // SCL <- 1
"j 25f",
// loop prologue
"25:",
"mv x20, x0", // SCL H1
"beq x9, x0, 60f", // don't decrement past 0; go to read processing instead
"addi x9, x9, -1", // if not, decrement total writes to transfer
"beq x9, x0, 60f", // check if writes are exhausted, if so, go to read processing
"mv x3, x17", // x3 <- x17 fetch outgoing from FIFO. Transfer will halt here until byte is available.
"j 26b", // loop back to "DATA bits"
"60:",
"beq x10, x0, 50f", // if x10 is already 0, go to STOP
"addi x10, x10, -1", // decrement x10 (bytes to read)
"j 26b", // loop back to "DATA bits"
// STOP
"50:",
"mv x20, x0", // SCL L0
"mv x24, x5", // SCL <- 0
"mv x24, x4", // SDA <- 0
"mv x20, x0", // SCL L1
"mv x20, x0", // SCL H1
"mv x25, x5", // SCL is input (SCL -> 1)
"mv x20, x0", // SCL H2
"mv x25, x4", // SDA is input (SDA -> 1)
// REPORT
"slli x8, x8, 16", // shift NACK count into place
"add x18, x7, x8", // x18 <- send ACK + NACK
"j 90b", // wait for next command
// gutter unused machines
"91:",
"j 91b",
"92:",
"j 92b",
"93:",
"j 93b"
);

View file

@ -0,0 +1,175 @@
//! FIFO - 8-deep fifo head/tail access. Cores halt on overflow/underflow.
//! - x16 r/w fifo[0]
//! - x17 r/w fifo[1]
//! - x18 r/w fifo[2]
//! - x19 r/w fifo[3]
//!
//! Quantum - core will halt until host-configured clock divider pules occurs,
//! or an external event comes in on a host-specified GPIO pin.
//! - x20 -/w halt to quantum
//!
//! GPIO - note clear-on-0 semantics for bit-clear for data pins!
//! This is done so we can do a shift-and-move without an invert to
//! bitbang a data pin. Direction retains a more "conventional" meaning
//! where a write of `1` to either clear or set will cause the action,
//! as pin direction toggling is less likely to be in a tight inner loop.
//! - x21 r/w write: (x26 & x21) -> gpio pins; read: gpio pins -> x21
//! - x22 -/w (x26 & x22) -> `1` will set corresponding pin on gpio
//! - x23 -/w (x26 & x23) -> `0` will clear corresponding pin on gpio
//! - x24 -/w (x26 & x24) -> `1` will make corresponding gpio pin an output
//! - x25 -/w (x26 & x25) -> `1` will make corresponding gpio pin an input
//! - x26 r/w mask GPIO action outputs
//!
//! Events - operate on a shared event register. Bits [7:0] are hard-wired to FIFO
//! level flags, configured by the host; writes to bits [7:0] are ignored.
//! - x27 -/w mask event sensitivity bits
//! - x28 -/w `1` will set the corresponding event bit. Only [31:8] are wired up.
//! - x29 -/w `1` will clear the corresponding event bit Only [31:8] are wired up.
//! - x30 r/- halt until ((x27 & events) == x27), and return unmasked `events` value
//!
//! Core ID & debug:
//! - x31 r/- [31:30] -> core ID; [29:0] -> cpu clocks since reset
#![cfg_attr(feature = "baremetal", no_std)]
use utralib::generated::*;
#[cfg(feature = "tests")]
pub mod bio_tests;
pub mod i2c;
#[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_bdma::SFR_CFGINFO.offset() as u32);
#[cfg(feature = "tests")]
bio_tests::report_api(utra::bio_bdma::HW_BIO_BDMA_BASE as u32);
bio_ss.bio.r(utra::bio_bdma::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
}
/// used to generate some test vectors
pub fn lfsr_next_u32(state: u32) -> u32 {
let bit = ((state >> 31) ^ (state >> 21) ^ (state >> 1) ^ (state >> 0)) & 1;
(state << 1) + bit
}
pub struct BioSharedState {
pub bio: CSR<u32>,
pub imem_slice: &'static mut [u32],
}
impl BioSharedState {
#[cfg(feature = "baremetal")]
pub fn new() -> Self {
// 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,
)
};
BioSharedState { bio: CSR::new(utra::bio_bdma::HW_BIO_BDMA_BASE as *mut u32), imem_slice }
}
#[cfg(not(feature = "baremetal"))]
pub fn new() -> Self {
let csr = xous::syscall::map_memory(
xous::MemoryAddress::new(utra::bio_bdma::HW_BIO_BDMA_BASE),
None,
4096,
xous::MemoryFlags::R | xous::MemoryFlags::W,
)
.unwrap();
let imem: xous::MemoryRange = xous::syscall::map_memory(
xous::MemoryAddress::new(utra::bio_bdma::HW_BIO_RAM_MEM),
None,
utra::bio_bdma::HW_BIO_RAM_MEM_LEN,
xous::MemoryFlags::R | xous::MemoryFlags::W,
)
.unwrap();
BioSharedState {
bio: CSR::new(csr.as_mut_ptr() as *mut u32),
imem_slice: unsafe { imem.as_slice_mut() },
}
}
pub fn load_code(&mut self, prog: &[u8], offset_bytes: usize) {
let offset = offset_bytes / core::mem::size_of::<u32>();
for (i, chunk) in prog.chunks(4).enumerate() {
if chunk.len() == 4 {
let word: u32 = u32::from_le_bytes(chunk.try_into().unwrap());
self.imem_slice[i + offset] = word;
} else {
// copy the last word as a "ragged word"
let mut ragged_word = 0;
for (j, &b) in chunk.iter().enumerate() {
ragged_word |= (b as u32) << (4 - chunk.len() + j);
}
self.imem_slice[i + offset] = ragged_word;
}
}
}
}
#[macro_export]
/// This macro takes three identifiers and assembly code:
/// - name of the function to call to retrieve the assembled code
/// - a unique identifier that serves as label name for the start of the code
/// - a unique identifier that serves as label name for the end of the code
/// - a comma separated list of strings that form the assembly itself
///
/// *** The comma separated list must *not* end in a comma. ***
///
/// The macro is unable to derive names of functions or identifiers for labels
/// due to the partially hygienic macro rules of Rust, so you have to come
/// up with a list of unique names by yourself.
macro_rules! bio_code {
($fn_name:ident, $name_start:ident, $name_end:ident, $($item:expr),*) => {
pub fn $fn_name() -> &'static [u8] {
extern {
static $name_start: *const u8;
static $name_end: *const u8;
}
/*
unsafe {
report_api($name_start as u32);
report_api($name_end as u32);
}
*/
// skip the first 4 bytes, as they contain the loading offset
unsafe { core::slice::from_raw_parts($name_start.add(4), ($name_end as usize) - ($name_start as usize) - 4)}
}
core::arch::global_asm!(
".align 4",
concat!(".globl ", stringify!($name_start)),
concat!(stringify!($name_start), ":"),
".word .",
$($item),*
, ".align 4",
concat!(".globl ", stringify!($name_end)),
concat!(stringify!($name_end), ":"),
".word .",
);
};
}