suppress formatting on in-line assembly in xous-pio

This commit is contained in:
bunnie 2024-01-14 19:22:18 +08:00
parent 508476a085
commit f00293a3a8
7 changed files with 557 additions and 579 deletions

View file

@ -2,9 +2,8 @@
use pio::Program;
use pio::RP2040_MAX_PROGRAM_SIZE;
use utralib::generated::*;
use utralib::generated::utra::rp_pio;
use utralib::generated::*;
#[cfg(feature = "tests")]
pub mod pio_tests;
@ -12,6 +11,7 @@ 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,12 +67,7 @@ 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 }
}
}
@ -87,44 +82,36 @@ pub fn get_id() -> u32 {
#[derive(Debug)]
pub struct LoadedProg {
program: Program::<RP2040_MAX_PROGRAM_SIZE>,
program: Program<RP2040_MAX_PROGRAM_SIZE>,
offset: usize,
entry_point: Option<usize>,
}
impl LoadedProg {
pub fn load(program: Program::<RP2040_MAX_PROGRAM_SIZE>, pio_sm: &mut PioSharedState) -> Result<Self, PioError> {
pub fn load(
program: Program<RP2040_MAX_PROGRAM_SIZE>,
pio_sm: &mut PioSharedState,
) -> Result<Self, PioError> {
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::<RP2040_MAX_PROGRAM_SIZE>, entry_point: usize, pio_sm: &mut PioSharedState) -> Result<Self, PioError> {
pub fn load_with_entrypoint(
program: Program<RP2040_MAX_PROGRAM_SIZE>,
entry_point: usize,
pio_sm: &mut PioSharedState,
) -> Result<Self, PioError> {
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());
@ -155,15 +142,13 @@ impl PioSharedState {
used_machines: [false; 4],
}
}
#[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")]
pub fn new() -> Self {
// Note: this requires a memory region window to be manually specified in create-image
@ -184,6 +169,7 @@ impl PioSharedState {
used_machines: [false; 4],
}
}
pub fn alloc_sm(&mut self) -> Result<PioSm, PioError> {
if let Some(sm_index) = self.used_machines.iter().position(|&used| used == false) {
self.used_machines[sm_index] = true;
@ -204,9 +190,10 @@ impl PioSharedState {
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.
///
@ -238,17 +225,12 @@ impl PioSharedState {
#[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<RP2040_MAX_PROGRAM_SIZE>) -> Option<usize> {
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<RP2040_MAX_PROGRAM_SIZE>) -> 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<RP2040_MAX_PROGRAM_SIZE>,
) -> Result<usize, PioError> {
pub fn add_program(&mut self, program: &Program<RP2040_MAX_PROGRAM_SIZE>) -> Result<usize, PioError> {
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<RP2040_MAX_PROGRAM_SIZE>,
loaded_offset: usize,
) {
pub fn remove_program(&mut self, program: &Program<RP2040_MAX_PROGRAM_SIZE>, 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,18 +498,21 @@ 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 };
match self.sm {
@ -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,6 +531,7 @@ 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 };
match self.sm {
@ -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.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.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.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.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_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,101 +675,84 @@ 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.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.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(
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 {
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(
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.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(
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()
)
);
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);
@ -786,22 +764,23 @@ 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(
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()
)
);
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);
@ -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<PioIntSource>) -> 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<PioIntSource>) -> 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();
@ -922,7 +916,7 @@ impl PioSm {
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_RXSTALL, self.sm_bitmask()),
);
// Finally, clear some internal SM state
@ -934,7 +928,7 @@ impl PioSm {
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_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
@ -947,15 +941,20 @@ impl PioSm {
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);
@ -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
}

View file

@ -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

View file

@ -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,9 +78,7 @@ 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;
#[allow(dead_code)]
@ -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

View file

@ -1,6 +1,7 @@
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;
@ -14,7 +15,6 @@ pub fn carrier_burst_init(pio_sm: &mut PioSm, program: &LoadedProg, pin: usize,
//
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);
@ -45,7 +45,8 @@ pub fn carrier_control_init(pio_sm: &mut PioSm, program: &LoadedProg, tick_rate:
pio_sm.config_set_out_shift(
true, // shift right
false, // disable autopull
bits_per_frame);
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,12 +133,7 @@ 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
}
@ -158,7 +154,8 @@ pub fn nec_receive_init(pio_sm: &mut PioSm, program: &LoadedProg, pin: usize, bu
pio_sm.config_set_in_shift(
true, // shift right
true, // enable autopush
32); // autopush after 32 bits
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);
}

View file

@ -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 {

View file

@ -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() {
@ -41,7 +42,7 @@ pub fn sticky_test() {
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);
@ -91,7 +94,7 @@ pub fn sticky_test() {
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_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 {
@ -128,7 +131,7 @@ pub fn sticky_test() {
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_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 {
@ -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
@ -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,8 +409,7 @@ 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| {
divs.map(|clkdiv: f32| {
report_api(0xcc00_0000 + divstate);
divstate += 0x1111;
sm_b.sm_set_enabled(false);
@ -420,7 +425,8 @@ pub fn corner_cases() {
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.
// 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
@ -428,12 +434,12 @@ pub fn corner_cases() {
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
@ -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
@ -621,6 +630,7 @@ pub fn corner_cases() {
// set all the output
#[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,8 +652,9 @@ 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
// 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")]
@ -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,12 +836,9 @@ 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 };
@ -840,7 +851,9 @@ pub fn instruction_tests() {
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",
@ -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",
@ -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",
);
@ -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;
@ -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);
@ -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);
@ -1429,6 +1451,7 @@ pub fn fifo_join_test() -> bool {
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;
}
@ -1524,9 +1543,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;
}
@ -1563,9 +1580,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;
}
@ -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
@ -1675,7 +1691,7 @@ pub fn register_tests() {
#[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
@ -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]",
@ -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]",
@ -1780,38 +1798,9 @@ pub fn register_tests() {
// must be re-generated every time programs are updated
#[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() };
@ -1823,12 +1812,7 @@ pub fn register_tests() {
// 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,7 +1856,8 @@ 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.
// 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")]
@ -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));
@ -1940,14 +1926,22 @@ pub fn register_tests() {
report_api(0x1336_0008);
#[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,8 +1952,9 @@ 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!
// 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);
@ -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

View file

@ -1,13 +1,11 @@
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
}
@ -25,9 +23,8 @@ pub fn basic_tests (pl230: &mut Pl230) -> bool {
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));
@ -43,7 +40,8 @@ pub fn basic_tests (pl230: &mut Pl230) -> bool {
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)};
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;
@ -87,7 +85,7 @@ pub fn basic_tests (pl230: &mut Pl230) -> bool {
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",
@ -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);
@ -185,9 +180,8 @@ pub fn pio_test(pl230: &mut Pl230) -> bool {
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;
@ -223,7 +217,8 @@ pub fn pio_test(pl230: &mut Pl230) -> bool {
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