use pastey::paste;
pub use rp2350_macro::pio_asm;
use crate::Steal;
use crate::gpio;
use crate::reset::Resettable;
pub const PIO_INSTRUCTION_COUNT: usize = 32;
const CTRL_SM_ENABLE_LSB: u32 = 0;
const CTRL_SM_RESTART_LSB: u32 = 4;
const CTRL_CLKDIV_RESTART_LSB: u32 = 8;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum InstallError {
NoSpace,
InvalidOrigin,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum FifoJoin {
None = 0,
Tx = 1,
Rx = 2,
PutGet0 = 3,
PutGet1 = 7,
PutGet2 = 11,
PutGet3 = 15,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum MovStatusType {
TxLessThan = 0,
RxLessThan = 1,
IrqSet = 2,
}
#[derive(Debug, Clone, Copy)]
pub struct PioProgram {
pub instructions: &'static [u16],
pub wrap_target: u8,
pub wrap: u8,
pub origin: Option<u8>,
pub side_set_count: u8,
pub side_set_opt: bool,
pub side_set_pindirs: bool,
}
#[derive(Debug, Clone, Copy)]
pub struct InstalledProgram {
pub offset: u8,
pub length: u8,
pub wrap_bottom: u8,
pub wrap_top: u8,
pub side_set_count: u8,
pub side_set_opt: bool,
pub side_set_pindirs: bool,
}
pub mod mov_src {
pub const PINS: u8 = 0;
pub const X: u8 = 1;
pub const Y: u8 = 2;
pub const NULL: u8 = 3;
pub const STATUS: u8 = 5;
pub const ISR: u8 = 6;
pub const OSR: u8 = 7;
}
pub mod mov_dest {
pub const PINS: u8 = 0;
pub const X: u8 = 1;
pub const Y: u8 = 2;
pub const PINDIRS: u8 = 3;
pub const EXEC: u8 = 4;
pub const PC: u8 = 5;
pub const ISR: u8 = 6;
pub const OSR: u8 = 7;
}
pub mod in_src {
pub const PINS: u8 = 0;
pub const X: u8 = 1;
pub const Y: u8 = 2;
pub const NULL: u8 = 3;
pub const ISR: u8 = 6;
pub const OSR: u8 = 7;
}
pub mod out_dest {
pub const PINS: u8 = 0;
pub const X: u8 = 1;
pub const Y: u8 = 2;
pub const NULL: u8 = 3;
pub const PINDIRS: u8 = 4;
pub const PC: u8 = 5;
pub const ISR: u8 = 6;
pub const EXEC: u8 = 7;
}
pub mod set_dest {
pub const PINS: u8 = 0;
pub const X: u8 = 1;
pub const Y: u8 = 2;
pub const PINDIRS: u8 = 4;
}
macro_rules! impl_state_machine {
($SmName:ident, $sm_index:literal, $pio_num:literal) => {
paste! {
impl $SmName<'_> {
pub const INDEX: u8 = $sm_index;
pub fn init(&mut self, initial_pc: u8) -> &mut Self {
self.disable();
self.clear_fifos();
self.restart();
self.restart_clock_divider();
self.exec(encode_jmp(initial_pc));
self
}
#[inline]
pub fn enable(&mut self) -> &mut Self {
let mask = 1u32 << $sm_index;
self.pio.ctrl.set_bits(mask);
self
}
#[inline]
pub fn disable(&mut self) -> &mut Self {
let mask = 1u32 << $sm_index;
self.pio.ctrl.clr_bits(mask);
self
}
#[inline]
pub fn is_enabled(&self) -> bool {
(self.pio.ctrl.sm_enable() & (1u32 << $sm_index)) != 0
}
#[inline]
pub fn restart(&mut self) -> &mut Self {
self.pio
.ctrl
.set_bits(1u32 << (CTRL_SM_RESTART_LSB + $sm_index));
self
}
#[inline]
pub fn restart_clock_divider(&mut self) -> &mut Self {
self.pio
.ctrl
.set_bits(1u32 << (CTRL_CLKDIV_RESTART_LSB + $sm_index));
self
}
#[inline]
pub fn pc(&self) -> u8 {
self.pio.[<sm $sm_index _addr>].read_bits() as u8 & 0x1F
}
pub fn set_clkdiv(&mut self, int_div: u16, frac_div: u8) -> &mut Self {
self.pio.[<sm $sm_index _clkdiv>].write_int(int_div as u32);
self.pio.[<sm $sm_index _clkdiv>].write_frac(frac_div as u32);
self
}
pub fn apply_program(&mut self, prog: &InstalledProgram) -> &mut Self {
self.set_wrap(prog.wrap_bottom, prog.wrap_top);
if prog.side_set_count > 0 {
self.pio.[<sm $sm_index _pinctrl>].write_sideset_count(prog.side_set_count as u32);
if prog.side_set_opt {
self.pio.[<sm $sm_index _execctrl>].set_side_en();
} else {
self.pio.[<sm $sm_index _execctrl>].clr_side_en();
}
if prog.side_set_pindirs {
self.pio.[<sm $sm_index _execctrl>].set_side_pindir();
} else {
self.pio.[<sm $sm_index _execctrl>].clr_side_pindir();
}
}
self
}
pub fn set_wrap(&mut self, wrap_target: u8, wrap_top: u8) -> &mut Self {
self.pio.[<sm $sm_index _execctrl>].write_wrap_bottom(wrap_target as u32);
self.pio.[<sm $sm_index _execctrl>].write_wrap_top(wrap_top as u32);
self
}
pub fn set_out_pins(&mut self, base: u8, count: u8) -> &mut Self {
self.pio.[<sm $sm_index _pinctrl>].write_out_base((base & 31) as u32);
self.pio.[<sm $sm_index _pinctrl>].write_out_count(count as u32);
self
}
pub fn set_set_pins(&mut self, base: u8, count: u8) -> &mut Self {
self.pio.[<sm $sm_index _pinctrl>].write_set_base((base & 31) as u32);
self.pio.[<sm $sm_index _pinctrl>].write_set_count(count as u32);
self
}
pub fn set_in_pins(&mut self, base: u8) -> &mut Self {
self.pio.[<sm $sm_index _pinctrl>].write_in_base((base & 31) as u32);
self
}
pub fn set_in_pin_count(&mut self, count: u8) -> &mut Self {
self.pio.[<sm $sm_index _shiftctrl>].write_in_count((count & 31) as u32);
self
}
pub fn set_sideset(
&mut self,
base: u8,
bit_count: u8,
optional: bool,
pindirs: bool,
) -> &mut Self {
self.pio.[<sm $sm_index _pinctrl>].write_sideset_base((base & 31) as u32);
self.pio.[<sm $sm_index _pinctrl>].write_sideset_count(bit_count as u32);
if optional {
self.pio.[<sm $sm_index _execctrl>].set_side_en();
} else {
self.pio.[<sm $sm_index _execctrl>].clr_side_en();
}
if pindirs {
self.pio.[<sm $sm_index _execctrl>].set_side_pindir();
} else {
self.pio.[<sm $sm_index _execctrl>].clr_side_pindir();
}
self
}
pub fn set_jmp_pin(&mut self, pin: u8) -> &mut Self {
self.pio.[<sm $sm_index _execctrl>].write_jmp_pin((pin & 31) as u32);
self
}
pub fn set_consecutive_pindirs(
&mut self,
pin_base: u8,
pin_count: u8,
is_out: bool,
) -> &mut Self {
let pindir_val = if is_out { 0x1F } else { 0 };
let pinctrl_saved = self.pio.[<sm $sm_index _pinctrl>].read_bits();
let execctrl_saved = self.pio.[<sm $sm_index _execctrl>].read_bits();
self.pio.[<sm $sm_index _execctrl>].clr_out_sticky();
let mut remaining = pin_count;
let mut pin = pin_base;
while remaining > 5 {
self.pio.[<sm $sm_index _pinctrl>].write_set_base((pin & 31) as u32);
self.pio.[<sm $sm_index _pinctrl>].write_set_count(5);
self.exec(encode_set_pindirs(pindir_val));
remaining -= 5;
pin = pin.wrapping_add(5) & 0x1F;
}
if remaining > 0 {
self.pio.[<sm $sm_index _pinctrl>].write_set_base((pin & 31) as u32);
self.pio.[<sm $sm_index _pinctrl>].write_set_count(remaining as u32);
self.exec(encode_set_pindirs(pindir_val));
}
self.pio.[<sm $sm_index _pinctrl>].write_bits(pinctrl_saved);
self.pio.[<sm $sm_index _execctrl>].write_bits(execctrl_saved);
self
}
pub fn set_in_shift(
&mut self,
shift_right: bool,
autopush: bool,
push_threshold: u8,
) -> &mut Self {
if shift_right {
self.pio.[<sm $sm_index _shiftctrl>].set_in_shiftdir();
} else {
self.pio.[<sm $sm_index _shiftctrl>].clr_in_shiftdir();
}
if autopush {
self.pio.[<sm $sm_index _shiftctrl>].set_autopush();
} else {
self.pio.[<sm $sm_index _shiftctrl>].clr_autopush();
}
self.pio.[<sm $sm_index _shiftctrl>]
.write_push_thresh((push_threshold & 0x1F) as u32);
self
}
pub fn set_out_shift(
&mut self,
shift_right: bool,
autopull: bool,
pull_threshold: u8,
) -> &mut Self {
if shift_right {
self.pio.[<sm $sm_index _shiftctrl>].set_out_shiftdir();
} else {
self.pio.[<sm $sm_index _shiftctrl>].clr_out_shiftdir();
}
if autopull {
self.pio.[<sm $sm_index _shiftctrl>].set_autopull();
} else {
self.pio.[<sm $sm_index _shiftctrl>].clr_autopull();
}
self.pio.[<sm $sm_index _shiftctrl>]
.write_pull_thresh((pull_threshold & 0x1F) as u32);
self
}
pub fn set_fifo_join(&mut self, join: FifoJoin) -> &mut Self {
let join_val = join as u32;
self.pio.[<sm $sm_index _shiftctrl>].clr_fjoin_tx();
self.pio.[<sm $sm_index _shiftctrl>].clr_fjoin_rx();
self.pio.[<sm $sm_index _shiftctrl>].clr_fjoin_rx_put();
self.pio.[<sm $sm_index _shiftctrl>].clr_fjoin_rx_get();
if join_val & 1 != 0 {
self.pio.[<sm $sm_index _shiftctrl>].set_fjoin_tx();
}
if join_val & 2 != 0 {
self.pio.[<sm $sm_index _shiftctrl>].set_fjoin_rx();
}
if join_val & 4 != 0 {
self.pio.[<sm $sm_index _shiftctrl>].set_fjoin_rx_put();
}
if join_val & 8 != 0 {
self.pio.[<sm $sm_index _shiftctrl>].set_fjoin_rx_get();
}
self
}
pub fn set_out_special(
&mut self,
sticky: bool,
has_enable_pin: bool,
enable_bit_index: u8,
) -> &Self {
if sticky {
self.pio.[<sm $sm_index _execctrl>].set_out_sticky();
} else {
self.pio.[<sm $sm_index _execctrl>].clr_out_sticky();
}
if has_enable_pin {
self.pio.[<sm $sm_index _execctrl>].set_inline_out_en();
} else {
self.pio.[<sm $sm_index _execctrl>].clr_inline_out_en();
}
self.pio.[<sm $sm_index _execctrl>].write_out_en_sel(enable_bit_index as u32);
self
}
pub fn set_mov_status(&mut self, status_sel: MovStatusType, status_n: u8) -> &Self {
self.pio.[<sm $sm_index _execctrl>].write_status_sel(status_sel as u32);
self.pio.[<sm $sm_index _execctrl>].write_status_n(status_n as u32);
self
}
#[inline]
pub fn exec(&self, instr: u16) -> &Self {
self.pio.[<sm $sm_index _instr>].write_bits(instr as u32);
self
}
pub fn exec_wait_blocking(&self, instr: u16) -> &Self {
self.exec(instr);
crate::spin::spin_wait("PIO instruction execute", || !self.is_exec_stalled());
self
}
#[inline]
pub fn is_exec_stalled(&self) -> bool {
self.pio.[<sm $sm_index _execctrl>].exec_stalled()
}
#[inline]
pub fn tx_fifo_is_full(&self) -> bool {
(self.pio.fstat.txfull() & (1 << $sm_index)) != 0
}
#[inline]
pub fn tx_fifo_is_empty(&self) -> bool {
(self.pio.fstat.txempty() & (1 << $sm_index)) != 0
}
#[inline]
pub fn rx_fifo_is_full(&self) -> bool {
(self.pio.fstat.rxfull() & (1 << $sm_index)) != 0
}
#[inline]
pub fn rx_fifo_is_empty(&self) -> bool {
(self.pio.fstat.rxempty() & (1 << $sm_index)) != 0
}
#[inline]
pub fn tx_fifo_level(&self) -> u8 {
self.pio.flevel.[<tx $sm_index>]() as u8
}
#[inline]
pub fn rx_fifo_level(&self) -> u8 {
self.pio.flevel.[<rx $sm_index>]() as u8
}
pub fn tx_fifo_write(&self, data: u32) {
while self.tx_fifo_is_full() {
core::hint::spin_loop();
}
self.pio.[<txf $sm_index>].write_bits(data);
}
pub fn tx_fifo_try_write(&self, data: u32) -> bool {
if self.tx_fifo_is_full() {
return false;
}
self.pio.[<txf $sm_index>].write_bits(data);
true
}
#[inline]
pub fn tx_fifo_put(&self, data: u32) {
self.pio.[<txf $sm_index>].write_bits(data);
}
pub fn rx_fifo_read(&self) -> u32 {
while self.rx_fifo_is_empty() {
core::hint::spin_loop();
}
self.pio.[<rxf $sm_index>].read_bits()
}
pub fn rx_fifo_try_read(&self) -> Option<u32> {
if self.rx_fifo_is_empty() {
return None;
}
Some(self.pio.[<rxf $sm_index>].read_bits())
}
#[inline]
pub fn rx_fifo_get(&self) -> u32 {
self.pio.[<rxf $sm_index>].read_bits()
}
pub fn clear_fifos(&self) -> &Self {
self.pio.[<sm $sm_index _shiftctrl>].xor_bits(1 << 31);
self.pio.[<sm $sm_index _shiftctrl>].xor_bits(1 << 31);
self
}
pub fn drain_tx_fifo(&self) {
let autopull = self.pio.[<sm $sm_index _shiftctrl>].autopull();
let instr = if autopull {
encode_out(out_dest::NULL, 32)
} else {
encode_pull(false, false)
};
crate::spin::spin_wait("PIO TX FIFO drain", || {
if self.tx_fifo_is_empty() {
return true;
}
self.exec(instr);
false
});
}
pub fn putget_read(&self, index: u8) -> u32 {
match index {
0 => self.pio.[<rxf $sm_index _putget0>].[<rxf $sm_index _putget0>](),
1 => self.pio.[<rxf $sm_index _putget1>].[<rxf $sm_index _putget1>](),
2 => self.pio.[<rxf $sm_index _putget2>].[<rxf $sm_index _putget2>](),
3 => self.pio.[<rxf $sm_index _putget3>].[<rxf $sm_index _putget3>](),
_ => panic!("PUTGET index must be 0-3"),
}
}
pub fn putget_write(&self, index: u8, data: u32) {
match index {
0 => self.pio.[<rxf $sm_index _putget0>].[<write_rxf $sm_index _putget0>](data),
1 => self.pio.[<rxf $sm_index _putget1>].[<write_rxf $sm_index _putget1>](data),
2 => self.pio.[<rxf $sm_index _putget2>].[<write_rxf $sm_index _putget2>](data),
3 => self.pio.[<rxf $sm_index _putget3>].[<write_rxf $sm_index _putget3>](data),
_ => panic!("PUTGET index must be 0-3"),
}
}
pub const TX_DREQ: crate::dma::TreqSel = crate::dma::TreqSel::[<Pio $pio_num Tx $sm_index>];
pub const RX_DREQ: crate::dma::TreqSel = crate::dma::TreqSel::[<Pio $pio_num Rx $sm_index>];
#[inline]
pub fn tx_fifo_address(&self) -> u32 {
&self.pio.[<txf $sm_index>] as *const _ as u32
}
#[inline]
pub fn rx_fifo_address(&self) -> u32 {
&self.pio.[<rxf $sm_index>] as *const _ as u32
}
}
} };
}
macro_rules! impl_pio_block {
($PioName:ident, $pio_num:literal) => {
paste! {
pub struct [<$PioName Sm0>]<'a> {
pio: &'a rp2350_pac::[<pio $pio_num>]::RegisterBlock,
}
pub struct [<$PioName Sm1>]<'a> {
pio: &'a rp2350_pac::[<pio $pio_num>]::RegisterBlock,
}
pub struct [<$PioName Sm2>]<'a> {
pio: &'a rp2350_pac::[<pio $pio_num>]::RegisterBlock,
}
pub struct [<$PioName Sm3>]<'a> {
pio: &'a rp2350_pac::[<pio $pio_num>]::RegisterBlock,
}
impl_state_machine!([<$PioName Sm0>], 0, $pio_num);
impl_state_machine!([<$PioName Sm1>], 1, $pio_num);
impl_state_machine!([<$PioName Sm2>], 2, $pio_num);
impl_state_machine!([<$PioName Sm3>], 3, $pio_num);
pub struct $PioName {
pio: rp2350_pac::[<PIO $pio_num>],
used_instruction_space: u32,
}
impl $PioName {
pub const ID: u8 = $pio_num;
pub fn init(pio: rp2350_pac::[<PIO $pio_num>]) -> Self {
<rp2350_pac::[<PIO $pio_num>] as Resettable>::init();
Self {
pio,
used_instruction_space: 0,
}
}
#[doc = concat!("let pio0 = unsafe { ", stringify!($PioName), "::steal() };")]
#[inline]
pub unsafe fn steal() -> Steal<Self> {
Steal(Self {
pio: unsafe { rp2350_pac::[<PIO $pio_num>]::steal() },
used_instruction_space: 0,
})
}
#[inline]
pub fn sm0(&self) -> [<$PioName Sm0>]<'_> {
[<$PioName Sm0>] { pio: &*self.pio }
}
#[inline]
pub fn sm1(&self) -> [<$PioName Sm1>]<'_> {
[<$PioName Sm1>] { pio: &*self.pio }
}
#[inline]
pub fn sm2(&self) -> [<$PioName Sm2>]<'_> {
[<$PioName Sm2>] { pio: &*self.pio }
}
#[inline]
pub fn sm3(&self) -> [<$PioName Sm3>]<'_> {
[<$PioName Sm3>] { pio: &*self.pio }
}
pub fn claim_pin<P: gpio::[<Pio $pio_num Pin>]>(&self, mut pin: P) {
pin.[<configure_pio $pio_num>]();
let _ = pin;
}
pub fn set_gpio_base(&mut self, high: bool) {
if high {
self.pio.gpiobase.set_gpiobase();
} else {
self.pio.gpiobase.clr_gpiobase();
}
}
pub fn gpio_base(&self) -> bool {
self.pio.gpiobase.gpiobase()
}
pub fn set_input_sync_bypass(&mut self, pin_mask: u32) {
self.pio.input_sync_bypass.write_input_sync_bypass(pin_mask);
}
pub fn install_program(
&mut self,
program: &PioProgram,
) -> Result<InstalledProgram, InstallError> {
let instructions = program.instructions;
if instructions.is_empty() || instructions.len() > PIO_INSTRUCTION_COUNT {
return Err(InstallError::NoSpace);
}
let length = instructions.len() as u8;
let program_mask = if length >= 32 {
u32::MAX
} else {
(1u32 << length) - 1
};
let offset = if let Some(orig) = program.origin {
if orig as usize + instructions.len() > PIO_INSTRUCTION_COUNT {
return Err(InstallError::InvalidOrigin);
}
if (self.used_instruction_space & (program_mask << orig)) != 0 {
return Err(InstallError::NoSpace);
}
orig
} else {
let mut found = None;
for i in (0..=(PIO_INSTRUCTION_COUNT - instructions.len())).rev() {
if (self.used_instruction_space & (program_mask << i)) == 0 {
found = Some(i as u8);
break;
}
}
found.ok_or(InstallError::NoSpace)?
};
for (i, &instr) in instructions.iter().enumerate() {
let relocated = if (instr >> 13) == 0b000 {
let addr = instr & 0x1F;
let new_addr = (addr + offset as u16) & 0x1F;
(instr & !0x1F) | new_addr
} else {
instr
};
self.write_instruction(offset + i as u8, relocated);
}
self.used_instruction_space |= program_mask << offset;
Ok(InstalledProgram {
offset,
length,
wrap_bottom: offset + program.wrap_target,
wrap_top: offset + program.wrap,
side_set_count: program.side_set_count,
side_set_opt: program.side_set_opt,
side_set_pindirs: program.side_set_pindirs,
})
}
pub fn uninstall_program(&mut self, program: InstalledProgram) {
let program_mask = if program.length >= 32 {
u32::MAX
} else {
(1u32 << program.length) - 1
};
self.used_instruction_space &= !(program_mask << program.offset);
}
fn write_instruction(&self, addr: u8, instr: u16) {
assert!((addr as usize) < PIO_INSTRUCTION_COUNT);
let base = &self.pio.instr_mem0 as *const _ as usize;
let target = (base + (addr as usize * 4)) as *mut u32;
unsafe { core::ptr::write_volatile(target, instr as u32); }
}
pub fn clear_instruction_memory(&mut self) {
for i in 0..PIO_INSTRUCTION_COUNT as u8 {
self.write_instruction(i, 0);
}
self.used_instruction_space = 0;
}
pub fn set_sm_mask_enabled(&mut self, mask: u32, enabled: bool) {
let mask = mask & 0xF;
if enabled {
self.pio.ctrl.set_bits(mask << CTRL_SM_ENABLE_LSB);
} else {
self.pio.ctrl.clr_bits(mask << CTRL_SM_ENABLE_LSB);
}
}
pub fn restart_sm_mask(&mut self, mask: u32) {
self.pio.ctrl.set_bits((mask & 0xF) << CTRL_SM_RESTART_LSB);
}
pub fn clkdiv_restart_sm_mask(&mut self, mask: u32) {
self.pio.ctrl.set_bits((mask & 0xF) << CTRL_CLKDIV_RESTART_LSB);
}
pub fn enable_sm_mask_in_sync(&mut self, mask: u32) {
let mask = mask & 0xF;
self.pio.ctrl.set_bits(
(mask << CTRL_CLKDIV_RESTART_LSB)
| (mask << CTRL_SM_ENABLE_LSB)
);
}
pub fn set_sm_multi_mask_enabled(
&self,
mask_prev: u32,
mask: u32,
mask_next: u32,
enabled: bool,
) {
self.pio.ctrl.modify_val(|v| {
let v = v
.with_sm_enable(if enabled { mask & 0xF } else { 0 })
.with_prev_pio_mask(mask_prev & 0xF)
.with_next_pio_mask(mask_next & 0xF);
if enabled {
v.with_nextprev_sm_enable(true)
} else {
v.with_nextprev_sm_disable(true)
}
});
}
pub fn enable_sm_multi_mask_in_sync(
&self,
mask_prev: u32,
mask: u32,
mask_next: u32,
) {
self.pio.ctrl.modify_val(|v| {
v.with_sm_enable(mask & 0xF)
.with_clkdiv_restart(mask & 0xF)
.with_prev_pio_mask(mask_prev & 0xF)
.with_next_pio_mask(mask_next & 0xF)
.with_nextprev_clkdiv_restart(true)
.with_nextprev_sm_enable(true)
});
}
#[inline]
pub fn irq_raw(&self) -> u8 {
self.pio.irq.irq() as u8
}
#[inline]
pub fn interrupt_get(&self, irq_num: u8) -> bool {
(self.pio.irq.irq() & (1 << irq_num)) != 0
}
#[inline]
pub fn interrupt_clear(&self, flags: u8) {
self.pio.irq.write_irq(flags as u32);
}
#[inline]
pub fn force_irq(&self, flags: u8) {
self.pio.irq_force.write_irq_force(flags as u32);
}
pub fn set_irq0_source_enabled(&mut self, source: u32, enabled: bool) {
if enabled {
self.pio.irq0_inte.set_bits(1 << source);
} else {
self.pio.irq0_inte.clr_bits(1 << source);
}
}
pub fn set_irq1_source_enabled(&mut self, source: u32, enabled: bool) {
if enabled {
self.pio.irq1_inte.set_bits(1 << source);
} else {
self.pio.irq1_inte.clr_bits(1 << source);
}
}
#[inline]
pub fn dbg_padout(&self) -> u32 {
self.pio.dbg_padout.read_bits()
}
#[inline]
pub fn dbg_padoe(&self) -> u32 {
self.pio.dbg_padoe.read_bits()
}
}
}
};
}
impl_pio_block!(Pio0, 0);
impl_pio_block!(Pio1, 1);
impl_pio_block!(Pio2, 2);
#[inline]
pub const fn encode_delay(cycles: u8) -> u16 {
((cycles as u16) & 0x1F) << 8
}
#[inline]
pub const fn encode_sideset(bit_count: u8, value: u8) -> u16 {
(value as u16) << (13 - bit_count)
}
#[inline]
pub const fn encode_sideset_opt(bit_count: u8, value: u8) -> u16 {
0x1000 | ((value as u16) << (12 - bit_count))
}
#[inline]
pub const fn encode_jmp(addr: u8) -> u16 {
(addr as u16) & 0x1F
}
#[inline]
pub const fn encode_jmp_cond(cond: u8, addr: u8) -> u16 {
((cond as u16 & 7) << 5) | (addr as u16 & 0x1F)
}
#[inline]
pub const fn encode_jmp_not_x(addr: u8) -> u16 {
encode_jmp_cond(1, addr)
}
#[inline]
pub const fn encode_jmp_x_dec(addr: u8) -> u16 {
encode_jmp_cond(2, addr)
}
#[inline]
pub const fn encode_jmp_not_y(addr: u8) -> u16 {
encode_jmp_cond(3, addr)
}
#[inline]
pub const fn encode_jmp_y_dec(addr: u8) -> u16 {
encode_jmp_cond(4, addr)
}
#[inline]
pub const fn encode_jmp_x_ne_y(addr: u8) -> u16 {
encode_jmp_cond(5, addr)
}
#[inline]
pub const fn encode_jmp_pin(addr: u8) -> u16 {
encode_jmp_cond(6, addr)
}
#[inline]
pub const fn encode_jmp_not_osre(addr: u8) -> u16 {
encode_jmp_cond(7, addr)
}
#[inline]
pub const fn encode_wait_gpio(polarity: bool, pin: u8) -> u16 {
let pol = if polarity { 4u16 } else { 0 };
0x2000 | (pol << 5) | (pin as u16 & 0x1F)
}
#[inline]
pub const fn encode_wait_pin(polarity: bool, pin: u8) -> u16 {
let pol = if polarity { 4u16 } else { 0 };
0x2000 | ((pol | 1) << 5) | (pin as u16 & 0x1F)
}
#[inline]
pub const fn encode_wait_irq(polarity: bool, relative: bool, irq: u8) -> u16 {
let pol = if polarity { 4u16 } else { 0 };
let rel = if relative { 0x10u16 } else { 0 };
0x2000 | ((pol | 2) << 5) | rel | (irq as u16 & 0x07)
}
#[inline]
pub const fn encode_in(src: u8, bit_count: u8) -> u16 {
0x4000 | ((src as u16 & 7) << 5) | (bit_count as u16 & 0x1F)
}
#[inline]
pub const fn encode_out(dest: u8, bit_count: u8) -> u16 {
0x6000 | ((dest as u16 & 7) << 5) | (bit_count as u16 & 0x1F)
}
#[inline]
pub const fn encode_push(if_full: bool, block: bool) -> u16 {
let iffull = if if_full { 1u16 << 6 } else { 0 };
let blk = if block { 1u16 << 5 } else { 0 };
0x8000 | iffull | blk
}
#[inline]
pub const fn encode_pull(if_empty: bool, block: bool) -> u16 {
let ifempty = if if_empty { 1u16 << 6 } else { 0 };
let blk = if block { 1u16 << 5 } else { 0 };
0x8080 | ifempty | blk
}
#[inline]
pub const fn encode_mov(dest: u8, src: u8) -> u16 {
0xA000 | ((dest as u16 & 7) << 5) | (src as u16 & 7)
}
#[inline]
pub const fn encode_mov_not(dest: u8, src: u8) -> u16 {
0xA000 | ((dest as u16 & 7) << 5) | (1 << 3) | (src as u16 & 7)
}
#[inline]
pub const fn encode_mov_reverse(dest: u8, src: u8) -> u16 {
0xA000 | ((dest as u16 & 7) << 5) | (2 << 3) | (src as u16 & 7)
}
#[inline]
pub const fn encode_nop() -> u16 {
encode_mov(mov_dest::Y, mov_src::Y)
}
#[inline]
pub const fn encode_irq_set(relative: bool, irq: u8) -> u16 {
let rel = if relative { 0x10u16 } else { 0 };
0xC000 | rel | (irq as u16 & 0x07)
}
#[inline]
pub const fn encode_irq_wait(relative: bool, irq: u8) -> u16 {
let rel = if relative { 0x10u16 } else { 0 };
0xC000 | (1 << 5) | rel | (irq as u16 & 0x07)
}
#[inline]
pub const fn encode_irq_clear(relative: bool, irq: u8) -> u16 {
let rel = if relative { 0x10u16 } else { 0 };
0xC000 | (2 << 5) | rel | (irq as u16 & 0x07)
}
#[inline]
pub const fn encode_set(dest: u8, value: u8) -> u16 {
0xE000 | ((dest as u16 & 7) << 5) | (value as u16 & 0x1F)
}
#[inline]
pub const fn encode_set_pins(value: u8) -> u16 {
encode_set(set_dest::PINS, value)
}
#[inline]
pub const fn encode_set_x(value: u8) -> u16 {
encode_set(set_dest::X, value)
}
#[inline]
pub const fn encode_set_y(value: u8) -> u16 {
encode_set(set_dest::Y, value)
}
#[inline]
pub const fn encode_set_pindirs(value: u8) -> u16 {
encode_set(set_dest::PINDIRS, value)
}