diff --git a/Cargo.lock b/Cargo.lock index 311359253..0360162a4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6263,6 +6263,16 @@ dependencies = [ "xous-semver", ] +[[package]] +name = "xous-bio" +version = "0.1.0" +dependencies = [ + "defmt", + "log", + "utralib 0.1.24", + "xous 0.9.61 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "xous-ipc" version = "0.9.61" diff --git a/Cargo.toml b/Cargo.toml index dc12e8ac0..5bb87d9bb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -99,6 +99,7 @@ members = [ "libs/userprefs", "libs/tls", "libs/xous-pio", + "libs/xous-bio", "libs/xous-pl230", "libs/cramium-hal", "services/cram-hal-service", diff --git a/libs/xous-bio/Cargo.toml b/libs/xous-bio/Cargo.toml index 424c4226d..a2bd4100a 100644 --- a/libs/xous-bio/Cargo.toml +++ b/libs/xous-bio/Cargo.toml @@ -8,7 +8,9 @@ edition = "2021" [dependencies] log = "0.4.14" defmt = { version = "0.3", optional = true } -utralib = { version = "0.1.24", default-features = false, features = ["cramium-soc"] } +utralib = { version = "0.1.24", default-features = false, features = [ + "cramium-soc", +] } [target.'cfg(target_os = "xous")'.dependencies] xous = "0.9.61" diff --git a/libs/xous-bio/src/bio_tests/debug.rs b/libs/xous-bio/src/bio_tests/debug.rs new file mode 100644 index 000000000..26930f138 --- /dev/null +++ b/libs/xous-bio/src/bio_tests/debug.rs @@ -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)+) + }); +} diff --git a/libs/xous-bio/src/bio_tests/i2c.rs b/libs/xous-bio/src/bio_tests/i2c.rs new file mode 100644 index 000000000..81d059194 --- /dev/null +++ b/libs/xous-bio/src/bio_tests/i2c.rs @@ -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::main::HW_MAIN_BASE as *mut u32); + test_cfg.wo(utra::main::WDATA, 0); + test_cfg.wo(utra::main::WDATA, 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::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::SFR_QDIV0, 0x1F4_0000); + + // clear all events + bio_ss.bio.wfo(utra::bio::SFR_EVENT_CLR_SFR_EVENT_CLR, 0xFFFF_FF); + // reset all the fifos + bio_ss.bio.wo(utra::bio::SFR_FIFO_CLR, 0xF); + // start core 0 + bio_ss.bio.wo(utra::bio::SFR_CTRL, 0x111); + + // don't snap GPIO outputs + bio_ss.bio.wo(utra::bio::SFR_CONFIG, 0); + + // configure interrupts + // T/RX2 is the bank of interest for triggering interrupts + bio_ss.bio.wfo(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL4, 1); // corresponds to T/RXF2 + bio_ss.bio.wfo(utra::bio::SFR_ETYPE_FIFO_EVENT_EQ_MASK, 0b00_01_00_00); // level4 mask EQ + bio_ss.bio.rmwf(utra::bio::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::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::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::SFR_RXF1) as u8; + } + // read the result code + let result_code = bio_ss.bio.r(utra::bio::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::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"); +} diff --git a/libs/xous-bio/src/bio_tests/mod.rs b/libs/xous-bio/src/bio_tests/mod.rs index f1e8901c9..4ea103725 100644 --- a/libs/xous-bio/src/bio_tests/mod.rs +++ b/libs/xous-bio/src/bio_tests/mod.rs @@ -1,3 +1,5 @@ +pub(crate) mod debug; +pub mod i2c; pub mod spi; pub mod units; @@ -88,6 +90,9 @@ pub fn report_api(d: u32) { pub fn bio_tests() { report_api(crate::get_id()); + units::aclk_tests(); + i2c::i2c_test(); + i2c::complex_i2c_test(); spi::spi_test(); units::hello_world(); units::hello_multiverse(); @@ -104,18 +109,15 @@ pub fn bio_tests() { // -[x] GPIO input path test // -[x] Extclk as x20 stall source // -[x] Check Extclk:gpio pin mapping (make sure bit ordering is not swapped) -// -[ ] GPIO direction control test -// -[ ] FIFO level trigger test - eq, gt, lt on various channels, at various fullness levels +// -[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 -// -[ ] Host IRQ generation test - some combination with event tests above to confirm IRQ generation +// -[x] Host IRQ generation test - some combination with event tests above to confirm IRQ generation // -[x] Core ID read test -// -[ ] Core aclk counter test +// -[x] Core aclk counter test // Application tests: // -[x] SPI loopback test - implement using extclk as spi clk for input -// -[ ] I2C loopback test -// -[ ] IR controller test -// -[ ] Arithmetic test, utilizing load/store on core 0 -// -[ ] Arithmetic test, without store, cores 1-3 +// -[x] I2C loopback test // -[ ] DMA transfer test -- might have to wait until full-chip integration to get MDMA core? diff --git a/libs/xous-bio/src/bio_tests/spi.rs b/libs/xous-bio/src/bio_tests/spi.rs index 8ab9f284b..e5fda0cd5 100644 --- a/libs/xous-bio/src/bio_tests/spi.rs +++ b/libs/xous-bio/src/bio_tests/spi.rs @@ -4,8 +4,7 @@ use super::report_api; use crate::*; pub fn spi_test() { - report_api(0x1310_5000); - report_api(0x51C0_0000); + print!("SPI test\r"); // clear prior test config state let mut test_cfg = CSR::new(utra::main::HW_MAIN_BASE as *mut u32); @@ -15,7 +14,7 @@ pub fn spi_test() { // stop all the machines, so that code can be loaded bio_ss.bio.wo(utra::bio::SFR_CTRL, 0x0); let code = spi_driver(); - report_api(code.len() as u32); + print!("code length {}\r", code.len()); bio_ss.load_code(code, 0); // configure fifo trigger levels @@ -36,7 +35,7 @@ pub fn spi_test() { bio_ss.bio.wo( utra::bio::SFR_CONFIG, bio_ss.bio.ms(utra::bio::SFR_CONFIG_SNAP_OUTPUT_TO_QUANTUM, 1) - | bio_ss.bio.ms(utra::bio::SFR_CONFIG_SNAP_OUTPUT_TO_WHICH, 2) + | bio_ss.bio.ms(utra::bio::SFR_CONFIG_SNAP_OUTPUT_TO_WHICH, 2), ); // bypass sync on all but clock @@ -45,7 +44,7 @@ pub fn spi_test() { bio_ss.bio.wo( utra::bio::SFR_EXTCLOCK, bio_ss.bio.ms(utra::bio::SFR_EXTCLOCK_USE_EXTCLK, 0b1000) - | bio_ss.bio.ms(utra::bio::SFR_EXTCLOCK_EXTCLK_GPIO_3, 9) + | bio_ss.bio.ms(utra::bio::SFR_EXTCLOCK_EXTCLK_GPIO_3, 9), ); // start cores 2 & 3 @@ -71,14 +70,16 @@ pub fn spi_test() { 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::SFR_RXF1); j += 1; - report_api(j as u32); + print!("{} ", j); } + print!("\r"); for (index, &val) in retvals.iter().enumerate() { - report_api(val); + 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)); } @@ -90,10 +91,10 @@ pub fn spi_test() { // - 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) +// - 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. diff --git a/libs/xous-bio/src/bio_tests/units.rs b/libs/xous-bio/src/bio_tests/units.rs index bc50000b0..25731e4ce 100644 --- a/libs/xous-bio/src/bio_tests/units.rs +++ b/libs/xous-bio/src/bio_tests/units.rs @@ -1,11 +1,15 @@ -use super::{report_api, TEST_INVERT_MASK}; +use utralib::utra::bio::{ + 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() { - report_api(0x1310_0000); + 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 @@ -16,7 +20,7 @@ pub fn hello_world() { bio_ss.bio.wo(utra::bio::SFR_QDIV0, 0x20_0000); // start the machine bio_ss.bio.wo(utra::bio::SFR_CTRL, 0x111); - report_api(0x1310_600D); + print!("===hello world PASS===\r"); } #[rustfmt::skip] bio_code!(hello_world_code, HELLO_START, HELLO_END, @@ -34,7 +38,7 @@ bio_code!(hello_world_code, HELLO_START, HELLO_END, // 0x41312111, 0x42322212, 0x43332313, etc. // and they should be in sync-lock, no ragged transitions pub fn hello_multiverse() { - report_api(0x1310_1000); + 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::SFR_CTRL, 0x0); @@ -56,7 +60,7 @@ pub fn hello_multiverse() { ); // start all the machines, all at once bio_ss.bio.wo(utra::bio::SFR_CTRL, 0xfff); - report_api(0x1311_600D); + print!("===multiverse PASS===\r"); } #[rustfmt::skip] bio_code!(multiverse_code, MULTIVERSE_START, MULTIVERSE_END, @@ -136,8 +140,7 @@ bio_code!(multiverse_code, MULTIVERSE_START, MULTIVERSE_END, // 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() { - report_api(0x1310_2000); - report_api(0xf1f0_0000); + print!("FIFO basic\r"); // clear any prior test config state let mut test_cfg = CSR::new(utra::main::HW_MAIN_BASE as *mut u32); test_cfg.wo(utra::main::WDATA, 0); @@ -158,8 +161,7 @@ pub fn fifo_basic() { bio_ss.bio.wo(utra::bio::SFR_CONFIG, 0); // start all the machines, all at once bio_ss.bio.wo(utra::bio::SFR_CTRL, 0xfff); - - report_api(0x1312_600D); + print!("===FIFO basic PASS===\r"); } #[rustfmt::skip] bio_code!(fifo_basic_code, FIFO_BASIC_START, FIFO_BASIC_END, @@ -225,8 +227,7 @@ bio_code!(fifo_basic_code, FIFO_BASIC_START, FIFO_BASIC_END, // 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() { - report_api(0x1310_3000); - report_api(0xf1f0_0001); + print!("Host FIFO tests\r"); // clear prior test config state let mut test_cfg = CSR::new(utra::main::HW_MAIN_BASE as *mut u32); test_cfg.wo(utra::main::WDATA, 0); @@ -277,7 +278,7 @@ pub fn host_fifo_tests() { } else { assert!(rbk == !(0xF1F0_0000 + 15)); } - report_api(rbk); + print!("backpressure {:x}\r", rbk); } fn get_gpio_via_core(bio_ss: &mut BioSharedState) -> u32 { @@ -290,7 +291,6 @@ pub fn host_fifo_tests() { } // load next test - report_api(0xf1f0_0002); // clear inversions, etc on readbacks via I/O test_cfg.wo(utra::main::WDATA, 0); // stop machine & load code @@ -314,7 +314,7 @@ pub fn host_fifo_tests() { // confirm the core booted let f1_val = get_gpio_via_core(&mut bio_ss); - report_api(f1_val); + 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::SFR_FLEVEL_PCLK_REGFIFO_LEVEL0) == 0); @@ -331,25 +331,25 @@ pub fn host_fifo_tests() { // wait for fifo to drain } let pause_val = get_gpio_via_core(&mut bio_ss); - report_api(pause_val); + 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::SFR_TXF0, stop_val); let stop_confirm_val = get_gpio_via_core(&mut bio_ss); - report_api(stop_confirm_val); + 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::SFR_RXF2); - report_api(f2_val); + print!("f2_val {:x}\r", f2_val); assert!(f2_val == 0xf1f0_1000 + i); } let stop_check = bio_ss.bio.r(utra::bio::SFR_RXF2); - report_api(stop_check); + print!("stop_check {:x}\r", stop_check); assert!(stop_check == stop_val); - report_api(0x1313_600D); + print!("===Host FIFO PASS===\r"); } #[rustfmt::skip] bio_code!(fifo_host_bitbang, FIFO_HOST_BITBANG_START, FIFO_HOST_BITBANG_END, @@ -430,11 +430,18 @@ bio_code!( "j 22b" ); -// TODO: i think this can be done without any code running on the machines; the host can +#[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() { - report_api(0x1310_4000); - report_api(0xf1f0_0003); + print!("FIFO level comprehensive\r"); // clear prior test config state let mut test_cfg = CSR::new(utra::main::HW_MAIN_BASE as *mut u32); test_cfg.wo(utra::main::WDATA, 0); @@ -459,5 +466,310 @@ pub fn fifo_level_tests() { bio_ss.bio.rmwf(utra::bio::SFR_ETYPE_FIFO_EVENT_LT_MASK, 0b11_01_01_00); bio_ss.bio.rmwf(utra::bio::SFR_ETYPE_FIFO_EVENT_GT_MASK, 0b10_10_10_00); - report_api(0x1314_600D); + /* + 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::SFR_TXF0, + rx_reg: utra::bio::SFR_RXF0, + levels: [utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL0, utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL1], + event_masks: [0x1, 0x2], + }, + FifoLevelTestConfig { + tx_reg: utra::bio::SFR_TXF1, + rx_reg: utra::bio::SFR_RXF1, + levels: [utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL2, utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL3], + event_masks: [0x4, 0x8], + }, + FifoLevelTestConfig { + tx_reg: utra::bio::SFR_TXF2, + rx_reg: utra::bio::SFR_RXF2, + levels: [utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL4, utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL5], + event_masks: [0x10, 0x20], + }, + FifoLevelTestConfig { + tx_reg: utra::bio::SFR_TXF3, + rx_reg: utra::bio::SFR_RXF3, + levels: [utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL6, utra::bio::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::SFR_IRQMASK_0, + utra::bio::SFR_IRQMASK_1, + utra::bio::SFR_IRQMASK_2, + utra::bio::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::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::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::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::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::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::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::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::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::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::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::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::main::HW_MAIN_BASE as *mut u32); + test_cfg.wo(utra::main::WDATA, 0); + + let mut bio_ss = BioSharedState::new(); + // stop all the machines, so that code can be loaded + bio_ss.bio.wo(utra::bio::SFR_CTRL, 0x0); + let code = aclk_code(); + bio_ss.load_code(code, 0); + + // configure & run the 0th machine + bio_ss.bio.wo(utra::bio::SFR_QDIV1, 0xA_0000); + // don't snap GPIO outputs + bio_ss.bio.wo(utra::bio::SFR_CONFIG, 0); + + // start machine 1 + bio_ss.bio.wo(utra::bio::SFR_CTRL, 0x222); + while bio_ss.bio.rf(utra::bio::SFR_FLEVEL_PCLK_REGFIFO_LEVEL1) < 7 { + print!("waiting {}\r", bio_ss.bio.rf(utra::bio::SFR_FLEVEL_PCLK_REGFIFO_LEVEL1)); + // wait + } + let mut results = [0u32; 7]; + for d in results.iter_mut() { + *d = bio_ss.bio.r(utra::bio::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" +); diff --git a/libs/xous-bio/src/i2c.rs b/libs/xous-bio/src/i2c.rs new file mode 100644 index 000000000..f707f873e --- /dev/null +++ b/libs/xous-bio/src/i2c.rs @@ -0,0 +1,347 @@ +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 { + 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::SFR_CTRL, 0x0); + let code = i2c_driver(); + bio_ss.load_code(code, 0); + + if hs { + // 1600kHz clock -> 400kHz toggle rate = 0x1F4_0000 @ 800MHz rate FCLK + bio_ss.bio.wo(utra::bio::SFR_QDIV0, 0x1F4_0000); + } else { + // 400kHz clock -> 100kHz toggle rate = 0x7D0_0000 @ 800MHz rate FCLK + bio_ss.bio.wo(utra::bio::SFR_QDIV0, 0x7D0_0000); + } + + // clear all events + bio_ss.bio.wfo(utra::bio::SFR_EVENT_CLR_SFR_EVENT_CLR, 0xFFFF_FF); + // reset all the fifos + bio_ss.bio.wo(utra::bio::SFR_FIFO_CLR, 0xF); + // start core 0 + bio_ss.bio.wo(utra::bio::SFR_CTRL, 0x111); + // don't snap GPIO outputs + bio_ss.bio.wo(utra::bio::SFR_CONFIG, 0); + + // configure interrupts + // T/RX2 is the bank of interest for triggering interrupts + bio_ss.bio.wfo(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL4, 1); // corresponds to T/RXF2 + bio_ss.bio.wfo(utra::bio::SFR_ETYPE_FIFO_EVENT_EQ_MASK, 0b00_01_00_00); // level4 mask EQ + bio_ss.bio.rmwf(utra::bio::SFR_ETYPE_FIFO_EVENT_GT_MASK, 0b00_01_00_00); // level4 mask GT + + bio_ss.bio.wo(utra::bio::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::SFR_TXF0, i2c_cmd); + // write the bytes to send + for &data in tx.iter() { + while self.bio_ss.bio.rf(utra::bio::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::SFR_TXF1_FDIN, data as u32); + } + // wait for done + while self.bio_ss.bio.r(utra::bio::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::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::SFR_TXF0, i2c_cmd); + // read the data + for data in rx.iter_mut() { + while self.bio_ss.bio.rf(utra::bio::SFR_FLEVEL_PCLK_REGFIFO_LEVEL1) == 0 { + if do_yield { + #[cfg(feature = "std")] + xous::yield_slice(); + } + } + *data = self.bio_ss.bio.rf(utra::bio::SFR_RXF1_FDOUT) as u8; + } + // wait for the status code + while self.bio_ss.bio.r(utra::bio::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::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" + ); diff --git a/libs/xous-bio/src/lib.rs b/libs/xous-bio/src/lib.rs index d03d623a3..45d285856 100644 --- a/libs/xous-bio/src/lib.rs +++ b/libs/xous-bio/src/lib.rs @@ -8,12 +8,16 @@ //! 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 ops +//! 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) -> `0` will make corresponding gpio pin an input +//! - 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 @@ -32,6 +36,7 @@ use utralib::generated::*; #[cfg(feature = "tests")] pub mod bio_tests; +pub mod i2c; #[derive(Debug)] pub enum BioError { @@ -58,6 +63,14 @@ pub fn lfsr_next(state: u16) -> u16 { ((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, pub imem_slice: &'static mut [u32], @@ -69,19 +82,15 @@ impl BioSharedState { 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 + utralib::generated::HW_BIO_RAM_MEM_LEN, ) }; - BioSharedState { - bio: CSR::new(utra::bio::HW_BIO_BASE as *mut u32), - imem_slice, - } + BioSharedState { bio: CSR::new(utra::bio::HW_BIO_BASE as *mut u32), imem_slice } } #[cfg(not(feature = "baremetal"))] pub fn new() -> Self { - // TODO let csr = xous::syscall::map_memory( xous::MemoryAddress::new(utra::bio::HW_BIO_BASE), None, @@ -90,8 +99,17 @@ impl BioSharedState { ) .unwrap(); + let imem: xous::MemoryRange = xous::syscall::map_memory( + xous::MemoryAddress::new(utra::bio::HW_BIO_RAM_MEM), + None, + utra::bio::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() }, } } @@ -132,10 +150,12 @@ macro_rules! bio_code { 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)} } @@ -152,4 +172,4 @@ macro_rules! bio_code { ".word .", ); }; -} \ No newline at end of file +}