#![allow(clippy::identity_op)]
use crate::Steal;
use pastey::paste;
use rp2350_pac::sio::RegisterBlock;
pub const SPINLOCK_COUNT: usize = 32;
pub(crate) const REGS: *const RegisterBlock = rp2350_pac::SIO::PTR;
#[inline]
pub(crate) fn release_all_spinlocks() {
let r = unsafe { &*REGS };
macro_rules! release {
($($reg:ident),+ $(,)?) => {
paste! {
$(
r.$reg.[<write_ $reg>](1);
)+
}
};
}
release!(
spinlock0, spinlock1, spinlock2, spinlock3, spinlock4, spinlock5, spinlock6, spinlock7,
spinlock8, spinlock9, spinlock10, spinlock11, spinlock12, spinlock13, spinlock14,
spinlock15, spinlock16, spinlock17, spinlock18, spinlock19, spinlock20, spinlock21,
spinlock22, spinlock23, spinlock24, spinlock25, spinlock26, spinlock27, spinlock28,
spinlock29, spinlock30, spinlock31,
);
}
#[derive(Debug)]
pub struct Sio {
_marker: core::marker::PhantomData<*const ()>,
}
impl Sio {
#[inline]
pub const fn init() -> Self {
Sio {
_marker: core::marker::PhantomData,
}
}
#[inline]
pub const unsafe fn steal() -> Steal<Self> {
Steal(Sio {
_marker: core::marker::PhantomData,
})
}
#[inline]
pub fn core_id(&self) -> u32 {
unsafe { &*REGS }.cpuid.cpuid()
}
#[inline]
pub fn spinlock_state(&self) -> u32 {
unsafe { &*REGS }.spinlock_st.read_bits()
}
#[inline]
pub fn is_spinlock_locked(&self, lock_num: u8) -> bool {
assert!(lock_num < 32, "Spinlock number must be 0-31");
(self.spinlock_state() & (1 << lock_num)) != 0
}
#[inline]
pub const fn interp0(&self) -> Interp0<'_> {
Interp0 {
_marker: core::marker::PhantomData,
}
}
#[inline]
pub const fn interp1(&self) -> Interp1<'_> {
Interp1 {
_marker: core::marker::PhantomData,
}
}
}
pub struct Spinlock<const N: u8>;
pub struct SpinlockGuard<const N: u8> {
_marker: core::marker::PhantomData<*const ()>,
}
impl<const N: u8> Drop for SpinlockGuard<N> {
fn drop(&mut self) {
Spinlock::<N>::release();
}
}
macro_rules! impl_spinlock_access {
($($num:literal => $spinlock:ident),+ $(,)?) => {
paste! {
impl<const N: u8> Spinlock<N> {
const _ASSERT: () = assert!(N < 32, "Spinlock number must be 0-31");
#[inline]
pub fn try_acquire() -> Option<SpinlockGuard<N>> {
let _ = Self::_ASSERT;
let r = unsafe { &*REGS };
let result = match N {
$(
$num => r.$spinlock.[<$spinlock>](),
)+
_ => unreachable!(),
};
if result != 0 {
Some(SpinlockGuard { _marker: core::marker::PhantomData })
} else {
None
}
}
#[inline]
pub fn acquire() -> SpinlockGuard<N> {
loop {
if let Some(guard) = Self::try_acquire() {
return guard;
}
core::hint::spin_loop();
}
}
#[inline]
pub fn release() {
let _ = Self::_ASSERT;
let r = unsafe { &*REGS };
match N {
$(
$num => r.$spinlock.[<write_ $spinlock>](1),
)+
_ => unreachable!(),
}
}
#[inline]
pub fn is_locked() -> bool {
(unsafe { &*REGS }.spinlock_st.read_bits() & (1u32 << N)) != 0
}
}
}
};
}
impl_spinlock_access!(
0 => spinlock0, 1 => spinlock1, 2 => spinlock2, 3 => spinlock3,
4 => spinlock4, 5 => spinlock5, 6 => spinlock6, 7 => spinlock7,
8 => spinlock8, 9 => spinlock9, 10 => spinlock10, 11 => spinlock11,
12 => spinlock12, 13 => spinlock13, 14 => spinlock14, 15 => spinlock15,
16 => spinlock16, 17 => spinlock17, 18 => spinlock18, 19 => spinlock19,
20 => spinlock20, 21 => spinlock21, 22 => spinlock22, 23 => spinlock23,
24 => spinlock24, 25 => spinlock25, 26 => spinlock26, 27 => spinlock27,
28 => spinlock28, 29 => spinlock29, 30 => spinlock30, 31 => spinlock31,
);
macro_rules! impl_interp {
($Interp:ident, $n:literal, $mode:ident, $set_mode:ident, $get_mode:ident) => {
impl<'a> $Interp<'a> {
paste! {
#[inline]
pub fn accum0(&self) -> u32 {
unsafe { &*REGS }.[<interp $n _accum0>].read_bits()
}
#[inline]
pub fn set_accum0(&self, value: u32) {
unsafe { &*REGS }.[<interp $n _accum0>].write_bits(value);
}
#[inline]
pub fn accum1(&self) -> u32 {
unsafe { &*REGS }.[<interp $n _accum1>].read_bits()
}
#[inline]
pub fn set_accum1(&self, value: u32) {
unsafe { &*REGS }.[<interp $n _accum1>].write_bits(value);
}
#[inline]
pub fn add_accum0(&self, value: u32) -> u32 {
let r = unsafe { &*REGS };
r.[<interp $n _accum0_add>].[<write_ interp $n _accum0_add>](value);
r.[<interp $n _accum0_add>].[<interp $n _accum0_add>]()
}
#[inline]
pub fn add_accum1(&self, value: u32) -> u32 {
let r = unsafe { &*REGS };
r.[<interp $n _accum1_add>].[<write_ interp $n _accum1_add>](value);
r.[<interp $n _accum1_add>].[<interp $n _accum1_add>]()
}
#[inline]
pub fn base0(&self) -> u32 {
unsafe { &*REGS }.[<interp $n _base0>].read_bits()
}
#[inline]
pub fn set_base0(&self, value: u32) {
unsafe { &*REGS }.[<interp $n _base0>].write_bits(value);
}
#[inline]
pub fn base1(&self) -> u32 {
unsafe { &*REGS }.[<interp $n _base1>].read_bits()
}
#[inline]
pub fn set_base1(&self, value: u32) {
unsafe { &*REGS }.[<interp $n _base1>].write_bits(value);
}
#[inline]
pub fn base2(&self) -> u32 {
unsafe { &*REGS }.[<interp $n _base2>].read_bits()
}
#[inline]
pub fn set_base2(&self, value: u32) {
unsafe { &*REGS }.[<interp $n _base2>].write_bits(value);
}
#[inline]
pub fn set_base_1and0(&self, base0: u16, base1: u16) {
unsafe { &*REGS }
.[<interp $n _base_1and0>]
.[<write_ interp $n _base_1and0>]((base0 as u32) | ((base1 as u32) << 16));
}
#[inline]
pub fn peek_lane0(&self) -> u32 {
unsafe { &*REGS }.[<interp $n _peek_lane0>].read_bits()
}
#[inline]
pub fn peek_lane1(&self) -> u32 {
unsafe { &*REGS }.[<interp $n _peek_lane1>].read_bits()
}
#[inline]
pub fn peek_full(&self) -> u32 {
unsafe { &*REGS }.[<interp $n _peek_full>].read_bits()
}
#[inline]
pub fn pop_lane0(&self) -> u32 {
unsafe { &*REGS }.[<interp $n _pop_lane0>].read_bits()
}
#[inline]
pub fn pop_lane1(&self) -> u32 {
unsafe { &*REGS }.[<interp $n _pop_lane1>].read_bits()
}
#[inline]
pub fn pop_full(&self) -> u32 {
unsafe { &*REGS }.[<interp $n _pop_full>].read_bits()
}
#[inline]
pub fn ctrl_lane0(&self) -> u32 {
unsafe { &*REGS }.[<interp $n _ctrl_lane0>].read_bits()
}
#[inline]
pub fn set_ctrl_lane0(&self, value: u32) {
unsafe { &*REGS }.[<interp $n _ctrl_lane0>].write_bits(value);
}
#[inline]
pub fn config_lane0(&self, shift: u8, mask_lsb: u8, mask_msb: u8, signed: bool) {
unsafe { &*REGS }.[<interp $n _ctrl_lane0>].modify_val(|v| {
v.with_shift(shift as u32)
.with_mask_lsb(mask_lsb as u32)
.with_mask_msb(mask_msb as u32)
.with_signed(signed)
});
}
#[inline]
pub fn $set_mode(&self, enable: bool) {
unsafe { &*REGS }
.[<interp $n _ctrl_lane0>]
.modify_val(|v| v.[<with_ $mode>](enable));
}
#[inline]
pub fn $get_mode(&self) -> bool {
unsafe { &*REGS }.[<interp $n _ctrl_lane0>].$mode()
}
#[inline]
pub fn ctrl_lane1(&self) -> u32 {
unsafe { &*REGS }.[<interp $n _ctrl_lane1>].read_bits()
}
#[inline]
pub fn set_ctrl_lane1(&self, value: u32) {
unsafe { &*REGS }.[<interp $n _ctrl_lane1>].write_bits(value);
}
#[inline]
pub fn config_lane1(&self, shift: u8, mask_lsb: u8, mask_msb: u8, signed: bool) {
unsafe { &*REGS }.[<interp $n _ctrl_lane1>].modify_val(|v| {
v.with_shift(shift as u32)
.with_mask_lsb(mask_lsb as u32)
.with_mask_msb(mask_msb as u32)
.with_signed(signed)
});
}
#[inline]
pub fn overf0(&self) -> bool {
unsafe { &*REGS }.[<interp $n _ctrl_lane0>].overf0()
}
#[inline]
pub fn overf1(&self) -> bool {
unsafe { &*REGS }.[<interp $n _ctrl_lane0>].overf1()
}
#[inline]
pub fn overf(&self) -> bool {
unsafe { &*REGS }.[<interp $n _ctrl_lane0>].overf()
}
}
}
};
}
pub struct Interp0<'a> {
_marker: core::marker::PhantomData<*const &'a ()>,
}
impl_interp!(Interp0, 0, blend, set_blend, blend);
pub struct Interp1<'a> {
_marker: core::marker::PhantomData<*const &'a ()>,
}
impl_interp!(Interp1, 1, clamp, set_clamp, clamp);