From f00293a3a8a9f183630f9effcb3e38b9da59a2a7 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 14 Jan 2024 19:22:18 +0800 Subject: [PATCH] suppress formatting on in-line assembly in xous-pio --- libs/xous-pio/src/lib.rs | 534 +++++++++++------------ libs/xous-pio/src/pio_tests/adder.rs | 6 +- libs/xous-pio/src/pio_tests/i2c.rs | 74 ++-- libs/xous-pio/src/pio_tests/nec.rs | 43 +- libs/xous-pio/src/pio_tests/spi.rs | 28 +- libs/xous-pio/src/pio_tests/units.rs | 370 ++++++++-------- libs/xous-pl230/src/pl230_tests/units.rs | 81 ++-- 7 files changed, 557 insertions(+), 579 deletions(-) diff --git a/libs/xous-pio/src/lib.rs b/libs/xous-pio/src/lib.rs index 0a563fad7..ee571a50e 100644 --- a/libs/xous-pio/src/lib.rs +++ b/libs/xous-pio/src/lib.rs @@ -2,16 +2,16 @@ use pio::Program; use pio::RP2040_MAX_PROGRAM_SIZE; - -use utralib::generated::*; use utralib::generated::utra::rp_pio; +use utralib::generated::*; -#[cfg(feature="tests")] +#[cfg(feature = "tests")] pub mod pio_tests; #[allow(dead_code)] #[derive(Debug, Copy, Clone)] #[repr(u32)] +#[rustfmt::skip] pub enum PioRawIntSource { Sm3 = 0b1000_0000_0000, Sm2 = 0b0100_0000_0000, @@ -67,64 +67,51 @@ pub struct SmConfig { impl SmConfig { pub fn default() -> SmConfig { // FIXME: use "proper" getters and setters to create the default config. - SmConfig { - clkdiv: 0x1_0000, - execctl: 31 << 12, - shiftctl: (1 << 18) | (1 << 19), - pinctl: 0, - } + SmConfig { clkdiv: 0x1_0000, execctl: 31 << 12, shiftctl: (1 << 18) | (1 << 19), pinctl: 0 } } } pub fn get_id() -> u32 { let pio_ss = PioSharedState::new(); - #[cfg(feature="tests")] + #[cfg(feature = "tests")] pio_tests::report_api(rp_pio::SFR_DBG_CFGINFO.offset() as u32); - #[cfg(feature="tests")] + #[cfg(feature = "tests")] pio_tests::report_api(rp_pio::HW_RP_PIO_BASE as u32); pio_ss.pio.r(rp_pio::SFR_DBG_CFGINFO) } #[derive(Debug)] pub struct LoadedProg { - program: Program::, + program: Program, offset: usize, entry_point: Option, } impl LoadedProg { - pub fn load(program: Program::, pio_sm: &mut PioSharedState) -> Result { + pub fn load( + program: Program, + pio_sm: &mut PioSharedState, + ) -> Result { let offset = pio_sm.add_program(&program)?; - Ok({ - LoadedProg { - program, - offset: offset as usize, - entry_point: None, - } - }) + Ok({ LoadedProg { program, offset: offset as usize, entry_point: None } }) } - pub fn load_with_entrypoint(program: Program::, entry_point: usize, pio_sm: &mut PioSharedState) -> Result { + + pub fn load_with_entrypoint( + program: Program, + entry_point: usize, + pio_sm: &mut PioSharedState, + ) -> Result { let offset = pio_sm.add_program(&program)?; - Ok({ - LoadedProg { - program, - offset: offset as usize, - entry_point: Some(entry_point) - } - }) + Ok({ LoadedProg { program, offset: offset as usize, entry_point: Some(entry_point) } }) } + pub fn entry(&self) -> usize { - if let Some(ep) = self.entry_point { - ep + self.offset - } else { - self.start() - } - } - pub fn start(&self) -> usize { - self.program.wrap.target as usize + self.offset - } - pub fn end(&self) -> usize { - self.program.wrap.source as usize + self.offset + if let Some(ep) = self.entry_point { ep + self.offset } else { self.start() } } + + pub fn start(&self) -> usize { self.program.wrap.target as usize + self.offset } + + pub fn end(&self) -> usize { self.program.wrap.source as usize + self.offset } + pub fn setup_default_config(&self, pio_sm: &mut PioSm) { pio_sm.config_set_defaults(); pio_sm.config_set_wrap(self.start(), self.end()); @@ -147,7 +134,7 @@ pub struct PioSharedState { used_machines: [bool; 4], } impl PioSharedState { - #[cfg(all(not(target_os="xous"),not(feature="rp2040")))] + #[cfg(all(not(target_os = "xous"), not(feature = "rp2040")))] pub fn new() -> Self { PioSharedState { pio: CSR::new(rp_pio::HW_RP_PIO_BASE as *mut u32), @@ -155,16 +142,14 @@ impl PioSharedState { used_machines: [false; 4], } } - #[cfg(all(not(target_os="xous"),feature="rp2040"))] + + #[cfg(all(not(target_os = "xous"), feature = "rp2040"))] pub fn new() -> Self { crate::pio_tests::report_api(0x5020_0000); - PioSharedState { - pio: CSR::new(0x5020_0000 as *mut u32), - used_mask: 0, - used_machines: [false; 4], - } + PioSharedState { pio: CSR::new(0x5020_0000 as *mut u32), used_mask: 0, used_machines: [false; 4] } } - #[cfg(target_os="xous")] + + #[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 @@ -184,6 +169,7 @@ impl PioSharedState { used_machines: [false; 4], } } + pub fn alloc_sm(&mut self) -> Result { if let Some(sm_index) = self.used_machines.iter().position(|&used| used == false) { self.used_machines[sm_index] = true; @@ -199,14 +185,15 @@ impl PioSharedState { // helps to ensure we don't overlap regions. However, there is always the possibility // that the global shared-state handle is used to smash into a machine's private // state. Just don't. - pio: unsafe{CSR::new(self.pio.base() as *mut u32)}, + pio: unsafe { CSR::new(self.pio.base() as *mut u32) }, sm, config: SmConfig::default(), }) } else { - return Err(PioError::NoFreeMachines) + return Err(PioError::NoFreeMachines); } } + /// Safety: it's up to the caller to make sure the SM index is not used. This function /// will happily double-allocate an SM if it is already used. /// @@ -223,7 +210,7 @@ impl PioSharedState { }; self.used_machines[sm] = true; - #[cfg(target_os="xous")] + #[cfg(target_os = "xous")] let csr = xous::syscall::map_memory( xous::MemoryAddress::new(rp_pio::HW_RP_PIO_BASE), None, @@ -231,24 +218,19 @@ impl PioSharedState { xous::MemoryFlags::R | xous::MemoryFlags::W, ) .unwrap(); - #[cfg(target_os="xous")] + #[cfg(target_os = "xous")] let pio = CSR::new(csr.as_mut_ptr() as *mut u32); - #[cfg(all(not(target_os="xous"),not(feature="rp2040")))] + #[cfg(all(not(target_os = "xous"), not(feature = "rp2040")))] let pio = CSR::new(rp_pio::HW_RP_PIO_BASE as *mut u32); - #[cfg(all(not(target_os="xous"),feature="rp2040"))] + #[cfg(all(not(target_os = "xous"), feature = "rp2040"))] let pio = CSR::new(0x5020_0000 as *mut u32); - Ok(PioSm { - pio, - sm: sm_bit, - config: SmConfig::default(), - }) + Ok(PioSm { pio, sm: sm_bit, config: SmConfig::default() }) } + /// Safety: the user must make sure that the SM in question is no longer running. #[allow(dead_code)] - pub unsafe fn free_sm(&mut self, sm: PioSm) { - self.used_machines[sm.sm_index()] = false; - } + pub unsafe fn free_sm(&mut self, sm: PioSm) { self.used_machines[sm.sm_index()] = false; } fn find_offset_for_program(&self, program: &Program) -> Option { let prog_mask = (1 << program.code.len() as u32) - 1; @@ -265,15 +247,17 @@ impl PioSharedState { } else { for i in (0..=(32 - program.code.len())).rev() { if (self.used_mask & (prog_mask << i)) == 0 { - return Some(i) + return Some(i); } } None } } + pub fn can_add_program(&self, program: &Program) -> bool { self.find_offset_for_program(program).is_some() } + /// Write an instruction to program memory. fn write_progmem(&mut self, offset: usize, data: u16) { assert!(offset < 32); @@ -281,11 +265,9 @@ impl PioSharedState { self.pio.base().add(offset + rp_pio::SFR_INSTR_MEM0.offset()).write_volatile(data as _); } } + /// returns the offset of the program once loaded - pub fn add_program( - &mut self, - program: &Program, - ) -> Result { + pub fn add_program(&mut self, program: &Program) -> Result { if self.can_add_program(&program) { if let Some(origin) = self.find_offset_for_program(&program) { for (i, &instr) in program.code.iter().enumerate() { @@ -312,22 +294,18 @@ impl PioSharedState { Err(PioError::Oom) } } + /// This merely de-allocates the space but it does not actually change the contents. #[allow(dead_code)] - pub fn remove_program( - &mut self, - program: &Program, - loaded_offset: usize, - ) { + pub fn remove_program(&mut self, program: &Program, loaded_offset: usize) { let prog_mask = (((1 << program.code.len()) - 1) << loaded_offset) as u32; self.used_mask &= !prog_mask; } + /// Clears all allocations and fills program memory with a set of instructions /// that jump to themselves (this mirrors the pattern in the Pi SDK) #[allow(dead_code)] - pub fn clear_instruction_memory( - &mut self, - ) { + pub fn clear_instruction_memory(&mut self) { self.used_mask = 0; // jump is instruction 0; so a jump to yourself is simply your address @@ -342,7 +320,7 @@ pub enum SmBit { Sm0 = 1, Sm1 = 2, Sm2 = 4, - Sm3 = 8 + Sm3 = 8, } /// Allocate a PioSm using PioSharedState. There is no way to create this object /// without going through that gate-keeper object. @@ -354,9 +332,8 @@ pub struct PioSm { } impl PioSm { #[allow(dead_code)] - pub fn dbg_get_shiftctl(&self) -> u32 { - self.config.shiftctl - } + pub fn dbg_get_shiftctl(&self) -> u32 { self.config.shiftctl } + pub fn sm_index(&self) -> usize { match self.sm { SmBit::Sm0 => 0, @@ -365,15 +342,17 @@ impl PioSm { SmBit::Sm3 => 3, } } - pub fn sm_bitmask(&self) -> u32 { - self.sm as u32 - } + + pub fn sm_bitmask(&self) -> u32 { self.sm as u32 } + pub fn sm_txfifo_is_full(&self) -> bool { (self.pio.rf(rp_pio::SFR_FSTAT_TX_FULL) & (self.sm_bitmask())) != 0 } + pub fn sm_txfifo_is_empty(&self) -> bool { (self.pio.rf(rp_pio::SFR_FSTAT_TX_EMPTY) & (self.sm_bitmask())) != 0 } + pub fn sm_txfifo_level(&self) -> usize { match self.sm { SmBit::Sm0 => self.pio.rf(rp_pio::SFR_FLEVEL_TX_LEVEL0) as usize, @@ -382,6 +361,7 @@ impl PioSm { SmBit::Sm3 => self.pio.rf(rp_pio::SFR_FLEVEL_TX_LEVEL3) as usize, } } + pub fn sm_txfifo_push_u32(&mut self, data: u32) { match self.sm { SmBit::Sm0 => self.pio.wo(rp_pio::SFR_TXF0, data), @@ -389,7 +369,9 @@ impl PioSm { SmBit::Sm2 => self.pio.wo(rp_pio::SFR_TXF2, data), SmBit::Sm3 => self.pio.wo(rp_pio::SFR_TXF3, data), } - unsafe { // let the write go through before continuing + unsafe { + // let the write go through before continuing + #[rustfmt::skip] core::arch::asm!( ".word 0x500F", "nop", @@ -404,6 +386,7 @@ impl PioSm { ); } } + pub fn sm_txfifo_push_u16_msb(&mut self, data: u16) { match self.sm { SmBit::Sm0 => self.pio.wo(rp_pio::SFR_TXF0, (data as u32) << 16), @@ -411,7 +394,9 @@ impl PioSm { SmBit::Sm2 => self.pio.wo(rp_pio::SFR_TXF2, (data as u32) << 16), SmBit::Sm3 => self.pio.wo(rp_pio::SFR_TXF3, (data as u32) << 16), } - unsafe { // let the write go through before continuing + unsafe { + // let the write go through before continuing + #[rustfmt::skip] core::arch::asm!( ".word 0x500F", "nop", @@ -426,6 +411,7 @@ impl PioSm { ); } } + #[allow(dead_code)] pub fn sm_txfifo_push_u16_lsb(&mut self, data: u16) { match self.sm { @@ -434,7 +420,9 @@ impl PioSm { SmBit::Sm2 => self.pio.wo(rp_pio::SFR_TXF2, data as u32), SmBit::Sm3 => self.pio.wo(rp_pio::SFR_TXF3, data as u32), } - unsafe { // let the write go through before continuing + unsafe { + // let the write go through before continuing + #[rustfmt::skip] core::arch::asm!( ".word 0x500F", "nop", @@ -449,6 +437,7 @@ impl PioSm { ); } } + pub fn sm_txfifo_push_u8_msb(&mut self, data: u8) { match self.sm { SmBit::Sm0 => self.pio.wo(rp_pio::SFR_TXF0, (data as u32) << 24), @@ -456,7 +445,9 @@ impl PioSm { SmBit::Sm2 => self.pio.wo(rp_pio::SFR_TXF2, (data as u32) << 24), SmBit::Sm3 => self.pio.wo(rp_pio::SFR_TXF3, (data as u32) << 24), } - unsafe { // let the write go through before continuing + unsafe { + // let the write go through before continuing + #[rustfmt::skip] core::arch::asm!( ".word 0x500F", "nop", @@ -471,12 +462,15 @@ impl PioSm { ); } } + pub fn sm_rxfifo_is_empty(&self) -> bool { (self.pio.rf(rp_pio::SFR_FSTAT_RX_EMPTY) & (self.sm_bitmask())) != 0 } + pub fn sm_rxfifo_is_full(&self) -> bool { (self.pio.rf(rp_pio::SFR_FSTAT_RX_FULL) & (self.sm_bitmask())) != 0 } + pub fn sm_rxfifo_level(&self) -> usize { match self.sm { SmBit::Sm0 => self.pio.rf(rp_pio::SFR_FLEVEL_RX_LEVEL0) as usize, @@ -485,6 +479,7 @@ impl PioSm { SmBit::Sm3 => self.pio.rf(rp_pio::SFR_FLEVEL_RX_LEVEL3) as usize, } } + #[allow(dead_code)] pub fn sm_rxfifo_pull_u32(&mut self) -> u32 { match self.sm { @@ -494,6 +489,7 @@ impl PioSm { SmBit::Sm3 => self.pio.r(rp_pio::SFR_RXF3), } } + pub fn sm_rxfifo_pull_u8_lsb(&mut self) -> u8 { match self.sm { SmBit::Sm0 => self.pio.r(rp_pio::SFR_RXF0) as u8, @@ -502,20 +498,23 @@ impl PioSm { SmBit::Sm3 => self.pio.r(rp_pio::SFR_RXF3) as u8, } } + pub fn sm_put_blocking(&mut self, data: u32) { while self.sm_txfifo_is_full() { // idle } self.sm_txfifo_push_u32(data); } + pub fn sm_get_blocking(&mut self) -> u32 { while self.sm_rxfifo_is_empty() { // idle } self.sm_rxfifo_pull_u32() } + pub fn sm_set_tx_fifo_margin(&mut self, margin: usize) { - let checked_margin = if margin > 3 {3u32} else {margin as u32}; + let checked_margin = if margin > 3 { 3u32 } else { margin as u32 }; match self.sm { SmBit::Sm0 => self.pio.rmwf(rp_pio::SFR_FIFO_MARGIN_FIFO_TX_MARGIN0, checked_margin), SmBit::Sm1 => self.pio.rmwf(rp_pio::SFR_FIFO_MARGIN_FIFO_TX_MARGIN1, checked_margin), @@ -523,6 +522,7 @@ impl PioSm { SmBit::Sm3 => self.pio.rmwf(rp_pio::SFR_FIFO_MARGIN_FIFO_TX_MARGIN3, checked_margin), } } + pub fn sm_get_tx_fifo_margin(&mut self) -> u32 { match self.sm { SmBit::Sm0 => self.pio.rf(rp_pio::SFR_FIFO_MARGIN_FIFO_TX_MARGIN0), @@ -531,8 +531,9 @@ impl PioSm { SmBit::Sm3 => self.pio.rf(rp_pio::SFR_FIFO_MARGIN_FIFO_TX_MARGIN3), } } + pub fn sm_set_rx_fifo_margin(&mut self, margin: usize) { - let checked_margin = if margin > 3 {3u32} else {margin as u32}; + let checked_margin = if margin > 3 { 3u32 } else { margin as u32 }; match self.sm { SmBit::Sm0 => self.pio.rmwf(rp_pio::SFR_FIFO_MARGIN_FIFO_RX_MARGIN0, checked_margin), SmBit::Sm1 => self.pio.rmwf(rp_pio::SFR_FIFO_MARGIN_FIFO_RX_MARGIN1, checked_margin), @@ -540,6 +541,7 @@ impl PioSm { SmBit::Sm3 => self.pio.rmwf(rp_pio::SFR_FIFO_MARGIN_FIFO_RX_MARGIN3, checked_margin), } } + pub fn sm_get_rx_fifo_margin(&mut self) -> u32 { match self.sm { SmBit::Sm0 => self.pio.rf(rp_pio::SFR_FIFO_MARGIN_FIFO_RX_MARGIN0), @@ -548,6 +550,7 @@ impl PioSm { SmBit::Sm3 => self.pio.rf(rp_pio::SFR_FIFO_MARGIN_FIFO_RX_MARGIN3), } } + pub fn sm_to_stride_offset(&self) -> usize { // derive the constant value of the stride between SMs const STRIDE: usize = rp_pio::SFR_SM1_EXECCTRL.offset() - rp_pio::SFR_SM0_EXECCTRL.offset(); @@ -558,6 +561,7 @@ impl PioSm { SmBit::Sm3 => STRIDE * 3, } } + pub fn sm_address(&self) -> usize { match self.sm { SmBit::Sm0 => self.pio.rf(rp_pio::SFR_SM0_ADDR_PC) as usize, @@ -566,6 +570,7 @@ impl PioSm { SmBit::Sm3 => self.pio.rf(rp_pio::SFR_SM3_ADDR_PC) as usize, } } + pub fn config_set_out_pins(&mut self, out_base: usize, out_count: usize) { assert!(out_base < 32); assert!(out_count <= 32); @@ -586,92 +591,82 @@ impl PioSm { | self.pio.ms(rp_pio::SFR_SM0_PINCTRL_PINS_OUT_BASE, out_base as _) | self.pio.ms(rp_pio::SFR_SM0_PINCTRL_PINS_OUT_COUNT, out_count as _); } + #[allow(dead_code)] pub fn config_set_set_pins(&mut self, set_base: usize, set_count: usize) { assert!(set_base < 32); assert!(set_count <= 5); - self.config.pinctl = - self.pio.zf(rp_pio::SFR_SM0_PINCTRL_PINS_SET_COUNT, - self.pio.zf(rp_pio::SFR_SM0_PINCTRL_PINS_SET_BASE, - self.config.pinctl - ) - ) - | self.pio.ms(rp_pio::SFR_SM0_PINCTRL_PINS_SET_BASE, set_base as _) + self.config.pinctl = self.pio.zf( + rp_pio::SFR_SM0_PINCTRL_PINS_SET_COUNT, + self.pio.zf(rp_pio::SFR_SM0_PINCTRL_PINS_SET_BASE, self.config.pinctl), + ) | self.pio.ms(rp_pio::SFR_SM0_PINCTRL_PINS_SET_BASE, set_base as _) | self.pio.ms(rp_pio::SFR_SM0_PINCTRL_PINS_SET_COUNT, set_count as _); } + pub fn config_set_in_pins(&mut self, in_base: usize) { assert!(in_base < 32); - self.config.pinctl = - self.pio.zf(rp_pio::SFR_SM0_PINCTRL_PINS_IN_BASE, - self.config.pinctl - ) - | self.pio.ms(rp_pio::SFR_SM0_PINCTRL_PINS_IN_BASE, in_base as _); + self.config.pinctl = self.pio.zf(rp_pio::SFR_SM0_PINCTRL_PINS_IN_BASE, self.config.pinctl) + | self.pio.ms(rp_pio::SFR_SM0_PINCTRL_PINS_IN_BASE, in_base as _); } + pub fn config_set_sideset_pins(&mut self, sideset_base: usize) { assert!(sideset_base < 32); - self.config.pinctl = - self.pio.zf(rp_pio::SFR_SM0_PINCTRL_PINS_SIDE_BASE, - self.config.pinctl - ) + self.config.pinctl = self.pio.zf(rp_pio::SFR_SM0_PINCTRL_PINS_SIDE_BASE, self.config.pinctl) | self.pio.ms(rp_pio::SFR_SM0_PINCTRL_PINS_SIDE_BASE, sideset_base as _); } + #[allow(dead_code)] pub fn config_set_sideset(&mut self, bit_count: usize, optional: bool, pindirs: bool) { assert!(bit_count <= 5); assert!(!optional || bit_count >= 1); - self.config.pinctl = - self.pio.zf(rp_pio::SFR_SM0_PINCTRL_PINS_SIDE_COUNT, - self.config.pinctl - ) + self.config.pinctl = self.pio.zf(rp_pio::SFR_SM0_PINCTRL_PINS_SIDE_COUNT, self.config.pinctl) | self.pio.ms(rp_pio::SFR_SM0_PINCTRL_PINS_SIDE_COUNT, bit_count as _); self.config.execctl = - self.pio.zf(rp_pio::SFR_SM0_EXECCTRL_SIDE_PINDIR, - self.pio.zf(rp_pio::SFR_SM0_EXECCTRL_SIDESET_ENABLE_BIT, - self.config.execctl - ) - ) - | self.pio.ms(rp_pio::SFR_SM0_EXECCTRL_SIDESET_ENABLE_BIT, if optional {1} else {0}) - | self.pio.ms(rp_pio::SFR_SM0_EXECCTRL_SIDE_PINDIR, if pindirs {1} else {0}); + self.pio.zf( + rp_pio::SFR_SM0_EXECCTRL_SIDE_PINDIR, + self.pio.zf(rp_pio::SFR_SM0_EXECCTRL_SIDESET_ENABLE_BIT, self.config.execctl), + ) | self.pio.ms(rp_pio::SFR_SM0_EXECCTRL_SIDESET_ENABLE_BIT, if optional { 1 } else { 0 }) + | self.pio.ms(rp_pio::SFR_SM0_EXECCTRL_SIDE_PINDIR, if pindirs { 1 } else { 0 }); } + pub fn config_set_out_shift(&mut self, shift_right: bool, autopull: bool, pull_threshold: usize) { assert!(pull_threshold <= 32); self.config.shiftctl = - self.pio.zf(rp_pio::SFR_SM0_SHIFTCTRL_OSR_THRESHOLD, - self.pio.zf(rp_pio::SFR_SM0_SHIFTCTRL_AUTO_PULL, - self.pio.zf(rp_pio::SFR_SM0_SHIFTCTRL_OUT_SHIFT_DIR, - self.config.shiftctl - ) - ) - ) - | self.pio.ms(rp_pio::SFR_SM0_SHIFTCTRL_OSR_THRESHOLD, if pull_threshold == 32 {0} else {pull_threshold as _}) - | self.pio.ms(rp_pio::SFR_SM0_SHIFTCTRL_AUTO_PULL, if autopull {1} else {0}) - | self.pio.ms(rp_pio::SFR_SM0_SHIFTCTRL_OUT_SHIFT_DIR, if shift_right {1} else {0}); + self.pio.zf( + rp_pio::SFR_SM0_SHIFTCTRL_OSR_THRESHOLD, + self.pio.zf( + rp_pio::SFR_SM0_SHIFTCTRL_AUTO_PULL, + self.pio.zf(rp_pio::SFR_SM0_SHIFTCTRL_OUT_SHIFT_DIR, self.config.shiftctl), + ), + ) | self.pio.ms( + rp_pio::SFR_SM0_SHIFTCTRL_OSR_THRESHOLD, + if pull_threshold == 32 { 0 } else { pull_threshold as _ }, + ) | self.pio.ms(rp_pio::SFR_SM0_SHIFTCTRL_AUTO_PULL, if autopull { 1 } else { 0 }) + | self.pio.ms(rp_pio::SFR_SM0_SHIFTCTRL_OUT_SHIFT_DIR, if shift_right { 1 } else { 0 }); } + pub fn config_set_in_shift(&mut self, shift_right: bool, autopush: bool, push_threshold: usize) { assert!(push_threshold <= 32); self.config.shiftctl = - self.pio.zf(rp_pio::SFR_SM0_SHIFTCTRL_ISR_THRESHOLD, - self.pio.zf(rp_pio::SFR_SM0_SHIFTCTRL_AUTO_PUSH, - self.pio.zf(rp_pio::SFR_SM0_SHIFTCTRL_IN_SHIFT_DIR, - self.config.shiftctl - ) - ) - ) - | self.pio.ms(rp_pio::SFR_SM0_SHIFTCTRL_ISR_THRESHOLD, if push_threshold == 32 {0} else {push_threshold as _}) - | self.pio.ms(rp_pio::SFR_SM0_SHIFTCTRL_AUTO_PUSH, if autopush {1} else {0}) - | self.pio.ms(rp_pio::SFR_SM0_SHIFTCTRL_IN_SHIFT_DIR, if shift_right {1} else {0}); - } - pub fn config_set_defaults(&mut self) { - self.config = SmConfig::default(); + self.pio.zf( + rp_pio::SFR_SM0_SHIFTCTRL_ISR_THRESHOLD, + self.pio.zf( + rp_pio::SFR_SM0_SHIFTCTRL_AUTO_PUSH, + self.pio.zf(rp_pio::SFR_SM0_SHIFTCTRL_IN_SHIFT_DIR, self.config.shiftctl), + ), + ) | self.pio.ms( + rp_pio::SFR_SM0_SHIFTCTRL_ISR_THRESHOLD, + if push_threshold == 32 { 0 } else { push_threshold as _ }, + ) | self.pio.ms(rp_pio::SFR_SM0_SHIFTCTRL_AUTO_PUSH, if autopush { 1 } else { 0 }) + | self.pio.ms(rp_pio::SFR_SM0_SHIFTCTRL_IN_SHIFT_DIR, if shift_right { 1 } else { 0 }); } + + pub fn config_set_defaults(&mut self) { self.config = SmConfig::default(); } + pub fn config_set_jmp_pin(&mut self, pin: usize) { assert!(pin < 32); - self.config.execctl = - self.pio.zf( - rp_pio::SFR_SM0_EXECCTRL_JMP_PIN, - self.config.execctl - ) + self.config.execctl = self.pio.zf(rp_pio::SFR_SM0_EXECCTRL_JMP_PIN, self.config.execctl) | self.pio.ms(rp_pio::SFR_SM0_EXECCTRL_JMP_PIN, pin as _); } @@ -680,105 +675,88 @@ impl PioSm { assert!(div >= 1.0); assert!(div <= 65536.0); let div_int = div as u16; - let div_frac = if div_int == 0 { - 0u8 - } else { - ((div - div_int as f32) * (1 << 8) as f32) as u8 - }; + let div_frac = if div_int == 0 { 0u8 } else { ((div - div_int as f32) * (1 << 8) as f32) as u8 }; (div_int, div_frac) } + pub fn config_set_clkdiv_int_frac(&mut self, div_int: u16, div_frac: u8) { assert!(!(div_int == 0 && (div_frac != 0))); - self.config.clkdiv = - self.pio.ms(rp_pio::SFR_SM0_CLKDIV_DIV_INT, div_int as _) + self.config.clkdiv = self.pio.ms(rp_pio::SFR_SM0_CLKDIV_DIV_INT, div_int as _) | self.pio.ms(rp_pio::SFR_SM0_CLKDIV_DIV_FRAC, div_frac as _); } + pub fn config_set_clkdiv(&mut self, div: f32) { let (div_int, div_frac) = self.clkdiv_from_float(div); self.config_set_clkdiv_int_frac(div_int, div_frac); } + pub fn config_set_wrap(&mut self, start: usize, end: usize) { - self.config.execctl = - self.pio.zf(rp_pio::SFR_SM0_EXECCTRL_WRAP_TARGET, - self.pio.zf(rp_pio::SFR_SM0_EXECCTRL_PEND, self.config.execctl) - ) - | self.pio.ms(rp_pio::SFR_SM0_EXECCTRL_PEND, end as _) - | self.pio.ms(rp_pio::SFR_SM0_EXECCTRL_WRAP_TARGET, start as _) - ; + self.config.execctl = self.pio.zf( + rp_pio::SFR_SM0_EXECCTRL_WRAP_TARGET, + self.pio.zf(rp_pio::SFR_SM0_EXECCTRL_PEND, self.config.execctl), + ) | self.pio.ms(rp_pio::SFR_SM0_EXECCTRL_PEND, end as _) + | self.pio.ms(rp_pio::SFR_SM0_EXECCTRL_WRAP_TARGET, start as _); } + pub fn config_set_out_special(&mut self, sticky: bool, has_enable: bool, enable_index: usize) { self.config.execctl = self.pio.zf( rp_pio::SFR_SM0_EXECCTRL_OUT_STICKY, self.pio.zf( rp_pio::SFR_SM0_EXECCTRL_OUT_EN_SEL, - self.pio.zf( - rp_pio::SFR_SM0_EXECCTRL_INLINE_OUT_EN, - self.config.execctl - ) - ) - ) - | self.pio.ms(rp_pio::SFR_SM0_EXECCTRL_OUT_STICKY, if sticky {1} else {0}) - | self.pio.ms(rp_pio::SFR_SM0_EXECCTRL_INLINE_OUT_EN, if has_enable {1} else {0}) - | self.pio.ms(rp_pio::SFR_SM0_EXECCTRL_OUT_EN_SEL, enable_index as u32) - ; + self.pio.zf(rp_pio::SFR_SM0_EXECCTRL_INLINE_OUT_EN, self.config.execctl), + ), + ) | self.pio.ms(rp_pio::SFR_SM0_EXECCTRL_OUT_STICKY, if sticky { 1 } else { 0 }) + | self.pio.ms(rp_pio::SFR_SM0_EXECCTRL_INLINE_OUT_EN, if has_enable { 1 } else { 0 }) + | self.pio.ms(rp_pio::SFR_SM0_EXECCTRL_OUT_EN_SEL, enable_index as u32); } + pub fn config_set_fifo_join(&mut self, join: PioFifoJoin) { - self.config.shiftctl = - self.pio.zf( - rp_pio::SFR_SM0_SHIFTCTRL_JOIN_RX, - self.pio.zf( - rp_pio::SFR_SM0_SHIFTCTRL_JOIN_TX, - self.config.shiftctl - ) - ) - | - match join { - PioFifoJoin::None => 0, - PioFifoJoin::JoinTx => self.pio.ms(rp_pio::SFR_SM0_SHIFTCTRL_JOIN_TX, 1), - PioFifoJoin::JoinRx => self.pio.ms(rp_pio::SFR_SM0_SHIFTCTRL_JOIN_RX, 1), - } + self.config.shiftctl = self.pio.zf( + rp_pio::SFR_SM0_SHIFTCTRL_JOIN_RX, + self.pio.zf(rp_pio::SFR_SM0_SHIFTCTRL_JOIN_TX, self.config.shiftctl), + ) | match join { + PioFifoJoin::None => 0, + PioFifoJoin::JoinTx => self.pio.ms(rp_pio::SFR_SM0_SHIFTCTRL_JOIN_TX, 1), + PioFifoJoin::JoinRx => self.pio.ms(rp_pio::SFR_SM0_SHIFTCTRL_JOIN_RX, 1), + } } + pub fn config_set_mov_status(&mut self, status_sel: MovStatusType, level: usize) { - self.config.execctl = - self.pio.zf( - rp_pio::SFR_SM0_EXECCTRL_STATUS_SEL, - self.pio.zf(rp_pio::SFR_SM0_EXECCTRL_STATUS_N, - self.config.execctl - ) - ) - | self.pio.ms(rp_pio::SFR_SM0_EXECCTRL_STATUS_SEL, status_sel as u32) - | self.pio.ms(rp_pio::SFR_SM0_EXECCTRL_STATUS_N, level as u32) - ; + self.config.execctl = self.pio.zf( + rp_pio::SFR_SM0_EXECCTRL_STATUS_SEL, + self.pio.zf(rp_pio::SFR_SM0_EXECCTRL_STATUS_N, self.config.execctl), + ) | self.pio.ms(rp_pio::SFR_SM0_EXECCTRL_STATUS_SEL, status_sel as u32) + | self.pio.ms(rp_pio::SFR_SM0_EXECCTRL_STATUS_N, level as u32); } pub fn sm_exec(&mut self, instr: u16) { let sm_offset = self.sm_to_stride_offset(); unsafe { - self.pio.base().add(rp_pio::SFR_SM0_INSTR.offset() + sm_offset) - .write_volatile(instr as u32); + self.pio.base().add(rp_pio::SFR_SM0_INSTR.offset() + sm_offset).write_volatile(instr as u32); } } + pub fn sm_set_pindirs_with_mask(&mut self, pindirs: usize, mut pin_mask: usize) { let sm_offset = self.sm_to_stride_offset(); unsafe { - let pinctrl_saved = self.pio.base().add(rp_pio::SFR_SM0_PINCTRL.offset() + sm_offset).read_volatile(); - let exectrl_saved = self.pio.base().add(rp_pio::SFR_SM0_EXECCTRL.offset() + sm_offset).read_volatile(); - self.pio.base().add(rp_pio::SFR_SM0_EXECCTRL.offset() + sm_offset).write_volatile( - self.pio.zf( - rp_pio::SFR_SM0_EXECCTRL_OUT_STICKY, - self.pio.base().add(rp_pio::SFR_SM0_EXECCTRL.offset() + sm_offset).read_volatile() - ) - ); + let pinctrl_saved = + self.pio.base().add(rp_pio::SFR_SM0_PINCTRL.offset() + sm_offset).read_volatile(); + let exectrl_saved = + self.pio.base().add(rp_pio::SFR_SM0_EXECCTRL.offset() + sm_offset).read_volatile(); + self.pio.base().add(rp_pio::SFR_SM0_EXECCTRL.offset() + sm_offset).write_volatile(self.pio.zf( + rp_pio::SFR_SM0_EXECCTRL_OUT_STICKY, + self.pio.base().add(rp_pio::SFR_SM0_EXECCTRL.offset() + sm_offset).read_volatile(), + )); while pin_mask != 0 { let base = pin_mask.trailing_zeros(); self.pio.base().add(rp_pio::SFR_SM0_PINCTRL.offset() + sm_offset).write_volatile( self.pio.ms(rp_pio::SFR_SM0_PINCTRL_PINS_SET_COUNT, 1) - | self.pio.ms(rp_pio::SFR_SM0_PINCTRL_PINS_SET_BASE, base as _) + | self.pio.ms(rp_pio::SFR_SM0_PINCTRL_PINS_SET_BASE, base as _), ); let mut a = pio::Assembler::<32>::new(); a.set(pio::SetDestination::PINDIRS, ((pindirs >> base) & 1) as u8); - let p= a.assemble_program(); + let p = a.assemble_program(); self.sm_exec(p.code[p.origin.unwrap_or(0) as usize]); pin_mask &= pin_mask - 1; } @@ -786,26 +764,27 @@ impl PioSm { self.pio.base().add(rp_pio::SFR_SM0_EXECCTRL.offset() + sm_offset).write_volatile(exectrl_saved); } } + pub fn sm_set_pins_with_mask(&mut self, pinvals: usize, mut pin_mask: usize) { let sm_offset = self.sm_to_stride_offset(); unsafe { - let pinctrl_saved = self.pio.base().add(rp_pio::SFR_SM0_PINCTRL.offset() + sm_offset).read_volatile(); - let exectrl_saved = self.pio.base().add(rp_pio::SFR_SM0_EXECCTRL.offset() + sm_offset).read_volatile(); - self.pio.base().add(rp_pio::SFR_SM0_EXECCTRL.offset() + sm_offset).write_volatile( - self.pio.zf( - rp_pio::SFR_SM0_EXECCTRL_OUT_STICKY, - self.pio.base().add(rp_pio::SFR_SM0_EXECCTRL.offset() + sm_offset).read_volatile() - ) - ); + let pinctrl_saved = + self.pio.base().add(rp_pio::SFR_SM0_PINCTRL.offset() + sm_offset).read_volatile(); + let exectrl_saved = + self.pio.base().add(rp_pio::SFR_SM0_EXECCTRL.offset() + sm_offset).read_volatile(); + self.pio.base().add(rp_pio::SFR_SM0_EXECCTRL.offset() + sm_offset).write_volatile(self.pio.zf( + rp_pio::SFR_SM0_EXECCTRL_OUT_STICKY, + self.pio.base().add(rp_pio::SFR_SM0_EXECCTRL.offset() + sm_offset).read_volatile(), + )); while pin_mask != 0 { let base = pin_mask.trailing_zeros(); self.pio.base().add(rp_pio::SFR_SM0_PINCTRL.offset() + sm_offset).write_volatile( self.pio.ms(rp_pio::SFR_SM0_PINCTRL_PINS_SET_COUNT, 1) - | self.pio.ms(rp_pio::SFR_SM0_PINCTRL_PINS_SET_BASE, base as _) + | self.pio.ms(rp_pio::SFR_SM0_PINCTRL_PINS_SET_BASE, base as _), ); let mut a = pio::Assembler::<32>::new(); a.set(pio::SetDestination::PINS, ((pinvals >> base) & 1) as u8); - let p= a.assemble_program(); + let p = a.assemble_program(); self.sm_exec(p.code[p.origin.unwrap_or(0) as usize]); pin_mask &= pin_mask - 1; } @@ -813,42 +792,47 @@ impl PioSm { self.pio.base().add(rp_pio::SFR_SM0_EXECCTRL.offset() + sm_offset).write_volatile(exectrl_saved); } } + #[allow(dead_code)] pub fn global_irq0_source_enabled(&mut self, source: PioRawIntSource, enabled: bool) { - self.pio.wo(rp_pio::SFR_IRQ0_INTE, - if enabled {source as u32} else {0} - | self.pio.r(rp_pio::SFR_IRQ0_INTE) & !(source as u32) + self.pio.wo( + rp_pio::SFR_IRQ0_INTE, + if enabled { source as u32 } else { 0 } | self.pio.r(rp_pio::SFR_IRQ0_INTE) & !(source as u32), ); } + #[allow(dead_code)] pub fn global_irq1_source_enabled(&mut self, source: PioRawIntSource, enabled: bool) { - self.pio.wo(rp_pio::SFR_IRQ1_INTE, - if enabled {source as u32} else {0} - | self.pio.r(rp_pio::SFR_IRQ1_INTE) & !(source as u32) + self.pio.wo( + rp_pio::SFR_IRQ1_INTE, + if enabled { source as u32 } else { 0 } | self.pio.r(rp_pio::SFR_IRQ1_INTE) & !(source as u32), ); } + pub fn sm_irq0_source_enabled(&mut self, source: PioIntSource, enabled: bool) { let mask = match source { PioIntSource::Sm => (self.sm_bitmask()) << 8, PioIntSource::TxNotFull => (self.sm_bitmask()) << 4, PioIntSource::RxNotEmpty => (self.sm_bitmask()) << 0, }; - self.pio.wo(rp_pio::SFR_IRQ0_INTE, - if enabled {mask} else {0} - | self.pio.r(rp_pio::SFR_IRQ0_INTE) & !mask + self.pio.wo( + rp_pio::SFR_IRQ0_INTE, + if enabled { mask } else { 0 } | self.pio.r(rp_pio::SFR_IRQ0_INTE) & !mask, ); } + pub fn sm_irq1_source_enabled(&mut self, source: PioIntSource, enabled: bool) { let mask = match source { PioIntSource::Sm => (self.sm_bitmask()) << 8, PioIntSource::TxNotFull => (self.sm_bitmask()) << 4, PioIntSource::RxNotEmpty => (self.sm_bitmask()) << 0, }; - self.pio.wo(rp_pio::SFR_IRQ1_INTE, - if enabled{mask} else {0} - | self.pio.r(rp_pio::SFR_IRQ1_INTE) & !mask + self.pio.wo( + rp_pio::SFR_IRQ1_INTE, + if enabled { mask } else { 0 } | self.pio.r(rp_pio::SFR_IRQ1_INTE) & !mask, ); } + #[allow(dead_code)] pub fn sm_irq0_status(&mut self, source: Option) -> bool { let mask = if let Some(source) = source { @@ -862,6 +846,7 @@ impl PioSm { }; (self.pio.r(rp_pio::SFR_IRQ0_INTS) & mask) != 0 } + pub fn sm_irq1_status(&mut self, source: Option) -> bool { let mask = if let Some(source) = source { match source { @@ -877,39 +862,48 @@ impl PioSm { pub fn sm_set_enabled(&mut self, enabled: bool) { if enabled { - self.pio.rmwf(rp_pio::SFR_CTRL_EN, - self.pio.rf(rp_pio::SFR_CTRL_EN) | (self.sm_bitmask()) - ) + self.pio.rmwf(rp_pio::SFR_CTRL_EN, self.pio.rf(rp_pio::SFR_CTRL_EN) | (self.sm_bitmask())) } else { - self.pio.rmwf(rp_pio::SFR_CTRL_EN, - self.pio.rf(rp_pio::SFR_CTRL_EN) & !(self.sm_bitmask()) - ) + self.pio.rmwf(rp_pio::SFR_CTRL_EN, self.pio.rf(rp_pio::SFR_CTRL_EN) & !(self.sm_bitmask())) } } pub fn sm_set_config(&mut self) { let sm_offset = self.sm_to_stride_offset(); unsafe { - self.pio.base().add(rp_pio::SFR_SM0_CLKDIV.offset() + sm_offset).write_volatile(self.config.clkdiv); - self.pio.base().add(rp_pio::SFR_SM0_EXECCTRL.offset() + sm_offset).write_volatile(self.config.execctl); - self.pio.base().add(rp_pio::SFR_SM0_SHIFTCTRL.offset() + sm_offset).write_volatile(self.config.shiftctl); - self.pio.base().add(rp_pio::SFR_SM0_PINCTRL.offset() + sm_offset).write_volatile(self.config.pinctl); + self.pio + .base() + .add(rp_pio::SFR_SM0_CLKDIV.offset() + sm_offset) + .write_volatile(self.config.clkdiv); + self.pio + .base() + .add(rp_pio::SFR_SM0_EXECCTRL.offset() + sm_offset) + .write_volatile(self.config.execctl); + self.pio + .base() + .add(rp_pio::SFR_SM0_SHIFTCTRL.offset() + sm_offset) + .write_volatile(self.config.shiftctl); + self.pio + .base() + .add(rp_pio::SFR_SM0_PINCTRL.offset() + sm_offset) + .write_volatile(self.config.pinctl); } } + /// Clears the FIFOs by flipping the RX join bit pub fn sm_clear_fifos(&mut self) { let sm_offset = self.sm_to_stride_offset(); unsafe { let baseval = self.pio.base().add(rp_pio::SFR_SM0_SHIFTCTRL.offset() + sm_offset).read_volatile(); let bitval = self.pio.ms(rp_pio::SFR_SM0_SHIFTCTRL_JOIN_RX, 1); - self.pio.base().add(rp_pio::SFR_SM0_SHIFTCTRL.offset() + sm_offset).write_volatile( - baseval ^ bitval - ); - self.pio.base().add(rp_pio::SFR_SM0_SHIFTCTRL.offset() + sm_offset).write_volatile( - baseval - ); + self.pio + .base() + .add(rp_pio::SFR_SM0_SHIFTCTRL.offset() + sm_offset) + .write_volatile(baseval ^ bitval); + self.pio.base().add(rp_pio::SFR_SM0_SHIFTCTRL.offset() + sm_offset).write_volatile(baseval); } } + pub fn sm_init(&mut self, initial_pc: usize) { self.sm_set_enabled(false); self.sm_set_config(); @@ -920,9 +914,9 @@ impl PioSm { self.pio.wo( rp_pio::SFR_FDEBUG, self.pio.ms(rp_pio::SFR_FDEBUG_TXSTALL, self.sm_bitmask()) - | self.pio.ms(rp_pio::SFR_FDEBUG_TXOVER, self.sm_bitmask()) - | self.pio.ms(rp_pio::SFR_FDEBUG_RXUNDER, self.sm_bitmask()) - | self.pio.ms(rp_pio::SFR_FDEBUG_RXSTALL, self.sm_bitmask()) + | self.pio.ms(rp_pio::SFR_FDEBUG_TXOVER, self.sm_bitmask()) + | self.pio.ms(rp_pio::SFR_FDEBUG_RXUNDER, self.sm_bitmask()) + | self.pio.ms(rp_pio::SFR_FDEBUG_RXSTALL, self.sm_bitmask()), ); // Finally, clear some internal SM state @@ -933,8 +927,8 @@ impl PioSm { self.pio.wo( rp_pio::SFR_CTRL, self.pio.rf(rp_pio::SFR_CTRL_EN) - | self.pio.ms(rp_pio::SFR_CTRL_CLKDIV_RESTART, self.sm_bitmask()) - | self.pio.ms(rp_pio::SFR_CTRL_RESTART, self.sm_bitmask()) + | self.pio.ms(rp_pio::SFR_CTRL_CLKDIV_RESTART, self.sm_bitmask()) + | self.pio.ms(rp_pio::SFR_CTRL_RESTART, self.sm_bitmask()), ); while (self.pio.r(rp_pio::SFR_CTRL) & !0xF) != 0 { // wait for the bits to self-reset to acknowledge that the clears have executed @@ -943,29 +937,34 @@ impl PioSm { let mut a = pio::Assembler::<32>::new(); let mut initial_label = a.label_at_offset(initial_pc as u8); a.jmp(pio::JmpCondition::Always, &mut initial_label); - let p= a.assemble_program(); + let p = a.assemble_program(); self.sm_exec(p.code[p.origin.unwrap_or(0) as usize]); } + pub fn sm_interrupt_get(&self, int_number: usize) -> bool { assert!(int_number < 8); (self.pio.r(rp_pio::SFR_IRQ) & (1 << int_number)) != 0 } + pub fn sm_drain_tx_fifo(&mut self) { let sm_offset = self.sm_to_stride_offset(); let instr = { - if (unsafe { self.pio.base().add(rp_pio::SFR_SM0_SHIFTCTRL.offset() + sm_offset).read_volatile() } - & self.pio.ms(rp_pio::SFR_SM0_SHIFTCTRL_AUTO_PULL, 1)) != 0 { + if (unsafe { + self.pio.base().add(rp_pio::SFR_SM0_SHIFTCTRL.offset() + sm_offset).read_volatile() + } & self.pio.ms(rp_pio::SFR_SM0_SHIFTCTRL_AUTO_PULL, 1)) + != 0 + { // autopull is true let mut a = pio::Assembler::<32>::new(); a.out(pio::OutDestination::NULL, 32); - let p= a.assemble_program(); + let p = a.assemble_program(); p.code[0] } else { // autopull is false let mut a = pio::Assembler::<32>::new(); a.pull(false, false); - let p= a.assemble_program(); + let p = a.assemble_program(); p.code[0] } }; @@ -973,6 +972,7 @@ impl PioSm { self.sm_exec(instr); } } + /// Changes the PC to the lowest address of the wrap range. /// Also restarts the relevant state machine. pub fn sm_jump_to_wrap_bottom(&mut self) { @@ -981,9 +981,8 @@ impl PioSm { self.pio.wo( rp_pio::SFR_CTRL, (self.pio.rf(rp_pio::SFR_CTRL_EN) // this is aligned to 0 so we're skipping the alignment - & !self.pio.ms(rp_pio::SFR_CTRL_EN, self.sm_bitmask()) - ) - | self.pio.ms(rp_pio::SFR_CTRL_RESTART, self.sm_bitmask()) + & !self.pio.ms(rp_pio::SFR_CTRL_EN, self.sm_bitmask())) + | self.pio.ms(rp_pio::SFR_CTRL_RESTART, self.sm_bitmask()), ); while (self.pio.r(rp_pio::SFR_CTRL) & !0xF) != 0 { // wait for the bits to self-reset to acknowledge that the clears have executed @@ -1001,8 +1000,7 @@ impl PioSm { // re-enable the machine self.pio.wo( rp_pio::SFR_CTRL, - self.pio.r(rp_pio::SFR_CTRL) - | self.pio.ms(rp_pio::SFR_CTRL_EN, self.sm_bitmask()) + self.pio.r(rp_pio::SFR_CTRL) | self.pio.ms(rp_pio::SFR_CTRL_EN, self.sm_bitmask()), ); } @@ -1011,32 +1009,34 @@ impl PioSm { self.pio.wo(rp_pio::SFR_IO_OE_INV, 0); self.pio.wo(rp_pio::SFR_IO_I_INV, 0); } + pub fn gpio_set_outover(&mut self, pin: usize, value: bool) { - self.pio.wo(rp_pio::SFR_IO_O_INV, - (if value {1} else {0}) << pin - | (self.pio.r(rp_pio::SFR_IO_O_INV) & !(1 << pin)) + self.pio.wo( + rp_pio::SFR_IO_O_INV, + (if value { 1 } else { 0 }) << pin | (self.pio.r(rp_pio::SFR_IO_O_INV) & !(1 << pin)), ); } + #[allow(dead_code)] pub fn gpio_set_oeover(&mut self, pin: usize, value: bool) { - self.pio.wo(rp_pio::SFR_IO_OE_INV, - (if value {1} else {0}) << pin - | (self.pio.r(rp_pio::SFR_IO_OE_INV) & !(1 << pin)) + self.pio.wo( + rp_pio::SFR_IO_OE_INV, + (if value { 1 } else { 0 }) << pin | (self.pio.r(rp_pio::SFR_IO_OE_INV) & !(1 << pin)), ); } + #[allow(dead_code)] pub fn gpio_set_inover(&mut self, pin: usize, value: bool) { - self.pio.wo(rp_pio::SFR_IO_I_INV, - (if value {1} else {0}) << pin - | (self.pio.r(rp_pio::SFR_IO_I_INV) & !(1 << pin)) + self.pio.wo( + rp_pio::SFR_IO_I_INV, + (if value { 1 } else { 0 }) << pin | (self.pio.r(rp_pio::SFR_IO_I_INV) & !(1 << pin)), ); } } /// used to generate some test vectors pub fn lfsr_next(state: u16) -> u16 { - let bit = ((state >> 8) ^ - (state >> 4)) & 1; + let bit = ((state >> 8) ^ (state >> 4)) & 1; ((state << 1) + bit) & 0x1_FF -} \ No newline at end of file +} diff --git a/libs/xous-pio/src/pio_tests/adder.rs b/libs/xous-pio/src/pio_tests/adder.rs index 23ace0035..14566a180 100644 --- a/libs/xous-pio/src/pio_tests/adder.rs +++ b/libs/xous-pio/src/pio_tests/adder.rs @@ -1,5 +1,5 @@ -use crate::*; use super::report_api; +use crate::*; pub fn adder_test() -> bool { report_api(0x5030_0000); @@ -7,7 +7,7 @@ pub fn adder_test() -> bool { let mut pio_ss = PioSharedState::new(); let mut pio_sm = pio_ss.alloc_sm().unwrap(); - + #[rustfmt::skip] let adder_prog = pio_proc::pio_asm!( // Pop two 32 bit integers from the TX FIFO, add them together, and push the // result to the TX FIFO. Autopush/pull should be disabled as we're using @@ -49,4 +49,4 @@ pub fn adder_test() -> bool { report_api(0x5030_600D); // loop panics if the sum doesn't work true -} \ No newline at end of file +} diff --git a/libs/xous-pio/src/pio_tests/i2c.rs b/libs/xous-pio/src/pio_tests/i2c.rs index 983a6af8d..528219783 100644 --- a/libs/xous-pio/src/pio_tests/i2c.rs +++ b/libs/xous-pio/src/pio_tests/i2c.rs @@ -1,14 +1,9 @@ use utralib::generated::utra::rp_pio; -use crate::*; use super::report_api; +use crate::*; -pub fn i2c_init( - pio_sm: &mut PioSm, - program: &LoadedProg, - pin_sda: usize, - pin_scl: usize, -) { +pub fn i2c_init(pio_sm: &mut PioSm, program: &LoadedProg, pin_sda: usize, pin_scl: usize) { pio_sm.sm_set_enabled(false); program.setup_default_config(pio_sm); @@ -42,9 +37,7 @@ pub fn i2c_init( pio_sm.sm_init(program.entry()); pio_sm.sm_set_enabled(true); } -pub fn i2c_check_error(pio_sm: &mut PioSm) -> bool { - pio_sm.sm_interrupt_get(pio_sm.sm_index()) -} +pub fn i2c_check_error(pio_sm: &mut PioSm) -> bool { pio_sm.sm_interrupt_get(pio_sm.sm_index()) } pub fn i2c_resume_after_error(pio_sm: &mut PioSm) { pio_sm.sm_drain_tx_fifo(); pio_sm.sm_jump_to_wrap_bottom(); @@ -52,7 +45,8 @@ pub fn i2c_resume_after_error(pio_sm: &mut PioSm) { pio_sm.pio.wfo(rp_pio::SFR_IRQ_SFR_IRQ, pio_sm.sm_bitmask()); while i2c_check_error(pio_sm) { // wait for the IRQ to report cleared. This in not necessary on the RP2040, but because - // our core can run much faster than the PIO we have to wait for the PIO to catch up to the core's state. + // our core can run much faster than the PIO we have to wait for the PIO to catch up to the core's + // state. } } pub fn i2c_rx_enable(pio_sm: &mut PioSm, en: bool) { @@ -60,10 +54,11 @@ pub fn i2c_rx_enable(pio_sm: &mut PioSm, en: bool) { unsafe { let baseval = pio_sm.pio.base().add(rp_pio::SFR_SM0_SHIFTCTRL.offset() + sm_offset).read_volatile(); let bitval = pio_sm.pio.ms(rp_pio::SFR_SM0_SHIFTCTRL_AUTO_PUSH, 1); - pio_sm.pio.base().add(rp_pio::SFR_SM0_SHIFTCTRL.offset() + sm_offset).write_volatile( - baseval & !bitval - | if en {bitval} else {0} - ); + pio_sm + .pio + .base() + .add(rp_pio::SFR_SM0_SHIFTCTRL.offset() + sm_offset) + .write_volatile(baseval & !bitval | if en { bitval } else { 0 }); } } pub fn i2c_put16(pio_sm: &mut PioSm, data: u16) { @@ -75,7 +70,7 @@ pub fn i2c_put16(pio_sm: &mut PioSm, data: u16) { pub fn i2c_put_or_err(pio_sm: &mut PioSm, data: u16) { while pio_sm.sm_txfifo_is_full() { if i2c_check_error(pio_sm) { - return + return; } } if i2c_check_error(pio_sm) { @@ -83,14 +78,12 @@ pub fn i2c_put_or_err(pio_sm: &mut PioSm, data: u16) { } pio_sm.sm_txfifo_push_u16_msb(data); } -pub fn i2c_get(pio_sm: &mut PioSm) -> u8 { - pio_sm.sm_rxfifo_pull_u8_lsb() as u8 -} +pub fn i2c_get(pio_sm: &mut PioSm) -> u8 { pio_sm.sm_rxfifo_pull_u8_lsb() as u8 } const PIO_I2C_ICOUNT_LSB: u16 = 10; -const PIO_I2C_FINAL_LSB: u16 = 9; +const PIO_I2C_FINAL_LSB: u16 = 9; #[allow(dead_code)] -const PIO_I2C_DATA_LSB: u16 = 1; -const PIO_I2C_NAK_LSB: u16 = 0; +const PIO_I2C_DATA_LSB: u16 = 1; +const PIO_I2C_NAK_LSB: u16 = 0; const I2C_SC0_SD0: usize = 0; #[allow(dead_code)] const I2C_SC0_SD1: usize = 1; @@ -117,14 +110,19 @@ pub fn i2c_repstart(pio_sm: &mut PioSm, set_scl_sda_program_instructions: &[u16; } pub fn i2c_wait_idle(pio_sm: &mut PioSm) { pio_sm.pio.wfo(rp_pio::SFR_FDEBUG_TXSTALL, pio_sm.sm_bitmask()); - while ((pio_sm.pio.rf(rp_pio::SFR_FDEBUG_TXSTALL) & pio_sm.sm_bitmask()) == 0) - || i2c_check_error(pio_sm) { + while ((pio_sm.pio.rf(rp_pio::SFR_FDEBUG_TXSTALL) & pio_sm.sm_bitmask()) == 0) || i2c_check_error(pio_sm) + { // busy loop } } /// returns false if there is an error; true if no error #[allow(dead_code)] -pub fn i2c_write_blocking(pio_sm: &mut PioSm, set_scl_sda_program_instructions: &[u16; 4], addr: u8, txbuf: &[u8]) -> bool { +pub fn i2c_write_blocking( + pio_sm: &mut PioSm, + set_scl_sda_program_instructions: &[u16; 4], + addr: u8, + txbuf: &[u8], +) -> bool { i2c_start(pio_sm, set_scl_sda_program_instructions); i2c_rx_enable(pio_sm, false); i2c_put16(pio_sm, ((addr as u16) << 2) | 1); @@ -135,9 +133,9 @@ pub fn i2c_write_blocking(pio_sm: &mut PioSm, set_scl_sda_program_instructions: } if !pio_sm.sm_txfifo_is_full() { if let Some(&d) = txbuf_iter.next() { - i2c_put_or_err(pio_sm, - (d as u16) << PIO_I2C_DATA_LSB | - if txbuf_iter.peek().is_none() { 1 } else { 0 } + i2c_put_or_err( + pio_sm, + (d as u16) << PIO_I2C_DATA_LSB | if txbuf_iter.peek().is_none() { 1 } else { 0 }, ); } else { break; @@ -155,7 +153,12 @@ pub fn i2c_write_blocking(pio_sm: &mut PioSm, set_scl_sda_program_instructions: } } /// returns false if there is an error; true if no error -pub fn i2c_read_blocking(pio_sm: &mut PioSm, set_scl_sda_program_instructions: &[u16; 4], addr: u8, rxbuf: &mut [u8]) -> bool { +pub fn i2c_read_blocking( + pio_sm: &mut PioSm, + set_scl_sda_program_instructions: &[u16; 4], + addr: u8, + rxbuf: &mut [u8], +) -> bool { report_api(0x12C0_0000); i2c_start(pio_sm, set_scl_sda_program_instructions); @@ -180,12 +183,9 @@ pub fn i2c_read_blocking(pio_sm: &mut PioSm, set_scl_sda_program_instructions: & report_api(0x12C0_0000 + ((tx_remain as u32) << 8) | len as u32); if (tx_remain != 0) && !pio_sm.sm_txfifo_is_full() { tx_remain -= 1; - i2c_put16(pio_sm, - (0xff << 1) - | if tx_remain != 0 { 0 } else { - 1 << PIO_I2C_FINAL_LSB - | 1 << PIO_I2C_NAK_LSB - } + i2c_put16( + pio_sm, + (0xff << 1) | if tx_remain != 0 { 0 } else { 1 << PIO_I2C_FINAL_LSB | 1 << PIO_I2C_NAK_LSB }, ); } if !pio_sm.sm_rxfifo_is_empty() { @@ -231,7 +231,7 @@ pub fn i2c_test() -> bool { let mut pio_ss = PioSharedState::new(); let mut pio_sm = pio_ss.alloc_sm().unwrap(); pio_sm.sm_set_enabled(false); // stop the machine from running so we can initialize it - + #[rustfmt::skip] let i2c_prog = pio_proc::pio_asm!( ".side_set 1 opt pindirs", @@ -304,7 +304,7 @@ pub fn i2c_test() -> bool { let prog_i2c = LoadedProg::load_with_entrypoint(i2c_prog.program, ep, &mut pio_ss).unwrap(); i2c_init(&mut pio_sm, &prog_i2c, PIN_SDA, PIN_SCL); report_api(0x012C_3333); - + #[rustfmt::skip] let i2c_cmds_raw = pio_proc::pio_asm!( ".side_set 1 opt", // Assemble a table of instructions which software can select from, and pass diff --git a/libs/xous-pio/src/pio_tests/nec.rs b/libs/xous-pio/src/pio_tests/nec.rs index f353566f2..ed82ceec2 100644 --- a/libs/xous-pio/src/pio_tests/nec.rs +++ b/libs/xous-pio/src/pio_tests/nec.rs @@ -1,9 +1,10 @@ -use crate::*; use utralib::utra::rp_pio; + use super::report_api; +use crate::*; const SIM_SCALE_FACTOR: f32 = 200.0; // speedup over real time to have simulation finish in a reasonable amount of time -const BURST_PERIOD: f32 = 562.5e-6 / SIM_SCALE_FACTOR; +const BURST_PERIOD: f32 = 562.5e-6 / SIM_SCALE_FACTOR; const CARRIER_PERIOD: f32 = 38.222e3 * SIM_SCALE_FACTOR; pub fn carrier_burst_init(pio_sm: &mut PioSm, program: &LoadedProg, pin: usize, freq: f32) { pio_sm.sm_set_enabled(false); @@ -12,8 +13,7 @@ pub fn carrier_burst_init(pio_sm: &mut PioSm, program: &LoadedProg, pin: usize, // Map the SET pin group to one pin, namely the `pin` // parameter to this function. // - pio_sm.config_set_set_pins (pin, 1); - + pio_sm.config_set_set_pins(pin, 1); // Set the pin direction to output at the PIO pio_sm.sm_set_pindirs_with_mask(pin, 1); @@ -43,9 +43,10 @@ pub fn carrier_control_init(pio_sm: &mut PioSm, program: &LoadedProg, tick_rate: // configure the output shift register // pio_sm.config_set_out_shift( - true, // shift right - false, // disable autopull - bits_per_frame); + true, // shift right + false, // disable autopull + bits_per_frame, + ); // join the FIFOs to make a single large transmit FIFO // @@ -68,7 +69,7 @@ pub fn carrier_control_init(pio_sm: &mut PioSm, program: &LoadedProg, tick_rate: pub fn nec_tx_init(pio_ss: &mut PioSharedState, pin_num: usize) -> PioSm { let mut burst_sm = pio_ss.alloc_sm().unwrap(); let mut control_sm = pio_ss.alloc_sm().unwrap(); - + #[rustfmt::skip] let carrier_burst_prog = pio_proc::pio_asm!( // Generate bursts of carrier. // @@ -89,7 +90,7 @@ pub fn nec_tx_init(pio_ss: &mut PioSharedState, pin_num: usize) -> PioSm { " jmp x--, cycle_loop ", // (1 more cycle) ".wrap ", ); - + #[rustfmt::skip] let carrier_control_prog = pio_proc::pio_asm!( // Transmit an encoded 32-bit frame in NEC IR format. // @@ -132,19 +133,14 @@ pub fn nec_tx_init(pio_ss: &mut PioSharedState, pin_num: usize) -> PioSm { carrier_burst_init(&mut burst_sm, &carrier_prog, pin_num, CARRIER_PERIOD); let control_prog = LoadedProg::load(carrier_control_prog.program, pio_ss).unwrap(); - carrier_control_init( - &mut control_sm, - &control_prog, - 2.0 * (1.0 / BURST_PERIOD), - 32 - ); + carrier_control_init(&mut control_sm, &control_prog, 2.0 * (1.0 / BURST_PERIOD), 32); control_sm } // Create a frame in `NEC` format from the provided 8-bit address and data // // Returns: a 32-bit encoded frame -pub fn nec_encode_frame(address: u8, data: u8) -> u32{ +pub fn nec_encode_frame(address: u8, data: u8) -> u32 { // a normal 32-bit frame is encoded as address, inverted address, data, inverse data, address as u32 | (address as u32 ^ 0xff) << 8 | (data as u32) << 16 | (data as u32 ^ 0xff) << 24 } @@ -156,9 +152,10 @@ pub fn nec_receive_init(pio_sm: &mut PioSm, program: &LoadedProg, pin: usize, bu // configure the Input Shift Register // pio_sm.config_set_in_shift( - true, // shift right - true, // enable autopush - 32); // autopush after 32 bits + true, // shift right + true, // enable autopush + 32, + ); // autopush after 32 bits // join the FIFOs to make a single large receive FIFO // @@ -189,7 +186,7 @@ pub fn nec_receive_init(pio_sm: &mut PioSm, program: &LoadedProg, pin: usize, bu pub fn nec_rx_init(pio_ss: &mut PioSharedState, pin_num: usize) -> PioSm { let mut rx_sm = pio_ss.alloc_sm().unwrap(); - + #[rustfmt::skip] let nec_receive = pio_proc::pio_asm!( // Decode IR frames in NEC format and push 32-bit words to the input FIFO. // @@ -234,19 +231,16 @@ pub fn nec_rx_init(pio_ss: &mut PioSharedState, pin_num: usize) -> PioSm { rx_sm } - // Validate a 32-bit frame and store the address and data at the locations // provided. // // Returns: `true` if the frame was valid, otherwise `false` pub fn nec_decode_frame(frame: u32, addr: &mut u8, d: &mut u8) -> bool { - let [address, inverted_address, data, inverted_data] = frame.to_le_bytes(); // a valid (non-extended) 'NEC' frame should contain 8 bit // address, inverted address, data and inverted data - if address != (inverted_address ^ 0xff) || - data != (inverted_data ^ 0xff) { + if address != (inverted_address ^ 0xff) || data != (inverted_data ^ 0xff) { return false; } @@ -292,4 +286,3 @@ pub fn nec_ir_loopback_test() { report_api(0x1300_600D); } - diff --git a/libs/xous-pio/src/pio_tests/spi.rs b/libs/xous-pio/src/pio_tests/spi.rs index b9228bae1..e77649a82 100644 --- a/libs/xous-pio/src/pio_tests/spi.rs +++ b/libs/xous-pio/src/pio_tests/spi.rs @@ -1,13 +1,10 @@ use utralib::generated::utra::rp_pio; -use crate::*; + use super::report_api; +use crate::*; #[inline(always)] -pub fn pio_spi_write8_read8_blocking ( - pio_sm: &mut PioSm, - src: &[u8], - dst: &mut [u8], -) { +pub fn pio_spi_write8_read8_blocking(pio_sm: &mut PioSm, src: &[u8], dst: &mut [u8]) { assert!(src.len() == dst.len(), "src and dst arrays are not the same length!"); let mut src_iter = src.iter(); @@ -50,7 +47,7 @@ pub fn pio_spi_write8_read8_blocking ( rx_done = true; } if tx_done && rx_done { - break + break; } } } @@ -89,7 +86,7 @@ pub fn pio_spi_init( cpol: bool, pin_sck: usize, pin_mosi: usize, - pin_miso: usize + pin_miso: usize, ) { pio_sm.sm_set_enabled(false); // this applies a default config to the PioSm object that is relevant to the program @@ -103,13 +100,10 @@ pub fn pio_spi_init( pio_sm.config_set_clkdiv(clkdiv); // MOSI, SCK output are low, MISO is input - pio_sm.sm_set_pins_with_mask( - 0, - (1 << pin_sck) | (1 << pin_mosi) - ); + pio_sm.sm_set_pins_with_mask(0, (1 << pin_sck) | (1 << pin_mosi)); pio_sm.sm_set_pindirs_with_mask( (1 << pin_sck) | (1 << pin_mosi), - (1 << pin_sck) | (1 << pin_mosi) | (1 << pin_miso) + (1 << pin_sck) | (1 << pin_mosi) | (1 << pin_miso), ); pio_sm.gpio_set_outover(pin_sck, cpol); @@ -136,12 +130,14 @@ pub fn spi_test() -> bool { let mut pio_sm = pio_ss.alloc_sm().unwrap(); // spi_cpha0 example + #[rustfmt::skip] let spi_cpha0_prog = pio_proc::pio_asm!( ".side_set 1", "out pins, 1 side 0 [1]", "in pins, 1 side 1 [1]", ); // spi_cpha1 example + #[rustfmt::skip] let spi_cpha1_prog = pio_proc::pio_asm!( ".side_set 1", "out x, 1 side 0", // Stall here on empty (keep SCK deasserted) @@ -169,7 +165,7 @@ pub fn spi_test() -> bool { cpol, PIN_SCK, PIN_MOSI, - PIN_MISO + PIN_MISO, ); report_api(0x05D1_0003); if spi_test_core(&mut pio_sm) == false { @@ -186,7 +182,7 @@ pub fn spi_test() -> bool { cpol, PIN_SCK, PIN_MOSI, - PIN_MISO + PIN_MISO, ); report_api(0x05D1_0005); if spi_test_core(&mut pio_sm) == false { @@ -211,4 +207,4 @@ pub fn spi_test() -> bool { } assert!(passing); passing -} \ No newline at end of file +} diff --git a/libs/xous-pio/src/pio_tests/units.rs b/libs/xous-pio/src/pio_tests/units.rs index 373c17535..df8b0ef34 100644 --- a/libs/xous-pio/src/pio_tests/units.rs +++ b/libs/xous-pio/src/pio_tests/units.rs @@ -1,7 +1,8 @@ use pio::RP2040_MAX_PROGRAM_SIZE; -use utralib::utra::rp_pio::{self, SFR_FDEBUG, SFR_FLEVEL, SFR_FSTAT, SFR_DBG_CFGINFO}; -use crate::*; +use utralib::utra::rp_pio::{self, SFR_DBG_CFGINFO, SFR_FDEBUG, SFR_FLEVEL, SFR_FSTAT}; + use super::report_api; +use crate::*; /// Test the sticky out bits pub fn sticky_test() { @@ -33,15 +34,15 @@ pub fn sticky_test() { report_api(0x51C2_0001); pio_ss.clear_instruction_memory(); report_api(0x51C2_0002); - let mut sm_a = unsafe{pio_ss.force_alloc_sm(1).unwrap()}; + let mut sm_a = unsafe { pio_ss.force_alloc_sm(1).unwrap() }; report_api(0x51C2_0003); - let mut sm_b = unsafe{pio_ss.force_alloc_sm(2).unwrap()}; + let mut sm_b = unsafe { pio_ss.force_alloc_sm(2).unwrap() }; report_api(0x51C2_0004); sm_a.sm_set_enabled(false); report_api(0x51C2_0005); sm_b.sm_set_enabled(false); report_api(0x51C2_0006); - + #[rustfmt::skip] let a_code = pio_proc::pio_asm!( "set pins, 1", "set pins, 2", @@ -51,6 +52,7 @@ pub fn sticky_test() { "nop" ); let a_prog = LoadedProg::load(a_code.program, &mut pio_ss).unwrap(); + #[rustfmt::skip] let b_code = pio_proc::pio_asm!( // bit 4 indicates enable; bit 3 is the "B" machine flag. bits 2:0 are the payload "nop", @@ -60,7 +62,8 @@ pub fn sticky_test() { "set pins, 0x1D", // with enable "set pins, 0x0E", // without enable ); - // note: this loads using sm_a so we can share the "used" vector state, but the code is global across all SM's + // note: this loads using sm_a so we can share the "used" vector state, but the code is global across all + // SM's let b_prog = LoadedProg::load(b_code.program, &mut pio_ss).unwrap(); report_api(0x51C2_0007); @@ -89,9 +92,9 @@ pub fn sticky_test() { sm_a.pio.wo( rp_pio::SFR_CTRL, sm_a.pio.ms(rp_pio::SFR_CTRL_CLKDIV_RESTART, sm_a.sm_bitmask()) - | sm_a.pio.ms(rp_pio::SFR_CTRL_RESTART, sm_a.sm_bitmask()) - | sm_a.pio.ms(rp_pio::SFR_CTRL_CLKDIV_RESTART, sm_b.sm_bitmask()) - | sm_a.pio.ms(rp_pio::SFR_CTRL_RESTART, sm_b.sm_bitmask()) + | sm_a.pio.ms(rp_pio::SFR_CTRL_RESTART, sm_a.sm_bitmask()) + | sm_a.pio.ms(rp_pio::SFR_CTRL_CLKDIV_RESTART, sm_b.sm_bitmask()) + | sm_a.pio.ms(rp_pio::SFR_CTRL_RESTART, sm_b.sm_bitmask()), ); while (sm_a.pio.r(rp_pio::SFR_CTRL) & !0xF) != 0 { // wait for the bits to self-reset to acknowledge that the command has executed @@ -101,7 +104,7 @@ pub fn sticky_test() { sm_a.pio.wo( rp_pio::SFR_CTRL, sm_a.pio.ms(rp_pio::SFR_CTRL_EN, sm_a.sm_bitmask()) - | sm_a.pio.ms(rp_pio::SFR_CTRL_EN, sm_b.sm_bitmask()) + | sm_a.pio.ms(rp_pio::SFR_CTRL_EN, sm_b.sm_bitmask()), ); // wait for it to run for i in 0..16 { @@ -126,9 +129,9 @@ pub fn sticky_test() { sm_a.pio.wo( rp_pio::SFR_CTRL, sm_a.pio.ms(rp_pio::SFR_CTRL_CLKDIV_RESTART, sm_a.sm_bitmask()) - | sm_a.pio.ms(rp_pio::SFR_CTRL_RESTART, sm_a.sm_bitmask()) - | sm_a.pio.ms(rp_pio::SFR_CTRL_CLKDIV_RESTART, sm_b.sm_bitmask()) - | sm_a.pio.ms(rp_pio::SFR_CTRL_RESTART, sm_b.sm_bitmask()) + | sm_a.pio.ms(rp_pio::SFR_CTRL_RESTART, sm_a.sm_bitmask()) + | sm_a.pio.ms(rp_pio::SFR_CTRL_CLKDIV_RESTART, sm_b.sm_bitmask()) + | sm_a.pio.ms(rp_pio::SFR_CTRL_RESTART, sm_b.sm_bitmask()), ); while (sm_a.pio.r(rp_pio::SFR_CTRL) & !0xF) != 0 { // wait for the bits to self-reset to acknowledge that the command has executed @@ -138,7 +141,7 @@ pub fn sticky_test() { sm_a.pio.wo( rp_pio::SFR_CTRL, sm_a.pio.ms(rp_pio::SFR_CTRL_EN, sm_a.sm_bitmask()) - | sm_a.pio.ms(rp_pio::SFR_CTRL_EN, sm_b.sm_bitmask()) + | sm_a.pio.ms(rp_pio::SFR_CTRL_EN, sm_b.sm_bitmask()), ); // wait for it to run for i in 0..16 { @@ -162,7 +165,7 @@ pub fn sticky_test() { pub fn delay(count: usize) { let mut target = [0u32; 1]; for i in 0..count * 2 { - unsafe {target.as_mut_ptr().write_volatile(i as u32)} + unsafe { target.as_mut_ptr().write_volatile(i as u32) } core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst); } } @@ -195,9 +198,9 @@ fn wait_rx_or_fail(sm: &mut PioSm, rxval: u32, mask: Option, timeout: Optio } fn wait_gpio_or_fail(ss: &PioSharedState, pinval: u32, mask: Option, timeout: Option) { - #[cfg(feature="rp2040")] + #[cfg(feature = "rp2040")] let io_mask = 0x3FFF_FFFF; - #[cfg(not(feature="rp2040"))] + #[cfg(not(feature = "rp2040"))] let io_mask = 0xFFFF_FFFF; let mask = mask.unwrap_or(io_mask); @@ -242,9 +245,9 @@ fn wait_irq_exactly_or_fail(sm: &PioSm, irq_index: usize, timeout: Option /// corner cases pub fn corner_cases() { report_api(0xF1F0_0000); - #[cfg(feature="rp2040")] + #[cfg(feature = "rp2040")] let io_mask = 0x3FFF_FFFF; - #[cfg(not(feature="rp2040"))] + #[cfg(not(feature = "rp2040"))] let io_mask = 0xFFFF_FFFF; report_api(0xcc00_0000); @@ -254,7 +257,7 @@ pub fn corner_cases() { let mut sm_a = pio_ss.alloc_sm().unwrap(); pio_ss.clear_instruction_memory(); pio_ss.pio.rmwf(rp_pio::SFR_CTRL_EN, 0); - + #[rustfmt::skip] let a_code = pio_proc::pio_asm!( "out y, 0", // 32 is coded as 0. If you put 32 in, this changes the "y" source to "null"! "in y, 0", // 32 is coded as 0 @@ -288,6 +291,7 @@ pub fn corner_cases() { report_api(0xcc00_1111); // exec corner case: EXEC instruction should side-effect the PC ----------------------- + #[rustfmt::skip] let a_code = pio_proc::pio_asm!( "set x, 17", // 18 "exec_loop:", // normally you'd want out exec, 32 to keep things simple but out exec, 16 will cause two instructions to run per pull of the fifo @@ -362,6 +366,7 @@ pub fn corner_cases() { report_api(0xcc00_2222); // exec corner case: exec can clear stalled IRQ ----------------------- let mut sm_b = pio_ss.alloc_sm().unwrap(); // run this on SM_B so we can test `rel` irqs + #[rustfmt::skip] let b_code = pio_proc::pio_asm!( "set x, 12", // 0x1d put an initial value in x "wait 1 irq 6 rel", // 0x1e this should stall <--- wait target @@ -378,7 +383,7 @@ pub fn corner_cases() { sm_b.sm_clear_fifos(); // ensure the fifos are cleared for this test sm_b.sm_set_enabled(true); - wait_addr_or_fail(&sm_b, 0x1e, Some(100)); // manual match to wait target + wait_addr_or_fail(&sm_b, 0x1e, Some(100)); // manual match to wait target let exec_set_irq6 = pio_proc::pio_asm!("irq set 6 rel").program.code[0]; sm_b.sm_exec(exec_set_irq6); wait_rx_or_fail(&mut sm_b, 12, None, None); @@ -388,6 +393,7 @@ pub fn corner_cases() { // exec corner case: exec can clear stalled IRQ -------------------------------- // exec corner case: exec can break out of stalled instruction with JMP -------- + #[rustfmt::skip] let b_code = pio_proc::pio_asm!( "top:", " set y, 12", // 0x1a put an initial value in y @@ -403,37 +409,37 @@ pub fn corner_cases() { // check multiple clkdiv cases to capture corners in the irq_stb logic let divs = [1.0f32, 3.5f32, 4.0f32]; let mut divstate = 0x3333u32; - divs.map( - |clkdiv: f32| { - report_api(0xcc00_0000 + divstate); - divstate += 0x1111; - sm_b.sm_set_enabled(false); - b_prog.setup_default_config(&mut sm_b); - sm_b.config_set_out_pins(0, 32); - sm_b.config_set_clkdiv(clkdiv); - sm_b.config_set_out_shift(true, true, 32); - sm_b.config_set_in_shift(false, true, 32); - sm_b.sm_init(b_prog.entry()); - sm_b.sm_clear_fifos(); // ensure the fifos are cleared for this test - sm_b.sm_set_enabled(true); + divs.map(|clkdiv: f32| { + report_api(0xcc00_0000 + divstate); + divstate += 0x1111; + sm_b.sm_set_enabled(false); + b_prog.setup_default_config(&mut sm_b); + sm_b.config_set_out_pins(0, 32); + sm_b.config_set_clkdiv(clkdiv); + sm_b.config_set_out_shift(true, true, 32); + sm_b.config_set_in_shift(false, true, 32); + sm_b.sm_init(b_prog.entry()); + sm_b.sm_clear_fifos(); // ensure the fifos are cleared for this test + sm_b.sm_set_enabled(true); - wait_addr_or_fail(&sm_b, 0x1b, Some(100)); // manual match to wait target - let exec_set_irq6 = pio_proc::pio_asm!("irq set 6 rel").program.code[0]; - sm_b.sm_exec(exec_set_irq6); - // exactly one entry should go into the Rx fifo. If the irq does not self-clear after exec, we will get more than one, and the next check fails. - wait_rx_or_fail(&mut sm_b, 12, None, None); + wait_addr_or_fail(&sm_b, 0x1b, Some(100)); // manual match to wait target + let exec_set_irq6 = pio_proc::pio_asm!("irq set 6 rel").program.code[0]; + sm_b.sm_exec(exec_set_irq6); + // exactly one entry should go into the Rx fifo. If the irq does not self-clear after exec, we will + // get more than one, and the next check fails. + wait_rx_or_fail(&mut sm_b, 12, None, None); - // program will loop back to top, and will be stuck at wait again - wait_addr_or_fail(&sm_b, 0x1b, Some(100)); // manual match to wait target - let exec_jmp_bypass = pio_proc::pio_asm!("jmp y--, 0x1e").program.code[0]; - sm_b.sm_exec(exec_jmp_bypass); - wait_rx_or_fail(&mut sm_b, !11, None, None); - } - ); + // program will loop back to top, and will be stuck at wait again + wait_addr_or_fail(&sm_b, 0x1b, Some(100)); // manual match to wait target + let exec_jmp_bypass = pio_proc::pio_asm!("jmp y--, 0x1e").program.code[0]; + sm_b.sm_exec(exec_jmp_bypass); + wait_rx_or_fail(&mut sm_b, !11, None, None); + }); // exec corner case: OUT EXEC can clear stalled IRQ ------------------- pio_ss.clear_instruction_memory(); report_api(0xcc00_6666); + #[rustfmt::skip] let a_code = pio_proc::pio_asm!( "top:", " set y, 5", // 0x19 put an initial value in y @@ -456,16 +462,16 @@ pub fn corner_cases() { sm_a.sm_clear_fifos(); // ensure the fifos are cleared for this test sm_a.sm_set_enabled(true); - wait_addr_or_fail(&sm_a, 0x1a, Some(100)); // manual match to wait target + wait_addr_or_fail(&sm_a, 0x1a, Some(100)); // manual match to wait target let exec_set_irq2 = pio_proc::pio_asm!("irq set 2 rel").program.code[0]; sm_a.sm_txfifo_push_u32(exec_set_irq2 as u32); wait_rx_or_fail(&mut sm_a, 5, None, None); // exec corner case: writing over stalled instruction breaks stall ---------- - wait_addr_or_fail(&sm_a, 0x1a, Some(100)); // manual match to wait target + wait_addr_or_fail(&sm_a, 0x1a, Some(100)); // manual match to wait target let nop = pio_proc::pio_asm!("mov y, y").program.code[0]; sm_a.sm_txfifo_push_u32(nop as u32); // break the out exec stall, but get stuck at "wait 1 irq 2 rel" - wait_addr_or_fail(&sm_a, 0x1b, Some(100)); // manual match to wait target + wait_addr_or_fail(&sm_a, 0x1b, Some(100)); // manual match to wait target let set_y_31 = pio_proc::pio_asm!("set y, 31").program.code[0]; // slot this over the blocked instruction: 0x1b == 27 pio_ss.pio.wfo(rp_pio::SFR_INSTR_MEM27_INSTR, set_y_31 as u32); @@ -488,6 +494,7 @@ pub fn corner_cases() { // IO corner case: side-set happens when SM is stalled -------------------- pio_ss.clear_instruction_memory(); report_api(0xcc00_7777); + #[rustfmt::skip] let a_code = pio_proc::pio_asm!( ".side_set 1 opt" "top:", @@ -527,6 +534,7 @@ pub fn corner_cases() { pio_ss.clear_instruction_memory(); report_api(0xcc00_8888); // this one will toggle pins via a side-set + #[rustfmt::skip] let a_code = pio_proc::pio_asm!( ".side_set 1 opt" " mov osr, null", // 0x1b load OSR with 0 @@ -562,6 +570,7 @@ pub fn corner_cases() { pio_ss.clear_instruction_memory(); sm_a.sm_set_enabled(false); report_api(0xcc00_9999); + #[rustfmt::skip] let a_code = pio_proc::pio_asm!( ".side_set 5", " out pins, 16 side 0x1A", // 0x1d @@ -574,7 +583,7 @@ pub fn corner_cases() { sm_a.config_set_clkdiv(1.15); sm_a.config_set_out_shift(true, true, 16); sm_a.config_set_out_pins(24, 16); // should wrap to lower 8 bits - sm_a.config_set_set_pins(28, 5); // should wrap 1 bit over + sm_a.config_set_set_pins(28, 5); // should wrap 1 bit over sm_a.config_set_sideset_pins(30); // should wrap 3 bits over sm_a.sm_init(a_prog.entry()); sm_a.sm_clear_fifos(); @@ -619,8 +628,9 @@ pub fn corner_cases() { sm_a.sm_set_enabled(false); report_api(0xcc00_aaaa); // set all the output - #[cfg(feature="rp2040")] + #[cfg(feature = "rp2040")] sm_a.sm_set_pindirs_with_mask(0xffff_ffff, 0xffff_ffff); + #[rustfmt::skip] let a_code = pio_proc::pio_asm!( " out pins, 0", " in pins, 24", @@ -642,12 +652,13 @@ pub fn corner_cases() { sm_a.sm_set_enabled(true); sm_a.sm_txfifo_push_u32(0xaa12_3455); - // the differences from the pushed is due to the test bench harnessing, some gpio pins are not looped back or are modified by the test bench for other tests! - // this value is just read back manually from the waveforms - #[cfg(not(feature="rp2040"))] - let model = rot_left(0x2a12_345D, 32-9) & 0xFF_FFFF; - #[cfg(feature="rp2040")] - let model = rot_left(0xaa12_3455 & io_mask & !(1 << 25), 32-9) & 0xFF_FFFF; // top 2 bits don't exist on rp2040, bit 25 is for LED + // the differences from the pushed is due to the test bench harnessing, some gpio pins are not looped back + // or are modified by the test bench for other tests! this value is just read back manually from the + // waveforms + #[cfg(not(feature = "rp2040"))] + let model = rot_left(0x2a12_345D, 32 - 9) & 0xFF_FFFF; + #[cfg(feature = "rp2040")] + let model = rot_left(0xaa12_3455 & io_mask & !(1 << 25), 32 - 9) & 0xFF_FFFF; // top 2 bits don't exist on rp2040, bit 25 is for LED report_api(model); wait_rx_or_fail(&mut sm_a, model, None, None); @@ -661,14 +672,15 @@ pub fn corner_cases() { // autopush/pull cases ---------------------------------------------------- // 1. auto p/p does not happen while SM is disabled - // 2. auto pull happens on an EXEC, even when SM is disabled - // a. case of EXEC is an instruction that would do a pull, but stalled instruction does not do pull - // b. case of EXEC is an instruction that is NOP, but stalled instruction does do pull + // 2. auto pull happens on an EXEC, even when SM is disabled a. case of EXEC is an instruction that would + // do a pull, but stalled instruction does not do pull b. case of EXEC is an instruction that is NOP, + // but stalled instruction does do pull // 3. OUT with empty OSR but nonempty TX FIFO should not set TX stall flag, + 1-cycle stall // 4. EXEC of OUT 32 to disabled SM, with empty OSR + filled FIFO consumes two words from FIFO pio_ss.clear_instruction_memory(); sm_a.sm_set_enabled(false); report_api(0xcc00_bbbb); + #[rustfmt::skip] let a_code = pio_proc::pio_asm!( " out x, 0", " in x, 0", // loop back for testing @@ -711,6 +723,7 @@ pub fn corner_cases() { pio_ss.clear_instruction_memory(); sm_a.sm_set_enabled(false); report_api(0xcc04_bbbb); + #[rustfmt::skip] let a_code = pio_proc::pio_asm!( " wait 1 irq 0", // this will do a wait " in x, 0", @@ -753,6 +766,7 @@ pub fn corner_cases() { report_api(0xcc08_cccc); pio_ss.clear_instruction_memory(); sm_a.sm_set_enabled(false); + #[rustfmt::skip] let a_code = pio_proc::pio_asm!( " out x, 0", " in x, 0", // loop back for testing @@ -776,9 +790,7 @@ pub fn corner_cases() { report_api(0xcc00_600d); } -fn rot_left(word: u32, count: u32) -> u32 { - (word << count) | ((word >> (32-count)) & ((1 << count) - 1)) -} +fn rot_left(word: u32, count: u32) -> u32 { (word << count) | ((word >> (32 - count)) & ((1 << count) - 1)) } pub fn instruction_tests() { report_api(0x1c5f_0000); @@ -788,6 +800,7 @@ pub fn instruction_tests() { pio_ss.pio.rmwf(rp_pio::SFR_CTRL_EN, 0); // check mov, irq, wait corners ------------------------------------------------------ + #[rustfmt::skip] let a_code = pio_proc::pio_asm!( "irq set 7", // just to make sure this is behaving correctly (staring at waveforms) "out x, 0", @@ -805,8 +818,9 @@ pub fn instruction_tests() { 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); - // if this is set to <2.0 the "retrograde PC" check at the bottom needs to be commented out, because the PC moves too fast for the APB to reliably sample - // also, this should not be an even multiple, otherwise we can end up sampling the exact same phase over and over again. + // if this is set to <2.0 the "retrograde PC" check at the bottom needs to be commented out, because the + // PC moves too fast for the APB to reliably sample also, this should not be an even multiple, + // otherwise we can end up sampling the exact same phase over and over again. sm_a.config_set_clkdiv(5.0); sm_a.config_set_out_shift(true, true, 32); sm_a.config_set_in_shift(true, true, 32); @@ -822,25 +836,24 @@ pub fn instruction_tests() { rp_pio::SFR_SM0_EXECCTRL, pio_ss.pio.zf( rp_pio::SFR_SM0_EXECCTRL_STATUS_SEL, - pio_ss.pio.zf(rp_pio::SFR_SM0_EXECCTRL_STATUS_N, - pio_ss.pio.r(rp_pio::SFR_SM0_EXECCTRL) - ) - ) - | pio_ss.pio.ms(rp_pio::SFR_SM0_EXECCTRL_STATUS_SEL, status_sel as u32) - | pio_ss.pio.ms(rp_pio::SFR_SM0_EXECCTRL_STATUS_N, level as u32) + pio_ss.pio.zf(rp_pio::SFR_SM0_EXECCTRL_STATUS_N, pio_ss.pio.r(rp_pio::SFR_SM0_EXECCTRL)), + ) | pio_ss.pio.ms(rp_pio::SFR_SM0_EXECCTRL_STATUS_SEL, status_sel as u32) + | pio_ss.pio.ms(rp_pio::SFR_SM0_EXECCTRL_STATUS_N, level as u32), ); - let expected_value = if 2 < level {0xFFFF_FFFF} else {0}; + let expected_value = if 2 < level { 0xFFFF_FFFF } else { 0 }; // reset the PC let mut a = pio::Assembler::<32>::new(); let mut initial_label = a.label_at_offset(a_prog.entry() as u8); a.jmp(pio::JmpCondition::Always, &mut initial_label); - let p= a.assemble_program(); + let p = a.assemble_program(); sm_a.sm_exec(p.code[p.origin.unwrap_or(0) as usize]); pio_ss.pio.wfo(rp_pio::SFR_CTRL_RESTART, sm_a.sm_bitmask()); sm_a.sm_set_enabled(true); - for i in 1..=5 { // put 5 entries in; the first two should go to the Rx fifo, 1 should be in the state machine, 2 remaining in the FIFO + for i in 1..=5 { + // put 5 entries in; the first two should go to the Rx fifo, 1 should be in the state machine, 2 + // remaining in the FIFO sm_a.sm_txfifo_push_u32(0x1c5f_0000 + i); } // we should now have a level == 2 for both @@ -864,9 +877,9 @@ pub fn instruction_tests() { wait_rx_or_fail(&mut sm_a, 0x1c5f_0002, None, None); report_api(expected_value); wait_rx_or_fail(&mut sm_a, expected_value, None, None); - // retrograde movement of address would indicate we're in the loop (this might not work perfectly on real hardware due to synchronizers) - // commented out, because this test is too sensitive to external clock config - // wait_addr_or_fail(&sm_a, 31, None); + // retrograde movement of address would indicate we're in the loop (this might not work perfectly on + // real hardware due to synchronizers) commented out, because this test is too sensitive to + // external clock config wait_addr_or_fail(&sm_a, 31, None); // wait_addr_or_fail(&sm_a, 30, None); level += 1; @@ -885,6 +898,7 @@ pub fn instruction_tests() { pio_ss.pio.rmwf(rp_pio::SFR_CTRL_EN, 0); report_api(0x1c5f_1111); // all the jumps -------------------------------------------------------------- + #[rustfmt::skip] let a_code = pio_proc::pio_asm!( " set x, 3", "test_notx:", @@ -950,6 +964,7 @@ pub fn instruction_tests() { pio_ss.pio.rmwf(rp_pio::SFR_CTRL_EN, 0); report_api(0x1c5f_2222); // all the ISRs and OSRs (left shifting) ------------------------------------------------- + #[rustfmt::skip] let a_code = pio_proc::pio_asm!( "start:", "pull ifempty block", // 09 get a value from the Tx FIFO -> OSR @@ -1028,6 +1043,7 @@ pub fn instruction_tests() { pio_ss.pio.rmwf(rp_pio::SFR_CTRL_EN, 0); report_api(0x1c5f_3333); // all the MOVs (plus right shifting) ------------------------------------------------- + #[rustfmt::skip] let a_code = pio_proc::pio_asm!( "start:", "pull block", @@ -1077,12 +1093,12 @@ pub fn instruction_tests() { sm_a.sm_txfifo_push_u32(0b101_110 | 0xFF00_0000); // shifting right, so we'll take these LSBs; or something else at the top so we can detect if it shifted the other way wait_irq_exactly_or_fail(&sm_a, 0, None); wait_rx_or_fail(&mut sm_a, !0x1234_abcd, None, None); - wait_rx_or_fail(&mut sm_a, 0b100_011 << (32-6), None, None); + wait_rx_or_fail(&mut sm_a, 0b100_011 << (32 - 6), None, None); report_api(0x1c5f_3334); - #[cfg(not(feature="rp2040"))] - let gpio_val = 0b011_101 << (32-6); - #[cfg(feature="rp2040")] - let gpio_val = (0b011_101 << (32-6)) & !(1 << 25) & 0x3FFF_FFFF; + #[cfg(not(feature = "rp2040"))] + let gpio_val = 0b011_101 << (32 - 6); + #[cfg(feature = "rp2040")] + let gpio_val = (0b011_101 << (32 - 6)) & !(1 << 25) & 0x3FFF_FFFF; report_api(gpio_val); wait_gpio_or_fail(&pio_ss, gpio_val, None, None); // clear the IRQ wait @@ -1091,12 +1107,12 @@ pub fn instruction_tests() { // pins <- pins mov test report_api(0x1c5f_3335); wait_irq_exactly_or_fail(&sm_a, 1, None); - #[cfg(not(feature="rp2040"))] + #[cfg(not(feature = "rp2040"))] { let rbk_val = !rot_left(gpio_val | 0xC, 16); // 0xC is or'd in due to the i2c loopback pins wait_gpio_or_fail(&pio_ss, rbk_val, Some(!0x8000_000C), None); // ignore the pins not looped back by the test bench (2,3,31) } - #[cfg(feature="rp2040")] + #[cfg(feature = "rp2040")] { let rbk_val = !rot_left(gpio_val & 0x3FFF_FFFF | 3, 16); // lowest 2 bits read back back as 11 report_api(rbk_val); @@ -1122,6 +1138,7 @@ pub fn instruction_tests() { pio_ss.clear_instruction_memory(); pio_ss.pio.rmwf(rp_pio::SFR_CTRL_EN, 0); report_api(0x1c5f_4444); + #[rustfmt::skip] let a_code = pio_proc::pio_asm!( "start:", " set x, 0", @@ -1182,6 +1199,7 @@ pub fn restart_imm_test() { pio_ss.pio.rmwf(rp_pio::SFR_CTRL_EN, 0); let mut sm_a = pio_ss.alloc_sm().unwrap(); + #[rustfmt::skip] let a_code = pio_proc::pio_asm!( "set pins, 1", "set pins, 2", @@ -1207,7 +1225,7 @@ pub fn restart_imm_test() { let mut a = pio::Assembler::<32>::new(); a.out(pio::OutDestination::PINS, 16); - let p= a.assemble_program(); + let p = a.assemble_program(); // this should stall the state machine sm_a.sm_exec(p.code[p.origin.unwrap_or(0) as usize]); @@ -1243,9 +1261,9 @@ pub fn restart_imm_test() { pub fn fifo_join_test() -> bool { report_api(0xF1F0_0000); - #[cfg(feature="rp2040")] + #[cfg(feature = "rp2040")] let io_mask = 0x3FFF_FFFF; - #[cfg(not(feature="rp2040"))] + #[cfg(not(feature = "rp2040"))] let io_mask = 0xFFFF_FFFF; let mut pio_ss = PioSharedState::new(); @@ -1254,6 +1272,7 @@ pub fn fifo_join_test() -> bool { // test TX fifo with non-join. Simple program that just copies the TX fifo content to pins, then stalls. let mut sm_a = pio_ss.alloc_sm().unwrap(); + #[rustfmt::skip] let a_code = pio_proc::pio_asm!( "out pins, 32", ); @@ -1263,9 +1282,9 @@ pub fn fifo_join_test() -> bool { pio_ss.pio.wo(rp_pio::SFR_IRQ1_INTE, 0); a_prog.setup_default_config(&mut sm_a); sm_a.config_set_out_pins(0, 32); - #[cfg(not(feature="rp2040"))] + #[cfg(not(feature = "rp2040"))] sm_a.config_set_clkdiv(192.0); // slow down the machine so we can read out the values after writing them... - #[cfg(feature="rp2040")] + #[cfg(feature = "rp2040")] sm_a.config_set_clkdiv(32768.0); sm_a.config_set_out_shift(false, true, 0); sm_a.sm_init(a_prog.entry()); @@ -1273,7 +1292,8 @@ pub fn fifo_join_test() -> bool { report_api(0xF1F0_1111); // load up the TX fifo, count how many entries it takes until it is full - // note: full test requires manual inspection of waveform to confirm GPIO out has the expected report value. + // note: full test requires manual inspection of waveform to confirm GPIO out has the expected report + // value. let mut entries = 0; while !sm_a.sm_txfifo_is_full() { entries += 1; @@ -1332,7 +1352,7 @@ pub fn fifo_join_test() -> bool { sm_a.sm_init(a_prog.entry()); // ----------- now test with "margin" on the FIFOs. ---------- - #[cfg(not(feature="rp2040"))] + #[cfg(not(feature = "rp2040"))] { assert!(sm_a.sm_get_tx_fifo_margin() == 0); sm_a.sm_set_tx_fifo_margin(1); @@ -1341,7 +1361,8 @@ pub fn fifo_join_test() -> bool { // repeat, this time measuring the depth of the FIFO with margin let mut entries = 0; - // loop looks at the raw interrupt value, the asserts look at the feedback INTS value, so we have coverage of both views + // loop looks at the raw interrupt value, the asserts look at the feedback INTS value, so we have + // coverage of both views while (pio_ss.pio.rf(rp_pio::SFR_INTR_INTR_TXNFULL) & sm_a.sm_bitmask()) != 0 { entries += 1; sm_a.sm_txfifo_push_u32(0xF1F0_0000 + entries); @@ -1373,7 +1394,7 @@ pub fn fifo_join_test() -> bool { last_val = latest_val; } } - report_api(0xF1F0_2100 + if passing {1} else {0}); + report_api(0xF1F0_2100 + if passing { 1 } else { 0 }); sm_a.sm_set_tx_fifo_margin(0); // this should reset join TX and also halt the engine @@ -1388,7 +1409,8 @@ pub fn fifo_join_test() -> bool { // repeat, this time measuring the depth of the FIFO with margin let mut entries = 0; - // loop looks at the raw interrupt value, the asserts look at the feedback INTS value, so we have coverage of both views + // loop looks at the raw interrupt value, the asserts look at the feedback INTS value, so we have + // coverage of both views while (sm_a.pio.rf(rp_pio::SFR_INTR_INTR_TXNFULL) & sm_a.sm_bitmask()) != 0 { entries += 1; sm_a.sm_txfifo_push_u32(0xF1F0_0000 + entries); @@ -1424,11 +1446,12 @@ pub fn fifo_join_test() -> bool { } } sm_a.sm_irq0_source_enabled(PioIntSource::TxNotFull, false); - #[cfg(not(feature="rp2040"))] + #[cfg(not(feature = "rp2040"))] sm_a.sm_set_tx_fifo_margin(0); sm_a.sm_irq0_source_enabled(PioIntSource::RxNotEmpty, true); // a program for testing IN + #[rustfmt::skip] let b_code = pio_proc::pio_asm!( " set x, 16", "loop: ", @@ -1460,9 +1483,7 @@ pub fn fifo_join_test() -> bool { if val != expected { passing = false; } - report_api( - 0xF1F0_0000 + val - ); + report_api(0xF1F0_0000 + val); entries += 1; expected -= 1; } @@ -1487,9 +1508,7 @@ pub fn fifo_join_test() -> bool { if val != (expected & io_mask) { passing = false; } - report_api( - 0xF1F0_0000 + val - ); + report_api(0xF1F0_0000 + val); entries += 1; expected -= 1; } @@ -1497,7 +1516,7 @@ pub fn fifo_join_test() -> bool { report_api(0xF1F0_4000 + entries); // no join, but with margin - #[cfg(not(feature="rp2040"))] + #[cfg(not(feature = "rp2040"))] { sm_a.config_set_fifo_join(PioFifoJoin::None); sm_a.sm_init(b_prog.entry()); @@ -1519,14 +1538,12 @@ pub fn fifo_join_test() -> bool { sm_a.pio.wfo(rp_pio::SFR_CTRL_EN, 0); entries = 0; expected = 16; - while (pio_ss.pio.rf(rp_pio::SFR_INTR_INTR_RXNEMPTY) & sm_a.sm_bitmask()) != 0 { + while (pio_ss.pio.rf(rp_pio::SFR_INTR_INTR_RXNEMPTY) & sm_a.sm_bitmask()) != 0 { let val = sm_a.sm_rxfifo_pull_u32(); if val != (expected & io_mask) { passing = false; } - report_api( - 0xF1F0_0000 + val - ); + report_api(0xF1F0_0000 + val); entries += 1; expected -= 1; } @@ -1558,14 +1575,12 @@ pub fn fifo_join_test() -> bool { sm_a.pio.wfo(rp_pio::SFR_CTRL_EN, 0); entries = 0; expected = 16; - while (pio_ss.pio.rf(rp_pio::SFR_INTR_INTR_RXNEMPTY) & sm_a.sm_bitmask()) != 0 { + while (pio_ss.pio.rf(rp_pio::SFR_INTR_INTR_RXNEMPTY) & sm_a.sm_bitmask()) != 0 { let val = sm_a.sm_rxfifo_pull_u32(); if val != (expected & io_mask) { passing = false; } - report_api( - 0xF1F0_0000 + val - ); + report_api(0xF1F0_0000 + val); entries += 1; expected -= 1; } @@ -1579,7 +1594,7 @@ pub fn fifo_join_test() -> bool { // clean up sm_a.sm_irq0_source_enabled(PioIntSource::RxNotEmpty, false); - #[cfg(not(feature="rp2040"))] + #[cfg(not(feature = "rp2040"))] sm_a.sm_set_rx_fifo_margin(0); pio_ss.clear_instruction_memory(); @@ -1621,7 +1636,6 @@ pub fn fifo_join_test() -> bool { /// 18..20 SM1 sideset via pindirs /// 20..22 SM2 sideset /// 31 synchronizing GPIO input -/// pub fn register_tests() { const REGTEST_DIV: f32 = 2.5; @@ -1636,6 +1650,7 @@ pub fn register_tests() { sm_a.pio.wo(rp_pio::SFR_FDEBUG, 0xFFFF_FFFF); // clear all the FIFO debug registers #[cfg(not(feature="rp2040"))] + #[rustfmt::skip] let a_code = pio_proc::pio_asm!( ".side_set 2 opt", " set x, 24", // 18 @@ -1649,6 +1664,7 @@ pub fn register_tests() { " jmp x--, loop", // 1F ); #[cfg(feature="rp2040")] + #[rustfmt::skip] let a_code = pio_proc::pio_asm!( ".side_set 2 opt", " set x, 24", // 18 @@ -1670,12 +1686,12 @@ pub fn register_tests() { sm_a.config_set_out_shift(false, true, 0); sm_a.config_set_sideset_pins(16); sm_a.config_set_clkdiv(REGTEST_DIV); - #[cfg(not(feature="rp2040"))] + #[cfg(not(feature = "rp2040"))] sm_a.config_set_set_pins(31, 1); // special case, A is used to set GPIO 31 to resume machines on wait - #[cfg(feature="rp2040")] + #[cfg(feature = "rp2040")] sm_a.config_set_set_pins(28, 1); // special case, A is used to set GPIO 31 to resume machines on wait sm_a.sm_init(a_prog.entry()); - + #[rustfmt::skip] let b_code = pio_proc::pio_asm!( ".side_set 2 opt pindirs", " set y, 16 [1]", // 10 start with some variable delay to prove syncing works @@ -1693,9 +1709,9 @@ pub fn register_tests() { sm_b.sm_set_enabled(false); b_prog.setup_default_config(&mut sm_b); sm_b.config_set_out_pins(4, 4); - #[cfg(not(feature="rp2040"))] + #[cfg(not(feature = "rp2040"))] sm_b.config_set_in_pins(31); // maps pin 0 to GPIO 31 - #[cfg(feature="rp2040")] + #[cfg(feature = "rp2040")] sm_b.config_set_in_pins(28); // maps pin 0 to GPIO 28 sm_b.config_set_out_shift(false, true, 0); sm_b.config_set_sideset_pins(18); @@ -1703,6 +1719,7 @@ pub fn register_tests() { sm_b.sm_init(b_prog.entry()); #[cfg(not(feature="rp2040"))] + #[rustfmt::skip] let c_code = pio_proc::pio_asm!( ".side_set 2 opt", " set x, 8 [2]", @@ -1716,6 +1733,7 @@ pub fn register_tests() { " jmp x--, loop", ); #[cfg(feature="rp2040")] + #[rustfmt::skip] let c_code = pio_proc::pio_asm!( ".side_set 2 opt", " set x, 8 [2]", @@ -1728,7 +1746,7 @@ pub fn register_tests() { " wait 0 gpio 28 side 1", // wait until GPIO 28 is 0 " jmp x--, loop", ); - let mut sm_c = pio_ss.alloc_sm().unwrap(); + let mut sm_c = pio_ss.alloc_sm().unwrap(); let c_prog = LoadedProg::load(c_code.program, &mut pio_ss).unwrap(); sm_c.sm_set_enabled(false); c_prog.setup_default_config(&mut sm_c); @@ -1738,7 +1756,7 @@ pub fn register_tests() { sm_c.config_set_sideset_pins(20); sm_c.config_set_clkdiv(REGTEST_DIV); sm_c.sm_init(c_prog.entry()); - + #[rustfmt::skip] let d_code = pio_proc::pio_asm!( ".side_set 2 opt", " set y, 2 [3]", @@ -1751,14 +1769,14 @@ pub fn register_tests() { " wait 0 pin 7 side 0", // wait until the mapped input pin is 0. Map this to GPIO 31. " jmp y--, loop", ); - let mut sm_d = pio_ss.alloc_sm().unwrap(); + let mut sm_d = pio_ss.alloc_sm().unwrap(); let d_prog = LoadedProg::load(d_code.program, &mut pio_ss).unwrap(); sm_d.sm_set_enabled(false); d_prog.setup_default_config(&mut sm_d); sm_d.config_set_out_pins(12, 4); - #[cfg(not(feature="rp2040"))] + #[cfg(not(feature = "rp2040"))] sm_d.config_set_in_pins(24); // maps pin 7 to GPIO 31 - #[cfg(feature="rp2040")] + #[cfg(feature = "rp2040")] sm_d.config_set_in_pins(21); // maps pin 7 to GPIO 28 sm_d.config_set_out_shift(false, true, 0); sm_d.config_set_sideset_pins(14); // deliberate conflict with out_pins @@ -1778,57 +1796,23 @@ pub fn register_tests() { // dump the loaded instructions. Tests all the INSTR registers for readback. // must be re-generated every time programs are updated - #[cfg(not(feature="rp2040"))] + #[cfg(not(feature = "rp2040"))] let expected_instrs: [u16; 32] = [ - 0xe342, - 0x5e40, - 0x8020, - 0xd801, - 0x7400, - 0x30a7, - 0x3027, - 0x0081, - 0xe228, - 0x5d20, - 0x8220, - 0x38c1, - 0x7400, - 0x309f, - 0x341f, - 0x0049, - 0xe150, - 0x5d40, - 0x8120, - 0x38c1, - 0x7400, - 0x30a0, - 0x3420, - 0x0091, - 0xe038, - 0x5e20, - 0x8120, - 0x38c1, - 0x7400, - 0x309f, - 0x341f, - 0x0059, + 0xe342, 0x5e40, 0x8020, 0xd801, 0x7400, 0x30a7, 0x3027, 0x0081, 0xe228, 0x5d20, 0x8220, 0x38c1, + 0x7400, 0x309f, 0x341f, 0x0049, 0xe150, 0x5d40, 0x8120, 0x38c1, 0x7400, 0x30a0, 0x3420, 0x0091, + 0xe038, 0x5e20, 0x8120, 0x38c1, 0x7400, 0x309f, 0x341f, 0x0059, ]; for i in 0..RP2040_MAX_PROGRAM_SIZE { - let rbk = unsafe{sm_a.pio.base().add(rp_pio::SFR_INSTR_MEM0.offset() + i).read_volatile()}; + let rbk = unsafe { sm_a.pio.base().add(rp_pio::SFR_INSTR_MEM0.offset() + i).read_volatile() }; report_api(rbk + ((i as u32) << 24)); - #[cfg(not(feature="rp2040"))] + #[cfg(not(feature = "rp2040"))] assert!(rbk as u16 == expected_instrs[i]); // on RP2040 you can't read back the instructions } report_api(0x1336_0002); // load the TX fifos with output data we expect to see on the GPIO pins let tx_vals: [[u32; 4]; 4] = - [ - [0x3, 0xC, 0x6, 0x0], - [0xA, 0x5, 0x0, 0xF], - [0xC, 0x0, 0x1, 0x2], - [0x3, 0x2, 0x1, 0x0], - ]; + [[0x3, 0xC, 0x6, 0x0], [0xA, 0x5, 0x0, 0xF], [0xC, 0x0, 0x1, 0x2], [0x3, 0x2, 0x1, 0x0]]; // load the FIFO values, and check that the levels & flags change as we expected. let mut sm_array = [sm_a, sm_b, sm_c, sm_d]; for (sm_index, sm) in sm_array.iter_mut().enumerate() { @@ -1872,10 +1856,11 @@ pub fn register_tests() { let clear_bit31_oe = pio_proc::pio_asm!("set pindirs, 0").program.code[0]; // prepare an instruction that stalls let p_wait = pio_proc::pio_asm!("wait 1 irq 0").program.code[0]; - // prepare an instruction that can set & clear the *pin*, not just the pindir. For RP2040 HW test, because we can't hack the OE to IN as a loopback like we can in a verilog model. - #[cfg(feature="rp2040")] + // prepare an instruction that can set & clear the *pin*, not just the pindir. For RP2040 HW test, because + // we can't hack the OE to IN as a loopback like we can in a verilog model. + #[cfg(feature = "rp2040")] let pinset = pio_proc::pio_asm!("set pins, 1").program.code[0]; - #[cfg(feature="rp2040")] + #[cfg(feature = "rp2040")] let pinclear = pio_proc::pio_asm!("set pins, 0").program.code[0]; // check that the RX FIFOs have the correct levels @@ -1889,7 +1874,7 @@ pub fn register_tests() { sm_array[0].pio.wo( rp_pio::SFR_CTRL, sm_array[0].pio.ms(rp_pio::SFR_CTRL_CLKDIV_RESTART, 0xF) - | sm_array[0].pio.ms(rp_pio::SFR_CTRL_EN, 0xF) + | sm_array[0].pio.ms(rp_pio::SFR_CTRL_EN, 0xF), ); // sync the clocks; the clock free-runs after the div is setup, and the divs are set up at arbitrary points in time while (sm_array[0].pio.r(rp_pio::SFR_CTRL) & !0xF) != 0 { // wait for the bits to self-reset to acknowledge that the command has executed @@ -1909,9 +1894,10 @@ pub fn register_tests() { for (index, vals) in tx_vals.iter().enumerate() { expected |= (vals[waiting_for] & 0xF) << (index as u32 * 4); } - // compensate for sideset override on SM3 (doesn't apply, because the sideset is out of phase with out) - // expected &= 0x3FFF; - // expected |= 0x4000; // "side 1" should be executed on SM3 on bits 14-16, overriding any TX fifo value + // compensate for sideset override on SM3 (doesn't apply, because the sideset is out of phase with + // out) expected &= 0x3FFF; + // expected |= 0x4000; // "side 1" should be executed on SM3 on bits 14-16, overriding any TX fifo + // value let outputs = sm_array[0].pio.r(rp_pio::SFR_DBG_PADOUT); report_api(0x1336_0000 | (outputs & 0xFFFF)); @@ -1938,16 +1924,24 @@ pub fn register_tests() { // read the address of the PC, and confirm the instruction is correct report_api(0x1336_0008); - #[cfg(not(feature="rp2040"))] // can't read instructions on actual rp2040 + #[cfg(not(feature = "rp2040"))] // can't read instructions on actual rp2040 { - assert!(expected_instrs[sm_array[0].pio.rf(rp_pio::SFR_SM0_ADDR_PC) as usize] == - sm_array[0].pio.rf(rp_pio::SFR_SM0_INSTR_IMM_INSTR) as u16); - assert!(expected_instrs[sm_array[1].pio.rf(rp_pio::SFR_SM1_ADDR_PC) as usize] == - sm_array[1].pio.rf(rp_pio::SFR_SM1_INSTR_IMM_INSTR) as u16); - assert!(expected_instrs[sm_array[2].pio.rf(rp_pio::SFR_SM2_ADDR_PC) as usize] == - sm_array[2].pio.rf(rp_pio::SFR_SM2_INSTR_IMM_INSTR) as u16); - assert!(expected_instrs[sm_array[3].pio.rf(rp_pio::SFR_SM3_ADDR_PC) as usize] == - sm_array[3].pio.rf(rp_pio::SFR_SM3_INSTR_IMM_INSTR) as u16); + assert!( + expected_instrs[sm_array[0].pio.rf(rp_pio::SFR_SM0_ADDR_PC) as usize] + == sm_array[0].pio.rf(rp_pio::SFR_SM0_INSTR_IMM_INSTR) as u16 + ); + assert!( + expected_instrs[sm_array[1].pio.rf(rp_pio::SFR_SM1_ADDR_PC) as usize] + == sm_array[1].pio.rf(rp_pio::SFR_SM1_INSTR_IMM_INSTR) as u16 + ); + assert!( + expected_instrs[sm_array[2].pio.rf(rp_pio::SFR_SM2_ADDR_PC) as usize] + == sm_array[2].pio.rf(rp_pio::SFR_SM2_INSTR_IMM_INSTR) as u16 + ); + assert!( + expected_instrs[sm_array[3].pio.rf(rp_pio::SFR_SM3_ADDR_PC) as usize] + == sm_array[3].pio.rf(rp_pio::SFR_SM3_INSTR_IMM_INSTR) as u16 + ); } // execute the "program" that flips the OE bit, which should get us to the next iteration @@ -1958,12 +1952,13 @@ pub fn register_tests() { report_api(0x1336_000A); assert!(sm_array[0].pio.rf(rp_pio::SFR_SM0_EXECCTRL_EXEC_STALLED_RO0) == 1); - // now exec the instruction that should clear the wait condition on an input pin by flipping bit 31 via a side-set operation - // this requires the testbench to reflect that bit back correctly! - #[cfg(feature="rp2040")] + // now exec the instruction that should clear the wait condition on an input pin by flipping bit + // 31 via a side-set operation this requires the testbench to reflect that bit back + // correctly! + #[cfg(feature = "rp2040")] sm_array[0].sm_exec(pinset); sm_array[0].sm_exec(set_bit31_oe); - #[cfg(feature="rp2040")] + #[cfg(feature = "rp2040")] sm_array[0].sm_exec(pinclear); sm_array[0].sm_exec(clear_bit31_oe); delay(4); // give some time for the state machines to run to the stall point (necessary for fast pclk case) @@ -1977,10 +1972,9 @@ pub fn register_tests() { report_api(0x1336_000C); sm_array[0].pio.wfo(rp_pio::SFR_CTRL_EN, 0); - // since the program ran one extra iteration out the bottom of the loop, we should have overflowed the RX fifo, etc. - report_api( - sm_array[0].pio.r(rp_pio::SFR_FDEBUG) - ); + // since the program ran one extra iteration out the bottom of the loop, we should have overflowed the RX + // fifo, etc. + report_api(sm_array[0].pio.r(rp_pio::SFR_FDEBUG)); assert!(sm_array[0].pio.r(rp_pio::SFR_FDEBUG) == 0xF); // stalls should be asserted sm_array[0].pio.wfo(rp_pio::SFR_FDEBUG_RXSTALL, 0xF); // clear the stall assert!(sm_array[0].pio.r(rp_pio::SFR_FDEBUG) == 0); // confirm it is cleared @@ -2041,4 +2035,4 @@ pub fn register_tests() { } report_api(0x1336_600d); -} \ No newline at end of file +} diff --git a/libs/xous-pl230/src/pl230_tests/units.rs b/libs/xous-pl230/src/pl230_tests/units.rs index f5c09bae2..b0e88a0ca 100644 --- a/libs/xous-pl230/src/pl230_tests/units.rs +++ b/libs/xous-pl230/src/pl230_tests/units.rs @@ -1,18 +1,16 @@ -use crate::*; -use super::report_api; use utralib::*; +use super::report_api; +use crate::*; + /// used to generate some test vectors pub fn lfsr_next(state: u32) -> u32 { - let bit = ((state >> 31) ^ - (state >> 21) ^ - (state >> 1) ^ - (state >> 0)) & 1; + let bit = ((state >> 31) ^ (state >> 21) ^ (state >> 1) ^ (state >> 0)) & 1; (state << 1) + bit } -pub fn basic_tests (pl230: &mut Pl230) -> bool { +pub fn basic_tests(pl230: &mut Pl230) -> bool { report_api("channels", pl230.csr.rf(utra::pl230::STATUS_CHNLS_MINUS1) + 1); //report_api("id0", pl230.csr.r(utra::pl230::PERIPH_ID_0)); //report_api("id1", pl230.csr.r(utra::pl230::PERIPH_ID_1)); @@ -21,13 +19,12 @@ pub fn basic_tests (pl230: &mut Pl230) -> bool { // conjure the DMA control structure in IFRAM0. In order to guarantee Rust // semantics, it must be initialized to 0: 4 word-sized entries * 8 channels * 2 banks = 4 * 8 * 2 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)}; + 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() - }; + let cc_struct: &mut ControlChannels = + unsafe { (utralib::HW_IFRAM0_MEM as *mut ControlChannels).as_mut().unwrap() }; // read the status register // report_api("status", pl230.csr.r(utra::pl230::STATUS)); @@ -37,23 +34,24 @@ pub fn basic_tests (pl230: &mut Pl230) -> bool { const DMA_LEN: usize = 16; // setup the PL230 to do a simple transfer between two memory regions // dma_mainram feature will cause us to DMA between main memory regions. This works under RTL sims. - #[cfg(feature="dma_mainram")] + #[cfg(feature = "dma_mainram")] let mut region_a = [0u32; DMA_LEN]; - #[cfg(feature="dma_mainram")] + #[cfg(feature = "dma_mainram")] let region_b = [0u32; DMA_LEN]; // The alternate is to DMA between IFRAM regions. This works under FPGA and RTL sim. - #[cfg(not(feature="dma_mainram"))] - let region_a = unsafe{core::slice::from_raw_parts_mut((utralib::HW_IFRAM0_MEM + 4096) as *mut u32, DMA_LEN)}; - #[cfg(not(feature="dma_mainram"))] - let region_b = unsafe{core::slice::from_raw_parts_mut(utralib::HW_IFRAM1_MEM as *mut u32, DMA_LEN)}; + #[cfg(not(feature = "dma_mainram"))] + let region_a = + unsafe { core::slice::from_raw_parts_mut((utralib::HW_IFRAM0_MEM + 4096) as *mut u32, DMA_LEN) }; + #[cfg(not(feature = "dma_mainram"))] + let region_b = unsafe { core::slice::from_raw_parts_mut(utralib::HW_IFRAM1_MEM as *mut u32, DMA_LEN) }; let mut state = 0x1111_1111; for d in region_a.iter_mut() { *d = state; state = lfsr_next(state); } - cc_struct.channels[0].dst_end_ptr = unsafe{region_b.as_ptr().add(region_b.len() - 1)} as u32; - cc_struct.channels[0].src_end_ptr = unsafe{region_a.as_ptr().add(region_a.len() - 1)} as u32; + cc_struct.channels[0].dst_end_ptr = unsafe { region_b.as_ptr().add(region_b.len() - 1) } 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); @@ -84,10 +82,10 @@ pub fn basic_tests (pl230: &mut Pl230) -> bool { let mut timeout = 0; while (DmaChanControl(cc_struct.channels[0].control).cycle_ctrl() != 0) && timeout < 16 { // report_api("dma progress ", cc_struct.channels[0].control); - report_api("progress as baseptr[2]", unsafe{cc_struct.channels.as_ptr().read()}.control); + report_api("progress as baseptr[2]", unsafe { cc_struct.channels.as_ptr().read() }.control); timeout += 1; } - + #[rustfmt::skip] unsafe {core::arch::asm!( ".word 0x500F", "nop", @@ -125,16 +123,16 @@ pub fn basic_tests (pl230: &mut Pl230) -> bool { passing } -#[cfg(feature="pio")] +#[cfg(feature = "pio")] pub fn pio_test(pl230: &mut Pl230) -> bool { use xous_pio::*; let iox_csr = utra::iox::HW_IOX_BASE as *mut u32; unsafe { - iox_csr.add(0x8 / core::mem::size_of::()).write_volatile(0b0101_0101_0101_0101); // PBL - iox_csr.add(0xC / core::mem::size_of::()).write_volatile(0b0101_0101_0101_0101); // PBH - iox_csr.add(0x10 / core::mem::size_of::()).write_volatile(0b0101_0101_0101_0101); // PCL - iox_csr.add(0x14 / core::mem::size_of::()).write_volatile(0b0101_0101_0101_0101); // PCH + iox_csr.add(0x8 / core::mem::size_of::()).write_volatile(0b0101_0101_0101_0101); // PBL + iox_csr.add(0xC / core::mem::size_of::()).write_volatile(0b0101_0101_0101_0101); // PBH + iox_csr.add(0x10 / core::mem::size_of::()).write_volatile(0b0101_0101_0101_0101); // PCL + iox_csr.add(0x14 / core::mem::size_of::()).write_volatile(0b0101_0101_0101_0101); // PCH iox_csr.add(0x200 / core::mem::size_of::()).write_volatile(0xffffffff); // PIO sel port D31-0 } @@ -144,7 +142,7 @@ pub fn pio_test(pl230: &mut Pl230) -> bool { let mut sm_a = pio_ss.alloc_sm().unwrap(); pio_ss.clear_instruction_memory(); sm_a.sm_set_enabled(false); - + #[rustfmt::skip] let a_code = pio_proc::pio_asm!( "out pins, 0", // 0 is 32 ); @@ -152,10 +150,7 @@ pub fn pio_test(pl230: &mut Pl230) -> bool { sm_a.sm_set_enabled(false); a_prog.setup_default_config(&mut sm_a); sm_a.config_set_out_pins(0, 32); - sm_a.sm_set_pindirs_with_mask( - 0xFFFF_FFFF, - 0xFFFF_FFFF - ); + sm_a.sm_set_pindirs_with_mask(0xFFFF_FFFF, 0xFFFF_FFFF); 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_set_pindirs_with_mask(0x10, 0x10); @@ -181,13 +176,12 @@ pub fn pio_test(pl230: &mut Pl230) -> bool { // 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)}; + 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() - }; + 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; @@ -200,7 +194,7 @@ pub fn pio_test(pl230: &mut Pl230) -> bool { 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; + 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); @@ -212,18 +206,19 @@ pub fn pio_test(pl230: &mut Pl230) -> bool { 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::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); + 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); + 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. + // 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 @@ -257,4 +252,4 @@ pub fn pio_test(pl230: &mut Pl230) -> bool { } else { true } */ -} \ No newline at end of file +}