From 62b2567ade2fa0e4e3d0fbdbf04e44e2731e121a Mon Sep 17 00:00:00 2001 From: bunnie Date: Wed, 20 Mar 2024 13:41:40 +0800 Subject: [PATCH 01/16] add comprehensive host fifo test --- libs/xous-bio/src/bio_tests/mod.rs | 6 +- libs/xous-bio/src/bio_tests/units.rs | 238 +++++++++++++++++++++++++++ libs/xous-bio/src/lib.rs | 8 + 3 files changed, 249 insertions(+), 3 deletions(-) diff --git a/libs/xous-bio/src/bio_tests/mod.rs b/libs/xous-bio/src/bio_tests/mod.rs index f1e8901c9..10b13edb8 100644 --- a/libs/xous-bio/src/bio_tests/mod.rs +++ b/libs/xous-bio/src/bio_tests/mod.rs @@ -88,12 +88,12 @@ pub fn report_api(d: u32) { pub fn bio_tests() { report_api(crate::get_id()); + units::fifo_level_tests(); spi::spi_test(); units::hello_world(); units::hello_multiverse(); units::fifo_basic(); units::host_fifo_tests(); - units::fifo_level_tests(); } // Test plan: @@ -105,11 +105,11 @@ pub fn bio_tests() { // -[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] 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 // Application tests: diff --git a/libs/xous-bio/src/bio_tests/units.rs b/libs/xous-bio/src/bio_tests/units.rs index bc50000b0..7c496dac8 100644 --- a/libs/xous-bio/src/bio_tests/units.rs +++ b/libs/xous-bio/src/bio_tests/units.rs @@ -1,3 +1,5 @@ +use utralib::utra::bio::{SFR_ETYPE_FIFO_EVENT_EQ_MASK, SFR_ETYPE_FIFO_EVENT_GT_MASK, SFR_ETYPE_FIFO_EVENT_LT_MASK}; + use super::{report_api, TEST_INVERT_MASK}; use crate::*; @@ -430,6 +432,14 @@ bio_code!( "j 22b" ); +#[derive(Clone, Copy)] +struct FifoLevelTestConfig { + tx_reg: crate::Register, + rx_reg: crate::Register, + levels: [crate::Field; 2], + event_masks: [u32; 2], +} + // TODO: i think 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() { @@ -459,5 +469,233 @@ 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); + /* + 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; + report_api(0xf1f0_f1f0 + 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 { + report_api(0xf1f0_4000 + test_level + ((bank as u32) << 8)); + // 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); + } + } report_api(0x1314_600D); } diff --git a/libs/xous-bio/src/lib.rs b/libs/xous-bio/src/lib.rs index d03d623a3..0b08994f2 100644 --- a/libs/xous-bio/src/lib.rs +++ b/libs/xous-bio/src/lib.rs @@ -58,6 +58,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], From ccc342d9f3d3cda95d8d8dd2c99a6aa369cee3ac Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 1 Jun 2024 21:53:54 +0800 Subject: [PATCH 02/16] add debug pretty-printing to test --- libs/xous-bio/src/bio_tests/debug.rs | 49 ++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 libs/xous-bio/src/bio_tests/debug.rs 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)+) + }); +} From e0f3ee8e4947568628f0c473ece2a18834a5e0c6 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 1 Jun 2024 21:58:05 +0800 Subject: [PATCH 03/16] pretty-print spi --- libs/xous-bio/src/bio_tests/spi.rs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/libs/xous-bio/src/bio_tests/spi.rs b/libs/xous-bio/src/bio_tests/spi.rs index 8ab9f284b..452ff4e6b 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 @@ -75,10 +74,10 @@ pub fn spi_test() { 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!("iter {}", j); } for (index, &val) in retvals.iter().enumerate() { - report_api(val); + print!("SPI rbk {:x}", 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 +89,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. From 24bd9da6ab13bbf217b773a63524c648fd6040f4 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 1 Jun 2024 21:58:18 +0800 Subject: [PATCH 04/16] pretty-print unit tests --- libs/xous-bio/src/bio_tests/units.rs | 52 ++++++++++++++-------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/libs/xous-bio/src/bio_tests/units.rs b/libs/xous-bio/src/bio_tests/units.rs index 7c496dac8..03c4b619a 100644 --- a/libs/xous-bio/src/bio_tests/units.rs +++ b/libs/xous-bio/src/bio_tests/units.rs @@ -1,4 +1,6 @@ -use utralib::utra::bio::{SFR_ETYPE_FIFO_EVENT_EQ_MASK, SFR_ETYPE_FIFO_EVENT_GT_MASK, SFR_ETYPE_FIFO_EVENT_LT_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::{report_api, TEST_INVERT_MASK}; use crate::*; @@ -7,7 +9,7 @@ use crate::*; // 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 @@ -18,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, @@ -36,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); @@ -58,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, @@ -138,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); @@ -160,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, @@ -227,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); @@ -279,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 { @@ -292,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 @@ -316,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); @@ -333,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, @@ -440,11 +438,10 @@ struct FifoLevelTestConfig { event_masks: [u32; 2], } -// TODO: i think this can be done without any code running on the machines; the host can +// 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); @@ -506,14 +503,17 @@ pub fn fifo_level_tests() { 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 + 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; - report_api(0xf1f0_f1f0 + irq_mask); + 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 @@ -522,7 +522,7 @@ pub fn fifo_level_tests() { // reset all the fifos bio_ss.bio.wo(utra::bio::SFR_FIFO_CLR, 0xF); for test_level in 0..FIFO_MAX { - report_api(0xf1f0_4000 + test_level + ((bank as u32) << 8)); + 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); @@ -697,5 +697,5 @@ pub fn fifo_level_tests() { bio_ss.bio.wo(irq_mask_reg, 0); } } - report_api(0x1314_600D); + print!("===FIFO level comprehensive PASS===\r") } From c113c4e9bd910ee613488da762cb979e80431eb9 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 1 Jun 2024 22:00:07 +0800 Subject: [PATCH 05/16] incorporate i2c module, pretty prints --- libs/xous-bio/Cargo.toml | 4 +++- libs/xous-bio/src/bio_tests/i2c.rs | 5 +++++ libs/xous-bio/src/bio_tests/mod.rs | 3 +++ libs/xous-bio/src/lib.rs | 14 ++++---------- 4 files changed, 15 insertions(+), 11 deletions(-) create mode 100644 libs/xous-bio/src/bio_tests/i2c.rs 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/i2c.rs b/libs/xous-bio/src/bio_tests/i2c.rs new file mode 100644 index 000000000..c9ee1d54d --- /dev/null +++ b/libs/xous-bio/src/bio_tests/i2c.rs @@ -0,0 +1,5 @@ +use crate::*; + +pub fn i2c_test() { + print!("I2C tests\r"); +} diff --git a/libs/xous-bio/src/bio_tests/mod.rs b/libs/xous-bio/src/bio_tests/mod.rs index 10b13edb8..a1c545d0f 100644 --- a/libs/xous-bio/src/bio_tests/mod.rs +++ b/libs/xous-bio/src/bio_tests/mod.rs @@ -1,3 +1,5 @@ +mod debug; +pub mod i2c; pub mod spi; pub mod units; @@ -88,6 +90,7 @@ pub fn report_api(d: u32) { pub fn bio_tests() { report_api(crate::get_id()); + i2c::i2c_test(); units::fifo_level_tests(); spi::spi_test(); units::hello_world(); diff --git a/libs/xous-bio/src/lib.rs b/libs/xous-bio/src/lib.rs index 0b08994f2..3db770741 100644 --- a/libs/xous-bio/src/lib.rs +++ b/libs/xous-bio/src/lib.rs @@ -32,7 +32,6 @@ use utralib::generated::*; #[cfg(feature = "tests")] pub mod bio_tests; - #[derive(Debug)] pub enum BioError { /// specified state machine is not valid @@ -77,14 +76,11 @@ 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"))] @@ -98,9 +94,7 @@ impl BioSharedState { ) .unwrap(); - BioSharedState { - bio: CSR::new(csr.as_mut_ptr() as *mut u32), - } + BioSharedState { bio: CSR::new(csr.as_mut_ptr() as *mut u32) } } pub fn load_code(&mut self, prog: &[u8], offset_bytes: usize) { @@ -160,4 +154,4 @@ macro_rules! bio_code { ".word .", ); }; -} \ No newline at end of file +} From 41e636bb8ca8deba65026dc725fe9819eea6e1e6 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 1 Jun 2024 22:00:30 +0800 Subject: [PATCH 06/16] fix formatting --- libs/xous-bio/src/bio_tests/spi.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/libs/xous-bio/src/bio_tests/spi.rs b/libs/xous-bio/src/bio_tests/spi.rs index 452ff4e6b..e5fda0cd5 100644 --- a/libs/xous-bio/src/bio_tests/spi.rs +++ b/libs/xous-bio/src/bio_tests/spi.rs @@ -70,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; - print!("iter {}", j); + print!("{} ", j); } + print!("\r"); for (index, &val) in retvals.iter().enumerate() { - print!("SPI rbk {:x}", 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)); } From 9fe1933611000fb45054cfdeb136875948e60856 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 2 Jun 2024 02:39:06 +0800 Subject: [PATCH 07/16] minor cleanup --- libs/xous-bio/src/bio_tests/mod.rs | 2 +- libs/xous-bio/src/bio_tests/units.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/xous-bio/src/bio_tests/mod.rs b/libs/xous-bio/src/bio_tests/mod.rs index a1c545d0f..db133231d 100644 --- a/libs/xous-bio/src/bio_tests/mod.rs +++ b/libs/xous-bio/src/bio_tests/mod.rs @@ -118,7 +118,7 @@ pub fn bio_tests() { // Application tests: // -[x] SPI loopback test - implement using extclk as spi clk for input // -[ ] I2C loopback test -// -[ ] IR controller test +// -[-] (cancelled) IR controller test // -[ ] Arithmetic test, utilizing load/store on core 0 // -[ ] Arithmetic test, without store, cores 1-3 // -[ ] DMA transfer test -- might have to wait until full-chip integration to get MDMA core? diff --git a/libs/xous-bio/src/bio_tests/units.rs b/libs/xous-bio/src/bio_tests/units.rs index 03c4b619a..90f525933 100644 --- a/libs/xous-bio/src/bio_tests/units.rs +++ b/libs/xous-bio/src/bio_tests/units.rs @@ -2,7 +2,7 @@ use utralib::utra::bio::{ SFR_ETYPE_FIFO_EVENT_EQ_MASK, SFR_ETYPE_FIFO_EVENT_GT_MASK, SFR_ETYPE_FIFO_EVENT_LT_MASK, }; -use super::{report_api, TEST_INVERT_MASK}; +use super::TEST_INVERT_MASK; use crate::*; // this test requires manual inspection of the outputs From 23f2b117aba2495cda43d55813d1b3aa1ca8e8e1 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 2 Jun 2024 02:39:30 +0800 Subject: [PATCH 08/16] dont use report_api inside the generation macro - just for debugging --- libs/xous-bio/src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libs/xous-bio/src/lib.rs b/libs/xous-bio/src/lib.rs index 3db770741..7ebec518e 100644 --- a/libs/xous-bio/src/lib.rs +++ b/libs/xous-bio/src/lib.rs @@ -134,10 +134,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)} } From 935857a675675164ee18afad1d47a237ace94a8d Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 2 Jun 2024 02:39:45 +0800 Subject: [PATCH 09/16] add basic i2c concept --- libs/xous-bio/src/bio_tests/i2c.rs | 238 +++++++++++++++++++++++++++++ 1 file changed, 238 insertions(+) diff --git a/libs/xous-bio/src/bio_tests/i2c.rs b/libs/xous-bio/src/bio_tests/i2c.rs index c9ee1d54d..46d48d44c 100644 --- a/libs/xous-bio/src/bio_tests/i2c.rs +++ b/libs/xous-bio/src/bio_tests/i2c.rs @@ -2,4 +2,242 @@ use crate::*; 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); + + 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 = i2c_driver(); + print!("code length {}\r", code.len()); + bio_ss.load_code(code, 0); + + test_cfg.wo(utra::main::WDATA, crate::bio_tests::TEST_I2C_MASK); + + // configure & run the 0th machine + // 400kHz clock -> 100kHz toggle rate + 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.wfo(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 << 2); + + // this is where the IRQ is wired to right now + let irqarray18 = CSR::new(utra::irqarray18::HW_IRQARRAY18_BASE as *mut u32); + + // unadapted code below + const PIN_SDA: u32 = 2; + const PIN_SCL: u32 = 3; + + 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); + + let i2c_cmd = PIN_SDA << 27 | PIN_SCL << 22 | ((rxbuf.len() & 0xFF) as u32) << 8 | addr << 1 | 0x1; + bio_ss.bio.wo(utra::bio::SFR_TXF0, i2c_cmd); + + let mut timeout = 0; + while irqarray18.r(utra::irqarray18::EV_STATUS) & (1 << 2) == 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); + print!("rbk {:x?}, result_code {:x}", rxbuf, result_code); + + if addr == failing_address { + if result_code != 0x2_0000 { + // expect two NACKs + passing = false; + } + } else { + if result_code != 0x2 { + // expect 2 ACKs + 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"); + } } + +// An I2C test that gives us coverage of GPIO direction control +// I2C is initiated by writing a 32-bit word to x16 that has the following format: +// bit[0..1] - r/w. R=1, W=0 +// bit[1..8] - device address +// bit[8..17] - bytes to read or write (0-256 is valid values; not bounds checked) +// bit[17..22] - reserved +// 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 +// +// Host determines if I2C is done by IRQ & value appearing on x18 which contains ack/nack status +// Format is: +// bit[0..9] - # of words successfully written or read (up to 257: 1 address + 0-256 words) +// bit[16..25] - # of words NACK'd +// +// 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 <- total bytes to transfer + "srli x9, x9, 8", + "andi x9, x9, 0x1ff",// no bounds check; max value should be 0x100 though + "li x10, 1", // x10 <- bytes to send (at least 1 for address cycle) + "bne x2, x0, 27f", // if read, don't add the byte count + "add x10, x9, x10", // write: add bytes to send to total send count + "27:", // reads leave x10 == 1 + "addi x9, x9, 1", // add 1 to x9, to account for address word + "mv x15, x1", // x15 <- SCL pin temp + "srli x15, x15, 22", + "andi x15, x15, 0x1f", + "mv x16, x1", // x16 <- SDA pin temp + "srli x16, x16, 27", + "andi x16, x16, 0x1f", + "li x5, 1", // setup GPIO masks + "sll x5, x5, x15", // x5 <- SCL bitmask + "li x4, 1", + "sll x4, x4, x16", // x4 <- SDA bitmask + "or x23, x4, x5", // pre-set SCL/SDA to 0 for "output low" + "or x25, x4, x5", // set SCL, SDA to 1 (inputs) + // setup done + "li x7, 0", // x7 <- ACK counter + "li x8, 0", // x8 <- NACK counter + // START bit + "mv x20, x0", // SCL HS + "mv x24, x4", // SDA <- 0 while SCL is high (start) + // main loop + // DATA bits + "26:", + "li x6, 8", // x6 <- loop counter + "mv x11, x0", // x11 is read shift register; initialize with 0 + "23:", + "mv x20, x0", // SCL L0 + "mv x24, x5", // SCL <- 0 + "beq x10, x0, 30f", // go to 30f if read cycle + // write code + "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 + "j 40f", // jump past read code + "30:", + // read code + "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", + "bne x6, x0, 23b", // loop back if we haven't shifted all the bits + // ACK/NACK + "mv x25, x4", // SDA is input + "mv x20, x0", // SCL L0 + "mv x24, x5", // SCL <- 0 + "mv x20, x0", // SCL L1 + "mv x25, x5", // SCL <- 1 + "mv x20, x0", // SCL H0 + "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 + "25:", + "mv x20, x0", // SCL H1 + "addi x9, x9, -1", // decrement total bytes to transfer + "beq x10, x0, 29f", // if x10 is already 0, don't decrement: commit read data instead + "addi x10, x10, -1", // decrement x10 (bytes to send) + "beq x10, x0, 28f", // if x10 is now 0, don't fetch new data to send + "mv x3, x17", // x3 <- x17 fetch outgoing from FIFO. Transfer will halt here until byte is available. + "28:", + "bne x9, x0, 26b", // loop back to "DATA bits" if more bytes + "j 50f", // fall through to STOP otherwise + "29:", + "mv x17, x11", // x17 <- x11 store read data into x17 FIFO. Will halt if FIFO is full. + "bne x9, x0, 26b", // loop back to "DATA bits" if more bytes + // 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" + ); From 559be323b7e900737ea53475082d89cc3dc018bf Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 2 Jun 2024 06:21:29 +0800 Subject: [PATCH 10/16] i2c demo about working --- libs/xous-bio/src/bio_tests/i2c.rs | 134 ++++++++++++++++++----------- 1 file changed, 84 insertions(+), 50 deletions(-) diff --git a/libs/xous-bio/src/bio_tests/i2c.rs b/libs/xous-bio/src/bio_tests/i2c.rs index 46d48d44c..80f0818a0 100644 --- a/libs/xous-bio/src/bio_tests/i2c.rs +++ b/libs/xous-bio/src/bio_tests/i2c.rs @@ -34,9 +34,9 @@ pub fn i2c_test() { // 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.wfo(utra::bio::SFR_ETYPE_FIFO_EVENT_GT_MASK, 0b00_01_00_00); // level4 mask GT + 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 << 2); + 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); @@ -53,11 +53,14 @@ pub fn i2c_test() { for addr in 10..14 { print!("reading from {}\r", addr); - let i2c_cmd = PIN_SDA << 27 | PIN_SCL << 22 | ((rxbuf.len() & 0xFF) as u32) << 8 | addr << 1 | 0x1; + // 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 << 2) == 0 { + while irqarray18.r(utra::irqarray18::EV_STATUS) & 1 == 0 { // wait timeout += 1; if (timeout % 1_000) == 0 { @@ -70,7 +73,7 @@ pub fn i2c_test() { } // read the result code let result_code = bio_ss.bio.r(utra::bio::SFR_RXF2); - print!("rbk {:x?}, result_code {:x}", rxbuf, result_code); + print!("rbk {:x?}, result_code {:x}\r", rxbuf, result_code); if addr == failing_address { if result_code != 0x2_0000 { @@ -78,8 +81,9 @@ pub fn i2c_test() { passing = false; } } else { - if result_code != 0x2 { - // expect 2 ACKs + if result_code != 0x1_0001 { + assert!(rxbuf[0] == wr_byte); + // expect 1 ACK, 1 NACK (last read is always a NACK) passing = false; } } @@ -94,12 +98,16 @@ pub fn i2c_test() { } } +// TODO: +// -- missing repeated start +// -- mock up some more complex I2C transactions + // An I2C test that gives us coverage of GPIO direction control // I2C is initiated by writing a 32-bit word to x16 that has the following format: // bit[0..1] - r/w. R=1, W=0 // bit[1..8] - device address -// bit[8..17] - bytes to read or write (0-256 is valid values; not bounds checked) -// bit[17..22] - reserved +// 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 // @@ -107,8 +115,8 @@ pub fn i2c_test() { // // Host determines if I2C is done by IRQ & value appearing on x18 which contains ack/nack status // Format is: -// bit[0..9] - # of words successfully written or read (up to 257: 1 address + 0-256 words) -// bit[16..25] - # of words NACK'd +// 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] @@ -128,42 +136,45 @@ bio_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 <- total bytes to transfer + "mv x9, x1", // x9 <- writes to transfer "srli x9, x9, 8", - "andi x9, x9, 0x1ff",// no bounds check; max value should be 0x100 though - "li x10, 1", // x10 <- bytes to send (at least 1 for address cycle) - "bne x2, x0, 27f", // if read, don't add the byte count - "add x10, x9, x10", // write: add bytes to send to total send count - "27:", // reads leave x10 == 1 - "addi x9, x9, 1", // add 1 to x9, to account for address word + "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 x16, x1", // x16 <- SDA pin temp - "srli x16, x16, 27", - "andi x16, x16, 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, x16", // x4 <- SDA bitmask - "or x23, x4, x5", // pre-set SCL/SDA to 0 for "output low" - "or x25, x4, x5", // set SCL, SDA to 1 (inputs) + "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 // START bit "mv x20, x0", // SCL HS "mv x24, x4", // SDA <- 0 while SCL is high (start) + // main loop - // DATA bits "26:", - "li x6, 8", // x6 <- loop counter + "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 x10, x0, 30f", // go to 30f if read cycle - // write code + "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 @@ -176,9 +187,27 @@ bio_code!( "mv x20, x0", // SCL H0 "mv x25, x5", // SCL <- 1 "mv x20, x0", // SCL H1 - "j 40f", // jump past read code + "addi x6, x6, -1", // decrement bit counter + "bne x6, x0, 23b", // loop back if we haven't shifted all the bits + + // incoming ACK + "mv x25, x4", // SDA is input + "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 + "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:", - // read code "mv x25, x4", // SDA is input "mv x20, x0", // SCL L1 "mv x20, x0", // SCL H0 @@ -190,35 +219,39 @@ bio_code!( "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", + "addi x6, x6, -1", // decrement bit counter "bne x6, x0, 23b", // loop back if we haven't shifted all the bits - // ACK/NACK - "mv x25, x4", // SDA is input + "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 x25, x5", // SCL <- 1 "mv x20, x0", // SCL H0 - "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 + "mv x25, x5", // SCL <- 1 "j 25f", - "24:", - "addi x7, x7, 1", // ACK += 1 + + // loop prologue "25:", "mv x20, x0", // SCL H1 - "addi x9, x9, -1", // decrement total bytes to transfer - "beq x10, x0, 29f", // if x10 is already 0, don't decrement: commit read data instead - "addi x10, x10, -1", // decrement x10 (bytes to send) - "beq x10, x0, 28f", // if x10 is now 0, don't fetch new data to send + "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. - "28:", - "bne x9, x0, 26b", // loop back to "DATA bits" if more bytes - "j 50f", // fall through to STOP otherwise - "29:", - "mv x17, x11", // x17 <- x11 store read data into x17 FIFO. Will halt if FIFO is full. - "bne x9, x0, 26b", // loop back to "DATA bits" if more bytes + "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 @@ -229,6 +262,7 @@ bio_code!( "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 From 3e263c1974217018a3ccf2d777fec9fecc0ca9a4 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 2 Jun 2024 06:21:42 +0800 Subject: [PATCH 11/16] update docs to match small change to spec --- libs/xous-bio/src/lib.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/libs/xous-bio/src/lib.rs b/libs/xous-bio/src/lib.rs index 7ebec518e..ab15d5b86 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 From cc7b1158f3a2076c209c8530b195e1e9c7670325 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 2 Jun 2024 19:16:27 +0800 Subject: [PATCH 12/16] create a "proper" i2c driver for BIO this is an actually usable driver for end applications. maybe some massaging needs to be done on shared resources, but from the i2c standpoint this can do SMB, I2C transactions with a fairly "normal" API. --- libs/xous-bio/src/i2c.rs | 347 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 347 insertions(+) create mode 100644 libs/xous-bio/src/i2c.rs 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" + ); From 5c9b91d22a939825750d292bfd1cf53af76ed109 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 2 Jun 2024 19:17:27 +0800 Subject: [PATCH 13/16] integrate i2c driver and update test log --- libs/xous-bio/src/bio_tests/i2c.rs | 226 +++++------------------------ libs/xous-bio/src/bio_tests/mod.rs | 10 +- libs/xous-bio/src/lib.rs | 16 +- 3 files changed, 56 insertions(+), 196 deletions(-) diff --git a/libs/xous-bio/src/bio_tests/i2c.rs b/libs/xous-bio/src/bio_tests/i2c.rs index 80f0818a0..81d059194 100644 --- a/libs/xous-bio/src/bio_tests/i2c.rs +++ b/libs/xous-bio/src/bio_tests/i2c.rs @@ -1,24 +1,28 @@ 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 = i2c_driver(); + let code = crate::i2c::i2c_driver(); print!("code length {}\r", code.len()); bio_ss.load_code(code, 0); - test_cfg.wo(utra::main::WDATA, crate::bio_tests::TEST_I2C_MASK); - // configure & run the 0th machine - // 400kHz clock -> 100kHz toggle rate - bio_ss.bio.wo(utra::bio::SFR_QDIV0, 0x7D0_0000); + // 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); @@ -41,10 +45,6 @@ pub fn i2c_test() { // this is where the IRQ is wired to right now let irqarray18 = CSR::new(utra::irqarray18::HW_IRQARRAY18_BASE as *mut u32); - // unadapted code below - const PIN_SDA: u32 = 2; - const PIN_SCL: u32 = 3; - 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 @@ -73,14 +73,14 @@ pub fn i2c_test() { } // read the result code let result_code = bio_ss.bio.r(utra::bio::SFR_RXF2); - print!("rbk {:x?}, result_code {:x}\r", rxbuf, result_code); - 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) @@ -98,180 +98,30 @@ pub fn i2c_test() { } } -// TODO: -// -- missing repeated start -// -- mock up some more complex I2C transactions - -// An I2C test that gives us coverage of GPIO direction control -// I2C is initiated by writing a 32-bit word to x16 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 -// -// Host determines if I2C is done by IRQ & value appearing on x18 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 - // START bit - "mv x20, x0", // SCL HS - "mv x24, x4", // SDA <- 0 while SCL is high (start) - - // main loop - "26:", - "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 x25, x4", // SDA is input - "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 - "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" - ); +/// 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 db133231d..5c8fa2d8f 100644 --- a/libs/xous-bio/src/bio_tests/mod.rs +++ b/libs/xous-bio/src/bio_tests/mod.rs @@ -1,4 +1,4 @@ -mod debug; +pub(crate) mod debug; pub mod i2c; pub mod spi; pub mod units; @@ -91,6 +91,7 @@ pub fn bio_tests() { report_api(crate::get_id()); i2c::i2c_test(); + i2c::complex_i2c_test(); units::fifo_level_tests(); spi::spi_test(); units::hello_world(); @@ -107,7 +108,7 @@ 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 +// -[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 @@ -117,8 +118,5 @@ pub fn bio_tests() { // -[ ] Core aclk counter test // Application tests: // -[x] SPI loopback test - implement using extclk as spi clk for input -// -[ ] I2C loopback test -// -[-] (cancelled) 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/lib.rs b/libs/xous-bio/src/lib.rs index ab15d5b86..45d285856 100644 --- a/libs/xous-bio/src/lib.rs +++ b/libs/xous-bio/src/lib.rs @@ -36,6 +36,8 @@ use utralib::generated::*; #[cfg(feature = "tests")] pub mod bio_tests; +pub mod i2c; + #[derive(Debug)] pub enum BioError { /// specified state machine is not valid @@ -89,7 +91,6 @@ impl BioSharedState { #[cfg(not(feature = "baremetal"))] pub fn new() -> Self { - // TODO let csr = xous::syscall::map_memory( xous::MemoryAddress::new(utra::bio::HW_BIO_BASE), None, @@ -98,7 +99,18 @@ impl BioSharedState { ) .unwrap(); - BioSharedState { bio: CSR::new(csr.as_mut_ptr() as *mut u32) } + 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() }, + } } pub fn load_code(&mut self, prog: &[u8], offset_bytes: usize) { From a721c60d458f276223a734d0caa23cec20973715 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 2 Jun 2024 19:17:46 +0800 Subject: [PATCH 14/16] add bio to project workspace --- Cargo.lock | 10 ++++++++++ Cargo.toml | 1 + 2 files changed, 11 insertions(+) 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", From c4d787356be0d483a7c2db55b6451120dd38496a Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 2 Jun 2024 20:20:06 +0800 Subject: [PATCH 15/16] add aclk unit test --- libs/xous-bio/src/bio_tests/mod.rs | 3 +- libs/xous-bio/src/bio_tests/units.rs | 74 ++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/libs/xous-bio/src/bio_tests/mod.rs b/libs/xous-bio/src/bio_tests/mod.rs index 5c8fa2d8f..124298839 100644 --- a/libs/xous-bio/src/bio_tests/mod.rs +++ b/libs/xous-bio/src/bio_tests/mod.rs @@ -90,6 +90,7 @@ 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(); units::fifo_level_tests(); @@ -115,7 +116,7 @@ pub fn bio_tests() { // -[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 -// -[ ] Core aclk counter test +// -[x] Core aclk counter test // Application tests: // -[x] SPI loopback test - implement using extclk as spi clk for input // -[x] I2C loopback test diff --git a/libs/xous-bio/src/bio_tests/units.rs b/libs/xous-bio/src/bio_tests/units.rs index 90f525933..25731e4ce 100644 --- a/libs/xous-bio/src/bio_tests/units.rs +++ b/libs/xous-bio/src/bio_tests/units.rs @@ -699,3 +699,77 @@ pub fn fifo_level_tests() { } 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" +); From 907b0c543ae878c5c7c06e746966ba51f125eb7f Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 3 Jun 2024 03:43:57 +0800 Subject: [PATCH 16/16] move order of tests --- libs/xous-bio/src/bio_tests/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/xous-bio/src/bio_tests/mod.rs b/libs/xous-bio/src/bio_tests/mod.rs index 124298839..4ea103725 100644 --- a/libs/xous-bio/src/bio_tests/mod.rs +++ b/libs/xous-bio/src/bio_tests/mod.rs @@ -93,12 +93,12 @@ pub fn bio_tests() { units::aclk_tests(); i2c::i2c_test(); i2c::complex_i2c_test(); - units::fifo_level_tests(); spi::spi_test(); units::hello_world(); units::hello_multiverse(); units::fifo_basic(); units::host_fifo_tests(); + units::fifo_level_tests(); } // Test plan: