diff --git a/libs/xous-pio/src/lib.rs b/libs/xous-pio/src/lib.rs index 00fb93d67..5ff9db570 100644 --- a/libs/xous-pio/src/lib.rs +++ b/libs/xous-pio/src/lib.rs @@ -79,7 +79,9 @@ impl SmConfig { pub fn get_id() -> u32 { let pio_ss = PioSharedState::new(); + #[cfg(feature="tests")] pio_tests::report_api(rp_pio::SFR_DBG_CFGINFO.offset() as u32); + #[cfg(feature="tests")] pio_tests::report_api(rp_pio::HW_RP_PIO_BASE as u32); pio_ss.pio.r(rp_pio::SFR_DBG_CFGINFO) } diff --git a/libs/xous-pl230/Cargo.toml b/libs/xous-pl230/Cargo.toml index 13a5b2f49..0cb15f43f 100644 --- a/libs/xous-pl230/Cargo.toml +++ b/libs/xous-pl230/Cargo.toml @@ -9,6 +9,9 @@ edition = "2021" log = "0.4.14" utralib = {version = "0.1.22", optional = true, default-features = false } bitfield = "0.13.2" +xous-pio = {path="../xous-pio", optional = true, default-features = false} +pio-proc = "0.2.2" +pio = "0.2.1" [target.'cfg(target_os = "xous")'.dependencies] xous = "0.9.49" @@ -19,6 +22,7 @@ cramium-fpga = ["utralib/cramium-fpga"] precursor = [] hosted = [] renode = [] +pio = ["xous-pio"] -tests = [] -default = ["tests"] \ No newline at end of file +tests = ["pio"] +default = ["tests", "pio"] \ No newline at end of file diff --git a/libs/xous-pl230/src/lib.rs b/libs/xous-pl230/src/lib.rs index d6f7181e2..1130ff796 100644 --- a/libs/xous-pl230/src/lib.rs +++ b/libs/xous-pl230/src/lib.rs @@ -27,6 +27,7 @@ struct ControlChannels { pub struct Pl230 { pub csr: CSR, + pub mdma: CSR, } impl Pl230 { @@ -34,14 +35,11 @@ impl Pl230 { pub fn new() -> Self { Pl230 { csr: CSR::new(utralib::HW_PL230_BASE as *mut u32), + mdma: CSR::new(utralib::HW_MDMA_BASE as *mut u32), } } #[cfg(target_os="xous")] pub fn new() -> Self { - // Note: this requires a memory region window to be manually specified in create-image - // so that the loader maps the pages for the PIO block. This is because the PIO block is - // an IP block that is created *outside* of the normal LiteX ecosystem. Specifically look in - // xtask/src/builder.rs for a "--extra-svd" argument that refers to precursors/pio.svd. let csr = xous::syscall::map_memory( xous::MemoryAddress::new(utralib::HW_PL230_BASE), None, @@ -49,9 +47,17 @@ impl Pl230 { xous::MemoryFlags::R | xous::MemoryFlags::W, ) .unwrap(); + let mdma = xous::syscall::map_memory( + xous::MemoryAddress::new(utralib::HW_MDMA_BASE), + None, + 4096, + xous::MemoryFlags::R | xous::MemoryFlags::W, + ) + .unwrap(); Pl230 { csr: CSR::new(csr.as_mut_ptr() as *mut u32), + mdma: CSR::new(mdma.as_mut_ptr() as *mut u32), } } } diff --git a/libs/xous-pl230/src/pl230_tests/mod.rs b/libs/xous-pl230/src/pl230_tests/mod.rs index 4cf4eac78..bca8c178c 100644 --- a/libs/xous-pl230/src/pl230_tests/mod.rs +++ b/libs/xous-pl230/src/pl230_tests/mod.rs @@ -14,4 +14,6 @@ pub fn report_api(desc: &str, d: u32) { pub fn pl230_tests() { let mut pl230 = crate::Pl230::new(); units::basic_tests(&mut pl230); + #[cfg(feature="pio")] + units::pio_test(&mut pl230); } diff --git a/libs/xous-pl230/src/pl230_tests/units.rs b/libs/xous-pl230/src/pl230_tests/units.rs index 93bf6ed04..c0c369d17 100644 --- a/libs/xous-pl230/src/pl230_tests/units.rs +++ b/libs/xous-pl230/src/pl230_tests/units.rs @@ -100,4 +100,124 @@ pub fn basic_tests (pl230: &mut Pl230) -> bool { } report_api("basic dma result (1=pass)", if passing { 1 } else { 0 }); passing +} + +#[cfg(feature="pio")] +pub fn pio_test(pl230: &mut Pl230) -> bool { + use xous_pio::*; + + // setup PIO block as DMA target -- just take the data coming into the TX + // FIFO and send it to the GPIO pins. + let mut pio_ss = PioSharedState::new(); + let mut sm_a = pio_ss.alloc_sm().unwrap(); + pio_ss.clear_instruction_memory(); + sm_a.sm_set_enabled(false); + + let a_code = pio_proc::pio_asm!( + "out pins, 0", // 0 is 32 + ); + let a_prog = LoadedProg::load(a_code.program, &mut pio_ss).unwrap(); + sm_a.sm_set_enabled(false); + a_prog.setup_default_config(&mut sm_a); + sm_a.config_set_out_pins(0, 32); + sm_a.config_set_clkdiv(133.0); // have it run slow so this test operates in the background + sm_a.config_set_out_shift(false, true, 32); + sm_a.sm_init(a_prog.entry()); + sm_a.sm_clear_fifos(); // ensure the fifos are cleared for this test + + // set a FIFO margin of 3. The DMA engine can do single requests ('sreq'), but the sreq + // signal must *fall* in order for the next cycle to be initiated. Thus, you can only + // write to a target that has "no FIFO" -- a margin of 3 is exactly that. One write, + // one pulse. It would be more performant on the bus to have more words transferred in + // one go, but actually, the MDMA controller is really terrible in terms of performance + // anyways. Every word transferred incurs many bus cycles, because it refreshes the + // control structures from RAM every single cycle. + + sm_a.sm_set_tx_fifo_margin(3); + // If the margin above is not set, prime the FIFO by pushing junk items in, so that sreq can fall + /* + let mut i = 0; + while !sm_a.sm_txfifo_is_full() { + sm_a.sm_txfifo_push_u32(0xdead_0000 + i); + i += 1 ; + } */ + + // setup control structure + let init_ptr = utralib::HW_IFRAM0_MEM as *mut u32; + for i in 0..(4*8*2) { + unsafe {init_ptr.add(i).write_volatile(0)}; + } + // safety: we guarantee that the pointer is aligned and initialized + let cc_struct: &mut ControlChannels = unsafe { + (utralib::HW_IFRAM0_MEM as *mut ControlChannels).as_mut().unwrap() + }; + pl230.csr.wfo(utra::pl230::CFG_MASTER_ENABLE, 1); // enable + + const DMA_LEN: usize = 1024; + let mut region_a = [0u32; DMA_LEN]; + let mut state = 0x1111_0000; + for d in region_a.iter_mut() { + *d = state; + state += 1; + } + + cc_struct.channels[0].dst_end_ptr = + (utra::rp_pio::SFR_TXF0.offset() * core::mem::size_of::() + utra::rp_pio::HW_RP_PIO_BASE) as u32; + cc_struct.channels[0].src_end_ptr = unsafe{region_a.as_ptr().add(region_a.len() - 1)} as u32; + let mut cc = DmaChanControl(0); + cc.set_src_size(DmaWidth::Word as u32); + cc.set_src_inc(DmaWidth::Word as u32); + cc.set_dst_size(DmaWidth::Word as u32); + cc.set_dst_inc(DmaWidth::NoInc as u32); + cc.set_r_power(ArbitrateAfter::Xfer2 as u32); + cc.set_n_minus_1(region_a.len() as u32 - 1); + cc.set_cycle_ctrl(DmaCycleControl::Basic as u32); + cc_struct.channels[0].control = cc.0; + + pl230.csr.wo(utra::pl230::CTRLBASEPTR, cc_struct.channels.as_ptr() as u32); + pl230.csr.wo(utra::pl230::CHNLREQMASKCLR, 1); // don't mask the hardware request line + pl230.csr.wo(utra::pl230::CHNLUSEBURSTCLR, 1); // don't mask single request line + pl230.csr.wo(utra::pl230::CHNLENABLESET, 1); + report_api("pio baseptr", cc_struct.channels.as_ptr() as u32); + report_api("pio src start", region_a.as_ptr() as u32); + report_api("pio dst start", unsafe{cc_struct.channels.as_ptr().read()}.dst_end_ptr); + + // setup EVC to route requests + report_api("mdma_base", unsafe{pl230.mdma.base()} as u32); + // select event number 83, which is PIO[0]. + // TODO: make this not hard-coded + // WTF: oddly enough, it actually maps to... channel 163??? weird. this probably indicates a bug of some sort. + pl230.mdma.wo(utra::mdma::SFR_EVSEL_CR_EVSEL0, 163); + // bit 0 - enable + // bit 1 - mode: 1 is edge, 0 is level + // bit 2 - enable dmareq + // bit 3 - enable dmareqs + // bit 4 - dmawaitonreq + // Assert enable + dmareqs + dmawaitonreq + pl230.mdma.wo(utra::mdma::SFR_CR_CR_MDMAREQ0, 0b11001); + + // now enable the PIO block so that DMA reqs can run + sm_a.sm_irq0_source_enabled(PioIntSource::TxNotFull, true); + sm_a.sm_set_enabled(true); + report_api("pio irq", if sm_a.sm_irq0_status(None) { 1 } else { 0 }); + + // irq0 will now fire and cause the DMA data to clock into the PIO block until the length is exhausted. + + // just return true to let the DMA run in the background while the rest of the tests happen, + // and check that the gpio_out pins have a value of DMA_LEN - 1 at the conclusion of the test + true + + // ... or we can wait until the transfers finish with this code here. + /* + const TIMEOUT: usize = 16; + let mut timeout = 0; + while (DmaChanControl(cc_struct.channels[0].control).cycle_ctrl() != 0) && timeout < TIMEOUT { + report_api("progress as baseptr[2]", unsafe{cc_struct.channels.as_ptr().read()}.control); + timeout += 1; + } + if timeout >= TIMEOUT { + false + } else { + true + } */ } \ No newline at end of file